#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;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ opt;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int 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*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_network_connection(int port) {
  int socket_fd = socket(PF_INET, SOCK_STREAM, 0);
  if (socket_fd < 0) {
    perror("Failed to create network socket");
    return EXIT_FAILURE;
  }
  int opt = 1;
  if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
                 sizeof(opt)) == -1) {
    perror("Warning: can't set SO_REUSEADDR");
  }
  struct sockaddr_in addr = {
      0,
  };
  addr.sin_family = PF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(socket_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    perror("Error while binding socket");
    goto socket_init_failed;
  }
  if (listen(socket_fd, 1) == -1) {
    perror("Error while started listing on socket");
    goto socket_init_failed;
  }
  return socket_fd;
socket_init_failed:
  close(socket_fd);
  return -1;
}


x86 assembly (clang, -O0):
<init_network_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11de <init_network_connection+0x4e>
lea    0xe38(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c2 <init_network_connection+0x132>
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    121e <init_network_connection+0x8e>
lea    0xe07(%rip),%rdi
call   10a0 <perror@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1281 <init_network_connection+0xf1>
lea    0xdc9(%rip),%rdi
call   10a0 <perror@plt>
jmp    12b3 <init_network_connection+0x123>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12a8 <init_network_connection+0x118>
lea    0xdbd(%rip),%rdi
call   10a0 <perror@plt>
jmp    12b3 <init_network_connection+0x123>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c2 <init_network_connection+0x132>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_network_connection>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
mov    0x2e37(%rip),%r14
mov    (%r14),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     123d <init_network_connection+0xbd>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1254 <init_network_connection+0xd4>
movl   $0x0,0x1c(%rsp)
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1265 <init_network_connection+0xe5>
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     126e <init_network_connection+0xee>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdbc(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%ebx
jmp    1232 <init_network_connection+0xb2>
lea    0xdc5(%rip),%rdi
call   1090 <perror@plt>
jmp    11df <init_network_connection+0x5f>
lea    0xdd4(%rip),%rdi
jmp    1275 <init_network_connection+0xf5>
lea    0xde6(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    1232 <init_network_connection+0xb2>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [32 x i8] c"Failed to create network socket\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [32 x i8] c"Warning: can't set SO_REUSEADDR\00"
@INADDR_ANY = global i32 0
@.str.2 = [27 x i8] c"Error while binding socket\00"
@.str.3 = [38 x i8] c"Error while started listing on socket\00"
define i32 @init_network_connection(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
br label %41
11: ; preds = %1
%12 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %6, i32 %13, i32 %14, i8* %12, i32 4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = bitcast %struct.sockaddr_in* %3 to i8*
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 0, i32* %21
%22 = load i32, i32* @PF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 %0)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @bind(i32 %6, %struct.sockaddr* %29, i32 16)
%31 = icmp eq i32 %30, -1
br i1 %31, label %35, label %32
32: ; preds = %19
%33 = call i32 @listen(i32 %6, i32 1)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %32, %19
%36 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), %19 ], [ getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), %32 ]
%37 = call i32 @perror(i8* %36)
%38 = call i32 @close(i32 %6)
br label %39
39: ; preds = %32, %35
%40 = phi i32 [ -1, %35 ], [ %6, %32 ]
br label %41
41: ; preds = %39, %8
%42 = phi i32 [ %10, %8 ], [ %40, %39 ]
ret i32 %42
}