I think the C program below shall output 1:
#include <stdio.h>
int main()
{
unsigned int n=18u;
while ((n+17u)>=17u) n-=17u;
printf("%u\n",n+17u);
return 0;
}
But compiled in VC6, Visual Studio 2010, or Visual Studio 2012, all in release mode, the program doesn't output anything and doesn't quit.
This is the assembly code generated by VS2012:
00BD1000 mov eax,12h
00BD1005 lea eax,[eax-11h]
00BD1008 jmp main+5h (0BD1005h)
It seems that the compiler did some optimization and generated an infinite loop.
I think that ((n+17u)>=17u)
is not always true, because if n==0xFFFF..FF
, n+17u
would wrap to 16u
.
Am I wrong, or are the compilers wrong?