SemaphoreTake timeout does not work with gcc optimization

You are right. I tried with CMSIS-OS native API (osSemaphoreId_t, osSemaphoreAcquire), only as well as the FreeRTOS semaphore API (SemaphoreHandle_t, xSemaphoreCreateBinary, xSemaphoreTake) and have the same issue.