It only gets optimized away on the highest optimization level on gcc. I am guessing perhaps it's because strlen is inlined in -O3 (-O3 turns on heuristic function inlining). I have never examined the asm output, however.I was unable to get it optimized away at all. I'm thinking the compiler doesn't know anything about strlen and thus cannot assume that it will return the same thing everytime you call it.