/* Try to produce the failure from fftw-2.1.5's testsuite in isolation.
 * See https://code.google.com/p/gcc-crunch/issues/detail?id=2
 *
 * So far, unsuccessful at reproducing the presumed cause of failure.
 */

double a = 42.0, b = 42000000000000.0;
double x = 43000000000086.0, y = 43.0;
volatile double c, z;		/* Results of a + b and x - y */
volatile double junk;		/* Scratch area to write mvd9 */
volatile int junk2[2];		/* Scratch area to write {r5,r6} */

test()
{
	register double *ap asm("r0") = &a;
	register double *bp asm("r1") = &b;
	register volatile double *cp asm("r3") = &c;
	register double *xp asm("r5") = &x;
	register double *yp asm("r6") = &y;
	register volatile double *zp asm("r8") = &z;

	register volatile double * volatile sl 	asm("r10") = &junk;	/* r10, a.k.a sl */
	register volatile int * 	  r7	asm("r7") = junk2;
	register volatile int 	  r9 	asm("r9");		/* written to */
	register volatile int 	  r2 	asm("r2");		/* scratch register */

	/* Prepare the ground: cfaddd's operands must be in the registers already */
	asm("cfldrd  mvd0, [r0]" : : "r"(ap));	/* args to cfaddd */
	asm("cfldrd  mvd10, [r1]": : "r"(bp));	/* args to cfsubd */
	asm("cfldrd  mvd1, [r5]" : : "r"(xp));
	asm("cfldrd  mvd5, [r6]" : : "r"(yp));
	/* Make sure the memory loaded by the ldm insn is in the data cache */
	asm("add     r2, sp, #96" : "=r"(r2));
	asm("ldm     r2, {r9, r2}" : "=r"(r9),"=r"(r2) : "r"(r2));

	/* Make sure all operands are in their registers */
	asm("nop;nop;nop;nop;nop;nop;nop;nop");
	asm("nop;nop;nop;nop;nop;nop;nop;nop");

	/* The exact failing sequence from fftw-2.1.5::fftw/frc_16.o */
	asm("cfaddd  mvd10, mvd0, mvd10");
	asm("cfsubd  mvd0, mvd1, mvd5");
	asm("cfstrd  mvd9, [sl]" : : "r"(sl));
	asm("add     sl, sp, #96");
	asm("ldm     sl, {r9, sl}" : "=r"(r9),"=r"(sl) : "r"(sl));
	asm("cfstrd  mvd10, [r3]" : : "r"(cp));
	asm("stm     r7, {r5, r6}" : : "r"(r7));
	asm("cfstrd  mvd0, [r8]" : : "r"(zp));
}

#include <stdio.h>
#include <stdlib.h>
main()
{
	test();
	if (c != a + b) fputs("c != a + b\n", stderr);
	if (z != x - y) fputs("z != x - y\n", stderr);
	exit (c != a + b || z != x - y);
}
