title: Are compilers allowed to optimize out realloc?
body: <p>I came across a situation where it would be useful to have unnecessary calls to <code>realloc</code> being optimized out. However, it seems like neither Clang nor GCC do such a thing (<a href="https://godbolt.org/z/XmFnuq" rel="nofollow noreferrer">Compiler Explorer</a> (godbolt.org)) - although I see optimizations being made with multiple calls to <code>malloc</code>.</p>
  
  <p>The example:</p>
  
  <pre><code>void *myfunc() {
      void *data;
      data = malloc(100);
      data = realloc(data, 200);
      return data;
  }
  </code></pre>
  
  <p>I expected it to be optimized to something like the following:</p>
  
  <pre><code>void *myfunc() {
      return malloc(200);
  }
  </code></pre>
  
  <p>Why is neither Clang nor GCC optimizing it out? - Are they not allowed to do so?</p>