#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*/ opt_val;
typedef int /*<<< orphan*/ name;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ SO_REUSEPORT;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ debugf(char *, ...);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int make_server_socket(uint16_t port) {
  int sock;
  struct sockaddr_in name;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    debugf("FATAL: Error opening socket.\n");
    return -1;
  }
  const int opt_val = 1;
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof(opt_val));
  setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &opt_val, sizeof(opt_val));
  name.sin_family = AF_INET;
  name.sin_port = htons(port);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  int ret = bind(sock, (struct sockaddr *)&name, sizeof(name));
  if (ret < 0) {
    debugf("FATAL: Error binding to port %d\n", port);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<make_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ab <make_server_socket+0x4b>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1060 <debugf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <make_server_socket+0x103>
movl   $0x1,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0xc(%rbp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    125d <make_server_socket+0xfd>
mov    -0x8(%rbp),%esi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1060 <debugf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <make_server_socket+0x103>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_server_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
mov    0x2e6a(%rip),%r15
mov    (%r15),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     122a <make_server_socket+0xca>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2e46(%rip),%rbx
mov    (%rbx),%esi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%rbx),%esi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r12,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
jns    123d <make_server_socket+0xdd>
lea    0xe00(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <debugf@plt>
jmp    1238 <make_server_socket+0xd8>
lea    0xdcf(%rip),%rdi
xor    %eax,%eax
call   1060 <debugf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [30 x i8] c"FATAL: Error opening socket.\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_REUSEPORT = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [33 x i8] c"FATAL: Error binding to port %d\0A\00"
define i32 @make_server_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 (i8*, ...) @debugf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %43
15: ; preds = %1
store i32 1, i32* %6
%16 = load i32, i32* %4
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %6, i32 4)
%20 = load i32, i32* %4
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEPORT
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %6, i32 4)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* %3
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = call i32 @htonl(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = load i32, i32* %4
%34 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%35 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 12)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %15
%39 = load i32, i32* %3
%40 = call i32 (i8*, ...) @debugf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %39)
store i32 -1, i32* %2
br label %43
41: ; preds = %15
%42 = load i32, i32* %4
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %38, %13
%44 = load i32, i32* %2
ret i32 %44
}

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 = [30 x i8] c"FATAL: Error opening socket.\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_REUSEPORT = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [33 x i8] c"FATAL: Error binding to port %d\0A\00"
define i32 @make_server_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%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 = %1
%10 = tail call i32 (i8*, ...) @debugf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %33
11: ; preds = %1
%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 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEPORT
%18 = call i32 @setsockopt(i32 %7, i32 %16, i32 %17, i32* %3, i32 4)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 %0)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, 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* %2, i64 0, i32 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i32 @bind(i32 %7, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %11
%30 = call i32 (i8*, ...) @debugf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %31
31: ; preds = %11, %29
%32 = phi i32 [ -1, %29 ], [ %7, %11 ]
br label %33
33: ; preds = %31, %9
%34 = phi i32 [ -1, %9 ], [ %32, %31 ]
ret i32 %34
}