Index: src/CodeGen/FixMain.cc
===================================================================
--- src/CodeGen/FixMain.cc	(revision 7cc2c8de391b81c66900242eeeedd230a3f06b5f)
+++ src/CodeGen/FixMain.cc	(revision 13de47bcd1133e72e5f8fcdee34f7cbc890ec088)
@@ -1,1 +1,55 @@
+//
+// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
+//
+// The contents of this file are covered under the licence agreement in the
+// file "LICENCE" distributed with Cforall.
+//
+// FixMain.cc -- 
+//
+// Author           : Thierry Delisle
+// Created On       : Thr Jan 12 14:11:09 2017
+// Last Modified By : 
+// Last Modified On : 
+// Update Count     : 0
+//
 
+
+#include "FixMain.h"	
+
+#include <fstream>
+#include <iostream>
+
+#include "Common/SemanticError.h"
+#include "SynTree/Declaration.h"
+
+namespace CodeGen {
+	bool FixMain::replace_main = false;
+	std::unique_ptr<FunctionDecl> FixMain::main_signature = nullptr;
+	
+	void FixMain::registerMain(FunctionDecl* functionDecl) 
+	{
+		if(main_signature) { 
+			throw SemanticError("Multiple definition of main routine\n", functionDecl); 
+		}
+		main_signature.reset( functionDecl->clone() );
+	}
+
+	void FixMain::fix(std::ostream &os, const char* bootloader_filename) {
+		if( main_signature ) {
+			os << "static inline int invoke_main(int argc, char* argv[], char* envp[]) { (void)argc; (void)argv; (void)envp; return ";
+
+			os << main_signature->get_scopedMangleName() << "(";
+			switch(main_signature->get_functionType()->get_parameters().size()) {
+				case 3: os << "argc, argv, envp"; break;
+				case 2: os << "argc, argv"; break;
+				case 0: break;
+				default : assert(false);
+			}
+			os << "); }\n";
+
+			std::ifstream bootloader(bootloader_filename, std::ios::in);
+			assertf( bootloader.is_open(), "cannot open bootloader.c\n" );
+			os << bootloader.rdbuf();
+		}
+	}
+};
Index: src/CodeGen/FixMain.h
===================================================================
--- src/CodeGen/FixMain.h	(revision 13de47bcd1133e72e5f8fcdee34f7cbc890ec088)
+++ src/CodeGen/FixMain.h	(revision 13de47bcd1133e72e5f8fcdee34f7cbc890ec088)
@@ -0,0 +1,47 @@
+//
+// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
+//
+// The contents of this file are covered under the licence agreement in the
+// file "LICENCE" distributed with Cforall.
+//
+// FixMain.h -- 
+//
+// Author           : Thierry Delisle
+// Created On       : Thr Jan 12 14:11:09 2017
+// Last Modified By : 
+// Last Modified On : 
+// Update Count     : 0
+//
+
+#ifndef FIXMAIN_H
+#define FIXMAIN_H
+
+#include <iosfwd>
+#include <memory>
+
+#include "Parser/LinkageSpec.h"
+
+class FunctionDecl;
+
+namespace CodeGen {
+	class FixMain {
+	  public :
+		static inline LinkageSpec::Spec mainLinkage() {
+			return replace_main ? LinkageSpec::Cforall : LinkageSpec::C;
+		}
+		
+		static inline void setReplaceMain(bool val) {
+			replace_main = val;
+		}
+
+		static void registerMain(FunctionDecl* val);
+
+		static void fix(std::ostream &os, const char* bootloader_filename);
+
+	  private:
+  		static bool replace_main;
+		static std::unique_ptr<FunctionDecl> main_signature;
+	};
+};
+
+#endif //FIXMAIN_H
Index: src/CodeGen/FixNames.cc
===================================================================
--- src/CodeGen/FixNames.cc	(revision 7cc2c8de391b81c66900242eeeedd230a3f06b5f)
+++ src/CodeGen/FixNames.cc	(revision 13de47bcd1133e72e5f8fcdee34f7cbc890ec088)
@@ -22,6 +22,5 @@
 #include "SymTab/Mangler.h"
 #include "OperatorTable.h"
-
-extern std::unique_ptr<FunctionDecl> translation_unit_main_signature;
+#include "FixMain.h"
 
 namespace CodeGen {
@@ -119,10 +118,10 @@
 
 		if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
-			if(translation_unit_main_signature) { 
-				throw SemanticError("Multiple definition of main routine\n", functionDecl); 
+			int nargs = functionDecl->get_functionType()->get_parameters().size();
+			if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
+				throw SemanticError("Main expected to have 0, 2 or 3 arguments\n", main_signature.get()); 
 			}
-
 			functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), "0") ) ) );
-			translation_unit_main_signature.reset( functionDecl->clone() );
+			CodeGen::FixMain::registerMain( functionDecl );
 		}
 	}
