diff --git a/m4/dovecot.m4 b/m4/dovecot.m4
index 69eb1c7d04b8fc5ad19f63a58e1eabad3e3afb4f..aeff3b802bba6f836e9697c9b25a6784abced6dd 100644
--- a/m4/dovecot.m4
+++ b/m4/dovecot.m4
@@ -6,7 +6,7 @@ dnl This file is free software; the authors give
 dnl unlimited permission to copy and/or distribute it, with or without
 dnl modifications, as long as this notice is preserved.
 
-# serial 36
+# serial 37
 
 dnl
 dnl Check for support for D_FORTIFY_SOURCE=2
@@ -30,56 +30,56 @@ AC_DEFUN([AC_CC_D_FORTIFY_SOURCE],[
 
 dnl * gcc specific options
 AC_DEFUN([DC_DOVECOT_CFLAGS],[
-   AC_PROG_CC_C99
-   AS_IF([test "$ac_cv_prog_cc_c99" = "no"], [
-      AC_MSG_ERROR(C99 capable compiler required)
-   ])
+  m4_version_prereq(2.70, [AC_PROG_CC], [AC_PROG_CC_C99])
 
-   AC_MSG_CHECKING([Which $CC -std flag to use])
-   old_cflags=$CFLAGS
-   std=
-   for mystd in gnu11 gnu99 c11 c99; do
-     CFLAGS="-std=$mystd"
-     AC_COMPILE_IFELSE([AC_LANG_PROGRAM()
-     ], [
-        CFLAGS="$CFLAGS $old_cflags"
-	std=$mystd
-        break
-     ], [
-       CFLAGS="$old_cflags"
-     ])
-   done
-   AC_MSG_RESULT($std)
-
-   AS_IF([test "x$ac_cv_c_compiler_gnu" = "xyes"], [
-        dnl -Wcast-qual -Wcast-align -Wconversion -Wunreachable-code # too many warnings
-        dnl -Wstrict-prototypes -Wredundant-decls # may give warnings in some systems
-        dnl -Wmissing-format-attribute -Wmissing-noreturn -Wwrite-strings # a couple of warnings
-        CFLAGS="$CFLAGS -Wall -W -Wmissing-prototypes -Wmissing-declarations -Wpointer-arith -Wchar-subscripts -Wformat=2 -Wbad-function-cast"
-
-        AS_IF([test "$have_clang" = "yes"], [
-          AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
-          #if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 3)
-          #  error new clang
-          #endif
-          ]], [[]])],[],[
-            dnl clang 3.3+ unfortunately this gives warnings with hash.h
-            CFLAGS="$CFLAGS -Wno-duplicate-decl-specifier"
-          ])
-        ], [
-          dnl This is simply to avoid warning when building strftime() wrappers..
-          CFLAGS="$CFLAGS -fno-builtin-strftime"
-        ])
-
-        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
-        #if __GNUC__ < 4
-        #  error old gcc
-        #endif
-        ]], [[]])],[
-          dnl gcc4
-          CFLAGS="$CFLAGS -Wstrict-aliasing=2"
-        ],[])
+  AS_IF([test "$ac_prog_cc_stdc" = "c89" || test "$ac_prog_cc_std" = "no" || test "$ac_cv_prog_cc_c99" = "no"], [
+    AC_MSG_ERROR(C99 capable compiler required)
+  ])
+
+  AC_MSG_CHECKING([Which $CC -std flag to use])
+  old_cflags=$CFLAGS
+  std=
+  for mystd in gnu11 gnu99 c11 c99; do
+    CFLAGS="-std=$mystd"
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM()
+    ], [
+      CFLAGS="$CFLAGS $old_cflags"
+      std=$mystd
+      break
+    ], [
+      CFLAGS="$old_cflags"
+    ])
+  done
+  AC_MSG_RESULT($std)
+
+  AS_IF([test "x$ac_cv_c_compiler_gnu" = "xyes"], [
+    dnl -Wcast-qual -Wcast-align -Wconversion -Wunreachable-code # too many warnings
+    dnl -Wstrict-prototypes -Wredundant-decls # may give warnings in some systems
+    dnl -Wmissing-format-attribute -Wmissing-noreturn -Wwrite-strings # a couple of warnings
+    CFLAGS="$CFLAGS -Wall -W -Wmissing-prototypes -Wmissing-declarations -Wpointer-arith -Wchar-subscripts -Wformat=2 -Wbad-function-cast"
+
+    AS_IF([test "$have_clang" = "yes"], [
+      AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+      #if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 3)
+      #  error new clang
+      #endif
+      ]], [[]])],[],[
+        dnl clang 3.3+ unfortunately this gives warnings with hash.h
+        CFLAGS="$CFLAGS -Wno-duplicate-decl-specifier"
+      ])
+    ], [
+      dnl This is simply to avoid warning when building strftime() wrappers..
+      CFLAGS="$CFLAGS -fno-builtin-strftime"
+    ])
 
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+      #if __GNUC__ < 4
+      #  error old gcc
+      #endif
+      ]], [[]])],[
+        dnl gcc4
+        CFLAGS="$CFLAGS -Wstrict-aliasing=2"
+      ],[])
   ])
 ])