title: Why the following function doesn't generate tail recursion with Clang?
body: <p>Under clang the following function generates object code for a tail recursive function:</p>
  
  <pre><code>template&lt;typename T&gt;
  constexpr bool is_prime(T number, T limit, T counter)
  {
      return counter &gt;= limit
          ? number % limit != 0
          : number % counter
              ? is_prime(number, number / counter, counter + 2)
              : false;
  }
  
  template&lt;typename T&gt;
  constexpr bool is_prime(T n)
  {
      return n == 2 || n == 3 || n == 5
          ? true
          : n &lt;= 1 || n % 2 == 0
              ? false
              : is_prime(n, n / 3, T{3});
  }
  </code></pre>
  
  <p>but changing one line (letting a boolean result "non-normalized"), it ceases to generate tail recursive object code:</p>
  
  <pre><code>template&lt;typename T&gt;
  constexpr bool is_prime(T number, T limit, T counter)
  {
      return counter &gt;= limit
          ? number % limit    // changed here
          : number % counter
              ? is_prime(number, number / counter, counter + 2)
              : false;
  }
  
  template&lt;typename T&gt;
  constexpr bool is_prime(T n)
  {
      return n == 2 || n == 3 || n == 5
          ? true
          : n &lt;= 1 || n % 2 == 0
              ? false
              : is_prime(n, n / 3, T{3});
  }
  </code></pre>
  
  <p>Is it clang failing to optimize it correctly or there's a logical reason for so?</p>
  
  <p>To force runtime evaluation, <code>x</code> is a runtime integral prime <code>≥ 13</code> (one recursion) or a large enough constexpr prime that would prevent compile-time evaluation because of large recursion depth:</p>
  
  <pre><code>is_prime(x);
  </code></pre>