#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setnonblocking(int);
int /*<<< orphan*/ setreuseaddr(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_sock(int port) {
  struct sockaddr_in server_addr;
  memset(&server_addr, 0, sizeof(struct sockaddr_in));
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  setnonblocking(fd);
  setreuseaddr(fd);
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(port);
  if (0 !=
      bind(fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in))) {
    perror("bind fail\n");
    close(fd);
    exit(1);
  }
  if (0 != listen(fd, SOMAXCONN)) {
    perror("listen fail\n");
    close(fd);
    exit(1);
  }
  return fd;
}


x86 assembly (clang, -O0):
<init_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11fd <init_sock+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <init_sock+0x105>
mov    -0x1c(%rbp),%edi
call   10a0 <setnonblocking@plt>
mov    -0x1c(%rbp),%edi
call   1030 <setreuseaddr@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1272 <init_sock+0xc2>
lea    0xda5(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
call   1080 <listen@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     12af <init_sock+0xff>
lea    0xd73(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_sock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e17(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     124e <init_sock+0x9e>
mov    %eax,%ebx
mov    %eax,%edi
call   10a0 <setnonblocking@plt>
mov    %ebx,%edi
call   1030 <setreuseaddr@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jne    125e <init_sock+0xae>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <listen@plt>
test   %rax,%rax
je     1253 <init_sock+0xa3>
lea    0xdbf(%rip),%rdi
jmp    1265 <init_sock+0xb5>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd9b(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [11 x i8] c"bind fail\0A\00"
@SOMAXCONN = global i32 0
@.str.1 = [13 x i8] c"listen fail\0A\00"
define i32 @init_sock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %3
%6 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %48
13: ; preds = %1
%14 = load i32, i32* %5
%15 = call i32 @setnonblocking(i32 %14)
%16 = load i32, i32* %5
%17 = call i32 @setreuseaddr(i32 %16)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %3
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %5
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = call i64 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp ne i64 0, %29
br i1 %30, label %31, label %36
31: ; preds = %13
%32 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%33 = load i32, i32* %5
%34 = call i32 @close(i32 %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %13
%37 = load i32, i32* %5
%38 = load i32, i32* @SOMAXCONN
%39 = call i64 @listen(i32 %37, i32 %38)
%40 = icmp ne i64 0, %39
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%43 = load i32, i32* %5
%44 = call i32 @close(i32 %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %36
%47 = load i32, i32* %5
store i32 %47, i32* %2
br label %48
48: ; preds = %46, %12
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [11 x i8] c"bind fail\0A\00"
@SOMAXCONN = global i32 0
@.str.1 = [13 x i8] c"listen fail\0A\00"
define i32 @init_sock(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %34, label %9
9: ; preds = %1
%10 = call i32 @setnonblocking(i32 %7)
%11 = call i32 @setreuseaddr(i32 %7)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %15, i32* %16
%17 = call i32 @htons(i32 %0)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%20 = call i64 @bind(i32 %7, %struct.sockaddr* %19, i32 12)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %9
%23 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @close(i32 %7)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %9
%27 = load i32, i32* @SOMAXCONN
%28 = call i64 @listen(i32 %7, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @close(i32 %7)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26, %1
%35 = phi i32 [ -1, %1 ], [ %7, %26 ]
ret i32 %35
}