
/*
 * Task-1: Code that can be optimized using the following
 * methods:
 *  Dead Code Elimination (primarily)
 *	Constant Propagation
 *  Constant Folding
 *
 * by: Avijit Kumar (kavijit@cs), Swapnil V. Patil (svp@cs)
 *
 */

int foo(int a){
  a = a + 1;
  return a;
}

void main(void) {

  int a,b,c,last,pi,pj;
  int isPrime;
  
  a=1000;
  b=20;
  c=300;
  a = (b*c+a)-b+c;
  last=1; 


	// Perform an operation that will never be used again
	// aka, "dead code"	

	// Calculate the a'th prime number by a simple method
	for(pi = 1; pi <= a; pi+=1) {
	  isPrime = (0!=0);
		while(!isPrime) {
			last+=1;
			for(pj=2;pj<last;pj+=1)
				if(last%pj == 0)
					break;
			if(pj==last)
			  isPrime=(0==0);
		}
	}

  /* The variable `last' now contains the a'th prime number.
     However, using constant propagation and folding, it can 
		 be verified that the `then' branch of the 'if-else' stmt 
		 below will never be executed. Thus the value of 'last' is 
		 never used and the code above for calculating 'last'
		 can be removed.
	*/

	if(a != b*c+a+c-b){
		a=foo(last);
  }
	else {
		a=foo(a);
	}
	return;
}
