#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 /*<<< orphan*/ rem_addr;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int accept(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ handle_connection(int);
int /*<<< orphan*/ stderr;
void main_loop(int sockfd) {
  pid_t pid;
  int c_sockfd;
  struct sockaddr_in rem_addr;
  unsigned int rem_addr_length;
  for (;;) {
    rem_addr_length = sizeof(rem_addr);
    if ((c_sockfd = accept(sockfd, (struct sockaddr *)&rem_addr,
                           &rem_addr_length)) == -1) {
      fprintf(stderr, "Failed to accept connection.\n");
      continue;
    }
    if ((pid = fork()) == 0) {
      if (handle_connection(c_sockfd)) {
        fprintf(stderr, "Failed to handle connection\n");
        exit(EXIT_FAILURE);
      }
      exit(EXIT_SUCCESS);
    } else if (pid < 0) {
      fprintf(stderr, "Failed to fork.\n");
      continue;
    }
  }
}


x86 assembly (clang, -O0):
<main_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <accept@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1198 <main_loop+0x48>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
call   1030 <fprintf@plt>
jmp    115b <main_loop+0xb>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11f0 <main_loop+0xa0>
mov    -0x14(%rbp),%edi
call   1040 <handle_connection@plt>
cmp    $0x0,%rax
je     11e2 <main_loop+0x92>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jge    1215 <main_loop+0xc5>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1030 <fprintf@plt>
jmp    115b <main_loop+0xb>
jmp    121a <main_loop+0xca>
jmp    115b <main_loop+0xb>

x86 assembly (clang, -O2):
<main_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0xe8f(%rip),%r13
lea    0xec3(%rip),%r12
jmp    118e <main_loop+0x3e>
nopw   0x0(%rax,%rax,1)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1030 <fprintf@plt>
movl   $0x4,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <accept@plt>
mov    %r13,%rsi
cmp    $0xffffffff,%eax
je     1180 <main_loop+0x30>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11c0 <main_loop+0x70>
mov    %r12,%rsi
jns    118e <main_loop+0x3e>
jmp    1180 <main_loop+0x30>
mov    %ebp,%edi
call   1040 <handle_connection@plt>
test   %rax,%rax
jne    11da <main_loop+0x8a>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

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