Both "-O2" and "-O1" don't work as well. Only "-O0" works.

Could be.  What GCC version are you using?

Latest:

gcc.exe (Rev2, Built by MSYS2 project) 4.9.2

Also, when -O3 is used, does it help to declare reserved_heap_size
volatile, like this:
 
  size_t volatile reserved_heap_size;
 
(You might need to change the extern declaration on w32heap.h as
well.)

Unfortunately, nope.