#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 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 /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int 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 *);
scalar_t__ getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                       struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(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 setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(void) {
  int master;
  struct addrinfo hints;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  struct addrinfo *servinfo;
  if (getaddrinfo(NULL, "42000", &hints, &servinfo) != 0) {
    perror("getaddrinfo");
    exit(EXIT_FAILURE);
  }
  struct addrinfo *p;
  for (p = servinfo; p != NULL; p = p->ai_next) {
    master = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (master == -1) {
      perror("socket");
      continue;
    }
    int yes = 1;
    if (setsockopt(master, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
      perror("setsockopt");
      exit(1);
    }
    if (bind(master, p->ai_addr, p->ai_addrlen) == -1) {
      perror("bind");
      close(master);
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "failed to bind\n");
    exit(EXIT_FAILURE);
  }
  freeaddrinfo(servinfo);
  if (listen(master, 10) == -1) {
    perror("listen");
    exit(EXIT_FAILURE);
  }
  fd_set pool_fds;
  fd_set read_fds;
  FD_ZERO(&pool_fds);
  FD_ZERO(&read_fds);
  FD_SET(master, &pool_fds);
  int max_descriptor = master;
  while (1) {
    read_fds = pool_fds;
    if (select(max_descriptor + 1, &read_fds, NULL, NULL, NULL) == -1) {
      perror("select");
      exit(EXIT_FAILURE);
    }
    int i;
    for (i = 0; i <= max_descriptor; i++) {
      if (!FD_ISSET(i, &read_fds)) {
        continue;
      }
      if (i == master) {
        int conn = accept(master, NULL, NULL);
        if (conn == -1) {
          perror("accept");
          continue;
        }
        FD_SET(conn, &pool_fds);
        if (conn > max_descriptor) {
          max_descriptor = conn;
        }
        continue;
      }
      char buffer[1024];
      int n = recv(i, buffer, sizeof buffer, 0);
      if (n == 0) {
        fprintf(stdout, "socket %d disconnected\n", i);
        close(i);
        FD_CLR(i, &pool_fds);
        continue;
      }
      if (n < 0) {
        perror("recv");
        close(i);
        FD_CLR(i, &pool_fds);
        continue;
      }
      int j;
      for (j = 0; j <= max_descriptor; j++) {
        if (!FD_ISSET(j, &pool_fds)) {
          continue;
        }
        if (j == master || j == i) {
          continue;
        }
        if (send(j, buffer, n, 0) == -1) {
          perror("send");
        }
      }
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd7f(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1120 <getaddrinfo@plt>
cmp    $0x0,%rax
je     12b2 <main+0x82>
lea    0xd67(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1394 <main+0x164>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   1150 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12fd <main+0xcd>
lea    0xd1f(%rip),%rdi
call   10e0 <perror@plt>
jmp    1383 <main+0x153>
movl   $0x1,-0x44(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
mov    0x2c91(%rip),%rax
mov    (%rax),%edx
lea    -0x44(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1347 <main+0x117>
lea    0xce1(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x8(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
jne    137e <main+0x14e>
lea    0xcb8(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    1383 <main+0x153>
jmp    1394 <main+0x164>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    12ba <main+0x8a>
cmpq   $0x0,-0x40(%rbp)
jne    13c4 <main+0x194>
mov    0x2c22(%rip),%rax
mov    (%rax),%edi
lea    0xc7a(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2bfb(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    -0x38(%rbp),%rdi
call   1140 <freeaddrinfo@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   10c0 <listen@plt>
cmp    $0xffffffff,%eax
jne    13fd <main+0x1cd>
lea    0xc4f(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2bc2(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
lea    -0x48(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
lea    -0x4c(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1130 <FD_SET@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x50(%rbp),%edi
add    $0x1,%edi
lea    -0x4c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10b0 <select@plt>
cmp    $0xffffffff,%eax
jne    1464 <main+0x234>
lea    0xbef(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2b5b(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x50(%rbp),%eax
jg     162f <main+0x3ff>
mov    -0x54(%rbp),%edi
lea    -0x4c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1491 <main+0x261>
jmp    1621 <main+0x3f1>
mov    -0x54(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    14ed <main+0x2bd>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10f0 <accept@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    14ca <main+0x29a>
lea    0xb87(%rip),%rdi
call   10e0 <perror@plt>
jmp    1621 <main+0x3f1>
mov    -0x58(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1130 <FD_SET@plt>
mov    -0x58(%rbp),%eax
cmp    -0x50(%rbp),%eax
jle    14e8 <main+0x2b8>
mov    -0x58(%rbp),%eax
mov    %eax,-0x50(%rbp)
jmp    1621 <main+0x3f1>
mov    -0x54(%rbp),%edi
lea    -0x460(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x464(%rbp)
cmpl   $0x0,-0x464(%rbp)
jne    1549 <main+0x319>
mov    0x2a93(%rip),%rax
mov    (%rax),%edi
mov    -0x54(%rbp),%edx
lea    0xb25(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x54(%rbp),%edi
call   1080 <close@plt>
mov    -0x54(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1100 <FD_CLR@plt>
jmp    1621 <main+0x3f1>
cmpl   $0x0,-0x464(%rbp)
jge    157b <main+0x34b>
lea    0xb09(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x54(%rbp),%edi
call   1080 <close@plt>
mov    -0x54(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1100 <FD_CLR@plt>
jmp    1621 <main+0x3f1>
movl   $0x0,-0x468(%rbp)
mov    -0x468(%rbp),%eax
cmp    -0x50(%rbp),%eax
jg     161c <main+0x3ec>
mov    -0x468(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    15b1 <main+0x381>
jmp    1608 <main+0x3d8>
mov    -0x468(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     15cf <main+0x39f>
mov    -0x468(%rbp),%eax
cmp    -0x54(%rbp),%eax
jne    15d4 <main+0x3a4>
jmp    1608 <main+0x3d8>
mov    -0x468(%rbp),%edi
lea    -0x460(%rbp),%rsi
mov    -0x464(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    1603 <main+0x3d3>
lea    0xa6d(%rip),%rdi
call   10e0 <perror@plt>
jmp    1608 <main+0x3d8>
mov    -0x468(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x468(%rbp)
jmp    1585 <main+0x355>
jmp    1621 <main+0x3f1>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    146b <main+0x23b>
jmp    1421 <main+0x1f1>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
lea    0xd7d(%rip),%rsi
lea    0x10(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rdx
call   1120 <getaddrinfo@plt>
test   %rax,%rax
jne    12ba <main+0x8a>
mov    0x10(%rsp),%rbp
test   %rbp,%rbp
jne    12d4 <main+0xa4>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    0xd78(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    12c6 <main+0x96>
lea    0xd45(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    0x2d0d(%rip),%r13
mov    0x2cc6(%rip),%r14
lea    0x40(%rsp),%r15
lea    0xd36(%rip),%r12
jmp    1309 <main+0xd9>
lea    0xd1b(%rip),%rdi
call   10e0 <perror@plt>
nopl   0x0(%rax)
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     12a1 <main+0x71>
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   1150 <socket@plt>
cmp    $0xffffffff,%eax
je     12f0 <main+0xc0>
mov    %eax,%ebx
movl   $0x1,0x40(%rsp)
mov    0x0(%r13),%esi
mov    (%r14),%edx
mov    %eax,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     153e <main+0x30e>
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %ebx,%edi
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1369 <main+0x139>
mov    %r12,%rdi
call   10e0 <perror@plt>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    1300 <main+0xd0>
mov    0x10(%rsp),%rdi
call   1140 <freeaddrinfo@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   10c0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1390 <main+0x160>
lea    0xcae(%rip),%rdi
jmp    12c1 <main+0x91>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   10a0 <FD_ZERO@plt>
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   10a0 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1130 <FD_SET@plt>
mov    0x8(%rsp),%eax
mov    %eax,0xc(%rsp)
lea    0x1(%rbx),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
cmp    $0xffffffff,%eax
jne    13df <main+0x1af>
lea    0xc66(%rip),%rdi
jmp    12c1 <main+0x91>
lea    0xc(%rsp),%r14
mov    %ebx,%r13d
jmp    1410 <main+0x1e0>
nopl   0x0(%rax)
mov    0x8(%rsp),%eax
mov    %eax,0xc(%rsp)
lea    0x1(%r13),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
cmp    $0xffffffff,%eax
je     13d3 <main+0x1a3>
test   %r13d,%r13d
js     13f0 <main+0x1c0>
xor    %r15d,%r15d
jmp    145a <main+0x22a>
mov    0x2b8f(%rip),%rax
mov    (%rax),%edi
lea    0xc24(%rip),%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %r15d,%edi
call   1080 <close@plt>
mov    %r15d,%edi
lea    0x8(%rsp),%rsi
call   1100 <FD_CLR@plt>
lea    0xc(%rsp),%r14
lea    0x1(%r15),%eax
cmp    %r13d,%r15d
mov    %eax,%r15d
jge    13f0 <main+0x1c0>
mov    %r15d,%edi
mov    %r14,%rsi
call   1050 <FD_ISSET@plt>
test   %eax,%eax
je     144e <main+0x21e>
cmp    %ebx,%r15d
jne    14a0 <main+0x270>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10f0 <accept@plt>
cmp    $0xffffffff,%eax
je     151c <main+0x2ec>
mov    %eax,%ebp
mov    %eax,%edi
lea    0x8(%rsp),%rsi
call   1130 <FD_SET@plt>
cmp    %r13d,%ebp
cmovg  %ebp,%r13d
jmp    144e <main+0x21e>
nopl   0x0(%rax)
mov    %r15d,%edi
lea    0x40(%rsp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     141a <main+0x1ea>
js     152d <main+0x2fd>
test   %r13d,%r13d
js     1449 <main+0x219>
mov    %eax,%ebp
lea    0x1(%r13),%r12d
xor    %r14d,%r14d
jmp    14dd <main+0x2ad>
xchg   %ax,%ax
add    $0x1,%r14d
cmp    %r14d,%r12d
je     1449 <main+0x219>
mov    %r14d,%edi
lea    0x8(%rsp),%rsi
call   1050 <FD_ISSET@plt>
test   %eax,%eax
je     14d0 <main+0x2a0>
cmp    %r14d,%ebx
je     14d0 <main+0x2a0>
cmp    %r14d,%r15d
je     14d0 <main+0x2a0>
mov    %r14d,%edi
lea    0x40(%rsp),%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    14d0 <main+0x2a0>
lea    0xb56(%rip),%rdi
call   10e0 <perror@plt>
jmp    14d0 <main+0x2a0>
lea    0xb24(%rip),%rdi
call   10e0 <perror@plt>
jmp    144e <main+0x21e>
lea    0xb32(%rip),%rdi
call   10e0 <perror@plt>
jmp    1434 <main+0x204>
lea    0xad4(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>

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

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