#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_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ reuse;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setnonblocking(int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int setup_server_socket(int port) {
  signal(SIGPIPE, SIG_IGN);
  struct sockaddr_in server_addr;
  int listen_sock;
  listen_sock = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == listen_sock) {
    fprintf(stderr, "error create listen socket\n");
    return -1;
  }
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_port = htons(port);
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  int ret;
  ret = bind(listen_sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
  if (-1 == ret) {
    fprintf(stderr, "error bind: %s\n", strerror(errno));
    return -1;
  }
  setnonblocking(listen_sock);
  int reuse = 1;
  setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  if (-1 == listen(listen_sock, 4096)) {
    fprintf(stderr, "error listen\n");
    return -1;
  }
  return listen_sock;
}


x86 assembly (clang, -O0):
<setup_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    121f <setup_server_socket+0x6f>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <setup_server_socket+0x17b>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,-0x20(%rbp)
mov    $0xffffffff,%eax
cmp    -0x20(%rbp),%eax
jne    12b3 <setup_server_socket+0x103>
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <setup_server_socket+0x17b>
mov    -0x1c(%rbp),%edi
call   10b0 <setnonblocking@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
mov    0x2ccb(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1000,%esi
call   1090 <listen@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1325 <setup_server_socket+0x175>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
lea    0xd1a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <setup_server_socket+0x17b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_server_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e06(%rip),%r15
mov    (%r15),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     129a <setup_server_socket+0xea>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12b3 <setup_server_socket+0x103>
mov    %ebx,%edi
call   10b0 <setnonblocking@plt>
movl   $0x1,0x4(%rsp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebx,%edi
mov    $0x1000,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12e2 <setup_server_socket+0x132>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
jmp    12aa <setup_server_socket+0xfa>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12dd <setup_server_socket+0x12d>
mov    0x2d06(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd4b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"error create listen socket\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [16 x i8] c"error bind: %s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [14 x i8] c"error listen\0A\00"
define i32 @setup_server_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @SIGPIPE
%9 = load i32, i32* @SIG_IGN
%10 = call i32 @signal(i32 %8, i32 %9)
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %55
19: ; preds = %1
%20 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%21 = load i32, i32* %3
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 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* %4, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* %5
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 -1, %33
br i1 %34, label %35, label %40
35: ; preds = %19
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %38)
store i32 -1, i32* %2
br label %55
40: ; preds = %19
%41 = load i32, i32* %5
%42 = call i32 @setnonblocking(i32 %41)
store i32 1, i32* %7
%43 = load i32, i32* %5
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_REUSEADDR
%46 = call i32 @setsockopt(i32 %43, i32 %44, i32 %45, i32* %7, i32 4)
%47 = load i32, i32* %5
%48 = call i32 @listen(i32 %47, i32 4096)
%49 = icmp eq i32 -1, %48
br i1 %49, label %50, label %53
50: ; preds = %40
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %55
53: ; preds = %40
%54 = load i32, i32* %5
store i32 %54, i32* %2
br label %55
55: ; preds = %53, %50, %35, %16
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"error create listen socket\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [16 x i8] c"error bind: %s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [14 x i8] c"error listen\0A\00"
define i32 @setup_server_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = load i32, i32* @SIGPIPE
%5 = load i32, i32* @SIG_IGN
%6 = tail call i32 @signal(i32 %4, i32 %5)
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %45
15: ; preds = %1
%16 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%17 = call i32 @htons(i32 %0)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%25 = call i32 @bind(i32 %10, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %15
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %30)
br label %45
32: ; preds = %15
%33 = call i32 @setnonblocking(i32 %10)
%34 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%35 = load i32, i32* @SOL_SOCKET
%36 = load i32, i32* @SO_REUSEADDR
%37 = call i32 @setsockopt(i32 %10, i32 %35, i32 %36, i32* %3, i32 4)
%38 = call i32 @listen(i32 %10, i32 4096)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %32, %40
%44 = phi i32 [ -1, %40 ], [ %10, %32 ]
br label %45
45: ; preds = %27, %43, %12
%46 = phi i32 [ -1, %12 ], [ -1, %27 ], [ %44, %43 ]
ret i32 %46
}