Is it possible to switch off optimization of a specific function? A friend of mine has the problem that the gcc optimization makes some (unknown to me) µ-controller-code not
Short of putting the function in its own file, I don't think there is any way to turn off optimization on a function by function level using earlier versions of GCC. But instead of switching off the optimization for this one function, you could try selecting switching off certain types of optimizations in the whole program. While you have identified a bug in this particular function, this likely points to the existence of other undiscovered bugs.
As others pointed out, it's likely that the culprit is optimizations regarding 'strict aliasing'. While in the long run you probably should fix the code in question, in the short run you could play with adding '-fno-strict-aliasing' to your command line. At -O2 and above, the compiler makes certain assumptions about interactions between pointers. Adding this option tells it not to make these assumptions.
If this fails, and if for some reason the code cannot be fixed, you could then try disabling other optimization options.