title: GCC optimization of (char) pointers makes no sense
body: <p>I am using STM32 Cube IDE version 1.8.0 for a STM32 MCU project.
  Builder settings are default. It is set to &quot;External builder&quot;.</p>
  <blockquote>
  <p>arm-none-eabi-gcc --version<br/>
  arm-none-eabi-gcc (GNU Tools for STM32 9-2020-q2-update.20201001-1621) 9.3.1 20200408 (release)
  Copyright (C) 2019 Free Software Foundation, Inc.</p>
  </blockquote>
  <p>In Properties-&gt;C/C++ Build-&gt;Settings-&gt;Tool Settings the MCU GCC compiler command is <code>gcc -c</code> and the content of &quot;All options&quot; is</p>
  <p><code>-mcpu=cortex-m4 -std=gnu11 '-D__weak=__attribute__((weak))' '-D__packed=__attribute__((__packed__))' -DUSE_HAL_DRIVER -DSTM32F412Rx -c -I../Drivers/STM32F4xx_HAL_Driver/Inc -I../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy -I../Middlewares/Third_Party/FreeRTOS/Source/include -I../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2 -I../Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F -I../Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc -I../Drivers/CMSIS/Device/ST/STM32F4xx/Include -I../Drivers/CMSIS/Include -Os -ffunction-sections -fdata-sections -Wall -fstack-usage --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -mthumb</code></p>
  <p>Pay attention to <code>-Os</code>!</p>
  <p>Now, if I use the following code (snippet)</p>
  <pre><code>bool ESP32_WifiCommCmds_ConnectToAP ( char* ssid, char* password, char* mac )
  {
      char cmd [ 96 ] = { 0 };
      char *ssidEscaped, *passwordEscaped;
      ESP32_ModuleCommCmds_EscapeSpecialCharacters ( ssid, &amp;ssidEscaped );
      if ( ssidEscaped == NULL )
      {
          return false;
      }
      ESP32_ModuleCommCmds_EscapeSpecialCharacters ( password, &amp;passwordEscaped );
      if ( passwordEscaped == NULL )
      {
          free ( ssidEscaped );
          return false;
      }
      if ( strlen(ssidEscaped) + strlen(passwordEscaped) + strlen(mac) + 17 &gt; sizeof(cmd) )
      {
          free ( ssidEscaped );
          free ( passwordEscaped );
          return false;
      }
      // THE FOLLOWING IF ELSE WILL GET OPTIMIZED !!!!!!
      if ( mac == NULL )
      {
          // THIS WILL GET OPTIMIZED OUT
          sprintf ( cmd, &quot;AT+CWJAP=\&quot;%s\&quot;,\&quot;%s\&quot;,,,1&quot;, ssidEscaped, passwordEscaped );
      }
      else
      {
          // THIS WILL STAY
          sprintf ( cmd, &quot;AT+CWJAP=\&quot;%s\&quot;,\&quot;%s\&quot;,\&quot;%s\&quot;,,1&quot;, ssidEscaped, passwordEscaped, mac );
      }
      free ( ssidEscaped );
      free ( passwordEscaped );
      char buffer [ 48 ] = { 0 };
      // Send cmd to ESP is left out here, for simplification
  }
  </code></pre>
  <p>And in the business logic, call this function with only:
  <code>success = ESP32_WifiCommCmds_ConnectToAP ( data-&gt;ssid, data-&gt;password, NULL );</code></p>
  <p>One would suggest, that the optimized code will remove the <code>if ( mac == NULL )</code> because it will always be <code>true</code>. But in fact the opposite is the case.</p>
  <p>The issue lies in the line with <code>strlen(mac)</code> but I do not know why this is. I need explanaition because I am afraid whatever I do wrong here, will have not yet seen impact on the rest of my code.</p>
  <p>PS: If you want to check for yourself, simply search the resulting binary to include the &quot;AT+CWJAP=...&quot; strings. Only one of them is in there (except you switch off the optimisations).</p>