#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 */
typedef int /*<<< orphan*/ value;
typedef int /*<<< orphan*/ uint16_t;
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit_cleanup();
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_single_server(struct in_addr ip, uint16_t port) {
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof(addr);
  int serverfd, flags, value;
  if ((serverfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Unable to open socket");
    exit_cleanup();
  }
  flags = fcntl(serverfd, F_GETFL, 0);
  flags |= O_NONBLOCK;
  fcntl(serverfd, F_SETFL, flags);
  value = 1;
  if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)) ==
      -1) {
    perror("Unable to set socket reuseaddr option");
    exit_cleanup();
  }
  memset(&addr, 0, addrlen);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr = ip;
  if (bind(serverfd, (struct sockaddr *)&addr, addrlen) == -1) {
    perror("Unable to bind socket");
    exit_cleanup();
  }
  if (listen(serverfd, 8192) != 0) {
    perror("Cannot listen for client connections");
    exit_cleanup();
  }
  return serverfd;
}


x86 assembly (clang, -O0):
<init_single_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xc,-0x1c(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11dd <init_single_server+0x4d>
lea    0xe2f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   10a0 <exit_cleanup@plt>
mov    -0x20(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
call   1040 <fcntl@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    125d <init_single_server+0xcd>
lea    0xdc5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   10a0 <exit_cleanup@plt>
mov    -0x1c(%rbp),%edx
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    12b3 <init_single_server+0x123>
lea    0xd95(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   10a0 <exit_cleanup@plt>
mov    -0x20(%rbp),%edi
mov    $0x2000,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
je     12dd <init_single_server+0x14d>
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   10a0 <exit_cleanup@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_single_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r15d
mov    %edi,%r14d
mov    0x2e37(%rip),%r12
mov    (%r12),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1287 <init_single_server+0xf7>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2df7(%rip),%rcx
or     (%rcx),%eax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
movl   $0x1,0xc(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     129f <init_single_server+0x10f>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %r14d,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     12b7 <init_single_server+0x127>
mov    %ebx,%edi
mov    $0x2000,%esi
call   1070 <listen@plt>
test   %rax,%rax
jne    12cc <init_single_server+0x13c>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd72(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   10a0 <exit_cleanup@plt>
jmp    11c8 <init_single_server+0x38>
lea    0xd70(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   10a0 <exit_cleanup@plt>
jmp    1226 <init_single_server+0x96>
lea    0xd7e(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   10a0 <exit_cleanup@plt>
jmp    1267 <init_single_server+0xd7>
lea    0xd7f(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   10a0 <exit_cleanup@plt>
jmp    1278 <init_single_server+0xe8>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Unable to open socket\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [38 x i8] c"Unable to set socket reuseaddr option\00"
@.str.2 = [22 x i8] c"Unable to bind socket\00"
@.str.3 = [37 x i8] c"Cannot listen for client connections\00"
define i32 @init_single_server(i32 %0, i32 %1) {
%3 = alloca %struct.in_addr
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
store i32 %0, i32* %10
store i32 %1, i32* %4
store i32 12, i32* %6
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%17 = call i32 (...) @exit_cleanup()
br label %18
18: ; preds = %15, %2
%19 = load i32, i32* %7
%20 = load i32, i32* @F_GETFL
%21 = call i32 @fcntl(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %8
%22 = load i32, i32* @O_NONBLOCK
%23 = load i32, i32* %8
%24 = or i32 %23, %22
store i32 %24, i32* %8
%25 = load i32, i32* %7
%26 = load i32, i32* @F_SETFL
%27 = load i32, i32* %8
%28 = call i32 @fcntl(i32 %25, i32 %26, i32 %27)
store i32 1, i32* %9
%29 = load i32, i32* %7
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_REUSEADDR
%32 = call i32 @setsockopt(i32 %29, i32 %30, i32 %31, i32* %9, i32 4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %18
%35 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 (...) @exit_cleanup()
br label %37
37: ; preds = %34, %18
%38 = load i32, i32* %6
%39 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 %38)
%40 = load i32, i32* @AF_INET
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %40, i32* %41
%42 = load i32, i32* %4
%43 = call i32 @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %43, i32* %44
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%46 = bitcast %struct.in_addr* %45 to i8*
%47 = bitcast %struct.in_addr* %3 to i8*
%48 = load i32, i32* %7
%49 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%50 = load i32, i32* %6
%51 = call i32 @bind(i32 %48, %struct.sockaddr* %49, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %37
%54 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 (...) @exit_cleanup()
br label %56
56: ; preds = %53, %37
%57 = load i32, i32* %7
%58 = call i64 @listen(i32 %57, i32 8192)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %56
%61 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%62 = call i32 (...) @exit_cleanup()
br label %63
63: ; preds = %60, %56
%64 = load i32, i32* %7
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Unable to open socket\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [38 x i8] c"Unable to set socket reuseaddr option\00"
@.str.2 = [22 x i8] c"Unable to bind socket\00"
@.str.3 = [37 x i8] c"Cannot listen for client connections\00"
define i32 @init_single_server(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%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 %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 (...) @exit_cleanup()
br label %14
14: ; preds = %11, %2
%15 = load i32, i32* @F_GETFL
%16 = tail call i32 @fcntl(i32 %9, i32 %15, i32 0)
%17 = load i32, i32* @O_NONBLOCK
%18 = or i32 %17, %16
%19 = load i32, i32* @F_SETFL
%20 = tail call i32 @fcntl(i32 %9, i32 %19, i32 %18)
store i32 1, i32* %4
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %9, i32 %21, i32 %22, i32* %4, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 (...) @exit_cleanup()
br label %28
28: ; preds = %25, %14
%29 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %30, i32* %31
%32 = call i32 @htons(i32 %1)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %0, i32* %34
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i32 @bind(i32 %9, %struct.sockaddr* %35, i32 12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %28
%39 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 (...) @exit_cleanup()
br label %41
41: ; preds = %38, %28
%42 = call i64 @listen(i32 %9, i32 8192)
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 (...) @exit_cleanup()
br label %47
47: ; preds = %44, %41
ret i32 %9
}