Fix gcc optimization level for debug builds

This fixes a minor "bug" I happened to notice.  For gcc, we were setting
the optimization level to -O2 for all builds instead of just for
CMAKE_BUILD_TYPE=release.  Debug builds should be -Og or -O0, otherwise
a debugger is not useful (source code no longer matches up 1-to-1 with
executable), defeating the purpose of CMAKE_BUILD_TYPE=debug.  The comment
makes clear that the intention was to lower the optimization in
release builds from -O3 to -O2, but the way this was done also raised
the optimization of debug builds (as well as all other build types) from
-Og to -O2
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7231565..1d9199e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -64,7 +64,7 @@
     SET(CMAKE_CXX_FLAGS_RELEASE "-O1 -DNDEBUG"
         CACHE STRING
         "Flags used by the CXX compiler during RELEASE builds." FORCE)
-    SET(CMAKE_CXX_FLAGS_DEBUG  "-O0 -g"
+    SET(CMAKE_CXX_FLAGS_DEBUG  "-Og -g"
         CACHE STRING
         "Flags used by the CXX compiler during DEBUG builds." FORCE)
     SET(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG"
@@ -129,7 +129,9 @@
     if((CMAKE_C_COMPILER_VERSION VERSION_EQUAL 5.0) OR (CMAKE_C_COMPILER_VERSION VERSION_GREATER 5.0))
         # Versions 5+ fail with the "Release" build type i.e. when optimization
         # level is -O3 or greater.
-        add_compile_options("-O2")
+        # For CXX11, we've already limited it to -O1 for similar reasons (causes crash)
+        SET(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG" CACHE STRING
+        "Flags used by the C compiler during RELEASE builds." FORCE)
     endif()
 
     # See comments below for C++:
@@ -175,13 +177,14 @@
         set(CMAKE_C_FLAGS "-std=gnu99 ${CMAKE_C_FLAGS}"
             CACHE STRING
             "Flags used by the compiler during all build types." FORCE)
+        if((CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 5.0) OR (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.0))
+            # Versions 5+ fail with the "Release" build type i.e. when optimization
+            # level is -O3 or greater.
+            SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG" CACHE STRING
+            "Flags used by the CXX compiler during RELEASE builds." FORCE)
+        endif()
     endif(CXX11)
 
-    if((CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 5.0) OR (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.0))
-        # Versions 5+ fail with the "Release" build type i.e. when optimization
-        # level is -O3 or greater.
-        add_compile_options("-O2")
-    endif()
     # We want to include some header files as system header files in order to
     # disable warnings. However, on Mac OS X, a CMake variable is not set
     # correctly on Mac OS X. http://www.cmake.org/Bug/view.php?id=10837