Doing &quot;something sensible&quot; sounds good, but it&#x27;s hard to actually do. You can point out the sensible approach to specific examples, but writing the optimizer to take a &quot;sensible approach&quot; in all cases is hard.<p>For every example like this, where the programmer wants a NULL check to be done even when the value &quot;can&#x27;t be NULL,&quot; there&#x27;s an opposite example where the programmer wants a NULL check to be elided because the value actually can&#x27;t be NULL.<p>Taking advantage of undefined behavior isn&#x27;t done because compiler writers are sadists who enjoy writing perverse implementations which rely on the letter of the law. It&#x27;s done because it allows the compiler to generate better, faster code.<p>I think a lot of the trouble is that too many programmers learn C in a completely ad-hoc, informal fashion and never take the time to actually become familiar with the fundamentals of the language. People simply don&#x27;t know how the language is specified, what operations are legal and what operations aren&#x27;t, why there are illegal operations that the compiler won&#x27;t (and can&#x27;t) warn you about, etc.<p>C is definitely a dangerous language and is a difficult language to use well. But that&#x27;s all the more reason people who plan to use it need to ensure they understand it.<p>More mandatory reading: <a href="http:&#x2F;&#x2F;blog.llvm.org&#x2F;2011&#x2F;05&#x2F;what-every-c-programmer-should-know.html" rel="nofollow">http:&#x2F;&#x2F;blog.llvm.org&#x2F;2011&#x2F;05&#x2F;what-every-c-programmer-should-...</a>