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

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ web_handler(int);
void main_loop(int server_socket) {
  int client_socket, pid;
  socklen_t client_address_length;
  static struct sockaddr_in client_address;
  while (1) {
    client_address_length = sizeof(client_address);
    if ((client_socket =
             accept(server_socket, (struct sockaddr *)&client_address,
                    &client_address_length)) < 0)
      printf("Error when accept()");
    if ((pid = fork()) < 0) {
      printf("Error when fork()");
    } else {
      if (pid == 0) {
        close(server_socket);
        web_handler(client_socket);
      } else {
        close(client_socket);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    0x2ee0(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    118d <main_loop+0x3d>
lea    0xe78(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11b1 <main_loop+0x61>
lea    0xe6d(%rip),%rdi
call   1030 <printf@plt>
jmp    11dd <main_loop+0x8d>
cmpl   $0x0,-0xc(%rbp)
jne    11d0 <main_loop+0x80>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <web_handler@plt>
jmp    11d8 <main_loop+0x88>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    11dd <main_loop+0x8d>
jmp    115b <main_loop+0xb>

x86 assembly (clang, -O2):
<main_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x2ee8(%rip),%r14
lea    0x4(%rsp),%r15
lea    0xe90(%rip),%r12
lea    0xe9d(%rip),%r13
jmp    1188 <main_loop+0x38>
nopl   0x0(%rax)
mov    %r13,%rdi
call   1030 <printf@plt>
movl   $0x4,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11ab <main_loop+0x5b>
mov    %r12,%rdi
call   1030 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
js     1180 <main_loop+0x30>
je     11d0 <main_loop+0x80>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1188 <main_loop+0x38>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1050 <web_handler@plt>
jmp    1188 <main_loop+0x38>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@main_loop.client_address = internal global %struct.sockaddr_in zeroinitializer
@.str = [20 x i8] c"Error when accept()\00"
@.str.1 = [18 x i8] c"Error when fork()\00"
define void @main_loop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
br label %6
6: ; preds = %1, %29
store i32 4, i32* %5
%7 = load i32, i32* %2
%8 = call i32 @accept(i32 %7, %struct.sockaddr* bitcast (%struct.sockaddr_in* @main_loop.client_address to %struct.sockaddr*), i32* %5)
store i32 %8, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = call i32 (...) @fork()
store i32 %13, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %29
17: ; preds = %12
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %3
%24 = call i32 @web_handler(i32 %23)
br label %28
25: ; preds = %17
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %25, %20
br label %29
29: ; preds = %28, %15
br label %6
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@main_loop.client_address = internal global %struct.sockaddr_in zeroinitializer
@.str = [20 x i8] c"Error when accept()\00"
@.str.1 = [18 x i8] c"Error when fork()\00"
define void @main_loop(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %19, %1
store i32 4, i32* %2
%5 = call i32 @accept(i32 %0, %struct.sockaddr* bitcast (%struct.sockaddr_in* @main_loop.client_address to %struct.sockaddr*), i32* %2)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %4
%10 = call i32 (...) @fork()
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %19
14: ; preds = %9
%15 = icmp eq i32 %10, 0
br i1 %15, label %16, label %20
16: ; preds = %14
%17 = call i32 @close(i32 %0)
%18 = call i32 @web_handler(i32 %5)
br label %19
19: ; preds = %16, %20, %12
br label %4
20: ; preds = %14
%21 = call i32 @close(i32 %5)
br label %19
}