Can we use the tailcall optimization in this function If no
Can we use the tail-call optimization in this function? If no, explain why not. If yes, what is the difference in the number of executed instructions in f with and without the optimization?
}
Solution
}
Yes, we can use the tail call optimization in this function. Last line of function f is the tail call.
With any tail call, the function call itself can be optimized away and turned into what is effectively a goto. This means that the work to setup the stack before the function call and restore it afterwards can all be removed.
The unoptimized code might look something like this:
... ! executing inside f()
push EIP ! push current instruction pointer on stack
push a ! push variable \'a\' on the stack
push b ! push variable \'b\' on the stack
push c ! push variable \'c\' on the stack
push d ! push variable \'d\' on the stack
jmp g ! call g() by jumping to its address
 ... ! executing inside g()
 pop g(a,b) ! prepare to return value of \'g(a,b)\'
pop c+d ! prepare to return value of \'c+d\'
pop EIP ! return to f()
pop d ! prepare to return value of \'d\'
pop c ! prepare to return value of \'c\'
pop b ! prepare to return value of \'b\'
pop a ! prepare to return value of \'a\'
pop EIP ! return to f() caller.
This shows the difference in number of executed instructions in f with and without the optimization.

