As you wrote, it's all in the JITter. In Release build and without debugger attached, with ++n
you get:
int n = 0;
00000000 push ebp
00000001 mov ebp,esp
while (!ok) ++n;
00000003 movzx eax,byte ptr [ecx+4]
00000007 test eax,eax
00000009 jne 0000000F
0000000b test eax,eax ; <---
0000000d je 0000000B ; <---
0000000f pop ebp
}
00000010 ret
And without ++n
:
while (!ok) ;
00000000 push ebp
00000001 mov ebp,esp
00000003 cmp byte ptr [ecx+4],0
00000007 je 00000003
00000009 pop ebp
}
0000000a ret
The real question should be why there is no code for ++n
emitted at all.
Edit: on x64 Release build results are similar:
Debugger.Break();
00000000 push rbx
00000001 sub rsp,20h
00000005 mov rbx,rcx
00000008 call FFFFFFFFED0EE4D0
0000000d mov ecx,2710h
00000012 call FFFFFFFFEDCFE460
while (!ok) ++n;
00000017 mov al,byte ptr [rbx+8]
0000001a movzx ecx,al
0000001d test ecx,ecx
0000001f jne 0000000000000025
00000021 test ecx,ecx
00000023 je 0000000000000021
00000025 add rsp,20h
00000029 pop rbx
0000002a rep ret