diff --git a/Makefile.moddir_rules b/Makefile.moddir_rules
index 2836dd9086e59d9f0020d99bceab9210b31207db..c2985fd7881de6f7cf60d76a01beab47e48d1cd1 100644
--- a/Makefile.moddir_rules
+++ b/Makefile.moddir_rules
@@ -110,7 +110,7 @@ modules.link:
 	@for file in $(patsubst %,$(SUBDIR)/%,$(filter-out %.eo,$^)); do echo "INPUT (../$${file})" >> $@; done
 
 clean::
-	rm -f *.so *.o *.oo *.eo
+	rm -f *.so *.o *.oo *.eo *.i *.ii
 	rm -f .*.o.d .*.oo.d
 	rm -f *.s *.i
 	rm -f modules.link
diff --git a/Makefile.rules b/Makefile.rules
index babdb76c8cd0a037cf4c66a47ea33f511358fb25..92fca37c93d5def87e3d9df62977386554b9ab8d 100644
--- a/Makefile.rules
+++ b/Makefile.rules
@@ -3,7 +3,7 @@
 # 
 # Makefile rules
 #
-# Copyright (C) 2006, Digium, Inc.
+# Copyright (C) 2006-2008, Digium, Inc.
 #
 # Kevin P. Fleming <kpfleming@digium.com>
 #
@@ -11,7 +11,6 @@
 # the GNU General Public License
 #
 
-# Rules for various build phases.
 # Each command is preceded by a short comment on what to do.
 # Prefixing one or the other with @\# or @ or nothing makes the desired
 # behaviour. ECHO_PREFIX prefixes the comment, CMD_PREFIX prefixes the command.
@@ -21,16 +20,18 @@
 .PHONY: dist-clean
 
 # extra cflags to build dependencies. Recursively expanded.
-MAKE_DEPS= -MD -MT $@ -MF .$(subst /,_,$@).d -MP
+MAKE_DEPS=-MD -MT $@ -MF .$(subst /,_,$@).d -MP
 
 ifeq ($(NOISY_BUILD),)
     ECHO_PREFIX=@
     CMD_PREFIX=@
 else
-    ECHO_PREFIX=@\#
+    ECHO_PREFIX=@\# 
     CMD_PREFIX=
 endif
 
+OPTIMIZE?=-O6
+
 ifeq ($(findstring DONT_OPTIMIZE,$(MENUSELECT_CFLAGS)),)
     # More GSM codec optimization
     # Uncomment to enable MMXTM optimizations for x86 architecture CPU's
@@ -38,30 +39,75 @@ ifeq ($(findstring DONT_OPTIMIZE,$(MENUSELECT_CFLAGS)),)
     # ppro's, etc, as well as the AMD K6 and K7.  
     #K6OPT=-DK6OPT
 
-    OPTIMIZE?=-O6
     ASTCFLAGS+=$(OPTIMIZE)
 endif
 
-# build rules for various targets
+# shortcuts for common combinations of flags; these must be recursively expanded so that
+# per-target settings will be applied
+CC_CFLAGS=$(PTHREAD_CFLAGS) $(ASTCFLAGS)
+CXX_CFLAGS=$(PTHREAD_CFLAGS) $(filter-out -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations $(AST_DECLARATION_AFTER_STATEMENT),$(ASTCFLAGS))
+CC_LDFLAGS_SO=$(PTHREAD_CFLAGS) $(ASTLDFLAGS) $(SOLINK)
+CXX_LDFLAGS_SO=$(PTHREAD_CFLAGS) $(ASTLDFLAGS) $(SOLINK)
+CC_LIBS=$(PTHREAD_LIBS) $(LIBS)
+CXX_LIBS=$(PTHREAD_LIBS) $(LIBS)
+
+# determine whether to double-compile so that the optimizer can report code path problems
+# this is only done when developer mode and DONT_OPTIMIZE are both enabled
+# in that case, we run the preprocessor to produce a .i or .ii file from the source
+# code, then compile once with optimizer enabled (and the output to /dev/null),
+# and if that doesn't fail then compile again with optimizer disabled
+ifeq ($(findstring DONT_OPTIMIZE,$(MENUSELECT_CFLAGS))$(AST_DEVMODE),DONT_OPTIMIZEyes)
+COMPILE_DOUBLE=yes
+endif
+
+%.o: %.s
+	$(ECHO_PREFIX) echo "   [AS] $< -> $@"
+ifeq ($(COMPILE_DOUBLE),yes)
+	$(CMD_PREFIX) $(CC) -o /dev/null -c $< $(CC_CFLAGS) $(OPTIMIZE)
+endif
+	$(CMD_PREFIX) $(CC) -o $@ -c $< $(CC_CFLAGS)
+
 %.o: %.i
 	$(ECHO_PREFIX) echo "   [CCi] $< -> $@"
