O2 Aggressive Optimization by Clang

Edited the Subject.

What are you actually trying to achieve? The sample code does not actually
DO anything useful, even if it's technically possible to find ways to
observe the value of `i` and `j` outside of `main`.
What do you expect the compiler to do with this?

If nothing else is using `i` and `j`, what's the purpose of changing their

If you ARE using those values elsewhere, the using `volatile` is indeed the
right thing to do. Or calling some functions inside `main`, that will cause
the compiler to understand that "this does something more than just update
some variables".

+1 for using either volatile or atomics, depending on precisely what your goal is.

Anyone from the community ,please share their insights why above
transformation is right ?

This looks like it might be helpful to you (there’s a C-specific answer at the bottom).

Hi Mats,

Thank you for coming back with your views and consider the application
is multi-threaded .

Thank you again.