diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 36ab9e32c7892f8a39f9cc25e76ae2430316e0a2..eab85704c9022cbe7edb1072685813263ed465ca 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -28,6 +28,23 @@ if(ZLIB_FOUND)
                INCLUDE_DIRECTORIES "${ZLIB_INCLUDE_DIR}")
 endif()
 
+# workaround for clang 6, which runs into an infinite loop on gitter_mgb.cc and gitter_pll_mgb.cc
+# when optimization level is -O2 or higher
+if(${CMAKE_CXX_COMPILER_ID} STREQUAL Clang AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL 6.0.0)
+  string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type_upper)
+  set(_flags "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${_build_type_upper}}")
+  string(REGEX MATCH " -O(2|3|fast) " _opt_level ${_flags})
+  if (NOT _opt_level STREQUAL "")
+    message(STATUS "detected clang 6 and optimized build: overriding optimization level for gitter_mgb.cc and gitter_pll_mgb.cc")
+    set_source_files_properties(
+      ../dune/alugrid/impl/serial/gitter_mgb.cc
+      ../dune/alugrid/impl/parallel/gitter_pll_mgb.cc
+      PROPERTIES
+        COMPILE_FLAGS -O1
+      )
+    endif()
+endif()
+
 ####################################################################
 #### general warning: avoid such and similar commands here
 #### as they will not influence anything after the library