#define MAXVSIZE 1000
#include <stdio.h>
#include <pcl.h>

void loop(double c[], double a[], double b[], int n) {
   int i;

   for (i=0;i<n;i++)
     c[i] = a[i] + b[i];
}


void main() {

     double a[MAXVSIZE], b[MAXVSIZE], c[MAXVSIZE];
     int i,n;

PCL_CNT_TYPE i_result[2];
PCL_FP_CNT_TYPE fp_result[2];
int counter_list[2], res;
unsigned int flags;
PCL_DESCR_TYPE descr;


     printf("Enter vector size:  ");
     scanf("%d",&n);

     for (i=0;i<n;i++) {
       a[i] = i;
       b[i] = n-i;
     }

/* Count instructions and cycles with pcl */

  if(PCLinit(&descr) != PCL_SUCCESS)
          printf("unable to allocate PCL handle\n");

        /* define events to be measured
           (instructions and cycles)
         */
        counter_list[0] = PCL_CYCLES;
        counter_list[1] = PCL_INSTR;

        /* define in what mode to count (only user mode) */
        flags = PCL_MODE_USER;

  if ((res = PCLquery(descr, counter_list, 2, flags)) != PCL_SUCCESS)
          printf("these two events are not available on this system\n");

/* start counting */
  if ((res = PCLstart(descr, counter_list, 2, flags)) != PCL_SUCCESS)
     {
        printf("problem with starting two events\n");
        exit(1);
     }


     loop(c,a,b,n);

/* stop counting */
  if ((res = PCLstop(descr, i_result, fp_result, 2)) != PCL_SUCCESS)
     printf("problem with stopping two events\n");
  else
     printf("%15.0f instructions in %15.0f cycles\n",
                     (double) i_result[0], (double) i_result[1]);

  if (PCLexit(descr) != PCL_SUCCESS)
     printf("unable to deallocate PCL handle\n");


    for (i=0;i<n;i++)
      printf("c[%d] = %g\n",i,c[i]);
}
