#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 u_short;
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*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int startup() {
  int server_fd = -1;
  u_short port = 9001;
  struct sockaddr_in server_addr;
  server_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (server_fd == -1) {
    perror("socket");
    exit(-1);
  }
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(9001);
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  int on = 1;
  if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) < 0) {
    perror("setsockopt");
    exit(-1);
  }
  if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
      0) {
    perror("bind");
    exit(-1);
  }
  if (listen(server_fd, 5) < 0) {
    perror("listen");
    exit(-1);
  }
  printf("http server running on port %d\n", port);
  return server_fd;
}


x86 assembly (clang, -O0):
<startup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0xffffffff,-0x4(%rbp)
movl   $0x2329,-0x8(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11f2 <startup+0x52>
lea    0xe1d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x2329,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1277 <startup+0xd7>
lea    0xd9f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12a8 <startup+0x108>
lea    0xd79(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    12d5 <startup+0x135>
lea    0xd51(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xd3f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startup>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e32(%rip),%r14
mov    (%r14),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1277 <startup+0xd7>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    $0x2329,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x8(%rsp)
movl   $0x1,0x4(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1280 <startup+0xe0>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1289 <startup+0xe9>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     1292 <startup+0xf2>
lea    0xdbb(%rip),%rdi
mov    $0x2329,%esi
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd82(%rip),%rdi
jmp    1299 <startup+0xf9>
lea    0xd80(%rip),%rdi
jmp    1299 <startup+0xf9>
lea    0xd82(%rip),%rdi
jmp    1299 <startup+0xf9>
lea    0xd7e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <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
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [32 x i8] c"http server running on port %d\0A\00"
define i32 @startup() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
store i32 -1, i32* %1
store i32 9001, i32* %2
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 9001)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
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* %3, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
store i32 1, i32* %4
%23 = load i32, i32* %1
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i64 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %4, i32 4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %13
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %13
%32 = load i32, i32* %1
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %31
%40 = load i32, i32* %1
%41 = call i64 @listen(i32 %40, i32 5)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %39
%47 = load i32, i32* %2
%48 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %1
ret i32 %49
}

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 = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [32 x i8] c"http server running on port %d\0A\00"
define i32 @startup() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 9001)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i64 @setsockopt(i32 %6, i32 %21, i32 %22, i32* %2, i32 4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %11
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %11
%29 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%30 = call i64 @bind(i32 %6, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %28
%36 = call i64 @listen(i32 %6, i32 5)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %35
%42 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 9001)
ret i32 %6
}