#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct sockaddr_storage {
  int /*<<< orphan*/ ss_family;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int /*<<< orphan*/ get_in_addr(struct sockaddr *);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
char *inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(void) {
  fd_set master;
  fd_set read_fds;
  int fdmax;
  int listener;
  int newfd;
  struct sockaddr_storage remoteaddr;
  socklen_t addrlen;
  char buf[256];
  int nbytes;
  char remoteIP[INET6_ADDRSTRLEN];
  int yes = 1;
  int i, j, rv;
  struct addrinfo hints, *ai, *p;
  FD_ZERO(&master);
  FD_ZERO(&read_fds);
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((rv = getaddrinfo(NULL, "9034", &hints, &ai)) != 0) {
    fprintf(stderr, "selectserver: %s\n", gai_strerror(rv));
    exit(1);
  }
  for (p = ai; p != NULL; p = p->ai_next) {
    listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (listener < 0) {
      continue;
    }
    setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) {
      close(listener);
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "selectserver: failed to bind\n");
    exit(2);
  }
  freeaddrinfo(ai);
  if (listen(listener, 10) == -1) {
    perror("listen");
    exit(3);
  }
  FD_SET(listener, &master);
  fdmax = listener;
  for (;;) {
    read_fds = master;
    if (select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1) {
      perror("select");
      exit(4);
    }
    for (i = 0; i <= fdmax; i++) {
      if (FD_ISSET(i, &read_fds)) {
        if (i == listener) {
          addrlen = sizeof remoteaddr;
          newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen);
          if (newfd == -1) {
            perror("accept");
          } else {
            FD_SET(newfd, &master);
            if (newfd > fdmax) {
              fdmax = newfd;
            }
            printf("selectserver: new connection from %s on "
                   "socket %d\n",
                   inet_ntop(remoteaddr.ss_family,
                             get_in_addr((struct sockaddr *)&remoteaddr),
                             remoteIP, INET6_ADDRSTRLEN),
                   newfd);
          }
        } else {
          if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
            if (nbytes == 0) {
              printf("selectserver: socket %d hung up\n", i);
            } else {
              perror("recv");
            }
            close(i);
            FD_CLR(i, &master);
          } else {
            for (j = 0; j <= fdmax; j++) {
              if (FD_ISSET(j, &master)) {
                if (j != listener && j != i) {
                  if (send(j, buf, nbytes, 0) == -1) {
                    perror("send");
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1a0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x140(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x198(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x148(%rbp)
movl   $0x1,-0x14c(%rbp)
lea    -0x8(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
lea    -0xc(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
lea    -0x180(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   10b0 <memset@plt>
mov    0x2ce3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x170(%rbp)
mov    0x2cbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x174(%rbp)
mov    0x2ccd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x160(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xce2(%rip),%rsi
lea    -0x180(%rbp),%rdx
lea    -0x188(%rbp),%rcx
call   1160 <getaddrinfo@plt>
mov    %eax,-0x158(%rbp)
cmp    $0x0,%eax
je     137b <main+0x10b>
mov    0x2c79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x19c(%rbp)
mov    -0x158(%rbp),%edi
call   10a0 <gai_strerror@plt>
mov    -0x19c(%rbp),%edi
mov    %rax,%rdx
lea    0xc9b(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0x1,%edi
call   1150 <exit@plt>
mov    -0x188(%rbp),%rax
mov    %rax,-0x190(%rbp)
cmpq   $0x0,-0x190(%rbp)
je     1441 <main+0x1d1>
mov    -0x190(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x190(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x190(%rbp),%rax
mov    0x8(%rax),%edx
call   1190 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    13cc <main+0x15c>
jmp    142a <main+0x1ba>
mov    -0x14(%rbp),%edi
mov    0x2c12(%rip),%rax
mov    (%rax),%esi
mov    0x2bd1(%rip),%rax
mov    (%rax),%edx
lea    -0x14c(%rbp),%rcx
mov    $0x4,%r8d
call   1060 <setsockopt@plt>
mov    -0x14(%rbp),%edi
mov    -0x190(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x190(%rbp),%rax
mov    (%rax),%edx
call   1110 <bind@plt>
cmp    $0x0,%rax
jge    1425 <main+0x1b5>
mov    -0x14(%rbp),%edi
call   10c0 <close@plt>
jmp    142a <main+0x1ba>
jmp    1441 <main+0x1d1>
mov    -0x190(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x190(%rbp)
jmp    1389 <main+0x119>
cmpq   $0x0,-0x190(%rbp)
jne    1470 <main+0x200>
mov    0x2b6a(%rip),%rax
mov    (%rax),%edi
lea    0xbb8(%rip),%rsi
mov    $0x0,%al
call   10d0 <fprintf@plt>
mov    $0x2,%edi
call   1150 <exit@plt>
mov    -0x188(%rbp),%rdi
call   1180 <freeaddrinfo@plt>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1100 <listen@plt>
cmp    $0xffffffff,%eax
jne    14a8 <main+0x238>
lea    0xb9c(%rip),%rdi
call   1120 <perror@plt>
mov    $0x3,%edi
call   1150 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1170 <FD_SET@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10f0 <select@plt>
cmp    $0xffffffff,%eax
jne    14f9 <main+0x289>
lea    0xb52(%rip),%rdi
call   1120 <perror@plt>
mov    $0x4,%edi
call   1150 <exit@plt>
movl   $0x0,-0x150(%rbp)
mov    -0x150(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     171b <main+0x4ab>
mov    -0x150(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1070 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1702 <main+0x492>
mov    -0x150(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    15d5 <main+0x365>
movl   $0x4,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1130 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    156f <main+0x2ff>
lea    0xade(%rip),%rdi
call   1120 <perror@plt>
jmp    15d0 <main+0x360>
mov    -0x18(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1170 <FD_SET@plt>
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    158d <main+0x31d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1a0(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <get_in_addr@plt>
mov    -0x1a0(%rbp),%edi
mov    -0x198(%rbp),%rdx
mov    %eax,%esi
mov    0x2a2b(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rsi
mov    -0x18(%rbp),%edx
lea    0xa81(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    16fd <main+0x48d>
mov    -0x150(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x134(%rbp)
cmp    $0x0,%eax
jg     164e <main+0x3de>
cmpl   $0x0,-0x134(%rbp)
jne    1623 <main+0x3b3>
mov    -0x150(%rbp),%esi
lea    0xa66(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    162f <main+0x3bf>
lea    0xa74(%rip),%rdi
call   1120 <perror@plt>
mov    -0x150(%rbp),%edi
call   10c0 <close@plt>
mov    -0x150(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1140 <FD_CLR@plt>
jmp    16f8 <main+0x488>
movl   $0x0,-0x154(%rbp)
mov    -0x154(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     16f3 <main+0x483>
mov    -0x154(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1070 <FD_ISSET@plt>
cmp    $0x0,%rax
je     16da <main+0x46a>
mov    -0x154(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     16d5 <main+0x465>
mov    -0x154(%rbp),%eax
cmp    -0x150(%rbp),%eax
je     16d5 <main+0x465>
mov    -0x154(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    -0x134(%rbp),%edx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0xffffffff,%eax
jne    16d0 <main+0x460>
lea    0x9d8(%rip),%rdi
call   1120 <perror@plt>
jmp    16d5 <main+0x465>
jmp    16da <main+0x46a>
jmp    16df <main+0x46f>
mov    -0x154(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x154(%rbp)
jmp    1658 <main+0x3e8>
jmp    16f8 <main+0x488>
jmp    16fd <main+0x48d>
jmp    1702 <main+0x492>
jmp    1707 <main+0x497>
mov    -0x150(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x150(%rbp)
jmp    1503 <main+0x293>
jmp    14ba <main+0x24a>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x158,%rsp
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
movl   $0x1,-0x3c(%rbp)
lea    -0x30(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
lea    -0x2c(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
lea    -0x80(%rbp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   10b0 <memset@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    0x2cd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    0x2ceb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
lea    0xd07(%rip),%rsi
lea    -0x48(%rbp),%rcx
xor    %edi,%edi
mov    %rbx,%rdx
call   1160 <getaddrinfo@plt>
test   %eax,%eax
jne    1335 <main+0xc5>
mov    -0x48(%rbp),%rbx
test   %rbx,%rbx
jne    1362 <main+0xf2>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
lea    0xcf3(%rip),%rsi
xor    %eax,%eax
call   10d0 <fprintf@plt>
mov    $0x2,%edi
call   1150 <exit@plt>
mov    0x2c84(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   10a0 <gai_strerror@plt>
lea    0xcb9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10d0 <fprintf@plt>
mov    $0x1,%edi
call   1150 <exit@plt>
mov    0x2c7f(%rip),%r15
mov    0x2c40(%rip),%r12
lea    -0x3c(%rbp),%r14
jmp    1389 <main+0x119>
cs nopw 0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1314 <main+0xa4>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1190 <socket@plt>
test   %eax,%eax
js     1380 <main+0x110>
mov    %eax,%r13d
mov    (%r15),%esi
mov    (%r12),%edx
mov    %eax,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1060 <setsockopt@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r13d,%edi
call   1110 <bind@plt>
test   %rax,%rax
jns    13d1 <main+0x161>
mov    %r13d,%edi
call   10c0 <close@plt>
jmp    1380 <main+0x110>
mov    -0x48(%rbp),%rdi
call   1180 <freeaddrinfo@plt>
mov    %r13d,%edi
mov    $0xa,%esi
call   1100 <listen@plt>
cmp    $0xffffffff,%eax
jne    1402 <main+0x192>
lea    0xc42(%rip),%rdi
call   1120 <perror@plt>
mov    $0x3,%edi
call   1150 <exit@plt>
lea    -0x30(%rbp),%rsi
mov    %r13d,%edi
call   1170 <FD_SET@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    0x1(%r13),%edi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10f0 <select@plt>
cmp    $0xffffffff,%eax
jne    1443 <main+0x1d3>
lea    0xc08(%rip),%rdi
call   1120 <perror@plt>
mov    $0x4,%edi
call   1150 <exit@plt>
lea    -0x2c(%rbp),%r15
mov    %r13d,%r14d
jmp    146e <main+0x1fe>
nopl   0x0(%rax)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    0x1(%r14),%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10f0 <select@plt>
cmp    $0xffffffff,%eax
je     142d <main+0x1bd>
test   %r14d,%r14d
js     1450 <main+0x1e0>
xor    %r12d,%r12d
jmp    148d <main+0x21d>
lea    -0x2c(%rbp),%r15
mov    -0x50(%rbp),%r14
lea    0x1(%r12),%eax
cmp    %r14d,%r12d
mov    %eax,%r12d
jge    1450 <main+0x1e0>
mov    %r12d,%edi
mov    %r15,%rsi
call   1070 <FD_ISSET@plt>
test   %rax,%rax
je     1480 <main+0x210>
cmp    %r13d,%r12d
jne    1520 <main+0x2b0>
movl   $0x4,-0x40(%rbp)
mov    %r13d,%edi
lea    -0x38(%rbp),%r15
mov    %r15,%rsi
lea    -0x40(%rbp),%rdx
call   1130 <accept@plt>
cmp    $0xffffffff,%eax
je     15e8 <main+0x378>
mov    %eax,%ebx
mov    %eax,%edi
lea    -0x30(%rbp),%rsi
call   1170 <FD_SET@plt>
cmp    %r14d,%ebx
cmovg  %ebx,%r14d
mov    %r15,%rdi
mov    %r14,%r15
mov    -0x38(%rbp),%r14d
call   1050 <get_in_addr@plt>
mov    0x2af1(%rip),%rcx
mov    (%rcx),%ecx
mov    %r14d,%edi
mov    %r15,%r14
mov    %eax,%esi
mov    -0x58(%rbp),%rdx
call   1030 <inet_ntop@plt>
lea    0xb41(%rip),%rdi
mov    %rax,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <printf@plt>
lea    -0x2c(%rbp),%r15
jmp    1480 <main+0x210>
xchg   %ax,%ax
mov    %r12d,%edi
lea    -0x180(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    15af <main+0x33f>
test   %r14d,%r14d
js     15df <main+0x36f>
mov    %eax,%r15d
mov    %r14,-0x50(%rbp)
lea    0x1(%r14),%ebx
xor    %r14d,%r14d
jmp    156d <main+0x2fd>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r14d
cmp    %r14d,%ebx
je     1478 <main+0x208>
mov    %r14d,%edi
lea    -0x30(%rbp),%rsi
call   1070 <FD_ISSET@plt>
test   %rax,%rax
je     1560 <main+0x2f0>
cmp    %r14d,%r13d
je     1560 <main+0x2f0>
cmp    %r14d,%r12d
je     1560 <main+0x2f0>
mov    %r14d,%edi
lea    -0x180(%rbp),%rsi
mov    %r15d,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0xffffffff,%eax
jne    1560 <main+0x2f0>
lea    0xafb(%rip),%rdi
call   1120 <perror@plt>
jmp    1560 <main+0x2f0>
jne    15fd <main+0x38d>
lea    0xac5(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1090 <printf@plt>
lea    -0x2c(%rbp),%r15
mov    %r12d,%edi
call   10c0 <close@plt>
mov    %r12d,%edi
lea    -0x30(%rbp),%rsi
call   1140 <FD_CLR@plt>
jmp    1480 <main+0x210>
lea    -0x2c(%rbp),%r15
jmp    1480 <main+0x210>
lea    0xa54(%rip),%rdi
call   1120 <perror@plt>
lea    -0x2c(%rbp),%r15
jmp    1480 <main+0x210>
lea    0xa9a(%rip),%rdi
call   1120 <perror@plt>
jmp    15c2 <main+0x352>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [5 x i8] c"9034\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"selectserver: %s\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [30 x i8] c"selectserver: failed to bind\0A\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"select\00"
@.str.5 = [7 x i8] c"accept\00"
@.str.6 = [51 x i8] c"selectserver: new connection from %s on socket %d\0A\00"
@.str.7 = [33 x i8] c"selectserver: socket %d hung up\0A\00"
@.str.8 = [5 x i8] c"recv\00"
@.str.9 = [5 x i8] c"send\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca %struct.addrinfo
%18 = alloca %struct.addrinfo*
%19 = alloca %struct.addrinfo*
store i32 0, i32* %1
%20 = load i32, i32* @INET6_ADDRSTRLEN
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %11
%23 = alloca i8, i64 %21
store i64 %21, i64* %12
store i32 1, i32* %13
%24 = call i32 @FD_ZERO(i32* %2)
%25 = call i32 @FD_ZERO(i32* %3)
%26 = call i32 @memset(%struct.addrinfo* %17, i32 0, i32 40)
%27 = load i32, i32* @AF_UNSPEC
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i32 0, i32 4
store i32 %27, i32* %28
%29 = load i32, i32* @SOCK_STREAM
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i32 0, i32 3
store i32 %29, i32* %30
%31 = load i32, i32* @AI_PASSIVE
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i32 0, i32 6
store i32 %31, i32* %32
%33 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %17, %struct.addrinfo** %18)
store i32 %33, i32* %16
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %0
%36 = load i32, i32* @stderr
%37 = load i32, i32* %16
%38 = call i8* @gai_strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %0
%42 = load %struct.addrinfo*, %struct.addrinfo** %18
store %struct.addrinfo* %42, %struct.addrinfo** %19
br label %43
43: ; preds = %78, %41
%44 = load %struct.addrinfo*, %struct.addrinfo** %19
%45 = icmp ne %struct.addrinfo* %44, null
br i1 %45, label %46, label %82
46: ; preds = %43
%47 = load %struct.addrinfo*, %struct.addrinfo** %19
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 4
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %19
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 3
%52 = load i32, i32* %51
%53 = load %struct.addrinfo*, %struct.addrinfo** %19
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 2
%55 = load i32, i32* %54
%56 = call i32 @socket(i32 %49, i32 %52, i32 %55)
store i32 %56, i32* %5
%57 = load i32, i32* %5
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %46
br label %78
60: ; preds = %46
%61 = load i32, i32* %5
%62 = load i32, i32* @SOL_SOCKET
%63 = load i32, i32* @SO_REUSEADDR
%64 = call i32 @setsockopt(i32 %61, i32 %62, i32 %63, i32* %13, i32 4)
%65 = load i32, i32* %5
%66 = load %struct.addrinfo*, %struct.addrinfo** %19
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 1
%68 = load i32, i32* %67
%69 = load %struct.addrinfo*, %struct.addrinfo** %19
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 0
%71 = load i32, i32* %70
%72 = call i64 @bind(i32 %65, i32 %68, i32 %71)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %60
%75 = load i32, i32* %5
%76 = call i32 @close(i32 %75)
br label %78
77: ; preds = %60
br label %82
78: ; preds = %74, %59
%79 = load %struct.addrinfo*, %struct.addrinfo** %19
%80 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %79, i32 0, i32 5
%81 = load %struct.addrinfo*, %struct.addrinfo** %80
store %struct.addrinfo* %81, %struct.addrinfo** %19
br label %43
82: ; preds = %77, %43
%83 = load %struct.addrinfo*, %struct.addrinfo** %19
%84 = icmp eq %struct.addrinfo* %83, null
br i1 %84, label %85, label %89
85: ; preds = %82
%86 = load i32, i32* @stderr
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%88 = call i32 @exit(i32 2)
unreachable
89: ; preds = %82
%90 = load %struct.addrinfo*, %struct.addrinfo** %18
%91 = call i32 @freeaddrinfo(%struct.addrinfo* %90)
%92 = load i32, i32* %5
%93 = call i32 @listen(i32 %92, i32 10)
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %98
95: ; preds = %89
%96 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%97 = call i32 @exit(i32 3)
unreachable
98: ; preds = %89
%99 = load i32, i32* %5
%100 = call i32 @FD_SET(i32 %99, i32* %2)
%101 = load i32, i32* %5
store i32 %101, i32* %4
br label %102
102: ; preds = %206, %98
%103 = load i32, i32* %2
store i32 %103, i32* %3
%104 = load i32, i32* %4
%105 = add nsw i32 %104, 1
%106 = call i32 @select(i32 %105, i32* %3, i32* null, i32* null, i32* null)
%107 = icmp eq i32 %106, -1
br i1 %107, label %108, label %111
108: ; preds = %102
%109 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%110 = call i32 @exit(i32 4)
unreachable
111: ; preds = %102
store i32 0, i32* %14
br label %112
112: ; preds = %203, %111
%113 = load i32, i32* %14
%114 = load i32, i32* %4
%115 = icmp sle i32 %113, %114
br i1 %115, label %116, label %206
116: ; preds = %112
%117 = load i32, i32* %14
%118 = call i64 @FD_ISSET(i32 %117, i32* %3)
%119 = icmp ne i64 %118, 0
br i1 %119, label %120, label %202
120: ; preds = %116
%121 = load i32, i32* %14
%122 = load i32, i32* %5
%123 = icmp eq i32 %121, %122
br i1 %123, label %124, label %150
124: ; preds = %120
store i32 4, i32* %8
%125 = load i32, i32* %5
%126 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%127 = call i32 @accept(i32 %125, %struct.sockaddr* %126, i32* %8)
store i32 %127, i32* %6
%128 = load i32, i32* %6
%129 = icmp eq i32 %128, -1
br i1 %129, label %130, label %132
130: ; preds = %124
%131 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %149
132: ; preds = %124
%133 = load i32, i32* %6
%134 = call i32 @FD_SET(i32 %133, i32* %2)
%135 = load i32, i32* %6
%136 = load i32, i32* %4
%137 = icmp sgt i32 %135, %136
br i1 %137, label %138, label %140
138: ; preds = %132
%139 = load i32, i32* %6
store i32 %139, i32* %4
br label %140
140: ; preds = %138, %132
%141 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %7, i32 0, i32 0
%142 = load i32, i32* %141
%143 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%144 = call i32 @get_in_addr(%struct.sockaddr* %143)
%145 = load i32, i32* @INET6_ADDRSTRLEN
%146 = call i8* @inet_ntop(i32 %142, i32 %144, i8* %23, i32 %145)
%147 = load i32, i32* %6
%148 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0), i8* %146, i32 %147)
br label %149
149: ; preds = %140, %130
br label %201
150: ; preds = %120
%151 = load i32, i32* %14
%152 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%153 = call i32 @recv(i32 %151, i8* %152, i32 256, i32 0)
store i32 %153, i32* %10
%154 = icmp sle i32 %153, 0
br i1 %154, label %155, label %168
155: ; preds = %150
%156 = load i32, i32* %10
%157 = icmp eq i32 %156, 0
br i1 %157, label %158, label %161
158: ; preds = %155
%159 = load i32, i32* %14
%160 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i32 %159)
br label %163
161: ; preds = %155
%162 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %163
163: ; preds = %161, %158
%164 = load i32, i32* %14
%165 = call i32 @close(i32 %164)
%166 = load i32, i32* %14
%167 = call i32 @FD_CLR(i32 %166, i32* %2)
br label %200
168: ; preds = %150
store i32 0, i32* %15
br label %169
169: ; preds = %196, %168
%170 = load i32, i32* %15
%171 = load i32, i32* %4
%172 = icmp sle i32 %170, %171
br i1 %172, label %173, label %199
173: ; preds = %169
%174 = load i32, i32* %15
%175 = call i64 @FD_ISSET(i32 %174, i32* %2)
%176 = icmp ne i64 %175, 0
br i1 %176, label %177, label %195
177: ; preds = %173
%178 = load i32, i32* %15
%179 = load i32, i32* %5
%180 = icmp ne i32 %178, %179
br i1 %180, label %181, label %194
181: ; preds = %177
%182 = load i32, i32* %15
%183 = load i32, i32* %14
%184 = icmp ne i32 %182, %183
br i1 %184, label %185, label %194
185: ; preds = %181
%186 = load i32, i32* %15
%187 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%188 = load i32, i32* %10
%189 = call i32 @send(i32 %186, i8* %187, i32 %188, i32 0)
%190 = icmp eq i32 %189, -1
br i1 %190, label %191, label %193
191: ; preds = %185
%192 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
br label %193
193: ; preds = %191, %185
br label %194
194: ; preds = %193, %181, %177
br label %195
195: ; preds = %194, %173
br label %196
196: ; preds = %195
%197 = load i32, i32* %15
%198 = add nsw i32 %197, 1
store i32 %198, i32* %15
br label %169
199: ; preds = %169
br label %200
200: ; preds = %199, %163
br label %201
201: ; preds = %200, %149
br label %202
202: ; preds = %201, %116
br label %203
203: ; preds = %202
%204 = load i32, i32* %14
%205 = add nsw i32 %204, 1
store i32 %205, i32* %14
br label %112
206: ; preds = %112
br label %102
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [5 x i8] c"9034\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"selectserver: %s\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [30 x i8] c"selectserver: failed to bind\0A\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"select\00"
@.str.5 = [7 x i8] c"accept\00"
@.str.6 = [51 x i8] c"selectserver: new connection from %s on socket %d\0A\00"
@.str.7 = [33 x i8] c"selectserver: socket %d hung up\0A\00"
@.str.8 = [5 x i8] c"recv\00"
@.str.9 = [5 x i8] c"send\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = bitcast i32* %1 to i8*
%10 = bitcast i32* %2 to i8*
%11 = bitcast %struct.sockaddr_storage* %3 to i8*
%12 = bitcast i32* %4 to i8*
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%14 = load i32, i32* @INET6_ADDRSTRLEN
%15 = zext i32 %14 to i64
%16 = alloca i8, i64 %15
%17 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%18 = bitcast %struct.addrinfo* %7 to i8*
%19 = bitcast %struct.addrinfo** %8 to i8*
%20 = call i32 @FD_ZERO(i32* %1)
%21 = call i32 @FD_ZERO(i32* %2)
%22 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 40)
%23 = load i32, i32* @AF_UNSPEC
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 4
store i32 %23, i32* %24
%25 = load i32, i32* @SOCK_STREAM
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 3
store i32 %25, i32* %26
%27 = load i32, i32* @AI_PASSIVE
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i64 0, i32 6
store i32 %27, i32* %28
%29 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %7, %struct.addrinfo** %8)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %0
%32 = load %struct.addrinfo*, %struct.addrinfo** %8
%33 = icmp eq %struct.addrinfo* %32, null
br i1 %33, label %65, label %39
34: ; preds = %0
%35 = load i32, i32* @stderr
%36 = call i8* @gai_strerror(i32 %29)
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %31, %61
%40 = phi %struct.addrinfo* [ %63, %61 ], [ %32, %31 ]
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 4
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 3
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 2
%46 = load i32, i32* %45
%47 = call i32 @socket(i32 %42, i32 %44, i32 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %61, label %49
49: ; preds = %39
%50 = load i32, i32* @SOL_SOCKET
%51 = load i32, i32* @SO_REUSEADDR
%52 = call i32 @setsockopt(i32 %47, i32 %50, i32 %51, i32* %6, i32 4)
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 1
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 0
%56 = load i32, i32* %55
%57 = call i64 @bind(i32 %47, i32 %54, i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %69
59: ; preds = %49
%60 = call i32 @close(i32 %47)
br label %61
61: ; preds = %39, %59
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i64 0, i32 5
%63 = load %struct.addrinfo*, %struct.addrinfo** %62
%64 = icmp eq %struct.addrinfo* %63, null
br i1 %64, label %65, label %39
65: ; preds = %61, %31
%66 = load i32, i32* @stderr
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%68 = call i32 @exit(i32 2)
unreachable
69: ; preds = %49
%70 = load %struct.addrinfo*, %struct.addrinfo** %8
%71 = call i32 @freeaddrinfo(%struct.addrinfo* %70)
%72 = call i32 @listen(i32 %47, i32 10)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %77
74: ; preds = %69
%75 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%76 = call i32 @exit(i32 3)
unreachable
77: ; preds = %69
%78 = call i32 @FD_SET(i32 %47, i32* %1)
%79 = load i32, i32* %1
store i32 %79, i32* %2
%80 = add nuw nsw i32 %47, 1
%81 = call i32 @select(i32 %80, i32* %2, i32* null, i32* null, i32* null)
%82 = icmp eq i32 %81, -1
br i1 %82, label %95, label %83
83: ; preds = %77
%84 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%85 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 0, i32 0
br label %92
86: ; preds = %149, %92
%87 = phi i32 [ %93, %92 ], [ %150, %149 ]
%88 = load i32, i32* %1
store i32 %88, i32* %2
%89 = add nsw i32 %87, 1
%90 = call i32 @select(i32 %89, i32* %2, i32* null, i32* null, i32* null)
%91 = icmp eq i32 %90, -1
br i1 %91, label %95, label %92
92: ; preds = %83, %86
%93 = phi i32 [ %47, %83 ], [ %87, %86 ]
%94 = icmp slt i32 %93, 0
br i1 %94, label %86, label %98
95: ; preds = %86, %77
%96 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%97 = call i32 @exit(i32 4)
unreachable
98: ; preds = %92, %149
%99 = phi i32 [ %150, %149 ], [ %93, %92 ]
%100 = phi i32 [ %151, %149 ], [ 0, %92 ]
%101 = call i64 @FD_ISSET(i32 %100, i32* %2)
%102 = icmp eq i64 %101, 0
br i1 %102, label %149, label %103
103: ; preds = %98
%104 = icmp eq i32 %100, %47
br i1 %104, label %105, label %119
105: ; preds = %103
store i32 4, i32* %4
%106 = call i32 @accept(i32 %47, %struct.sockaddr* %84, i32* %4)
%107 = icmp eq i32 %106, -1
br i1 %107, label %108, label %110
108: ; preds = %105
%109 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %149
110: ; preds = %105
%111 = call i32 @FD_SET(i32 %106, i32* %1)
%112 = icmp sgt i32 %106, %99
%113 = select i1 %112, i32 %106, i32 %99
%114 = load i32, i32* %85
%115 = call i32 @get_in_addr(%struct.sockaddr* %84)
%116 = load i32, i32* @INET6_ADDRSTRLEN
%117 = call i8* @inet_ntop(i32 %114, i32 %115, i8* %16, i32 %116)
%118 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0), i8* %117, i32 %106)
br label %149
119: ; preds = %103
%120 = call i32 @recv(i32 %100, i8* %13, i32 256, i32 0)
%121 = icmp slt i32 %120, 1
br i1 %121, label %124, label %122
122: ; preds = %119
%123 = icmp slt i32 %99, 0
br i1 %123, label %149, label %133
124: ; preds = %119
%125 = icmp eq i32 %120, 0
br i1 %125, label %126, label %128
126: ; preds = %124
%127 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i32 %100)
br label %130
128: ; preds = %124
%129 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %130
130: ; preds = %128, %126
%131 = call i32 @close(i32 %100)
%132 = call i32 @FD_CLR(i32 %100, i32* %1)
br label %149
133: ; preds = %122, %146
%134 = phi i32 [ %147, %146 ], [ 0, %122 ]
%135 = call i64 @FD_ISSET(i32 %134, i32* %1)
%136 = icmp eq i64 %135, 0
%137 = icmp eq i32 %134, %47
%138 = select i1 %136, i1 true, i1 %137
%139 = icmp eq i32 %134, %100
%140 = or i1 %138, %139
br i1 %140, label %146, label %141
141: ; preds = %133
%142 = call i32 @send(i32 %134, i8* %13, i32 %120, i32 0)
%143 = icmp eq i32 %142, -1
br i1 %143, label %144, label %146
144: ; preds = %141
%145 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
br label %146
146: ; preds = %133, %141, %144
%147 = add nuw i32 %134, 1
%148 = icmp eq i32 %134, %99
br i1 %148, label %149, label %133
149: ; preds = %146, %122, %98, %130, %108, %110
%150 = phi i32 [ %99, %108 ], [ %113, %110 ], [ %99, %130 ], [ %99, %98 ], [ %99, %122 ], [ %99, %146 ]
%151 = add nuw nsw i32 %100, 1
%152 = icmp slt i32 %100, %150
br i1 %152, label %98, label %86
}