When RAM reads 0, it remains in the first while loop. Otherwise jumps to the other.
At level3 optimization, the jump doesn't occur!
Reason is well stated in the assembly code. https://github.com/manvendra88/STM32/blob/master/Volatile_Assembly.c This is done by the compiler so that it uC doesn't have to read from the memory again and again as it costs time. Compiler assumed that the content of the memory location won't change during the lifespan of an application. Addresses in microcontrollers are highly volatile, they change rapidly as any of the peripherals may change there values. Addresses of peripherals are highly volatile in nature as they receive data from the external world. Therefore, those addresses of data-register have to treated as volatile in nature. To solve this problem use the pointer as a volatile variable.