#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;
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*/ one;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
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*/ 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 bind_socket(uint16_t port, int *out_listen_socket) {
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket");
    return sock;
  }
  int ret;
  int one = 1;
  ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  if (ret != 0) {
    perror("setsockopt(SO_REUSEADDR)");
    goto error;
  }
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
  if (ret != 0) {
    perror("bind");
    goto error;
  }
  ret = listen(sock, 1);
  if (ret != 0) {
    perror("listen");
    goto error;
  }
  *out_listen_socket = sock;
  return 0;
error:
  close(sock);
  return ret;
}


x86 assembly (clang, -O0):
<bind_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11dc <bind_socket+0x4c>
lea    0xe34(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12da <bind_socket+0x14a>
movl   $0x1,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1225 <bind_socket+0x95>
lea    0xdec(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cc <bind_socket+0x13c>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     128c <bind_socket+0xfc>
lea    0xd9e(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cc <bind_socket+0x13c>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     12b7 <bind_socket+0x127>
lea    0xd78(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cc <bind_socket+0x13c>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12da <bind_socket+0x14a>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e37(%rip),%r12
mov    (%r12),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     126e <bind_socket+0xde>
movl   $0x1,0xc(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
jne    127c <bind_socket+0xec>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %eax,%eax
jne    1287 <bind_socket+0xf7>
mov    %ebp,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
test   %eax,%eax
jne    1292 <bind_socket+0x102>
mov    %ebp,(%r14)
xor    %ebx,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8b(%rip),%rdi
call   10a0 <perror@plt>
jmp    125f <bind_socket+0xcf>
mov    %eax,%ebx
lea    0xd82(%rip),%rdi
jmp    129b <bind_socket+0x10b>
mov    %eax,%ebx
lea    0xd90(%rip),%rdi
jmp    129b <bind_socket+0x10b>
mov    %eax,%ebx
lea    0xd8a(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    125d <bind_socket+0xcd>

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

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