-	$(CMD_PREFIX) $(CC) -o $@ -c $< $(PTHREAD_CFLAGS) $(ASTCFLAGS) $(MAKE_DEPS)
+ifeq ($(COMPILE_DOUBLE),yes)
+	$(CMD_PREFIX) $(CC) -o /dev/null -c $< $(CC_CFLAGS) $(OPTIMIZE)
+endif
+	$(CMD_PREFIX) $(CC) -o $@ -c $< $(CC_CFLAGS)
 
 %.o: %.c
 	$(ECHO_PREFIX) echo "   [CC] $< -> $@"
-	$(CMD_PREFIX) $(CC) -o $@ -c $< $(PTHREAD_CFLAGS) $(ASTCFLAGS) $(MAKE_DEPS)
+ifeq ($(COMPILE_DOUBLE),yes)
+	$(CMD_PREFIX) $(CC) -o $(@:%.o=%.i) -E $< $(CC_CFLAGS) $(MAKE_DEPS)
+	$(CMD_PREFIX) $(CC) -o /dev/null -c $(@:%.o=%.i) $(CC_CFLAGS) $(OPTIMIZE)
+	$(CMD_PREFIX) $(CC) -o $@ -c $(@:%.o=%.i) $(CC_CFLAGS)
+else
+	$(CMD_PREFIX) $(CC) -o $@ -c $< $(CC_CFLAGS) $(MAKE_DEPS)
+endif
 
 %.i: %.c
 	$(ECHO_PREFIX) echo "   [CPP] $< -> $@"
-	$(CMD_PREFIX) $(CC) -o $@ -E $< $(PTHREAD_CFLAGS) $(ASTCFLAGS) $(MAKE_DEPS)
+	$(CMD_PREFIX) $(CC) -o $@ -E $< $(CC_CFLAGS) $(MAKE_DEPS)
 
-%.o: %.s
-	$(ECHO_PREFIX) echo "   [AS] $< -> $@"
-	$(CMD_PREFIX) $(CC) -o $@ -c $< $(PTHREAD_CFLAGS) $(ASTCFLAGS) $(MAKE_DEPS)
+%.oo: %.ii
+	$(ECHO_PREFIX) echo "   [CXXi] $< -> $@"
+ifeq ($(COMPILE_DOUBLE),yes)
+	$(CMD_PREFIX) $(CXX) -o /dev/null -c $< $(CXX_CFLAGS) $(OPTIMIZE)
+endif
+	$(CMD_PREFIX) $(CXX) -o $@ -c $< $(CXX_CFLAGS)
 
 %.oo: %.cc
 	$(ECHO_PREFIX) echo "   [CXX] $< -> $@"
-	$(CMD_PREFIX) $(CXX) -o $@ -c $< $(PTHREAD_CFLAGS) $(filter-out -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations $(AST_DECLARATION_AFTER_STATEMENT),$(ASTCFLAGS)) $(MAKE_DEPS)
+ifeq ($(COMPILE_DOUBLE),yes)
+	$(CMD_PREFIX) $(CXX) -o $(@:%.oo=%.ii) -E $< $(CXX_CFLAGS) $(MAKE_DEPS)
+	$(CMD_PREFIX) $(CXX) -o /dev/null -c $(@:%.oo=%.ii) $(CXX_CFLAGS) $(MAKE_DEPS) $(OPTIMIZE)
+	$(CMD_PREFIX) $(CXX) -o $@ -c $(@:%.oo=%.ii) $(CXX_CFLAGS) $(MAKE_DEPS)
+else
+	$(CMD_PREFIX) $(CXX) -o $@ -c $< $(CXX_CFLAGS) $(MAKE_DEPS)
+endif
+
+%.ii: %.cc
+	$(ECHO_PREFIX) echo "   [CPP] $< -> $@"
+	$(CMD_PREFIX) $(CXX) -o $@ -E $< $(CXX_CFLAGS) $(MAKE_DEPS)
 
 %.c: %.y
 	$(ECHO_PREFIX) echo "   [BISON] $< -> $@"
@@ -73,11 +119,11 @@ endif
 
 %.so: %.o
 	$(ECHO_PREFIX) echo "   [LD] $^ -> $@"
-	$(CMD_PREFIX) $(CC) $(STATIC_BUILD) -o $@ $(PTHREAD_CFLAGS) $(ASTLDFLAGS) $(SOLINK) $^ $(PTHREAD_LIBS) $(LIBS)
+	$(CMD_PREFIX) $(CC) $(STATIC_BUILD) -o $@ $(CC_LDFLAGS_SO) $^ $(CC_LIBS)
 
 %.so: %.oo
 	$(ECHO_PREFIX) echo "   [LDXX] $^ -> $@"
