title: Does `const T *restrict` guarantee the object pointed-to isn’t modified?
body: <p>Consider the following code:</p>
  
  <pre><code>void doesnt_modify(const int *);
  
  int foo(int *n) {
      *n = 42;
      doesnt_modify(n);
      return *n;
  }
  </code></pre>
  
  <p>where the definition of <code>doesnt_modify</code> isn’t visible for the compiler. Thus, it must assume, that <code>doesnt_modify</code> changes the object <code>n</code> points to and must read <code>*n</code> before the <code>return</code> (the last line cannot be replaced by <code>return 42;</code>).</p>
  
  <p>Assume, <code>doesnt_modify</code> doesn’t modify <code>*n</code>. I thought about the following to allow the optimization:</p>
  
  <pre><code>int foo_r(int *n) {
      *n = 42;
      { /* New scope is important, I think. */
          const int *restrict n_restr = n;
          doesnt_modify(n_restr);
          return *n_restr;
      }
  }
  </code></pre>
  
  <p>This has the drawback that the caller of <code>doesnt_modify</code> has to tell the compiler <code>*n</code> isn’t modified, rather than that the function itself could tell the compiler via its prototype. Simply <code>restrict</code>-qualifying the parameter to <code>doesnt_modify</code> in the declaration doesn’t suffice, cf. <a href="https://stackoverflow.com/questions/28819855/is-top-level-volatile-or-restrict-significant-in-a-function-prototype">“Is top-level <code>volatile</code> or <code>restrict</code> significant [...]?”</a>.</p>
  
  <p>When compiling with <code>gcc -std=c99 -O3 -S</code> (or Clang with the same options), all functions are compiled to equivalent assembly, all re-reading the <code>42</code> from <code>*n</code>.</p>
  
  <ol>
  <li><p>Would a compiler be allowed to do this optimization (replace the last line by <code>return 42;</code>) for <code>foo_r</code>? If not, is there a (portable, if possible) way to tell the compiler <code>doesnt_modify</code> doesn’t modify what its argument points to? Is there a way compilers do understand and make use of?</p></li>
  <li><p>Does any function have UB (provided <code>doesnt_modify</code> doesn’t modify its argument’s pointee)?</p></li>
  </ol>
  
  <p>Why I think, <code>restrict</code> could help here (From C11 (n1570) 6.7.3.1 “Formal definition of <code>restrict</code>”, p4 [emph. mine]):</p>
  
  <p>[In this case, <code>B</code> is the inner block of <code>foo_r</code>, <code>P</code> is <code>n_restr</code>, <code>T</code> is <code>const int</code>, and <code>X</code> is the object denoted by <code>*n</code>, I think.]</p>
  
  <blockquote>
    <p>During each execution of <code>B</code>, let <code>L</code> be any lvalue that has <code>&amp;L</code> based on <code>P</code>. If <code>L</code> is used to access the value of the object <code>X</code> that it designates, and <strong><code>X</code> is also modified (by any means)</strong>, then the following requirements apply: <strong><code>T</code> shall not be const-qualified</strong>. […]</p>
  </blockquote>
  
  <pre><code>$ clang --version
  Ubuntu clang version 3.5.0-4ubuntu2 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
  Target: x86_64-pc-linux-gnu
  </code></pre>
  
  <p>Gcc version is 4.9.2, on an x86 32bit target.</p>