previous | start | next

Practice: Tracing Function Execution (3)

We could try to rewrite the code even further into C:

To keep the statements in the same order, the conditions for each 'if' is negated:

                                   int f(int n)                  
                                   {                             
                                     int ret;                    
      if (n >= 0) goto L2            if (n < 0) {          
      ret = n                          ret = n;                  
      ret++                            ret++;                    
      goto L3                                                    
  L2:                                                                    
                                                                         
      if (n >= 0) goto L4            } else if ( n > 0 ) { 
      ret = n                          ret = n;                  
      ret--                            ret--;                    
      goto L3                                                            
  L4:                                } else {                    
      ret = 0                          ret = 0;                  
  L3:                                }                           
                                                                 
      return ret                     return ret;                 
                                   }                             
   

This C version needs some clean up.

That is because it still reflects how the optimization is trying to keep values in registers (ret) for faster access and for computation.

But ret is declared as a memory variable and looks unnecessary - we could just use n.



previous | start | next