Index: src/InitTweak/FixInit.cc
===================================================================
--- src/InitTweak/FixInit.cc	(revision 5b2f5bb4b0269a726d7840b6f270858c3ff12d9a)
+++ src/InitTweak/FixInit.cc	(revision e0323a238ee2dc0c70557f1cae4fd4fb3899b3bf)
@@ -10,5 +10,5 @@
 // Created On       : Wed Jan 13 16:29:30 2016
 // Last Modified By : Rob Schluntz
-// Last Modified On : Wed Mar 30 14:34:46 2016
+// Last Modified On : Thu Mar 31 10:08:49 2016
 // Update Count     : 30
 //
@@ -28,4 +28,5 @@
 	namespace {
 		const std::list<Label> noLabels;
+		const std::list<Expression*> noDesignators;
 	}
 
@@ -56,6 +57,52 @@
 			assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() );
 			if ( Statement * ctor = ctorInit->get_ctor() ) {
-				stmtsToAddAfter.push_back( ctor );
-				dtorStmts.push_front( ctorInit->get_dtor() );
+				if ( objDecl->get_storageClass() == DeclarationNode::Static ) {
+					// generate:
+					// static bool __objName_uninitialized = true;
+					// if (__objName_uninitialized) {
+					//   __ctor(__objName);
+					//   void dtor_atexit() {
+					//     __dtor(__objName);
+					//   }
+					//   on_exit(dtorOnExit, &__objName);
+					//   __objName_uninitialized = false;
+					// }
+
+					// generate first line
+					BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool );
+					SingleInit * boolInitExpr = new SingleInit( new ConstantExpr( Constant( boolType->clone(), "1" ) ), noDesignators );
+					ObjectDecl * isUninitializedVar = new ObjectDecl( objDecl->get_mangleName() + "_uninitialized", DeclarationNode::Static, LinkageSpec::Cforall, 0, boolType, boolInitExpr );
+					isUninitializedVar->fixUniqueId();
+
+					// void dtor_atexit(...) {...}
+					FunctionDecl * dtorCaller = new FunctionDecl( objDecl->get_mangleName() + "_dtor_atexit", DeclarationNode::NoStorageClass, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
+					dtorCaller->fixUniqueId();
+					dtorCaller->get_statements()->get_kids().push_back( ctorInit->get_dtor() );
+
+					// on_exit(dtor_atexit);
+					UntypedExpr * callAtexit = new UntypedExpr( new NameExpr( "atexit" ) );
+					callAtexit->get_args().push_back( new VariableExpr( dtorCaller ) );
+
+					// __objName_uninitialized = false;
+					UntypedExpr * setTrue = new UntypedExpr( new NameExpr( "?=?" ) );
+					setTrue->get_args().push_back( new VariableExpr( isUninitializedVar ) );
+					setTrue->get_args().push_back( new ConstantExpr( Constant( boolType->clone(), "0" ) ) );
+
+					// generate body of if
+					CompoundStmt * initStmts = new CompoundStmt( noLabels );
+					std::list< Statement * > & body = initStmts->get_kids();
+					body.push_back( ctor );
+					body.push_back( new DeclStmt( noLabels, dtorCaller ) );
+					body.push_back( new ExprStmt( noLabels, callAtexit ) );
+					body.push_back( new ExprStmt( noLabels, setTrue ) );
+
+					// put it all together
+					IfStmt * ifStmt = new IfStmt( noLabels, new VariableExpr( isUninitializedVar ), initStmts, 0 );
+					stmtsToAddAfter.push_back( new DeclStmt( noLabels, isUninitializedVar ) );
+					stmtsToAddAfter.push_back( ifStmt );
+				} else {
+					stmtsToAddAfter.push_back( ctor );
+					dtorStmts.push_front( ctorInit->get_dtor() );
+				}
 				objDecl->set_init( NULL );
 				ctorInit->set_ctor( NULL );
