#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 int pid_t;

/* Variables and functions */
int fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  setbuf(stdout, NULL);
  printf("checkpoint1\n");
  pid_t pid = fork();
  if (pid == -1) {
    printf("fail");
    return 0;
  } else if (pid == 0) {
    printf("checkpoint2");
  } else {
    wait(NULL);
    printf("checkpoint3\n");
    pid_t pid = fork();
    if (pid == -1) {
      printf("checkpoint4");
      sleep(1);
    } else if (pid == 0) {
      printf("child");
    } else {
      printf("parent");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setbuf@plt>
lea    0xe88(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a9 <main+0x59>
lea    0xe75(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <main+0xf5>
cmpl   $0x0,-0x8(%rbp)
jne    11c4 <main+0x74>
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
jmp    1239 <main+0xe9>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1208 <main+0xb8>
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    1234 <main+0xe4>
cmpl   $0x0,-0xc(%rbp)
jne    1223 <main+0xd3>
lea    0xe1e(%rip),%rdi
call   1040 <printf@plt>
jmp    122f <main+0xdf>
lea    0xe13(%rip),%rdi
call   1040 <printf@plt>
jmp    1234 <main+0xe4>
jmp    1239 <main+0xe9>
jmp    123e <main+0xee>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <setbuf@plt>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1196 <main+0x36>
cmp    $0xffffffff,%eax
jne    119f <main+0x3f>
lea    0xe6c(%rip),%rdi
jmp    11ea <main+0x8a>
lea    0xe68(%rip),%rdi
jmp    11ea <main+0x8a>
xor    %edi,%edi
call   1070 <wait@plt>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11da <main+0x7a>
cmp    $0xffffffff,%eax
jne    11e3 <main+0x83>
lea    0xe48(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11ef <main+0x8f>
lea    0xe3c(%rip),%rdi
jmp    11ea <main+0x8a>
lea    0xe39(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [13 x i8] c"checkpoint1\0A\00"
@.str.1 = [5 x i8] c"fail\00"
@.str.2 = [12 x i8] c"checkpoint2\00"
@.str.3 = [13 x i8] c"checkpoint3\0A\00"
@.str.4 = [12 x i8] c"checkpoint4\00"
@.str.5 = [6 x i8] c"child\00"
@.str.6 = [7 x i8] c"parent\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @stdout
%5 = call i32 @setbuf(i32 %4, i32* null)
%6 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %37
12: ; preds = %0
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %35
17: ; preds = %12
%18 = call i32 @wait(i32* null)
%19 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 (...) @fork()
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%25 = call i32 @sleep(i32 1)
br label %34
26: ; preds = %17
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %33
31: ; preds = %26
%32 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %33
33: ; preds = %31, %29
br label %34
34: ; preds = %33, %23
br label %35
35: ; preds = %34, %15
br label %36
36: ; preds = %35
store i32 0, i32* %1
br label %37
37: ; preds = %36, %10
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@stdout = global i32 0
@.str.1 = [5 x i8] c"fail\00"
@.str.2 = [12 x i8] c"checkpoint2\00"
@.str.4 = [12 x i8] c"checkpoint4\00"
@.str.5 = [6 x i8] c"child\00"
@.str.6 = [7 x i8] c"parent\00"
@str = [12 x i8] c"checkpoint1\00"
@str.7 = [12 x i8] c"checkpoint3\00"
define i32 @main() {
%1 = load i32, i32* @stdout
%2 = tail call i32 @setbuf(i32 %1, i32* null)
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @fork()
switch i32 %4, label %9 [
i32 -1, label %5
i32 0, label %7
]
5: ; preds = %0
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %20
7: ; preds = %0
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %20
9: ; preds = %0
%10 = tail call i32 @wait(i32* null)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
%12 = tail call i32 (...) @fork()
switch i32 %12, label %18 [
i32 -1, label %13
i32 0, label %16
]
13: ; preds = %9
%14 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 @sleep(i32 1)
br label %20
16: ; preds = %9
%17 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %20
18: ; preds = %9
%19 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %20
20: ; preds = %7, %16, %18, %13, %5
ret i32 0
}