@define RANDCNT %( 4000 %)
@define RANDMAX %( 1000 %)
@define ITERS %( 1500 %)

@define time
%(
    %( runtime == "dyninst" %?
	gettimeofday_us()
    %:
	cpu_clock_us(cpu())
    %)
%)

@define feed(agg, tagg)
%(
    t = @time
    foreach(k in randvals)
	@agg <<< k
    @tagg += @time - t
%)

global x, tx = 0, y, ty = 0
global a, ta = 0, b, tb = 0
global randvals[@RANDCNT]

probe begin
{
    /* TEST 1: test optimizations for @count, @sum, @min, and @max. */

    for (i=0; i<@ITERS; i++)
    {

	for (j=0; j<@RANDCNT; j++)
	    randvals[j] = randint(@RANDMAX)

	/* The "ordering dance" described above happens here */
	if(i%2)
	{
	    @feed(x[1], tx)
	    @feed(y[1], ty)
	}
	else
	{
	    @feed(y[1], ty)
	    @feed(x[1], tx)
	}
    }

    /*
     * We need to print the stats out to avoid compiler elision.
     * The list of stats mentioned below makes the actual difference
     * between stats under test and is the gist of this test.  The test
     * should show no measurable shrinkage, if the below list doesn't
     * differ for measured stats.
     */
    printdln(" ", "IGNORE", @count(x[1]))
    printdln(" ", "IGNORE", @count(y[1]), @sum(y[1]), @min(y[1]), @max(y[1]))

    /*
     * Measured shrinkage [%], prevent division by zero caused by
     * rounding errors
     */
    printf("TEST3 %d\n", ((ty-tx)*10000)/(ty*100))


    /* TEST 2: test optimizations for @variance. */

    for (i=0; i<(@ITERS / 4); i++)
    {

	for (j=0; j<@RANDCNT; j++)
	    randvals[j] = randint(@RANDMAX)

	if(i%2)
	{
	    @feed(a[1], ta)
	    @feed(b[1], tb)
	}
	else
	{
	    @feed(b[1], tb)
	    @feed(a[1], ta)
	}
    }

    printdln(" ", "IGNORE", @count(a[1]))
    printdln(" ", "IGNORE", @variance(b[1]))

    /*
     * Measured shrinkage [%], prevent division by zero caused by
     * rounding errors
     */
    printf("TEST4 %d\n", ((tb-ta)*10000)/(tb*100))

    exit()
}
