#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 dummy;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ one;
typedef int /*<<< orphan*/ hints;
typedef int /*<<< orphan*/ clientAddr;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ handleIncomingConnection(int);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int setupListen(char hostname[], char port[], int protocol) {
  struct addrinfo hints;
  struct addrinfo *addrs;
  int s, clientFd, clientPid;
  int one = 1;
  int fd = -1;
  int pid = -1;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = protocol;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  s = getaddrinfo((char *)hostname, port, &hints, &addrs);
  if (s != 0) {
    fprintf(stderr, "getaddrinfo says %s", gai_strerror(s));
    return 1;
  }
  while (addrs != NULL) {
    fd = socket(addrs->ai_family, SOCK_STREAM, 0);
    if (fd < 0) {
      fprintf(stderr, "socket creation failed\n");
      addrs = addrs->ai_next;
      continue;
    }
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one)) <
        0) {
      fprintf(stderr, "could not set SO_REUSEADDR\n");
      close(fd);
      addrs = addrs->ai_next;
      continue;
    }
    if (bind(fd, addrs->ai_addr, addrs->ai_addrlen) != -1 &&
        listen(fd, SOMAXCONN) != -1) {
      pid = fork();
      if (pid > 0) {
        return 0;
      } else if (pid == 0) {
        break;
      } else {
        fprintf(stderr, "failed to fork process");
        exit(1);
      }
    } else
      close(fd);
    addrs = addrs->ai_next;
  }
  if (pid < 0) {
    fprintf(stderr, "failed to listen on port");
    return 1;
  }
  struct sockaddr_storage clientAddr;
  socklen_t clientAddrlen = sizeof(clientAddr);
  while (1) {
    clientFd = accept(fd, (struct sockaddr *)&clientAddr, &clientAddrlen);
    if (clientFd < 0) {
      perror("error from accept call on server");
      exit(1);
    }
    clientPid = fork();
    if (clientPid < 0) {
      perror("error forking process for incoming connection");
      exit(1);
    }
    if (clientPid == 0) {
      handleIncomingConnection(clientFd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<setupListen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x1,-0x58(%rbp)
movl   $0xffffffff,-0x5c(%rbp)
movl   $0xffffffff,-0x60(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10e0 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     128c <setupListen+0xac>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    -0x4c(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x70(%rbp),%edi
mov    %rax,%rdx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    148f <setupListen+0x2af>
jmp    1291 <setupListen+0xb1>
cmpq   $0x0,-0x48(%rbp)
je     13e7 <setupListen+0x207>
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    12e7 <setupListen+0x107>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    0xd45(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1291 <setupListen+0xb1>
mov    -0x5c(%rbp),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
mov    0x2cbe(%rip),%rax
mov    (%rax),%edx
lea    -0x58(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1345 <setupListen+0x165>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
lea    0xd07(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x5c(%rbp),%edi
call   1070 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1291 <setupListen+0xb1>
mov    -0x5c(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     13ce <setupListen+0x1ee>
mov    -0x5c(%rbp),%edi
mov    0x2c5a(%rip),%rax
mov    (%rax),%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
je     13ce <setupListen+0x1ee>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0x0,-0x60(%rbp)
jle    139e <setupListen+0x1be>
movl   $0x0,-0x4(%rbp)
jmp    148f <setupListen+0x2af>
cmpl   $0x0,-0x60(%rbp)
jne    13ad <setupListen+0x1cd>
jmp    13e7 <setupListen+0x207>
mov    0x2c1c(%rip),%rax
mov    (%rax),%edi
lea    0xc8b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x5c(%rbp),%edi
call   1070 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1291 <setupListen+0xb1>
cmpl   $0x0,-0x60(%rbp)
jge    1414 <setupListen+0x234>
mov    0x2bd8(%rip),%rax
mov    (%rax),%edi
lea    0xc5e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    148f <setupListen+0x2af>
movl   $0x4,-0x6c(%rbp)
mov    -0x5c(%rbp),%edi
lea    -0x68(%rbp),%rsi
lea    -0x6c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jge    144e <setupListen+0x26e>
lea    0xc39(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    1478 <setupListen+0x298>
lea    0xc30(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x54(%rbp)
jne    148a <setupListen+0x2aa>
mov    -0x50(%rbp),%edi
call   1040 <handleIncomingConnection@plt>
jmp    141b <setupListen+0x23b>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setupListen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%rbp
movl   $0x1,0x14(%rsp)
lea    0x18(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    %ebx,0x18(%rsp)
mov    0x2da3(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x34(%rsp)
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   10e0 <getaddrinfo@plt>
test   %eax,%eax
je     126f <setupListen+0x8f>
mov    0x2d82(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xda2(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    135e <setupListen+0x17e>
mov    (%rsp),%rax
test   %rax,%rax
je     1347 <setupListen+0x167>
mov    0x2d4d(%rip),%r12
mov    0x2d5e(%rip),%rbp
mov    0x2d27(%rip),%r14
lea    0x14(%rsp),%r15
jmp    12ca <setupListen+0xea>
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edi
lea    0xd81(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    (%rsp),%rax
mov    0x8(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
je     1347 <setupListen+0x167>
mov    (%rax),%edi
mov    0x0(%r13),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1330 <setupListen+0x150>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
mov    (%r14),%edx
mov    %eax,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     12a0 <setupListen+0xc0>
mov    (%rsp),%rax
mov    0x10(%rax),%edx
mov    0x14(%rax),%esi
mov    %ebx,%edi
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12b2 <setupListen+0xd2>
mov    0x2cb3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
je     12b2 <setupListen+0xd2>
jmp    1374 <setupListen+0x194>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12),%edi
lea    0xcd9(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12b9 <setupListen+0xd9>
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
lea    0xd08(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
jg     1363 <setupListen+0x183>
jne    13a9 <setupListen+0x1c9>
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <accept@plt>
test   %eax,%eax
jns    13c5 <setupListen+0x1e5>
lea    0xcd1(%rip),%rdi
jmp    1403 <setupListen+0x223>
mov    (%r12),%edi
lea    0xc94(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
jmp    13e6 <setupListen+0x206>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     13a0 <setupListen+0x1c0>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
js     13fc <setupListen+0x21c>
jne    13d3 <setupListen+0x1f3>
mov    %ebp,%edi
call   1040 <handleIncomingConnection@plt>
jmp    13d3 <setupListen+0x1f3>
lea    0xc96(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, %struct.addrinfo*, i32, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"getaddrinfo says %s\00"
@.str.1 = [24 x i8] c"socket creation failed\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [28 x i8] c"could not set SO_REUSEADDR\0A\00"
@SOMAXCONN = global i32 0
@.str.3 = [23 x i8] c"failed to fork process\00"
@.str.4 = [25 x i8] c"failed to listen on port\00"
@.str.5 = [33 x i8] c"error from accept call on server\00"
@.str.6 = [46 x i8] c"error forking process for incoming connection\00"
define i32 @setupListen(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.sockaddr_storage
%17 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 1, i32* %13
store i32 -1, i32* %14
store i32 -1, i32* %15
%18 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 32)
%19 = load i32, i32* %7
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @SOCK_STREAM
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 5
store i32 %21, i32* %22
%23 = load i32, i32* @IPPROTO_TCP
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %23, i32* %24
%25 = load i8*, i8** %5
%26 = load i8*, i8** %6
%27 = call i32 @getaddrinfo(i8* %25, i8* %26, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %3
%31 = load i32, i32* @stderr
%32 = load i32, i32* %10
%33 = call i8* @gai_strerror(i32 %32)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %33)
store i32 1, i32* %4
br label %133
35: ; preds = %3
br label %36
36: ; preds = %99, %60, %47, %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %9
%38 = icmp ne %struct.addrinfo* %37, null
br i1 %38, label %39, label %103
39: ; preds = %36
%40 = load %struct.addrinfo*, %struct.addrinfo** %9
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i32, i32* @SOCK_STREAM
%44 = call i32 @socket(i32 %42, i32 %43, i32 0)
store i32 %44, i32* %14
%45 = load i32, i32* %14
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %39
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%50 = load %struct.addrinfo*, %struct.addrinfo** %9
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 1
%52 = load %struct.addrinfo*, %struct.addrinfo** %51
store %struct.addrinfo* %52, %struct.addrinfo** %9
br label %36
53: ; preds = %39
%54 = load i32, i32* %14
%55 = load i32, i32* @SOL_SOCKET
%56 = load i32, i32* @SO_REUSEADDR
%57 = bitcast i32* %13 to i8*
%58 = call i64 @setsockopt(i32 %54, i32 %55, i32 %56, i8* %57, i32 4)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %68
60: ; preds = %53
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%63 = load i32, i32* %14
%64 = call i32 @close(i32 %63)
%65 = load %struct.addrinfo*, %struct.addrinfo** %9
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 1
%67 = load %struct.addrinfo*, %struct.addrinfo** %66
store %struct.addrinfo* %67, %struct.addrinfo** %9
br label %36
68: ; preds = %53
%69 = load i32, i32* %14
%70 = load %struct.addrinfo*, %struct.addrinfo** %9
%71 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %70, i32 0, i32 3
%72 = load i32, i32* %71
%73 = load %struct.addrinfo*, %struct.addrinfo** %9
%74 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %73, i32 0, i32 2
%75 = load i32, i32* %74
%76 = call i32 @bind(i32 %69, i32 %72, i32 %75)
%77 = icmp ne i32 %76, -1
br i1 %77, label %78, label %96
78: ; preds = %68
%79 = load i32, i32* %14
%80 = load i32, i32* @SOMAXCONN
%81 = call i32 @listen(i32 %79, i32 %80)
%82 = icmp ne i32 %81, -1
br i1 %82, label %83, label %96
83: ; preds = %78
%84 = call i32 (...) @fork()
store i32 %84, i32* %15
%85 = load i32, i32* %15
%86 = icmp sgt i32 %85, 0
br i1 %86, label %87, label %88
87: ; preds = %83
store i32 0, i32* %4
br label %133
88: ; preds = %83
%89 = load i32, i32* %15
%90 = icmp eq i32 %89, 0
br i1 %90, label %91, label %92
91: ; preds = %88
br label %103
92: ; preds = %88
%93 = load i32, i32* @stderr
%94 = call i32 (i32, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%95 = call i32 @exit(i32 1)
unreachable
96: ; preds = %78, %68
%97 = load i32, i32* %14
%98 = call i32 @close(i32 %97)
br label %99
99: ; preds = %96
%100 = load %struct.addrinfo*, %struct.addrinfo** %9
%101 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %100, i32 0, i32 1
%102 = load %struct.addrinfo*, %struct.addrinfo** %101
store %struct.addrinfo* %102, %struct.addrinfo** %9
br label %36
103: ; preds = %91, %36
%104 = load i32, i32* %15
%105 = icmp slt i32 %104, 0
br i1 %105, label %106, label %109
106: ; preds = %103
%107 = load i32, i32* @stderr
%108 = call i32 (i32, i8*, ...) @fprintf(i32 %107, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %4
br label %133
109: ; preds = %103
store i32 4, i32* %17
br label %110
110: ; preds = %109, %132
%111 = load i32, i32* %14
%112 = bitcast %struct.sockaddr_storage* %16 to %struct.sockaddr*
%113 = call i32 @accept(i32 %111, %struct.sockaddr* %112, i32* %17)
store i32 %113, i32* %11
%114 = load i32, i32* %11
%115 = icmp slt i32 %114, 0
br i1 %115, label %116, label %119
116: ; preds = %110
%117 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%118 = call i32 @exit(i32 1)
unreachable
119: ; preds = %110
%120 = call i32 (...) @fork()
store i32 %120, i32* %12
%121 = load i32, i32* %12
%122 = icmp slt i32 %121, 0
br i1 %122, label %123, label %126
123: ; preds = %119
%124 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0))
%125 = call i32 @exit(i32 1)
unreachable
126: ; preds = %119
%127 = load i32, i32* %12
%128 = icmp eq i32 %127, 0
br i1 %128, label %129, label %132
129: ; preds = %126
%130 = load i32, i32* %11
%131 = call i32 @handleIncomingConnection(i32 %130)
br label %132
132: ; preds = %129, %126
br label %110
133: ; preds = %106, %87, %30
%134 = load i32, i32* %4
ret i32 %134
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, %struct.addrinfo*, i32, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"getaddrinfo says %s\00"
@.str.1 = [24 x i8] c"socket creation failed\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [28 x i8] c"could not set SO_REUSEADDR\0A\00"
@SOMAXCONN = global i32 0
@.str.3 = [23 x i8] c"failed to fork process\00"
@.str.4 = [25 x i8] c"failed to listen on port\00"
@.str.5 = [33 x i8] c"error from accept call on server\00"
@.str.6 = [46 x i8] c"error forking process for incoming connection\00"
define i32 @setupListen(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage
%8 = alloca i32
%9 = bitcast %struct.addrinfo* %4 to i8*
%10 = bitcast %struct.addrinfo** %5 to i8*
%11 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%12 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %2, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %14, i32* %15
%16 = load i32, i32* @IPPROTO_TCP
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %16, i32* %17
%18 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = call i8* @gai_strerror(i32 %18)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %107
24: ; preds = %3
%25 = load %struct.addrinfo*, %struct.addrinfo** %5
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %81, label %27
27: ; preds = %24, %40
%28 = phi %struct.addrinfo* [ %41, %40 ], [ %25, %24 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %43
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%37 = load %struct.addrinfo*, %struct.addrinfo** %5
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 1
%39 = load %struct.addrinfo*, %struct.addrinfo** %38
store %struct.addrinfo* %39, %struct.addrinfo** %5
br label %40
40: ; preds = %34, %48, %76
%41 = phi %struct.addrinfo* [ %80, %76 ], [ %54, %48 ], [ %39, %34 ]
%42 = icmp eq %struct.addrinfo* %41, null
br i1 %42, label %81, label %27
43: ; preds = %27
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_REUSEADDR
%46 = call i64 @setsockopt(i32 %32, i32 %44, i32 %45, i8* %11, i32 4)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @close(i32 %32)
%52 = load %struct.addrinfo*, %struct.addrinfo** %5
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i64 0, i32 1
%54 = load %struct.addrinfo*, %struct.addrinfo** %53
store %struct.addrinfo* %54, %struct.addrinfo** %5
br label %40
55: ; preds = %43
%56 = load %struct.addrinfo*, %struct.addrinfo** %5
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i64 0, i32 3
%58 = load i32, i32* %57
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i64 0, i32 2
%60 = load i32, i32* %59
%61 = call i32 @bind(i32 %32, i32 %58, i32 %60)
%62 = icmp eq i32 %61, -1
br i1 %62, label %76, label %63
63: ; preds = %55
%64 = load i32, i32* @SOMAXCONN
%65 = call i32 @listen(i32 %32, i32 %64)
%66 = icmp eq i32 %65, -1
br i1 %66, label %76, label %67
67: ; preds = %63
%68 = call i32 (...) @fork()
%69 = icmp sgt i32 %68, 0
br i1 %69, label %107, label %70
70: ; preds = %67
%71 = icmp eq i32 %68, 0
br i1 %71, label %84, label %72
72: ; preds = %70
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %63, %55
%77 = call i32 @close(i32 %32)
%78 = load %struct.addrinfo*, %struct.addrinfo** %5
%79 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %78, i64 0, i32 1
%80 = load %struct.addrinfo*, %struct.addrinfo** %79
store %struct.addrinfo* %80, %struct.addrinfo** %5
br label %40
81: ; preds = %40, %24
%82 = load i32, i32* @stderr
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %107
84: ; preds = %70
%85 = bitcast %struct.sockaddr_storage* %7 to i8*
%86 = bitcast i32* %8 to i8*
store i32 4, i32* %8
%87 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%88 = call i32 @accept(i32 %32, %struct.sockaddr* %87, i32* %8)
%89 = icmp slt i32 %88, 0
br i1 %89, label %90, label %93
90: ; preds = %104, %84
%91 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%92 = call i32 @exit(i32 1)
unreachable
93: ; preds = %84, %104
%94 = phi i32 [ %105, %104 ], [ %88, %84 ]
%95 = call i32 (...) @fork()
%96 = icmp slt i32 %95, 0
br i1 %96, label %97, label %100
97: ; preds = %93
%98 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0))
%99 = call i32 @exit(i32 1)
unreachable
100: ; preds = %93
%101 = icmp eq i32 %95, 0
br i1 %101, label %102, label %104
102: ; preds = %100
%103 = call i32 @handleIncomingConnection(i32 %94)
br label %104
104: ; preds = %102, %100
%105 = call i32 @accept(i32 %32, %struct.sockaddr* %87, i32* %8)
%106 = icmp slt i32 %105, 0
br i1 %106, label %90, label %93
107: ; preds = %67, %81, %20
%108 = phi i32 [ 1, %20 ], [ 1, %81 ], [ 0, %67 ]
ret i32 %108
}