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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
void fork_daemon(void) {
  pid_t pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork daemon failed\n");
    exit(EXIT_FAILURE);
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork daemon failed (second fork)\n");
    exit(EXIT_FAILURE);
  } else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
}


x86 assembly (clang, -O0):
<fork_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    1171 <fork_daemon+0x41>
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
lea    0xea2(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    118a <fork_daemon+0x5a>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
jmp    118f <fork_daemon+0x5f>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11c8 <fork_daemon+0x98>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    11e1 <fork_daemon+0xb1>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
jmp    11e6 <fork_daemon+0xb6>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_daemon>:
push   %rax
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
js     115d <fork_daemon+0x2d>
jne    114f <fork_daemon+0x1f>
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
js     116f <fork_daemon+0x3f>
jne    114f <fork_daemon+0x1f>
pop    %rax
ret
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    0xe93(%rip),%rsi
jmp    117f <fork_daemon+0x4f>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    0xe95(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Fork daemon failed\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [34 x i8] c"Fork daemon failed (second fork)\0A\00"
define void @fork_daemon() {
%1 = alloca i64
%2 = call i64 (...) @fork()
store i64 %2, i64* %1
%3 = load i64, i64* %1
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = load i64, i64* %1
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @EXIT_SUCCESS
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %10
br label %17
17: ; preds = %16
%18 = call i64 (...) @fork()
store i64 %18, i64* %1
%19 = load i64, i64* %1
%20 = icmp slt i64 %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 ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %17
%27 = load i64, i64* %1
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @EXIT_SUCCESS
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %26
br label %33
33: ; preds = %32
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Fork daemon failed\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [34 x i8] c"Fork daemon failed (second fork)\0A\00"
define void @fork_daemon() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %1, 0
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = load i32, i32* @EXIT_SUCCESS
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %8
%14 = tail call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %13
%22 = icmp eq i64 %14, 0
br i1 %22, label %26, label %23
23: ; preds = %21
%24 = load i32, i32* @EXIT_SUCCESS
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %21
ret void
}