#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 EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main() {
  sleep(2);
  switch (fork()) {
  case -1:
    fprintf(stderr, "forking: error: fork failed\n");
    exit(EXIT_FAILURE);
    break;
  case 0:
    fprintf(stdout, "I am the child process (fork succeeded)\n");
    break;
  default:
    fprintf(stdout, "I am the parent process (fork succeeded)\n");
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x2,%edi
call   1050 <sleep@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
sub    $0xffffffff,%eax
je     1183 <main+0x43>
jmp    1173 <main+0x33>
mov    -0x8(%rbp),%eax
test   %eax,%eax
je     11a6 <main+0x66>
jmp    11c0 <main+0x80>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11d5 <main+0x95>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    $0x2,%edi
call   1050 <sleep@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     1164 <main+0x24>
cmp    $0xffffffff,%eax
je     1184 <main+0x44>
lea    0xee4(%rip),%rsi
jmp    116b <main+0x2b>
lea    0xeb2(%rip),%rsi
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1030 <fprintf@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"forking: error: fork failed\0A\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@.str.1 = [41 x i8] c"I am the child process (fork succeeded)\0A\00"
@.str.2 = [42 x i8] c"I am the parent process (fork succeeded)\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = call i32 @sleep(i32 2)
%3 = call i32 (...) @fork()
switch i32 %3, label %12 [
i32 -1, label %4
i32 0, label %9
]
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = load i32, i32* @stdout
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %15
12: ; preds = %0
%13 = load i32, i32* @stdout
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %12, %9
%16 = load i32, i32* @EXIT_SUCCESS
ret i32 %16
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"forking: error: fork failed\0A\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@.str.1 = [41 x i8] c"I am the child process (fork succeeded)\0A\00"
@.str.2 = [42 x i8] c"I am the parent process (fork succeeded)\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i32 @sleep(i32 2)
%2 = tail call i32 (...) @fork()
switch i32 %2, label %8 [
i32 -1, label %3
i32 0, label %9
]
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
br label %9
9: ; preds = %0, %8
%10 = phi i8* [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), %0 ]
%11 = load i32, i32* @stdout
%12 = tail call i32 @fprintf(i32 %11, i8* %10)
%13 = load i32, i32* @EXIT_SUCCESS
ret i32 %13
}