title: gcc and clang not willing to lazily initialize a partially-used struct
body: <p>Consider the following code:</p>
  
  <pre><code>__attribute__((noinline)) int foo1(int x, int y)
  {
      return x;
  }
  
  int bar1(int* a)
  {
      int b = foo1(a[5], a[10]);
      return b * b;
  }
  </code></pre>
  
  <p>Even though <code>foo1</code> is not inlined, a compiler can easily tell one of its parameters is unused; so it's not actually necessary to bother initializing it. And indeed, both GCC 8.2 and clang 7.0 compile <code>bar1()</code> to only read the first argument's value from memory.</p>
  
  <p>Now suppose we put those two int parameters into a struct:</p>
  
  <pre><code>struct two_ints { int x, y; };
  
  __attribute__((noinline)) int foo2(struct two_ints s)
  {
      return s.x;
  }
  
  int bar2(int* a)
  {
      struct two_ints ti = { a[5], a[10] };
      int b = foo2(ti);
      return b * b;
  }
  </code></pre>
  
  <p>There shouldn't be any difference, right? The parameters (certainly as far as the assembly is concerned) are two integers; and the second one is not used. So I would expect to see the same assembly code. Yet... I don't. Both clang and gcc initialize <em>both</em> fields of <code>ti</code> before calling <code>foo2()</code>.</p>
  
  <p><a href="https://gcc.godbolt.org/z/_U4X80" rel="nofollow noreferrer">Both code fragments with gcc &amp; clang</a> (GodBolt.org)</p>
  
  <p>So, is there anything obligating compilers to initialize both fields, or is this a missed optimization opportunity?</p>