#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*/ u_short;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ reuse_addr;
typedef int /*<<< orphan*/ name;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEPORT;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ error_die(char *);
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int startup(u_short *port) {
  int httpd = 0;
  struct sockaddr_in name;
  httpd = socket(PF_INET, SOCK_STREAM, 0);
  if (httpd == -1)
    error_die("socket");
  memset(&name, 0, sizeof(name));
  name.sin_family = AF_INET;
  name.sin_port = htons(*port);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  int reuse_addr = 1;
  setsockopt(httpd, SOL_SOCKET, SO_REUSEPORT, (const char *)&reuse_addr,
             sizeof(reuse_addr));
  if (bind(httpd, (struct sockaddr *)&name, sizeof(name)) < 0)
    error_die("bind");
  if (*port == 0) {
    socklen_t namelen = sizeof(name);
    if (getsockname(httpd, (struct sockaddr *)&name, &namelen) == -1)
      error_die("getsockname");
    *port = ntohs(name.sin_port);
  }
  if (listen(httpd, SOMAXCONN) < 0)
    error_die("listen");
  return (httpd);
}


x86 assembly (clang, -O0):
<startup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11e5 <startup+0x45>
lea    0xe20(%rip),%rdi
call   1070 <error_die@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1272 <startup+0xd2>
lea    0xd9a(%rip),%rdi
call   1070 <error_die@plt>
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jne    12bb <startup+0x11b>
movl   $0xc,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    12ab <startup+0x10b>
lea    0xd66(%rip),%rdi
call   1070 <error_die@plt>
mov    -0x18(%rbp),%edi
call   1090 <ntohs@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%edi
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    12e2 <startup+0x142>
lea    0xd3b(%rip),%rdi
call   1070 <error_die@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startup>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11d8 <startup+0x38>
lea    0xe2d(%rip),%rdi
call   1070 <error_die@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    (%r14),%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
movl   $0x1,0x1c(%rsp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    0x1c(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     125f <startup+0xbf>
cmpl   $0x0,(%r14)
jne    12a7 <startup+0x107>
jmp    1271 <startup+0xd1>
lea    0xda1(%rip),%rdi
call   1070 <error_die@plt>
cmpl   $0x0,(%r14)
jne    12a7 <startup+0x107>
movl   $0xc,0x18(%rsp)
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   10b0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    129b <startup+0xfb>
lea    0xd76(%rip),%rdi
call   1070 <error_die@plt>
mov    0x8(%rsp),%edi
call   1090 <ntohs@plt>
mov    %eax,(%r14)
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <listen@plt>
test   %rax,%rax
jns    12c8 <startup+0x128>
lea    0xd55(%rip),%rdi
call   1070 <error_die@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, 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"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEPORT = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [12 x i8] c"getsockname\00"
@SOMAXCONN = global i32 0
@.str.3 = [7 x i8] c"listen\00"
define i32 @startup(i32* nocapture %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @error_die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* %0
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %17, i32* %18
%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 1, i32 0
store i32 %20, i32* %21
%22 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEPORT
%25 = call i32 @setsockopt(i32 %8, i32 %23, i32 %24, i8* %22, i32 4)
%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 %29, label %31
29: ; preds = %12
%30 = call i32 @error_die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %12
%32 = load i32, i32* %0
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %43
34: ; preds = %31
%35 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%36 = call i32 @getsockname(i32 %8, %struct.sockaddr* %26, i32* %4)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @error_die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = load i32, i32* %18
%42 = call i32 @ntohs(i32 %41)
store i32 %42, i32* %0
br label %43
43: ; preds = %40, %31
%44 = load i32, i32* @SOMAXCONN
%45 = call i64 @listen(i32 %8, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @error_die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
ret i32 %8
}