#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ dieSysError(char *);
int /*<<< orphan*/ htons(int);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ logDebug(char *, int);
int /*<<< orphan*/ logNorm(char *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ipListenSocket(int port) {
  int fd;
  int opt = 1;
  struct sockaddr_in sinInterface;
  logDebug("listenSocket(%d)\n", port);
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1)
    dieSysError("Failed to create server socket\n");
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
    dieSysError("setsock() failed\n");
  sinInterface.sin_family = AF_INET;
  sinInterface.sin_addr.s_addr = INADDR_ANY;
  sinInterface.sin_port = htons(port);
  if (bind(fd, (struct sockaddr *)&sinInterface, sizeof(struct sockaddr_in)) ==
      -1)
    dieSysError("bind() failed\n");
  if (listen(fd, SOMAXCONN) == -1)
    dieSysError("listen() failed\n");
  logNorm("Listening on port %d\n", port);
  return fd;
}


x86 assembly (clang, -O0):
<ipListenSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe64(%rip),%rdi
call   1090 <logDebug@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d3 <ipListenSocket+0x53>
lea    0xe44(%rip),%rdi
call   1080 <dieSysError@plt>
mov    -0x8(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    120c <ipListenSocket+0x8c>
lea    0xe2b(%rip),%rdi
call   1080 <dieSysError@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1255 <ipListenSocket+0xd5>
lea    0xdf4(%rip),%rdi
call   1080 <dieSysError@plt>
mov    -0x8(%rbp),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    127b <ipListenSocket+0xfb>
lea    0xddd(%rip),%rdi
call   1080 <dieSysError@plt>
mov    -0x4(%rbp),%esi
lea    0xddf(%rip),%rdi
call   1050 <logNorm@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipListenSocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
movl   $0x1,0xc(%rsp)
lea    0xe67(%rip),%rdi
mov    %ebx,%esi
call   1090 <logDebug@plt>
mov    0x2e39(%rip),%r14
mov    (%r14),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11cd <ipListenSocket+0x4d>
lea    0xe4a(%rip),%rdi
call   1080 <dieSysError@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1202 <ipListenSocket+0x82>
lea    0xe35(%rip),%rdi
call   1080 <dieSysError@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1243 <ipListenSocket+0xc3>
lea    0xe06(%rip),%rdi
call   1080 <dieSysError@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1264 <ipListenSocket+0xe4>
lea    0xdf4(%rip),%rdi
call   1080 <dieSysError@plt>
lea    0xdf9(%rip),%rdi
mov    %ebx,%esi
call   1050 <logNorm@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"listenSocket(%d)\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [32 x i8] c"Failed to create server socket\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [18 x i8] c"setsock() failed\0A\00"
@INADDR_ANY = global i32 0
@.str.3 = [15 x i8] c"bind() failed\0A\00"
@SOMAXCONN = global i32 0
@.str.4 = [17 x i8] c"listen() failed\0A\00"
@.str.5 = [22 x i8] c"Listening on port %d\0A\00"
define i32 @ipListenSocket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %2
store i32 1, i32* %4
%6 = load i32, i32* %2
%7 = call i32 @logDebug(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @dieSysError(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %1
%16 = load i32, i32* %3
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %4, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @dieSysError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %2
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %3
%33 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%34 = call i32 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %23
%37 = call i32 @dieSysError(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %23
%39 = load i32, i32* %3
%40 = load i32, i32* @SOMAXCONN
%41 = call i32 @listen(i32 %39, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @dieSysError(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %38
%46 = load i32, i32* %2
%47 = call i32 @logNorm(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"listenSocket(%d)\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [32 x i8] c"Failed to create server socket\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [18 x i8] c"setsock() failed\0A\00"
@INADDR_ANY = global i32 0
@.str.3 = [15 x i8] c"bind() failed\0A\00"
@SOMAXCONN = global i32 0
@.str.4 = [17 x i8] c"listen() failed\0A\00"
@.str.5 = [22 x i8] c"Listening on port %d\0A\00"
define i32 @ipListenSocket(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = tail call i32 @logDebug(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @dieSysError(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %9, i32 %14, i32 %15, i32* %2, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @dieSysError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %23, i32* %24
%25 = call i32 @htons(i32 %0)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @bind(i32 %9, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = call i32 @dieSysError(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %20
%33 = load i32, i32* @SOMAXCONN
%34 = call i32 @listen(i32 %9, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @dieSysError(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = call i32 @logNorm(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %0)
ret i32 %9
}