#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(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    printf("fail to fork\n");
    exit(1);
  } else if (0 == pid) {
    printf("the child\n");
    pid = fork();
    if (pid < 0) {
      printf("fail to fork\n");
      exit(1);
    } else if (0 == pid) {
      printf("do something you want\n");
      sleep(5);
      printf("done\n");
      exit(0);
    } else
      exit(0);
  } else {
    if (waitpid(pid, NULL, 0) > 0)
      printf("child %d exited\n", pid);
    sleep(10);
    printf("the parent\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    118d <main+0x3d>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1215 <main+0xc5>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11d5 <main+0x85>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    120e <main+0xbe>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x5,%edi
call   1060 <sleep@plt>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0x0,%rax
jle    1240 <main+0xf0>
mov    -0x10(%rbp),%rsi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xa,%edi
call   1060 <sleep@plt>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125d <main+0x10d>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11c6 <main+0x66>
je     11ae <main+0x4e>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    1194 <main+0x34>
lea    0xe76(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jns    11dc <main+0x7c>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jne    1200 <main+0xa0>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"fail to fork\0A\00"
@.str.1 = [11 x i8] c"the child\0A\00"
@.str.2 = [23 x i8] c"do something you want\0A\00"
@.str.3 = [6 x i8] c"done\0A\00"
@.str.4 = [17 x i8] c"child %d exited\0A\00"
@.str.5 = [12 x i8] c"the parent\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = load i64, i64* %2
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 0, %10
br i1 %11, label %12, label %30
12: ; preds = %9
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%14 = call i64 (...) @fork()
store i64 %14, i64* %2
%15 = load i64, i64* %2
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %12
%21 = load i64, i64* %2
%22 = icmp eq i64 0, %21
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @sleep(i32 5)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %20
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %9
%31 = load i64, i64* %2
%32 = call i64 @waitpid(i64 %31, i32* null, i32 0)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i64, i64* %2
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i64 %35)
br label %37
37: ; preds = %34, %30
%38 = call i32 @sleep(i32 10)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %37
br label %41
41: ; preds = %40
ret i32 0
}

LLVM IR (-O2):
@.str.4 = [17 x i8] c"child %d exited\0A\00"
@str = [11 x i8] c"the parent\00"
@str.6 = [10 x i8] c"the child\00"
@str.7 = [22 x i8] c"do something you want\00"
@str.8 = [5 x i8] c"done\00"
@str.10 = [13 x i8] c"fail to fork\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %24
8: ; preds = %6
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.6, i64 0, i64 0))
%10 = tail call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %8
%16 = icmp eq i64 %10, 0
br i1 %16, label %17, label %22
17: ; preds = %15
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.7, i64 0, i64 0))
%19 = tail call i32 @sleep(i32 5)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.8, i64 0, i64 0))
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %15
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %6
%25 = tail call i64 @waitpid(i64 %1, i32* null, i32 0)
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i64 %1)
br label %29
29: ; preds = %27, %24
%30 = tail call i32 @sleep(i32 10)
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
ret i32 0
}