configure.ac: improve detection of optimisation support
Missing support is only a compiler warning, so we need to force
-Werror on to be able to detect it.
Convert to AC_COMPILE_IFELSE and AC_LANG_PROGRAM so we can fiddle
with CFLAGS, and also convert to AC_CACHE_CHECK since we're
refactoring anyway...
Part of #3265