Wednesday, May 4, 2011

C declarations and the almighty Hex-Rays

Today at GNIF, someone asked us the following question : what is the output of this program ?
int p=0;
p = p++ + ++p;
printf("p=%d\n", p);

I let the readers test their knowledge on this one. On my side, after some spending a few minutes arguing with coworkers, I decided to see if IDA and the Hex-Rays plugin could help me. Here is the disassembly :

int __cdecl main()
{
  printf("p=%d\n", 3);
  return 0;
}

Hail to the almighty IDA !