#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_port;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int SOCK_NONBLOCK;
int SOCK_STREAM;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int open_port() {
  int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  int on = 1;
  struct sockaddr_in addr = {0};
  if (fd < 0) {
    perror("socket");
    exit(1);
  }
  if (setsockopt(fd, 0, SO_REUSEADDR, &on, sizeof(int)) < 0) {
    perror("setsockopt SO_REUSEADDR");
    exit(1);
  }
  addr.sin_port = htons(5678);
  addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind");
    exit(1);
  }
  (void)listen(fd, 10);
  return (fd);
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
cmpl   $0x0,-0x4(%rbp)
jge    11e4 <open_port+0x64>
lea    0xe2b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1221 <open_port+0xa1>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x162e,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    126b <open_port+0xeb>
lea    0xdc3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
movl   $0x1,0xc(%rsp)
movl   $0x0,0x18(%rsp)
test   %eax,%eax
js     1214 <open_port+0xa4>
mov    %eax,%ebx
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
xor    %esi,%esi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     121d <open_port+0xad>
mov    $0x162e,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     1226 <open_port+0xb6>
mov    %ebx,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xde5(%rip),%rdi
jmp    122d <open_port+0xbd>
lea    0xde3(%rip),%rdi
jmp    122d <open_port+0xbd>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
@.str = [7 x i8] c"socket\00"
@SO_REUSEADDR = global i32 0
@.str.1 = [24 x i8] c"setsockopt SO_REUSEADDR\00"
@INADDR_ANY = global i32 0
@.str.2 = [5 x i8] c"bind\00"
define i32 @open_port() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = load i32, i32* @SOCK_NONBLOCK
%7 = or i32 %5, %6
%8 = call i32 @socket(i32 %4, i32 %7, i32 0)
store i32 %8, i32* %1
store i32 1, i32* %2
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = load i32, i32* %1
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %1
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i64 @setsockopt(i32 %16, i32 0, i32 %17, i32* %2, i32 4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = call i32 @htons(i32 5678)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, 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* %1
%30 = bitcast %struct.sockaddr_in* %3 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 %36
33: ; preds = %23
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %23
%37 = load i32, i32* %1
%38 = call i32 @listen(i32 %37, i32 10)
%39 = load i32, i32* %1
ret i32 %39
}

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