title: Socket code fails when compiled with -O flag
body: <p>I'm on Ubuntu 16.04 with gcc version 5.4.0.  I have a fairly simple sockets example in C that fails when I compile with optimization (-O) (it works with no optimization).  I trimmed my original code to:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;poll.h&gt;
  
  int main() {
      struct addrinfo *ai, hints;
      memset(&amp;hints, 0, sizeof hints);
      getaddrinfo(NULL, "7471", &amp;hints, &amp;ai);
  
      int listen_fd = socket(ai-&gt;ai_family, SOCK_STREAM, 0);
      bind(listen_fd, ai-&gt;ai_addr, ai-&gt;ai_addrlen);
      freeaddrinfo(ai);
  
      listen(listen_fd, 128);
  
      struct pollfd fds;
      fds.fd = listen_fd;
      fds.events = POLLIN;
      poll(&amp;fds, -1, -1);
  }
  </code></pre>
  
  <p>The compiler has problems with the call to poll().  The warning message is</p>
  
  <pre><code>in function ‘poll’,
      inlined from ‘main’ at simplecode.c:25:5:
  /usr/include/x86_64-linux-gnu/bits/poll2.h:43:9: warning: call to ‘__poll_chk_warn’ declared with attribute warning: poll called with fds buffer too small file nfds entries
    return __poll_chk_warn (__fds, __nfds, __timeout, __bos (__fds));
  </code></pre>
  
  <p>The actual runtime error is longer, but starts with:</p>
  
  <pre><code>*** buffer overflow detected ***: ./simplecode terminated
  </code></pre>
  
  <p>Any ideas?</p>