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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
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);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int32_t startup(uint16_t *port) {
  int32_t httpdSocket = 0;
  struct sockaddr_in name;
  httpdSocket = socket(PF_INET, SOCK_STREAM, 0);
  if (httpdSocket == -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);
  while (bind(httpdSocket, (struct sockaddr *)&name, sizeof(name)) < 0) {
    printf("error binding\n");
    sleep(1);
  }
  if (*port == 0) {
    uint32_t namelen = sizeof(name);
    if (getsockname(httpdSocket, (struct sockaddr *)&name, &namelen) == -1) {
      error_die("getsockname");
    }
    *port = ntohs(name.sin_port);
  }
  if (listen(httpdSocket, 5) < 0) {
    error_die("listen");
  }
  return (httpdSocket);
}


x86 assembly (clang, -O0):
<startup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11f5 <startup+0x45>
lea    0xe10(%rip),%rdi
call   1070 <error_die@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1266 <startup+0xb6>
lea    0xdb5(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    1230 <startup+0x80>
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jne    12af <startup+0xff>
movl   $0xc,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10b0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    129f <startup+0xef>
lea    0xd7c(%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    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    12d2 <startup+0x122>
lea    0xd55(%rip),%rdi
call   1070 <error_die@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11ea <startup+0x3a>
lea    0xe1b(%rip),%rdi
call   1070 <error_die@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    (%r14),%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    1277 <startup+0xc7>
lea    0xdd5(%rip),%rbx
lea    0x8(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     1250 <startup+0xa0>
cmpl   $0x0,(%r14)
jne    12b4 <startup+0x104>
movl   $0xc,0x14(%rsp)
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %r15d,%edi
call   10b0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    12a8 <startup+0xf8>
lea    0xd64(%rip),%rdi
call   1070 <error_die@plt>
mov    0x8(%rsp),%edi
call   1090 <ntohs@plt>
mov    %eax,(%r14)
mov    %r15d,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %rax,%rax
jns    12d2 <startup+0x122>
lea    0xd46(%rip),%rdi
call   1070 <error_die@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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
@.str.1 = [15 x i8] c"error binding\0A\00"
@.str.2 = [12 x i8] c"getsockname\00"
@.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
store i32* %0, i32** %2
store i32 0, i32* %3
%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 eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @error_die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32*, i32** %2
%18 = load i32, i32* %17
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
br label %25
25: ; preds = %30, %13
%26 = load i32, i32* %3
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i64 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @sleep(i32 1)
br label %25
33: ; preds = %25
%34 = load i32*, i32** %2
%35 = load i32, i32* %34
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %33
store i32 12, i32* %5
%38 = load i32, i32* %3
%39 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%40 = call i32 @getsockname(i32 %38, %struct.sockaddr* %39, i32* %5)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @error_die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = load i32*, i32** %2
store i32 %47, i32* %48
br label %49
49: ; preds = %44, %33
%50 = load i32, i32* %3
%51 = call i64 @listen(i32 %50, i32 5)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @error_die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %49
%56 = load i32, i32* %3
ret i32 %56
}

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
@.str.2 = [12 x i8] c"getsockname\00"
@.str.3 = [7 x i8] c"listen\00"
@str = [14 x i8] c"error binding\00"
define i32 @startup(i32* nocapture %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 eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @error_die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* %0
%16 = call i32 @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%22 = call i64 @bind(i32 %7, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %11, %24
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%26 = call i32 @sleep(i32 1)
%27 = call i64 @bind(i32 %7, %struct.sockaddr* %21, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %24, label %29
29: ; preds = %24, %11
%30 = load i32, i32* %0
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %29
%33 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%34 = call i32 @getsockname(i32 %7, %struct.sockaddr* %21, i32* %3)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @error_die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = load i32, i32* %17
%40 = call i32 @ntohs(i32 %39)
store i32 %40, i32* %0
br label %41
41: ; preds = %38, %29
%42 = call i64 @listen(i32 %7, i32 5)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @error_die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
ret i32 %7
}