#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ log_error(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_server(int port_number, int max_connections) {
  int server_fd, optval = 1, bind_result, listen_result;
  struct sockaddr_in socket_details;
  if ((server_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    log_error("Could not create server", 2, errno);
    exit(EXIT_FAILURE);
  }
  setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
  socket_details.sin_family = AF_INET;
  socket_details.sin_port = htons(port_number);
  socket_details.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(server_fd, (struct sockaddr *)&socket_details,
           sizeof(struct sockaddr_in)) == -1) {
    log_error("Could not bind server address", 2, errno);
    exit(EXIT_FAILURE);
  }
  if (listen(server_fd, 0) == -1) {
    log_error("Could not listen on specified port", 2, errno);
    exit(EXIT_FAILURE);
  }
  return server_fd;
}


x86 assembly (clang, -O0):
<create_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11e2 <create_server+0x62>
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
lea    0xe36(%rip),%rdi
mov    $0x2,%esi
call   1040 <log_error@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1270 <create_server+0xf0>
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
lea    0xdc0(%rip),%rdi
mov    $0x2,%esi
call   1040 <log_error@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ab <create_server+0x12b>
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
lea    0xda3(%rip),%rdi
mov    $0x2,%esi
call   1040 <log_error@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_server>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
movl   $0x1,0x4(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     122b <create_server+0xab>
mov    %eax,%ebx
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     123d <create_server+0xbd>
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     124f <create_server+0xcf>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
lea    0xdc5(%rip),%rdi
jmp    125f <create_server+0xdf>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
lea    0xdcb(%rip),%rdi
jmp    125f <create_server+0xdf>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edx
lea    0xdd7(%rip),%rdi
mov    $0x2,%esi
call   1040 <log_error@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Could not create server\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [30 x i8] c"Could not bind server address\00"
@.str.2 = [35 x i8] c"Could not listen on specified port\00"
define i32 @create_server(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = load i32, i32* @PF_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 %16
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = tail call i32 @log_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 2, i32 %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %9, i32 %17, i32 %18, i32* %3, i32 4)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @htons(i32 %0)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %4 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 %35
30: ; preds = %16
%31 = load i32, i32* @errno
%32 = call i32 @log_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %16
%36 = call i32 @listen(i32 %9, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = call i32 @log_error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 %39)
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %35
ret i32 %9
}