/* Task 1, test 1
 * A test that strongly resembles (but isn't actually) a field-equilibrium program.
 * Target for:
 *   CSE
 *   Constant propagation
 * Author: Bryan Mills & Daniel Lee
 */

void print_int (int i) {}
void print_newline (void) {}
void *alloc(int count, int size) {}

void main (void)
{
    int *foo;
    int SIZE;
    int ITERS;
    int PRIME_A, PRIME_B;
    int BOUNDARY_A, BOUNDARY_B;
    int b, c, d;
    
    SIZE = 100;
    ITERS = 500;
    PRIME_A = 197;
    PRIME_B = 203;
    BOUNDARY_A = 0;
    BOUNDARY_B = 2006;
    
    foo = alloc (SIZE * SIZE, 4);
    
    for (c = SIZE + 1; c < (SIZE - 1) * (SIZE - 1); c = c + 1)
    {
        foo [c] = (c * PRIME_A) % PRIME_B;
    }

    for (c = 0; c < SIZE; c = c + 1)
    {
        foo [c] = BOUNDARY_A;
        foo [c * SIZE] = BOUNDARY_A;
        foo [(c + 1) * SIZE - 1] = BOUNDARY_B;
        foo [(SIZE - 1) * SIZE + c] = BOUNDARY_B;
    }

    for (b = 0; b < ITERS; b = b + 1)
    {
        for (c = 1; c < SIZE - 1; c = c + 1)
            for (d = 1; d < SIZE - 1; d = d + 1)
                foo [c * SIZE + d] =
                  ( foo [(c - 1) * SIZE + d - 1] + foo [(c - 1) * SIZE + d] + foo [(c - 1) * SIZE + d + 1]
                  + foo [c * SIZE + d - 1] + foo [c * SIZE + d] + foo [c * SIZE + d + 1]
                  + foo [(c + 1) * SIZE + d - 1] + foo [(c + 1) * SIZE + d] + foo [(c + 1) * SIZE + d + 1])
                   / 9;
    }

    b = 0;
    for (c = 0; c < (SIZE - 1) * (SIZE - 1); c = c + 1)
    {
        b += ((BOUNDARY_A + BOUNDARY_B) / 2) - foo [c];
    }
    print_int (b);
    print_newline ();
}
