Index: src/CodeGen/FixNames.cc
===================================================================
--- src/CodeGen/FixNames.cc	(revision 907eccb29a08fe51aa5e5c20aa28f841482b4436)
+++ src/CodeGen/FixNames.cc	(revision 3fe34ae393b9647360e8310b11c0d6f9863d9062)
@@ -14,4 +14,6 @@
 //
 
+#include <memory>
+
 #include "FixNames.h"
 #include "SynTree/Declaration.h"
@@ -20,4 +22,6 @@
 #include "SymTab/Mangler.h"
 #include "OperatorTable.h"
+
+extern std::unique_ptr<FunctionDecl> translation_unit_main_signature;
 
 namespace CodeGen {
@@ -28,5 +32,4 @@
 
 		virtual void visit( CompoundStmt *compoundStmt );
-
 	  private:
 		int scopeLevel = 1;
@@ -34,4 +37,61 @@
 		void fixDWT( DeclarationWithType *dwt );
 	};
+
+	std::string mangle_main() {
+		FunctionType* main_type;
+		std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl(
+			"main", 
+			DeclarationNode::NoStorageClass, 
+			LinkageSpec::Cforall, 
+			main_type = new FunctionType( Type::Qualifiers(), true ), 
+			nullptr, false, false
+		) };
+		main_type->get_returnVals().push_back( 
+			new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
+		);
+
+		auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
+		// std::cerr << name << std::endl;
+		return name;
+	}
+	std::string mangle_main_args() {
+		FunctionType* main_type;
+		std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl(
+			"main", 
+			DeclarationNode::NoStorageClass, 
+			LinkageSpec::Cforall, 
+			main_type = new FunctionType( Type::Qualifiers(), false ), 
+			nullptr, false, false
+		) };
+		main_type->get_returnVals().push_back( 
+			new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
+		);
+
+		mainDecl->get_functionType()->get_parameters().push_back( 
+			new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
+		);
+
+		mainDecl->get_functionType()->get_parameters().push_back( 
+			new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, 
+			new PointerType( Type::Qualifiers(), new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Char ) ) ), 
+			nullptr )
+		);
+
+		auto&& name = SymTab::Mangler::mangle( mainDecl.get() );
+		// std::cerr << name << std::endl;
+		return name;
+	}
+
+	bool is_main(const std::string& name) {
+		static std::string mains[] = { 
+			mangle_main(), 
+			mangle_main_args()
+		};
+
+		for(const auto& m : mains) {
+			if( name == m ) return true;
+		}
+		return false;
+	}
 
 	void fixNames( std::list< Declaration* > translationUnit ) {
@@ -57,4 +117,13 @@
 		Visitor::visit( functionDecl );
 		fixDWT( functionDecl );
+
+		if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
+			if(translation_unit_main_signature) { 
+				throw SemanticError("Multiple definition of main routine\n", functionDecl); 
+			}
+
+			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() );
+		}
 	}
 
