title: Why is this call to a pure function with a string literal argument not optimized to a constant value?
body: <p>I have a simple function which counts the letter <strong>t</strong> in a string:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  static int count_t_letters(const char *t) {
      int r;
  
      r = 0;
      while(*t) {
          if(*t == 't') {
              ++r;
          }
  
          ++t;
      }
  
      return r;
  }
  
  int main() {
      printf("%i", count_t_letters("test"));
  }
  </code></pre>
  
  <p>here's the optimization I was expecting:</p>
  
  <pre><code>int main() {
      printf("%i", 2);
  }
  </code></pre>
  
  <p>Why is this simple function not optimized like I expected in neither gcc nor clang? (<a href="https://godbolt.org/z/zi4Hxq" rel="nofollow noreferrer">godbolt</a>)</p>
  
  <p>What I figured out so far:</p>
  
  <ul>
  <li>Simple functions with integer pointer arguments are optimized to a constant
  (<a href="https://godbolt.org/z/e3lGUT" rel="nofollow noreferrer">godbolt</a>)</li>
  <li>Using C++ with a constexpr enables this optimization (<a href="https://godbolt.org/z/XR8hvU" rel="nofollow noreferrer">godbolt</a>)</li>
  <li>Clang is able to do such an optimization if there is no <code>++t</code> after the <code>if</code> (<a href="https://godbolt.org/z/TH9aqQ" rel="nofollow noreferrer">godbolt</a>)</li>
  </ul>