Index: src/CodeGen/FixMain.cc
===================================================================
--- src/CodeGen/FixMain.cc	(revision f42fc138a94bc24e5cc4c91a0dcf40b7e1fb4b66)
+++ src/CodeGen/FixMain.cc	(revision 0c577f7b3078d0a929f6d13463746d71c745126a)
@@ -22,4 +22,6 @@
 #include <string>                  // for operator<<
 
+#include "AST/Decl.hpp"
+#include "AST/Type.hpp"
 #include "Common/PassVisitor.h"
 #include "Common/SemanticError.h"  // for SemanticError
@@ -105,4 +107,18 @@
 }
 
+bool is_main( const std::string & mangled_name ) {
+	// 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(),
+	};
+
+	for ( auto main_name : mangled_mains ) {
+		if ( main_name == mangled_name ) return true;
+	}
+	return false;
+}
+
 struct FindMainCore {
 	void previsit( FunctionDecl * decl ) {
@@ -116,19 +132,15 @@
 
 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 is_main( SymTab::Mangler::mangle( decl, true, true ) );
+}
+
+bool FixMain::isMain( const ast::FunctionDecl * decl ) {
+	if ( std::string("main") != decl->name ) {
+		return false;
 	}
-	return false;
+	return is_main( Mangle::mangle( decl, Mangle::Type ) );
 }
 
Index: src/CodeGen/FixMain.h
===================================================================
--- src/CodeGen/FixMain.h	(revision f42fc138a94bc24e5cc4c91a0dcf40b7e1fb4b66)
+++ src/CodeGen/FixMain.h	(revision 0c577f7b3078d0a929f6d13463746d71c745126a)
@@ -10,6 +10,6 @@
 // Created On       : Thr Jan 12 14:11:09 2017
 // Last Modified By : Andrew Beach
-// Last Modified On : Fri Oct 29 11:07:00 2021
-// Update Count     : 6
+// Last Modified On : Fri Oct 29 14:49:00 2021
+// Update Count     : 7
 //
 
@@ -24,4 +24,7 @@
 class Declaration;
 class FunctionDecl;
+namespace ast {
+	class FunctionDecl;
+}
 
 namespace CodeGen {
@@ -31,5 +34,5 @@
 			return replace_main ? LinkageSpec::Cforall : LinkageSpec::C;
 		}
-		
+
 		static inline void setReplaceMain(bool val) {
 			replace_main = val;
@@ -38,4 +41,5 @@
 		static void registerMain(FunctionDecl* val);
 		static bool isMain(FunctionDecl* decl);
+		static bool isMain(const ast::FunctionDecl * decl);
 
 		static void fix(std::ostream &os, const char* bootloader_filename);
Index: src/CodeGen/FixNames.cc
===================================================================
--- src/CodeGen/FixNames.cc	(revision f42fc138a94bc24e5cc4c91a0dcf40b7e1fb4b66)
+++ src/CodeGen/FixNames.cc	(revision 0c577f7b3078d0a929f6d13463746d71c745126a)
@@ -10,6 +10,6 @@
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Andrew Beach
-// Last Modified On : Fri Oct 29 14:06:00 2021
-// Update Count     : 22
+// Last Modified On : Fri Oct 29 15:49:00 2021
+// Update Count     : 23
 //
 
@@ -19,4 +19,7 @@
 #include <string>                  // for string, operator!=, operator==
 
+#include "AST/Chain.hpp"
+#include "AST/Expr.hpp"
+#include "AST/Pass.hpp"
 #include "Common/PassVisitor.h"
 #include "Common/SemanticError.h"  // for SemanticError
@@ -82,4 +85,56 @@
 		GuardAction( [this](){ scopeLevel--; } );
 	}
+
+/// Does work with the main function and scopeLevels.
+class FixNames_new : public ast::WithGuards {
+	int scopeLevel = 1;
+
+	bool shouldSetScopeLevel( const ast::DeclWithType * dwt ) {
+		return !dwt->name.empty() && dwt->linkage.is_mangled
+			&& dwt->scopeLevel != scopeLevel;
+	}
+public:
+	const ast::ObjectDecl *postvisit( const ast::ObjectDecl *objectDecl ) {
+		if ( shouldSetScopeLevel( objectDecl ) ) {
+			return ast::mutate_field( objectDecl, &ast::ObjectDecl::scopeLevel, scopeLevel );
+		}
+		return objectDecl;
+	}
+
+	const ast::FunctionDecl *postvisit( const ast::FunctionDecl *functionDecl ) {
+		// This store is used to ensure a maximum of one call to mutate.
+		ast::FunctionDecl * mutDecl = nullptr;
+
+		if ( shouldSetScopeLevel( functionDecl ) ) {
+			mutDecl = ast::mutate( functionDecl );
+			mutDecl->scopeLevel = scopeLevel;
+		}
+
+		if ( FixMain::isMain( functionDecl ) ) {
+			if ( !mutDecl ) { mutDecl = ast::mutate( functionDecl ); }
+
+			int nargs = mutDecl->params.size();
+			if ( 0 != nargs && 2 != nargs && 3 != nargs ) {
+				SemanticError( functionDecl, "Main expected to have 0, 2 or 3 arguments\n" );
+			}
+			ast::chain_mutate( mutDecl->stmts )->kids.push_back(
+				new ast::ReturnStmt(
+					mutDecl->location,
+					ast::ConstantExpr::from_int( mutDecl->location, 0 )
+				)
+			);
+		}
+		return mutDecl ? mutDecl : functionDecl;
+	}
+
+	void previsit( const ast::CompoundStmt * ) {
+		GuardValue( scopeLevel ) += 1;
+	}
+};
+
+void fixNames( ast::TranslationUnit & translationUnit ) {
+	ast::Pass<FixNames_new>::run( translationUnit );
+}
+
 } // namespace CodeGen
 
Index: src/CodeGen/FixNames.h
===================================================================
--- src/CodeGen/FixNames.h	(revision f42fc138a94bc24e5cc4c91a0dcf40b7e1fb4b66)
+++ src/CodeGen/FixNames.h	(revision 0c577f7b3078d0a929f6d13463746d71c745126a)
@@ -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 Jul 21 22:17:33 2017
-// Update Count     : 3
+// Last Modified By : Andrew Beach
+// Last Modified On : Tue Oct 26 13:47:00 2021
+// Update Count     : 4
 //
 
@@ -19,8 +19,12 @@
 
 class Declaration;
+namespace ast {
+	struct TranslationUnit;
+}
 
 namespace CodeGen {
 	/// mangles object and function names
 	void fixNames( std::list< Declaration* > & translationUnit );
+	void fixNames( ast::TranslationUnit & translationUnit );
 } // namespace CodeGen
 
