#include <stdio.h>
#define N 12

int A[N][N];

int *magic_address = (int *) 0x7ffffff0;

/* This function clears the contents of the D-cache and resets
   statistics */
void flush_cache(){
  *magic_address = 1;	/* Flush D-cache */
  *magic_address = 2;	/* Reset Cache Statistics */
}

/* This functions dumps cache statistics to file */
void dump_cache_stats(int tag){
  *magic_address = 3 | (tag<< 16);	/* Dump cache statistics with tag*/
}

/* Initialize the matrix */
void InitMatrix (int Matrix[N][N]){
  int i, j;

  for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++) {
      Matrix[i][j] = i+j;
    }
  }
 }

int SumByColRow (int Matrix[N][N])
{
  int i, j, Sum = 0, Time;

  flush_cache();
  for (j = 0; j < N; j ++) {
    for (i = 0; i < N; i ++) {
      Sum += Matrix[i][j];
    }
  }
  dump_cache_stats(1);
  return Sum;
}

int SumByRowCol (int Matrix[N][N])
{
  int i, j, Sum = 0, Time;

  flush_cache(); 
  for (i = 0; i < N; i ++) {
    for (j = 0; j < N; j ++) {
      Sum += Matrix[i][j];
    }
  }
  dump_cache_stats(2);
  return Sum;
}

main ()
{
  int a, b;

  InitMatrix(A);
  a = SumByColRow (A);
  b = SumByRowCol (A);
  printf ("The sums are %d and %d\n", a, b);
}

