#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_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
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;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ FD_ISSET(unsigned 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, struct sockaddr *, int *);
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ handle(unsigned int, int /*<<< orphan*/ *);
int htons(int /*<<< orphan*/);
char *inet_ntoa(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sock;
  fd_set socks;
  fd_set readsocks;
  int maxsock;
  int reuseaddr = 1;
  struct addrinfo hints, *res;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  if (getaddrinfo(NULL, "32001", &hints, &res) != 0) {
    perror("getaddrinfo");
    return 1;
  }
  sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (sock == -1) {
    perror("socket");
    return 1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) ==
      -1) {
    perror("setsockopt");
    return 1;
  }
  if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
    perror("bind");
    return 1;
  }
  freeaddrinfo(res);
  if (listen(sock, 10) == -1) {
    perror("listen");
    return 1;
  }
  FD_ZERO(&socks);
  FD_SET(sock, &socks);
  maxsock = sock;
  while (1) {
    unsigned int s;
    readsocks = socks;
    if (select(maxsock + 1, &readsocks, NULL, NULL, NULL) == -1) {
      perror("select");
      return 1;
    }
    for (s = 0; s <= maxsock; s++) {
      if (FD_ISSET(s, &readsocks)) {
        printf("socket %d was ready\n", s);
        if (s == sock) {
          int newsock;
          struct sockaddr_in their_addr;
          socklen_t size = sizeof(struct sockaddr_in);
          newsock = accept(sock, (struct sockaddr *)&their_addr, &size);
          if (newsock == -1) {
            perror("accept");
          } else {
            printf("Got a connection from %s on port %d\n",
                   inet_ntoa(their_addr.sin_addr), htons(their_addr.sin_port));
            FD_SET(newsock, &socks);
            if (newsock > maxsock) {
              maxsock = newsock;
            }
          }
        } else {
          handle(s, &socks);
        }
      }
    }
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1080 <memset@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xda7(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1100 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1288 <main+0x78>
lea    0xd8f(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    14c9 <main+0x2b9>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   1130 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12c7 <main+0xb7>
lea    0xd5c(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    14c9 <main+0x2b9>
mov    -0x8(%rbp),%edi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
mov    0x2cee(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    130c <main+0xfc>
lea    0xd1e(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    14c9 <main+0x2b9>
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1342 <main+0x132>
lea    0xcf3(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    14c9 <main+0x2b9>
mov    -0x38(%rbp),%rdi
call   1120 <freeaddrinfo@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   10c0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1379 <main+0x169>
lea    0xcc1(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    14c9 <main+0x2b9>
lea    -0xc(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10b0 <select@plt>
cmp    $0xffffffff,%eax
jne    13d5 <main+0x1c5>
lea    0xc6c(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    14c9 <main+0x2b9>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x14(%rbp),%eax
ja     14c4 <main+0x2b4>
mov    -0x3c(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14b1 <main+0x2a1>
mov    -0x3c(%rbp),%esi
lea    0xc2f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x3c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    14a0 <main+0x290>
movl   $0x8,-0x4c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x48(%rbp),%rsi
lea    -0x4c(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1450 <main+0x240>
lea    0xc06(%rip),%rdi
call   10e0 <perror@plt>
jmp    149b <main+0x28b>
mov    -0x44(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%edi
call   1060 <htons@plt>
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
lea    0xbe2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x40(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x40(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1496 <main+0x286>
mov    -0x40(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    149b <main+0x28b>
jmp    14ac <main+0x29c>
mov    -0x3c(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1090 <handle@plt>
jmp    14b1 <main+0x2a1>
jmp    14b6 <main+0x2a6>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    13dc <main+0x1cc>
jmp    1394 <main+0x184>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movl   $0x1,0x14(%rsp)
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1080 <memset@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
lea    0xda5(%rip),%rsi
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rdx
call   1100 <getaddrinfo@plt>
test   %rax,%rax
jne    1290 <main+0x80>
mov    0x8(%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1130 <socket@plt>
cmp    $0xffffffff,%eax
jne    12b0 <main+0xa0>
lea    0xd84(%rip),%rdi
jmp    1297 <main+0x87>
lea    0xd6f(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r12d
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %r12d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12e6 <main+0xd6>
lea    0xd35(%rip),%rdi
jmp    1297 <main+0x87>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %r12d,%edi
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1306 <main+0xf6>
lea    0xd20(%rip),%rdi
jmp    1297 <main+0x87>
mov    0x8(%rsp),%rdi
call   1120 <freeaddrinfo@plt>
mov    %r12d,%edi
mov    $0xa,%esi
call   10c0 <listen@plt>
cmp    $0xffffffff,%eax
jne    132e <main+0x11e>
lea    0xd00(%rip),%rdi
jmp    1297 <main+0x87>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10a0 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1110 <FD_SET@plt>
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
lea    0x1(%r12),%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
lea    0xcc8(%rip),%rdi
cmp    $0xffffffff,%eax
je     1297 <main+0x87>
lea    0x4(%rsp),%r14
lea    0xcba(%rip),%rbx
xor    %r13d,%r13d
mov    %r12d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13d,%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     143b <main+0x22b>
mov    %rbx,%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    %r12d,%r13d
jne    1430 <main+0x220>
movl   $0x8,0x10(%rsp)
mov    %r12d,%edi
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   10f0 <accept@plt>
cmp    $0xffffffff,%eax
je     1470 <main+0x260>
mov    %eax,%ebp
mov    0x1c(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %r14,%rbx
mov    %r12,%r14
mov    %rax,%r12
mov    0x18(%rsp),%edi
call   1060 <htons@plt>
lea    0xc56(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%r12
mov    %rbx,%r14
lea    0xc2a(%rip),%rbx
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
mov    %rsp,%rsi
call   1110 <FD_SET@plt>
cmp    %r15d,%ebp
cmovg  %ebp,%r15d
jmp    143b <main+0x22b>
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rsp,%rsi
call   1090 <handle@plt>
add    $0x1,%r13d
cmp    %r15d,%r13d
jbe    1390 <main+0x180>
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
lea    0x1(%r15),%edi
xor    %r13d,%r13d
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
cmp    $0xffffffff,%eax
jne    1390 <main+0x180>
jmp    147e <main+0x26e>
lea    0xbd5(%rip),%rdi
call   10e0 <perror@plt>
jmp    143b <main+0x22b>
lea    0xbab(%rip),%rdi
jmp    1297 <main+0x87>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [6 x i8] c"32001\00"
@.str.1 = [12 x i8] c"getaddrinfo\00"
@.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"
@.str.5 = [7 x i8] c"listen\00"
@.str.6 = [7 x i8] c"select\00"
@.str.7 = [21 x i8] c"socket %d was ready\0A\00"
@.str.8 = [7 x i8] c"accept\00"
@.str.9 = [37 x i8] c"Got a connection from %s on port %d\0A\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.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %6
%13 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 20)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %16, i32* %17
%18 = call i64 @getaddrinfo(i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %7, %struct.addrinfo** %8)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %127
22: ; preds = %0
%23 = load %struct.addrinfo*, %struct.addrinfo** %8
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 4
%25 = load i32, i32* %24
%26 = load %struct.addrinfo*, %struct.addrinfo** %8
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 3
%28 = load i32, i32* %27
%29 = load %struct.addrinfo*, %struct.addrinfo** %8
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %25, i32 %28, i32 %31)
store i32 %32, i32* %2
%33 = load i32, i32* %2
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %22
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %127
37: ; preds = %22
%38 = load i32, i32* %2
%39 = load i32, i32* @SOL_SOCKET
%40 = load i32, i32* @SO_REUSEADDR
%41 = call i32 @setsockopt(i32 %38, i32 %39, i32 %40, i32* %6, i32 4)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %127
45: ; preds = %37
%46 = load i32, i32* %2
%47 = load %struct.addrinfo*, %struct.addrinfo** %8
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %8
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @bind(i32 %46, i32 %49, i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %45
%56 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %127
57: ; preds = %45
%58 = load %struct.addrinfo*, %struct.addrinfo** %8
%59 = call i32 @freeaddrinfo(%struct.addrinfo* %58)
%60 = load i32, i32* %2
%61 = call i32 @listen(i32 %60, i32 10)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %57
%64 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %127
65: ; preds = %57
%66 = call i32 @FD_ZERO(i32* %3)
%67 = load i32, i32* %2
%68 = call i32 @FD_SET(i32 %67, i32* %3)
%69 = load i32, i32* %2
store i32 %69, i32* %5
br label %70
70: ; preds = %65, %126
%71 = load i32, i32* %3
store i32 %71, i32* %4
%72 = load i32, i32* %5
%73 = add nsw i32 %72, 1
%74 = call i32 @select(i32 %73, i32* %4, i32* null, i32* null, i32* null)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %78
76: ; preds = %70
%77 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %127
78: ; preds = %70
store i32 0, i32* %9
br label %79
79: ; preds = %123, %78
%80 = load i32, i32* %9
%81 = load i32, i32* %5
%82 = icmp ule i32 %80, %81
br i1 %82, label %83, label %126
83: ; preds = %79
%84 = load i32, i32* %9
%85 = call i64 @FD_ISSET(i32 %84, i32* %4)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %122
87: ; preds = %83
%88 = load i32, i32* %9
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %88)
%90 = load i32, i32* %9
%91 = load i32, i32* %2
%92 = icmp eq i32 %90, %91
br i1 %92, label %93, label %118
93: ; preds = %87
store i32 8, i32* %12
%94 = load i32, i32* %2
%95 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%96 = call i32 @accept(i32 %94, %struct.sockaddr* %95, i32* %12)
store i32 %96, i32* %10
%97 = load i32, i32* %10
%98 = icmp eq i32 %97, -1
br i1 %98, label %99, label %101
99: ; preds = %93
%100 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %117
101: ; preds = %93
%102 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%103 = load i32, i32* %102
%104 = call i8* @inet_ntoa(i32 %103)
%105 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%106 = load i32, i32* %105
%107 = call i32 @htons(i32 %106)
%108 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.9, i64 0, i64 0), i8* %104, i32 %107)
%109 = load i32, i32* %10
%110 = call i32 @FD_SET(i32 %109, i32* %3)
%111 = load i32, i32* %10
%112 = load i32, i32* %5
%113 = icmp sgt i32 %111, %112
br i1 %113, label %114, label %116
114: ; preds = %101
%115 = load i32, i32* %10
store i32 %115, i32* %5
br label %116
116: ; preds = %114, %101
br label %117
117: ; preds = %116, %99
br label %121
118: ; preds = %87
%119 = load i32, i32* %9
%120 = call i32 @handle(i32 %119, i32* %3)
br label %121
121: ; preds = %118, %117
br label %122
122: ; preds = %121, %83
br label %123
123: ; preds = %122
%124 = load i32, i32* %9
%125 = add i32 %124, 1
store i32 %125, i32* %9
br label %79
126: ; preds = %79
br label %70
127: ; preds = %76, %63, %55, %43, %35, %20
%128 = load i32, i32* %1
ret i32 %128
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [6 x i8] c"32001\00"
@.str.1 = [12 x i8] c"getaddrinfo\00"
@.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"
@.str.5 = [7 x i8] c"listen\00"
@.str.6 = [7 x i8] c"select\00"
@.str.7 = [21 x i8] c"socket %d was ready\0A\00"
@.str.8 = [7 x i8] c"accept\00"
@.str.9 = [37 x i8] c"Got a connection from %s on port %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = bitcast i32* %1 to i8*
%9 = bitcast i32* %2 to i8*
%10 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%11 = bitcast %struct.addrinfo* %4 to i8*
%12 = bitcast %struct.addrinfo** %5 to i8*
%13 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 20)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %16, i32* %17
%18 = call i64 @getaddrinfo(i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %97
20: ; preds = %0
%21 = load %struct.addrinfo*, %struct.addrinfo** %5
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 4
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 3
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %23, i32 %25, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %97, label %30
30: ; preds = %20
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_REUSEADDR
%33 = call i32 @setsockopt(i32 %28, i32 %31, i32 %32, i32* %3, i32 4)
%34 = icmp eq i32 %33, -1
br i1 %34, label %97, label %35
35: ; preds = %30
%36 = load %struct.addrinfo*, %struct.addrinfo** %5
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 1
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @bind(i32 %28, i32 %38, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %97, label %43
43: ; preds = %35
%44 = load %struct.addrinfo*, %struct.addrinfo** %5
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
%46 = call i32 @listen(i32 %28, i32 10)
%47 = icmp eq i32 %46, -1
br i1 %47, label %97, label %48
48: ; preds = %43
%49 = call i32 @FD_ZERO(i32* %1)
%50 = call i32 @FD_SET(i32 %28, i32* %1)
%51 = bitcast %struct.sockaddr_in* %6 to i8*
%52 = bitcast i32* %7 to i8*
%53 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%56 = load i32, i32* %1
store i32 %56, i32* %2
%57 = add nuw nsw i32 %28, 1
%58 = call i32 @select(i32 %57, i32* %2, i32* null, i32* null, i32* null)
%59 = icmp eq i32 %58, -1
br i1 %59, label %97, label %65
60: ; preds = %91
%61 = load i32, i32* %1
store i32 %61, i32* %2
%62 = add nuw nsw i32 %92, 1
%63 = call i32 @select(i32 %62, i32* %2, i32* null, i32* null, i32* null)
%64 = icmp eq i32 %63, -1
br i1 %64, label %97, label %95
65: ; preds = %48, %95
%66 = phi i32 [ %96, %95 ], [ 0, %48 ]
%67 = phi i32 [ %92, %95 ], [ %28, %48 ]
%68 = call i64 @FD_ISSET(i32 %66, i32* %2)
%69 = icmp eq i64 %68, 0
br i1 %69, label %91, label %70
70: ; preds = %65
%71 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %66)
%72 = icmp eq i32 %66, %28
br i1 %72, label %73, label %89
73: ; preds = %70
store i32 8, i32* %7
%74 = call i32 @accept(i32 %28, %struct.sockaddr* %53, i32* %7)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %78
76: ; preds = %73
%77 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %87
78: ; preds = %73
%79 = load i32, i32* %54
%80 = call i8* @inet_ntoa(i32 %79)
%81 = load i32, i32* %55
%82 = call i32 @htons(i32 %81)
%83 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.9, i64 0, i64 0), i8* %80, i32 %82)
%84 = call i32 @FD_SET(i32 %74, i32* %1)
%85 = icmp sgt i32 %74, %67
%86 = select i1 %85, i32 %74, i32 %67
br label %87
87: ; preds = %78, %76
%88 = phi i32 [ %67, %76 ], [ %86, %78 ]
br label %91
89: ; preds = %70
%90 = call i32 @handle(i32 %66, i32* %1)
br label %91
91: ; preds = %65, %89, %87
%92 = phi i32 [ %88, %87 ], [ %67, %89 ], [ %67, %65 ]
%93 = add i32 %66, 1
%94 = icmp ugt i32 %93, %92
br i1 %94, label %60, label %95
95: ; preds = %91, %60
%96 = phi i32 [ %93, %91 ], [ 0, %60 ]
br label %65
97: ; preds = %60, %48, %43, %35, %30, %20, %0
%98 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %20 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), %30 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %35 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %43 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), %48 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), %60 ]
%99 = call i32 @perror(i8* %98)
ret i32 1
}