-	$(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(PTHREAD_CFLAGS) $(ASTLDFLAGS) $(SOLINK) $^ $(PTHREAD_LIBS) $(LIBS)
+	$(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(CXX_LDFLAGS_SO) $^ $(CXX_LIBS)
 
 %.eo: %.o
 	$(ECHO_PREFIX) echo "   [EMBED] $< -> $@"
@@ -93,6 +139,6 @@ endif
 
 %: %.o
 	$(ECHO_PREFIX) echo "   [LD] $^ -> $@"
-	$(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(PTHREAD_CFLAGS) $(ASTLDFLAGS) $^ $(PTHREAD_LIBS) $(LIBS)
+	$(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(PTHREAD_CFLAGS) $(ASTLDFLAGS) $^ $(CXX_LIBS)
 
 dist-clean:: clean
diff --git a/channels/misdn/Makefile b/channels/misdn/Makefile
index 85478225b8ee57abf06b43b7c7e22928499e764b..e277636e65bbb0dd52afbc68eae10c5fe3dd6c71 100644
--- a/channels/misdn/Makefile
+++ b/channels/misdn/Makefile
@@ -14,4 +14,4 @@ portinfo: portinfo.o
 	$(CC) -o $@ $^ -lisdnnet -lmISDN -lpthread
 
 clean: 
-	rm -rf *.a *.o *.so portinfo
+	rm -rf *.a *.o *.so portinfo *.i
diff --git a/codecs/gsm/Makefile b/codecs/gsm/Makefile
index d112a2558f15afe0aba6699f5a07df443286b209..81071e98c95b6a7f0f2d45dab93113ac825bb308 100644
--- a/codecs/gsm/Makefile
+++ b/codecs/gsm/Makefile
@@ -477,7 +477,7 @@ clean:	semi-clean
 			$(TOAST) $(TCAT) $(UNTOAST)	\
 			$(ROOT)/gsm-1.0.tar.Z
 		rm -rf lib
-		rm -f .*.d
+		rm -f .*.d *.i */*.i
 
 # Two tools that helped me generate gsm_encode.c and gsm_decode.c,
 # but aren't generally needed to port this.
diff --git a/main/db1-ast/Makefile b/main/db1-ast/Makefile
index 56657f88f09498feb2858ba02757656a78468290..c9d29b36967148b58e5b26a4920801aa8f6ce1a2 100644
--- a/main/db1-ast/Makefile
+++ b/main/db1-ast/Makefile
@@ -46,8 +46,7 @@ $(PROG): db_dump185.o $(LIBDBSO)
 clean-depend:
 
 clean:
-	rm -f $(LIBDB) $(LIBDBSO) $(OBJS) $(SHOBJS)
-	rm -f *.s *.i
+	rm -f $(LIBDB) $(LIBDBSO) $(OBJS) $(SHOBJS) */*.s */*.i
 
 ASTCFLAGS:=-Wall -D__DBINTERFACE_PRIVATE -I. -I.. -Iinclude -Ihash -Ibtree -Irecno $(ASTCFLAGS)
 
diff --git a/main/stdtime/Makefile b/main/stdtime/Makefile
index cbe3c48f70d5bf39d66dacb167b9f1165916e015..d4b7f0093832c741902db4b2d5c5e74e2f931448 100644
--- a/main/stdtime/Makefile
+++ b/main/stdtime/Makefile
@@ -14,7 +14,7 @@ clean-depend:
 	rm -f .depend
 
 clean: clean-depend
-	rm -f libtime.a *.o test
+	rm -f libtime.a *.o test *.i
 
 depend: .depend
 
diff --git a/pbx/Makefile b/pbx/Makefile
index 368f6f15a086979536bf0e2569297dea03dd8414..d161b1b18a02733b4dc0120a3321a0432c687682 100644
--- a/pbx/Makefile
+++ b/pbx/Makefile
@@ -24,7 +24,7 @@ ifneq ($(findstring $(OSARCH), mingw32 cygwin ),)
 endif
 
 clean::
-	rm -f ael/*.o
+	rm -f ael/*.o ael/*.i
 
 dundi-parser.o: dundi-parser.h
 dundi-parser.o: ASTCFLAGS+=-I.
diff --git a/res/Makefile b/res/Makefile
index 1beb8e5c7c880046eea7e48e74b6f003d3fb09f9..8084858eebb5b2bf4d2747daa40c716685aa3d9c 100644
--- a/res/Makefile
+++ b/res/Makefile
@@ -53,4 +53,4 @@ ael/ael.tab.c ael/ael.tab.h:
 ael/pval.o: ael/pval.c
 
 clean::
-	rm -f snmp/*.o ael/*.o ais/*.o
+	rm -f snmp/*.o snmp/*.i ael/*.o ael/*.i ais/*.o ais/*.i