#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 ai_flags;
  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_canonname;
};
typedef int /*<<< orphan*/ optval;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int AI_NUMERICSERV;
int AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errExit(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                       struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char const *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int bindServerSocket(const char *portNum, int backlog) {
  printf("Binding server socket to port %s.\n", portNum);
  struct addrinfo hints;
  struct addrinfo *result;
  struct addrinfo *rp;
  int optval;
  int lfd = -1;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_canonname = NULL;
  hints.ai_addr = NULL;
  hints.ai_next = NULL;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_family = AF_UNSPEC;
  hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
  if (getaddrinfo(NULL, portNum, &hints, &result) != 0)
    errExit("getaddrinfo\n");
  optval = 1;
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    lfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (lfd == -1) {
      continue;
    }
    if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) ==
        -1) {
      errExit("setsockopt\n");
    }
    if (bind(lfd, rp->ai_addr, rp->ai_addrlen) == 0) {
      break;
    }
    close(lfd);
  }
  if (rp == NULL) {
    errExit("Could not bind socket to any address\n");
  }
  if (listen(lfd, backlog) == -1) {
    errExit("Listen\n");
  }
  freeaddrinfo(result);
  return lfd;
}


x86 assembly (clang, -O0):
<bindServerSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe46(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x58(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1060 <memset@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    0x2de2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1246 <bindServerSocket+0xa6>
lea    0xde2(%rip),%rdi
call   1040 <errExit@plt>
movl   $0x1,-0x54(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1304 <bindServerSocket+0x164>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    128c <bindServerSocket+0xec>
jmp    12f3 <bindServerSocket+0x153>
mov    -0x58(%rbp),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12c5 <bindServerSocket+0x125>
lea    0xd70(%rip),%rdi
call   1040 <errExit@plt>
mov    -0x58(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jne    12eb <bindServerSocket+0x14b>
jmp    1304 <bindServerSocket+0x164>
mov    -0x58(%rbp),%edi
call   1070 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1255 <bindServerSocket+0xb5>
cmpq   $0x0,-0x50(%rbp)
jne    131b <bindServerSocket+0x17b>
lea    0xd26(%rip),%rdi
call   1040 <errExit@plt>
mov    -0x58(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    133b <bindServerSocket+0x19b>
lea    0xd2c(%rip),%rdi
call   1040 <errExit@plt>
mov    -0x48(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x58(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bindServerSocket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %esi,0x8(%rsp)
mov    %rdi,%rbx
lea    0xe44(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
lea    0x18(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1060 <memset@plt>
movq   $0x0,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x38(%rsp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2dce(%rip),%rax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x18(%rsp)
lea    0x10(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   10a0 <getaddrinfo@plt>
test   %rax,%rax
je     123c <bindServerSocket+0x9c>
lea    0xdec(%rip),%rdi
call   1040 <errExit@plt>
movl   $0x1,0xc(%rsp)
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     12ce <bindServerSocket+0x12e>
mov    0x2d87(%rip),%r13
mov    0x2d58(%rip),%r14
lea    0xc(%rsp),%r12
lea    0xdc4(%rip),%r15
jmp    1279 <bindServerSocket+0xd9>
xchg   %ax,%ax
mov    0x20(%rbx),%rbx
test   %rbx,%rbx
je     12d3 <bindServerSocket+0x133>
mov    0x18(%rbx),%edi
mov    0x10(%rbx),%edx
mov    0x14(%rbx),%esi
call   10c0 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1270 <bindServerSocket+0xd0>
mov    0x0(%r13),%esi
mov    (%r14),%edx
mov    %ebp,%edi
mov    %r12,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12b2 <bindServerSocket+0x112>
mov    %r15,%rdi
call   1040 <errExit@plt>
mov    0x8(%rbx),%rsi
mov    0x4(%rbx),%edx
mov    %ebp,%edi
call   1090 <bind@plt>
test   %rax,%rax
je     12df <bindServerSocket+0x13f>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1270 <bindServerSocket+0xd0>
mov    $0xffffffff,%ebp
lea    0xd62(%rip),%rdi
call   1040 <errExit@plt>
mov    0x8(%rsp),%esi
mov    %ebp,%edi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12fb <bindServerSocket+0x15b>
lea    0xd6c(%rip),%rdi
call   1040 <errExit@plt>
mov    0x10(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32*, i32, i32, i32, %struct.addrinfo*, i32* }
@.str = [35 x i8] c"Binding server socket to port %s.\0A\00"
@SOCK_STREAM = global i32 0
@AF_UNSPEC = global i32 0
@AI_PASSIVE = global i32 0
@AI_NUMERICSERV = global i32 0
@.str.1 = [13 x i8] c"getaddrinfo\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [12 x i8] c"setsockopt\0A\00"
@.str.3 = [38 x i8] c"Could not bind socket to any address\0A\00"
@.str.4 = [8 x i8] c"Listen\0A\00"
define i32 @bindServerSocket(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i8*, i8** %3
%11 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %10)
store i32 -1, i32* %9
%12 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 48)
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 7
store i32* null, i32** %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 2
store i32* null, i32** %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 6
store %struct.addrinfo* null, %struct.addrinfo** %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 4
store i32 %16, i32* %17
%18 = load i32, i32* @AF_UNSPEC
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 5
store i32 %18, i32* %19
%20 = load i32, i32* @AI_PASSIVE
%21 = load i32, i32* @AI_NUMERICSERV
%22 = or i32 %20, %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i8*, i8** %3
%25 = call i64 @getaddrinfo(i32* null, i8* %24, %struct.addrinfo* %5, %struct.addrinfo** %6)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %2
%28 = call i32 @errExit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %2
store i32 1, i32* %8
%30 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %30, %struct.addrinfo** %7
br label %31
31: ; preds = %70, %29
%32 = load %struct.addrinfo*, %struct.addrinfo** %7
%33 = icmp ne %struct.addrinfo* %32, null
br i1 %33, label %34, label %74
34: ; preds = %31
%35 = load %struct.addrinfo*, %struct.addrinfo** %7
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 5
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %7
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 4
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %7
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 3
%43 = load i32, i32* %42
%44 = call i32 @socket(i32 %37, i32 %40, i32 %43)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %34
br label %70
48: ; preds = %34
%49 = load i32, i32* %9
%50 = load i32, i32* @SOL_SOCKET
%51 = load i32, i32* @SO_REUSEADDR
%52 = call i32 @setsockopt(i32 %49, i32 %50, i32 %51, i32* %8, i32 4)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %48
%55 = call i32 @errExit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %54, %48
%57 = load i32, i32* %9
%58 = load %struct.addrinfo*, %struct.addrinfo** %7
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 2
%60 = load i32*, i32** %59
%61 = load %struct.addrinfo*, %struct.addrinfo** %7
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 1
%63 = load i32, i32* %62
%64 = call i64 @bind(i32 %57, i32* %60, i32 %63)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %56
br label %74
67: ; preds = %56
%68 = load i32, i32* %9
%69 = call i32 @close(i32 %68)
br label %70
70: ; preds = %67, %47
%71 = load %struct.addrinfo*, %struct.addrinfo** %7
%72 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %71, i32 0, i32 6
%73 = load %struct.addrinfo*, %struct.addrinfo** %72
store %struct.addrinfo* %73, %struct.addrinfo** %7
br label %31
74: ; preds = %66, %31
%75 = load %struct.addrinfo*, %struct.addrinfo** %7
%76 = icmp eq %struct.addrinfo* %75, null
br i1 %76, label %77, label %79
77: ; preds = %74
%78 = call i32 @errExit(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %79
79: ; preds = %77, %74
%80 = load i32, i32* %9
%81 = load i32, i32* %4
%82 = call i32 @listen(i32 %80, i32 %81)
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %86
84: ; preds = %79
%85 = call i32 @errExit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %86
86: ; preds = %84, %79
%87 = load %struct.addrinfo*, %struct.addrinfo** %6
%88 = call i32 @freeaddrinfo(%struct.addrinfo* %87)
%89 = load i32, i32* %9
ret i32 %89
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32*, i32, i32, i32, %struct.addrinfo*, i32* }
@.str = [35 x i8] c"Binding server socket to port %s.\0A\00"
@SOCK_STREAM = global i32 0
@AF_UNSPEC = global i32 0
@AI_PASSIVE = global i32 0
@AI_NUMERICSERV = global i32 0
@.str.1 = [13 x i8] c"getaddrinfo\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [12 x i8] c"setsockopt\0A\00"
@.str.3 = [38 x i8] c"Could not bind socket to any address\0A\00"
@.str.4 = [8 x i8] c"Listen\0A\00"
define i32 @bindServerSocket(i8* %0, i32 %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = bitcast %struct.addrinfo* %3 to i8*
%8 = bitcast %struct.addrinfo** %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 48)
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32* null, i32** %11
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
%13 = bitcast %struct.addrinfo** %12 to i8*
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @AF_UNSPEC
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %16, i32* %17
%18 = load i32, i32* @AI_PASSIVE
%19 = load i32, i32* @AI_NUMERICSERV
%20 = or i32 %19, %18
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i32 %20, i32* %21
%22 = call i64 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %3, %struct.addrinfo** %4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %2
%25 = call i32 @errExit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %2
store i32 1, i32* %5
%27 = load %struct.addrinfo*, %struct.addrinfo** %4
%28 = icmp eq %struct.addrinfo* %27, null
br i1 %28, label %59, label %29
29: ; preds = %26, %55
%30 = phi %struct.addrinfo* [ %57, %55 ], [ %27, %26 ]
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 5
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 4
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 3
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %32, i32 %34, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %55, label %39
39: ; preds = %29
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_REUSEADDR
%42 = call i32 @setsockopt(i32 %37, i32 %40, i32 %41, i32* %5, i32 4)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @errExit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %39
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 2
%48 = load i32*, i32** %47
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 1
%50 = load i32, i32* %49
%51 = call i64 @bind(i32 %37, i32* %48, i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %62, label %53
53: ; preds = %46
%54 = call i32 @close(i32 %37)
br label %55
55: ; preds = %29, %53
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 6
%57 = load %struct.addrinfo*, %struct.addrinfo** %56
%58 = icmp eq %struct.addrinfo* %57, null
br i1 %58, label %59, label %29
59: ; preds = %55, %26
%60 = phi i32 [ -1, %26 ], [ %37, %55 ]
%61 = call i32 @errExit(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %46, %59
%63 = phi i32 [ %60, %59 ], [ %37, %46 ]
%64 = call i32 @listen(i32 %63, i32 %1)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = call i32 @errExit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %68
68: ; preds = %66, %62
%69 = load %struct.addrinfo*, %struct.addrinfo** %4
%70 = call i32 @freeaddrinfo(%struct.addrinfo* %69)
ret i32 %63
}