diff --git a/configure.ac b/configure.ac
index 6bbb800db73385cdbd6030e5ea38a548677318dc..48fc27aaced34b383fdee2d5100a59035e2dd8af 100644
--- a/configure.ac
+++ b/configure.ac
@@ -432,14 +432,8 @@ HAVEVECTORIZATION=0
 
 if test "$enable_opt" = "yes" ; then
 
-   # Add code optimisation flags and tuning to host. This is a funny macro
-   # that does not like CFLAGS being already set. Work around that as we have
-   # at least set it to "", so it is set.
-   ac_test_CFLAGS="no"
-   old_CFLAGS="$CFLAGS"
+   # Choose the best flags for this compiler and architecture
    AX_CC_MAXOPT
-   ac_test_CFLAGS="yes"
-   CFLAGS="$old_CFLAGS $CFLAGS"
 
    # Check SSE & AVX support (some overlap with AX_CC_MAXOPT).
    # Don't use the SIMD_FLAGS result with Intel compilers. The -x<code>
diff --git a/m4/ax_cc_maxopt.m4 b/m4/ax_cc_maxopt.m4
index 7523d9d09ee741914fe9fe1cb22d3e0550763233..72407f3b6a5f29758fbb7b41afda9357ce4210ec 100644
--- a/m4/ax_cc_maxopt.m4
+++ b/m4/ax_cc_maxopt.m4
@@ -1,5 +1,5 @@
 # ===========================================================================
-#       http://www.gnu.org/software/autoconf-archive/ax_cc_maxopt.html
+#       https://www.gnu.org/software/autoconf-archive/ax_cc_maxopt.html
 # ===========================================================================
 #
 # SYNOPSIS
@@ -40,7 +40,7 @@
 #   Public License for more details.
 #
 #   You should have received a copy of the GNU General Public License along
-#   with this program. If not, see <http://www.gnu.org/licenses/>.
+#   with this program. If not, see <https://www.gnu.org/licenses/>.
 #
 #   As a special exception, the respective Autoconf Macro's copyright owner
 #   gives unlimited permission to copy, distribute and modify the configure
@@ -55,7 +55,7 @@
 #   modified version of the Autoconf Macro, you may extend this special
 #   exception to the GPL to apply to your modified version as well.
 
-#serial 16
+#serial 18
 
 AC_DEFUN([AX_CC_MAXOPT],
 [
@@ -68,19 +68,18 @@ AC_ARG_ENABLE(portable-binary, [AS_HELP_STRING([--enable-portable-binary], [disa
 
 # Try to determine "good" native compiler flags if none specified via CFLAGS
 if test "$ac_test_CFLAGS" != "set"; then
-  CFLAGS=""
   case $ax_cv_c_compiler_vendor in
-    dec) CFLAGS="-newc -w0 -O5 -ansi_alias -ansi_args -fp_reorder -tune host"
+    dec) CFLAGS="$CFLAGS -newc -w0 -O5 -ansi_alias -ansi_args -fp_reorder -tune host"
 	 if test "x$acx_maxopt_portable" = xno; then
            CFLAGS="$CFLAGS -arch host"
          fi;;
 
-    sun) CFLAGS="-native -fast -xO5 -dalign"
+    sun) CFLAGS="$CFLAGS -native -fast -xO5 -dalign"
 	 if test "x$acx_maxopt_portable" = xyes; then
 	   CFLAGS="$CFLAGS -xarch=generic"
          fi;;
 
-    hp)  CFLAGS="+Oall +Optrs_ansi +DSnative"
+    hp)  CFLAGS="$CFLAGS +Oall +Optrs_ansi +DSnative"
 	 if test "x$acx_maxopt_portable" = xyes; then
 	   CFLAGS="$CFLAGS +DAportable"
 	 fi;;
@@ -91,8 +90,8 @@ if test "$ac_test_CFLAGS" != "set"; then
            xlc_opt="-qtune=auto"
 	 fi
          AX_CHECK_COMPILE_FLAG($xlc_opt,
-		CFLAGS="-O3 -qansialias -w $xlc_opt",
-               [CFLAGS="-O3 -qansialias -w"
+		CFLAGS="$CFLAGS -O3 -qansialias -w $xlc_opt",
+               [CFLAGS="$CFLAGS -O3 -qansialias -w"
                 echo "******************************************************"
                 echo "*  You seem to have the IBM  C compiler.  It is      *"
                 echo "*  recommended for best performance that you use:    *"
@@ -105,7 +104,7 @@ if test "$ac_test_CFLAGS" != "set"; then
                 echo "******************************************************"])
          ;;
 
-    intel) CFLAGS="-O3 -ansi-alias"
+    intel) CFLAGS="$CFLAGS -O3 -ansi-alias"
 	if test "x$acx_maxopt_portable" = xno; then
 	  icc_archflag=unknown
 	  icc_flags=""
@@ -151,7 +150,7 @@ if test "$ac_test_CFLAGS" != "set"; then
 
     clang)
      # default optimization flags for clang on all systems
-     CFLAGS="-O3 -fomit-frame-pointer"
+     CFLAGS="$CFLAGS -O3 -fomit-frame-pointer"
 
      # Always good optimisation to have
      AX_CHECK_COMPILE_FLAG(-fstrict-aliasing, CFLAGS="$CFLAGS -fstrict-aliasing")
@@ -167,7 +166,7 @@ if test "$ac_test_CFLAGS" != "set"; then
 
     gnu)
      # default optimization flags for gcc on all systems
-     CFLAGS="-O3 -fomit-frame-pointer"
+     CFLAGS="$CFLAGS -O3 -fomit-frame-pointer"
 
      # -malign-double for x86 systems
      AX_CHECK_COMPILE_FLAG(-malign-double, CFLAGS="$CFLAGS -malign-double")
@@ -187,7 +186,7 @@ if test "$ac_test_CFLAGS" != "set"; then
 
     microsoft)
      # default optimization flags for MSVC opt builds
-     CFLAGS="-O2"
+     CFLAGS="$CFLAGS -O2"
      ;;
   esac
 
@@ -199,7 +198,7 @@ if test "$ac_test_CFLAGS" != "set"; then
 	echo "* (otherwise, a default of CFLAGS=-O3 will be used)    *"
 	echo "********************************************************"
 	echo ""
-        CFLAGS="-O3"
+        CFLAGS="$CFLAGS -O3"
   fi
 
   AX_CHECK_COMPILE_FLAG($CFLAGS, [], [
@@ -210,7 +209,6 @@ if test "$ac_test_CFLAGS" != "set"; then
         echo "* Use ./configure CFLAGS=... to specify your own flags *"
         echo "********************************************************"
         echo ""
-        CFLAGS=""
   ])
 
 fi