title: C unchecked function return undefined behavior
body: <p>In c, this pattern is fairly common:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  int init_ptr_or_return_err(int *p) {
    srand(time(NULL));
    // random to make code compile/demonstrate the question
    int error = rand() % 2;
    if (error) {
      return 1;
    }
    *p = 10;
    return 0;
  }
  
  int main() {
    int a;
    init_ptr_or_return_err(&amp;a);
    printf("a = %d\n", a);
    return 0;
  }
  </code></pre>
  
  <p>In the main function above, without checking the return code of the function, accessing the value of a <em>might</em> be undefined at runtime (but this is not statically determinable). So, it is usually wrapped in a block such as:</p>
  
  <pre><code>if (init_ptr_or_return_err(&amp;a)) {
    // error handling
  } else {
    // access a
  }
  </code></pre>
  
  <p>In this case, the compiler knows that a is initialized in the else because the function returns 0 if and only if it sets a. So, technically, accessing a in the else is defined, but accessing a in the if is undefined. However, <code>return 0</code> could easily be "return some fixed, but statically unknown value from a file" (and then check just that value before accessing a). So in either case, it isn't statically determinable whether a is initialized or not.</p>
  
  <p>Therefore, it seems to me like in general, the compiler cannot statically decide if this is undefined behavior or not and therefore should not be able to e.g. optimize it out.</p>
  
  <p>What are the exact semantics of such code (is it undefined behavior, something else, or is there a difference between static and runtime undefined behavior) and where does the standard specify this? If this is not defined by the standard, I am using gcc, so answers in the context of gcc would be helpful.</p>