Uninitialized variables -- LLVM bug?

Consider the following C code. When it is compiled using clang with ‘ –O’, the assert fires. Gcc seems to do the opposite.

#include <assert.h>

int main()


unsigned a; // uninitialized!

unsigned b = a;

assert(b == a); // shouldn’t this be always true?

return 0;


Would you consider this a LLVM bug? I think GVN is responsible for this. I don’t know if it falls within the realm of “undefined” or “implementation specific” behavior but there are probably examples of code where one can “correctly” skip initialization. The Briggs-Torczon sparse set data structure comes to mind (where you are not comparing two uninitialized variables but an uninitialized variable with an initialized variable—LLVM seems to do the right thing in that case, though).

n Riyaz

Loading from an uninitialized variable is undefined behavior. You might be
interested in Chris Lattner's series on undefined behavior at
http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html .