dtgreene: When writing security sensitive code, optimizations are sometimes *not* what you want.
rtcvb32: Hmmm... Optimizations are suppose to run code properly, the fact that it doesn't mean it's possibly a bug, or perhaps you need a special pragma to override how it handles in-lining functions. To be fair though, a huge amount of code doesn't need to be super sensitive.
And usually it will optimize out things it knows statically. Something that changes your stuff by routines it can't guarantee it's results will generally not be optimized; Although i couldn't duplicate that effect with my quick tests, which is too bad.
Here is the issue: "running code properly" means that the code has the same observable properties, provided there is no undefined behavior. The memset() call can be optimized out because it has no observable effects (again, ignoring undefined behavior).
Unfortunately, if buggy code happens to read data past the end of an array, the result is undefined, meaning it could (in theory) do anything. This is the problem here; we want the buffer to be zeroed out so that it isn't there to be disclosed as a result of undefined behavior.
Heartbleed is one example of what I am thinking of here. You have code that is something like the following:
scanf("%d", &len);
buff = malloc(len);
fread(buff, 1, len, stdin);
fwrite(buff, 1, len, stdout);
free(buff);
If len is less than the size of the buffer, only part of the buffer is overwritten by fread(), and the rest is unchanged. fwrite(), then, writes the entire buffer, including the part that was not overwritten, which could contain sensitive data that wasn't cleared earlier. (In particular, note that the behavior here is technically undefined, so the compiler is allowed to have it do literally anything.)
(By the way, this code can be fixed; store the return value of fread() into a variable and pass it instead of len to fwrite. Unfortunately, it is easy to forget to do so, leading to a bug that can be exploited, so you shouldn't assume that programmers will get this correct all the time.)
When doing quick tests of this sort of thing, make sure to compile with optimization (-O2 in gcc/clang) and that you check the assembly language output so you know what code is actually being generated.