#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 */
typedef int /*<<< orphan*/ uint16_t;
typedef int /*<<< orphan*/ u_int32_t;
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;
};
typedef int /*<<< orphan*/ s_addr;
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ AF_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 /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_server(int port, u_int32_t addr) {
  int s_sock_fd, one = 1;
  struct sockaddr_in s_addr;
  s_addr.sin_family = AF_INET;
  s_addr.sin_port = htons((uint16_t)port);
  s_addr.sin_addr.s_addr = htonl(addr);
  s_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (s_sock_fd == -1) {
    printf("create socket failed\n");
    close(s_sock_fd);
    return -1;
  }
  if (setsockopt(s_sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one,
                 sizeof(one)) < 0) {
    printf("set sockopt failed\n");
    close(s_sock_fd);
    return -1;
  }
  if (bind(s_sock_fd, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1) {
    printf("bind failed\n");
    close(s_sock_fd);
    return -1;
  }
  if (listen(s_sock_fd, 10) == -1) {
    printf("listen failed\n");
    close(s_sock_fd);
    return -1;
  }
  return s_sock_fd;
}


x86 assembly (clang, -O0):
<init_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x14(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11fd <init_server+0x7d>
lea    0xe1c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <init_server+0x141>
mov    -0x10(%rbp),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    124b <init_server+0xcb>
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <init_server+0x141>
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1285 <init_server+0x105>
lea    0xdbe(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <init_server+0x141>
mov    -0x10(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12bb <init_server+0x13b>
lea    0xd95(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <init_server+0x141>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_server>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e48(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1060 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1226 <init_server+0xa6>
mov    %eax,%ebx
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     1243 <init_server+0xc3>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     124c <init_server+0xcc>
mov    %ebx,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    1264 <init_server+0xe4>
lea    0xddc(%rip),%rdi
jmp    1253 <init_server+0xd3>
lea    0xe00(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
call   1070 <close@plt>
jmp    1264 <init_server+0xe4>
lea    0xdd0(%rip),%rdi
jmp    1253 <init_server+0xd3>
lea    0xdbb(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@str = [14 x i8] c"listen failed\00"
@str.4 = [12 x i8] c"bind failed\00"
@str.5 = [19 x i8] c"set sockopt failed\00"
@str.6 = [21 x i8] c"create socket failed\00"
define i32 @init_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* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 %0)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %9, i32* %10
%11 = tail call i32 @htonl(i32 %1)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.6, i64 0, i64 0))
%19 = tail call i32 @close(i32 -1)
br label %41
20: ; preds = %2
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i64 @setsockopt(i32 %15, i32 %21, i32 %22, i8* %5, i32 4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%27 = call i32 @close(i32 %15)
br label %41
28: ; preds = %20
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i32 @bind(i32 %15, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
%34 = call i32 @close(i32 %15)
br label %41
35: ; preds = %28
%36 = call i32 @listen(i32 %15, i32 10)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%40 = call i32 @close(i32 %15)
br label %41
41: ; preds = %35, %38, %32, %25, %17
%42 = phi i32 [ -1, %17 ], [ -1, %25 ], [ -1, %32 ], [ -1, %38 ], [ %15, %35 ]
ret i32 %42
}