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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ handle_request(int);
int /*<<< orphan*/ stderr;
void handle_clients_fork(int s) {
  while (true) {
    int fd = accept(s, NULL, NULL);
    if (fd != -1) {
      switch (fork()) {
      case 0:
        handle_request(fd);
        close(fd);
        exit(EXIT_SUCCESS);
      case -1:
        fprintf(stderr, "error: fork() failed!\n");
        exit(EXIT_FAILURE);
        break;
      default:
        close(fd);
        break;
      }
    }
  }
}


x86 assembly (clang, -O0):
<handle_clients_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     11ff <handle_clients_fork+0x9f>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     11cf <handle_clients_fork+0x6f>
jmp    11a1 <handle_clients_fork+0x41>
mov    -0xc(%rbp),%eax
test   %eax,%eax
jne    11f2 <handle_clients_fork+0x92>
jmp    11b1 <handle_clients_fork+0x51>
mov    -0x8(%rbp),%edi
call   1040 <handle_request@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
jmp    11ff <handle_clients_fork+0x9f>
jmp    116b <handle_clients_fork+0xb>

x86 assembly (clang, -O2):
<handle_clients_fork>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <accept@plt>
cmp    $0xffffffff,%eax
je     1170 <handle_clients_fork+0x10>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     119b <handle_clients_fork+0x3b>
test   %eax,%eax
je     11be <handle_clients_fork+0x5e>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    1170 <handle_clients_fork+0x10>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    %ebp,%edi
call   1040 <handle_request@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"error: fork() failed!\0A\00"
@EXIT_FAILURE = global i32 0
define void @handle_clients_fork(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
br label %4
4: ; preds = %1, %27
%5 = load i32, i32* %2
%6 = call i32 @accept(i32 %5, i32* null, i32* null)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, -1
br i1 %8, label %9, label %27
9: ; preds = %4
%10 = call i32 (...) @fork()
switch i32 %10, label %23 [
i32 0, label %11
i32 -1, label %18
]
11: ; preds = %9
%12 = load i32, i32* %3
%13 = call i32 @handle_request(i32 %12)
%14 = load i32, i32* %3
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* @EXIT_SUCCESS
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %9
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %9
%24 = load i32, i32* %3
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %23
br label %27
27: ; preds = %26, %4
br label %4
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"error: fork() failed!\0A\00"
@EXIT_FAILURE = global i32 0
define void @handle_clients_fork(i32 %0) {
br label %2
2: ; preds = %19, %1
%3 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%4 = icmp eq i32 %3, -1
br i1 %4, label %19, label %5
5: ; preds = %2
%6 = tail call i32 (...) @fork()
switch i32 %6, label %17 [
i32 0, label %7
i32 -1, label %12
]
7: ; preds = %5
%8 = tail call i32 @handle_request(i32 %3)
%9 = tail call i32 @close(i32 %3)
%10 = load i32, i32* @EXIT_SUCCESS
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %5
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %5
%18 = tail call i32 @close(i32 %3)
br label %19
19: ; preds = %17, %2
br label %2
}