Index: src/CodeGen/FixMain.cc
===================================================================
--- src/CodeGen/FixMain.cc	(revision c600df18f9fbbecdee5b62a88fa82ac6773b2d67)
+++ src/CodeGen/FixMain.cc	(revision f42fc138a94bc24e5cc4c91a0dcf40b7e1fb4b66)
@@ -22,4 +22,5 @@
 #include <string>                  // for operator<<
 
+#include "Common/PassVisitor.h"
 #include "Common/SemanticError.h"  // for SemanticError
 #include "CodeGen/GenType.h"       // for GenType
@@ -65,3 +66,75 @@
 		}
 	}
+
+namespace {
+
+ObjectDecl * signedIntObj() {
+	return new ObjectDecl(
+		"", Type::StorageClasses(), LinkageSpec::Cforall, 0,
+		new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr );
+}
+
+ObjectDecl * charStarObj() {
+	return new ObjectDecl(
+		"", Type::StorageClasses(), LinkageSpec::Cforall, 0,
+		new PointerType( Type::Qualifiers(),
+			new PointerType( Type::Qualifiers(),
+				new BasicType( Type::Qualifiers(), BasicType::Char ) ) ),
+		nullptr );
+}
+
+std::string create_mangled_main_function_name( FunctionType * function_type ) {
+	std::unique_ptr<FunctionDecl> decl( new FunctionDecl(
+		"main", Type::StorageClasses(), LinkageSpec::Cforall,
+		function_type, nullptr ) );
+	return SymTab::Mangler::mangle( decl.get() );
+}
+
+std::string mangled_0_argument_main() {
+	FunctionType* main_type = new FunctionType( Type::Qualifiers(), true );
+	main_type->get_returnVals().push_back( signedIntObj() );
+	return create_mangled_main_function_name( main_type );
+}
+
+std::string mangled_2_argument_main() {
+	FunctionType* main_type = new FunctionType( Type::Qualifiers(), false );
+	main_type->get_returnVals().push_back( signedIntObj() );
+	main_type->get_parameters().push_back( signedIntObj() );
+	main_type->get_parameters().push_back( charStarObj() );
+	return create_mangled_main_function_name( main_type );
+}
+
+struct FindMainCore {
+	void previsit( FunctionDecl * decl ) {
+		if ( FixMain::isMain( decl ) ) {
+			FixMain::registerMain( decl );
+		}
+	}
 };
+
+} // namespace
+
+bool FixMain::isMain( FunctionDecl * decl ) {
+	// This breaks if you move it out of the function.
+	static const std::string mangled_mains[] = {
+		mangled_0_argument_main(),
+		mangled_2_argument_main(),
+		//mangled_3_argument_main(),
+	};
+
+	if ( std::string("main") != decl->name ) {
+		return false;
+	}
+	auto mangled_name = SymTab::Mangler::mangle( decl, true, true );
+	for ( auto main_name : mangled_mains ) {
+		if ( main_name == mangled_name ) return true;
+	}
+	return false;
+}
+
+void FixMain::findMain( std::list< Declaration * > & translationUnit ) {
+	PassVisitor< FindMainCore > mainFinder;
+	acceptAll( translationUnit, mainFinder );
+}
+
+};
Index: src/CodeGen/FixMain.h
===================================================================
--- src/CodeGen/FixMain.h	(revision c600df18f9fbbecdee5b62a88fa82ac6773b2d67)
+++ src/CodeGen/FixMain.h	(revision f42fc138a94bc24e5cc4c91a0dcf40b7e1fb4b66)
@@ -9,7 +9,7 @@
 // Author           : Thierry Delisle
 // Created On       : Thr Jan 12 14:11:09 2017
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Sun Feb 16 03:24:32 2020
-// Update Count     : 5
+// Last Modified By : Andrew Beach
+// Last Modified On : Fri Oct 29 11:07:00 2021
+// Update Count     : 6
 //
 
@@ -18,7 +18,9 @@
 #include <iosfwd>
 #include <memory>
+#include <list>
 
 #include "SynTree/LinkageSpec.h"
 
+class Declaration;
 class FunctionDecl;
 
@@ -35,6 +37,9 @@
 
 		static void registerMain(FunctionDecl* val);
+		static bool isMain(FunctionDecl* decl);
 
 		static void fix(std::ostream &os, const char* bootloader_filename);
+
+		static void findMain( std::list< Declaration * > & decls );
 
 	  private:
Index: src/CodeGen/FixNames.cc
===================================================================
--- src/CodeGen/FixNames.cc	(revision c600df18f9fbbecdee5b62a88fa82ac6773b2d67)
+++ src/CodeGen/FixNames.cc	(revision f42fc138a94bc24e5cc4c91a0dcf40b7e1fb4b66)
@@ -9,7 +9,7 @@
 // Author           : Richard C. Bilson
 // Created On       : Mon May 18 07:44:20 2015
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Fri Dec 13 23:39:14 2019
-// Update Count     : 21
+// Last Modified By : Andrew Beach
+// Last Modified On : Fri Oct 29 14:06:00 2021
+// Update Count     : 22
 //
 
@@ -46,53 +46,4 @@
 	};
 
-	std::string mangle_main() {
-		FunctionType* main_type;
-		std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,
-																   main_type = new FunctionType( Type::Qualifiers(), true ), nullptr )
-				};
-		main_type->get_returnVals().push_back(
-			new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
-		);
-
-		auto && name = SymTab::Mangler::mangle( mainDecl.get() );
-		// std::cerr << name << std::endl;
-		return std::move(name);
-	}
-	std::string mangle_main_args() {
-		FunctionType* main_type;
-		std::unique_ptr<FunctionDecl> mainDecl { new FunctionDecl( "main", Type::StorageClasses(), LinkageSpec::Cforall,
-																   main_type = new FunctionType( Type::Qualifiers(), false ), nullptr )
-				};
-		main_type->get_returnVals().push_back(
-			new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
-		);
-
-		main_type->get_parameters().push_back(
-			new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr )
-		);
-
-		main_type->get_parameters().push_back(
-			new ObjectDecl( "", Type::StorageClasses(), 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 std::move(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 ) {
 		PassVisitor<FixNames> fixer;
@@ -118,5 +69,5 @@
 		fixDWT( functionDecl );
 
-		if(is_main( SymTab::Mangler::mangle(functionDecl, true, true) )) {
+		if ( FixMain::isMain( functionDecl ) ) {
 			int nargs = functionDecl->get_functionType()->get_parameters().size();
 			if( !(nargs == 0 || nargs == 2 || nargs == 3) ) {
@@ -124,5 +75,4 @@
 			}
 			functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( new ConstantExpr( Constant::from_int( 0 ) ) ) );
-			CodeGen::FixMain::registerMain( functionDecl );
 		}
 	}
Index: src/main.cc
===================================================================
--- src/main.cc	(revision c600df18f9fbbecdee5b62a88fa82ac6773b2d67)
+++ src/main.cc	(revision f42fc138a94bc24e5cc4c91a0dcf40b7e1fb4b66)
@@ -10,6 +10,6 @@
 // Created On       : Fri May 15 23:12:02 2015
 // Last Modified By : Andrew Beach
-// Last Modified On : Fri Oct 22 16:06:00 2021
-// Update Count     : 653
+// Last Modified On : Fri Oct 29 10:10:00 2021
+// Update Count     : 654
 //
 
@@ -471,4 +471,5 @@
 		PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ) );
 
+		CodeGen::FixMain::findMain( translationUnit );
 		CodeGen::FixMain::fix( *output, (PreludeDirector + "/bootloader.c").c_str() );
 		if ( output != &cout ) {
