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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEPORT;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int make_socket(uint16_t port) {
  int sock;
  struct sockaddr_in name;
  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  int optval = 1;
  setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));
  name.sin_family = AF_INET;
  name.sin_port = htons(port);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
    perror("bind");
    exit(EXIT_FAILURE);
  }
  return sock;
}


x86 assembly (clang, -O0):
<make_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11bb <make_socket+0x4b>
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1243 <make_socket+0xd3>
lea    0xdd7(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_socket>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     120c <make_socket+0x9c>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     1215 <make_socket+0xa5>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xded(%rip),%rdi
jmp    121c <make_socket+0xac>
lea    0xdeb(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEPORT = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
define i32 @make_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @PF_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 %13
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
%14 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEPORT
%17 = call i32 @setsockopt(i32 %7, i32 %15, i32 %16, i32* %3, i32 4)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 %0)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i64 @bind(i32 %7, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %13
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %13
ret i32 %7
}