#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_family;
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ opt;
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*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ listen(int, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_set_nonblocking(int);
int setup_socket_server(unsigned short port) {
  struct sockaddr_in addr;
  socklen_t addr_length;
  int fd, opt, opt_length;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return -1;
  opt = 1;
  opt_length = sizeof(opt);
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, opt_length) == -1)
    return -1;
  if (socket_set_nonblocking(fd) == -1)
    return -1;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(port);
  addr.sin_family = AF_INET;
  addr_length = sizeof(addr);
  if (bind(fd, (struct sockaddr *)&addr, addr_length) < 0)
    return -1;
  if (listen(fd, SOMAXCONN) < 0)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<setup_socket_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11b0 <setup_socket_server+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <setup_socket_server+0x124>
movl   $0x1,-0x24(%rbp)
movl   $0x4,-0x28(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    -0x28(%rbp),%r8d
lea    -0x24(%rbp),%rcx
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11f5 <setup_socket_server+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <setup_socket_server+0x124>
mov    -0x20(%rbp),%edi
call   1030 <socket_set_nonblocking@plt>
cmp    $0xffffffff,%eax
jne    1212 <setup_socket_server+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <setup_socket_server+0x124>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x10(%rbp)
movzwl -0x6(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0xc,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1267 <setup_socket_server+0xf7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <setup_socket_server+0x124>
mov    -0x20(%rbp),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    128e <setup_socket_server+0x11e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <setup_socket_server+0x124>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_socket_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    0x2e5c(%rip),%r15
mov    (%r15),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     123c <setup_socket_server+0xcc>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     123c <setup_socket_server+0xcc>
mov    %ebx,%edi
call   1030 <socket_set_nonblocking@plt>
cmp    $0xffffffff,%eax
je     123c <setup_socket_server+0xcc>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x10(%rsp)
movzwl %r14w,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     123c <setup_socket_server+0xcc>
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <listen@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%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
@INADDR_ANY = global i32 0
@SOMAXCONN = global i32 0
define i32 @setup_socket_server(i16 zeroext %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i16 %0, i16* %3
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %51
14: ; preds = %1
store i32 1, i32* %7
store i32 4, i32* %8
%15 = load i32, i32* %6
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = load i32, i32* %8
%19 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %7, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 -1, i32* %2
br label %51
22: ; preds = %14
%23 = load i32, i32* %6
%24 = call i32 @socket_set_nonblocking(i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %2
br label %51
27: ; preds = %22
%28 = load i32, i32* @INADDR_ANY
%29 = call i32 @htonl(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load i16, i16* %3
%33 = call i32 @htons(i16 zeroext %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %33, i32* %34
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %35, i32* %36
store i32 12, i32* %5
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = load i32, i32* %5
%40 = call i64 @bind(i32 %37, %struct.sockaddr* %38, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %27
store i32 -1, i32* %2
br label %51
43: ; preds = %27
%44 = load i32, i32* %6
%45 = load i32, i32* @SOMAXCONN
%46 = call i64 @listen(i32 %44, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
store i32 -1, i32* %2
br label %51
49: ; preds = %43
%50 = load i32, i32* %6
store i32 %50, i32* %2
br label %51
51: ; preds = %49, %48, %42, %26, %21, %13
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%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
@INADDR_ANY = global i32 0
@SOMAXCONN = global i32 0
define i32 @setup_socket_server(i16 zeroext %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %34, label %10
10: ; preds = %1
store i32 1, i32* %3
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_REUSEADDR
%13 = call i32 @setsockopt(i32 %8, i32 %11, i32 %12, i32* %3, i32 4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %34, label %15
15: ; preds = %10
%16 = call i32 @socket_set_nonblocking(i32 %8)
%17 = icmp eq i32 %16, -1
br i1 %17, label %34, label %18
18: ; preds = %15
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2, i32 0
store i32 %20, i32* %21
%22 = call i32 @htons(i16 zeroext %0)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i64 @bind(i32 %8, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %18
%30 = load i32, i32* @SOMAXCONN
%31 = call i64 @listen(i32 %8, i32 %30)
%32 = icmp slt i64 %31, 0
%33 = select i1 %32, i32 -1, i32 %8
br label %34
34: ; preds = %29, %18, %15, %10, %1
%35 = phi i32 [ -1, %1 ], [ -1, %10 ], [ -1, %15 ], [ -1, %18 ], [ %33, %29 ]
ret i32 %35
}