Index: lib/CodeGen/CGCall.cpp =================================================================== --- lib/CodeGen/CGCall.cpp (revision 77045) +++ lib/CodeGen/CGCall.cpp (working copy) @@ -505,6 +505,13 @@ void CodeGenModule::ConstructAttributeLi void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, llvm::Function *Fn, const FunctionArgList &Args) { + + // If this is main, initialize the return value to zero. + // Implements C++ [basic.start.main]p5 and C99 5.1.2.2.3. + if (const FunctionDecl* FD = dyn_cast(CurFuncDecl)) + if (FD->isMain()) + Builder.CreateStore(llvm::ConstantInt::get(LLVMIntTy, 0), ReturnValue); + // FIXME: We no longer need the types from FunctionArgList; lift up and // simplify.