#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 */

/* Type definitions */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ clientAddress;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ _sendFile(int);
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void _startConnectionHandler(int _serverSocket) {
  while (1) {
    struct sockaddr_in clientAddress = {};
    socklen_t clientAddressLength = sizeof(clientAddress);
    fprintf(stdout, "Waiting..\n\n");
    int clientSocket = accept(_serverSocket, (struct sockaddr *)&clientAddress,
                              &clientAddressLength);
    if (clientSocket < 0) {
      fprintf(stderr, "Error: Accepting connection.\n");
      exit(EXIT_FAILURE);
    }
    int pid = fork();
    if (pid < 0) {
      fprintf(stderr, "Error: Creating child process.\n");
      exit(EXIT_FAILURE);
    }
    if (pid == 0) {
      close(_serverSocket);
      _sendFile(clientSocket);
      exit(EXIT_SUCCESS);
    } else {
      close(clientSocket);
    }
  }
}


x86 assembly (clang, -O0):
<_startConnectionHandler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movl   $0x4,-0xc(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11e7 <_startConnectionHandler+0x77>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    121e <_startConnectionHandler+0xae>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1246 <_startConnectionHandler+0xd6>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1030 <_sendFile@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
jmp    117b <_startConnectionHandler+0xb>

x86 assembly (clang, -O2):
<_startConnectionHandler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
movl   $0x0,(%rsp)
movl   $0x4,0x4(%rsp)
mov    0x2e4d(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe72(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1060 <accept@plt>
test   %eax,%eax
js     1202 <_startConnectionHandler+0xa2>
mov    %eax,%ebp
lea    0xe51(%rip),%r14
mov    %rsp,%r15
lea    0x4(%rsp),%r12
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     1214 <_startConnectionHandler+0xb4>
je     1237 <_startConnectionHandler+0xd7>
mov    %ebp,%edi
call   1040 <close@plt>
movl   $0x0,(%rsp)
movl   $0x4,0x4(%rsp)
mov    0x0(%r13),%edi
mov    %r14,%rsi
call   1050 <fprintf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c0 <_startConnectionHandler+0x60>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
jmp    1224 <_startConnectionHandler+0xc4>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1030 <_sendFile@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stdout = global i32 0
@.str = [12 x i8] c"Waiting..\0A\0A\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Error: Accepting connection.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [32 x i8] c"Error: Creating child process.\0A\00"
@EXIT_SUCCESS = global i32 0
define void @_startConnectionHandler(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
br label %7
7: ; preds = %1, %43
%8 = bitcast %struct.sockaddr_in* %3 to i8*
store i32 4, i32* %4
%9 = load i32, i32* @stdout
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* %2
%12 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%13 = call i32 @accept(i32 %11, %struct.sockaddr* %12, i32* %4)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %7
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %7
%22 = call i32 (...) @fork()
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %21
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %5
%37 = call i32 @_sendFile(i32 %36)
%38 = load i32, i32* @EXIT_SUCCESS
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %30
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40
br label %7
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stdout = global i32 0
@.str = [12 x i8] c"Waiting..\0A\0A\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Error: Accepting connection.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [32 x i8] c"Error: Creating child process.\0A\00"
@EXIT_SUCCESS = global i32 0
define void @_startConnectionHandler(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %5
store i32 4, i32* %3
%7 = load i32, i32* @stdout
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%9 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%10 = call i32 @accept(i32 %0, %struct.sockaddr* %9, i32* %3)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %33, %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1, %33
%18 = phi i32 [ %37, %33 ], [ %10, %1 ]
%19 = call i32 (...) @fork()
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %17
%27 = icmp eq i32 %19, 0
br i1 %27, label %28, label %33
28: ; preds = %26
%29 = call i32 @close(i32 %0)
%30 = call i32 @_sendFile(i32 %18)
%31 = load i32, i32* @EXIT_SUCCESS
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %26
%34 = call i32 @close(i32 %18)
store i32 0, i32* %5
store i32 4, i32* %3
%35 = load i32, i32* @stdout
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @accept(i32 %0, %struct.sockaddr* %9, i32* %3)
%38 = icmp slt i32 %37, 0
br i1 %38, label %12, label %17
}