#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stdout;
int waitpid(int, int *, int /*<<< orphan*/);
void fork_chain(int level) {
  int pid;
  printf("forking level %d\n", level);
  fflush(stdout);
  pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(1);
  }
  if (pid == 0) {
    if (level > 0) {
      fork_chain(level - 1);
    }
  } else {
    int ret;
    int status;
    while ((ret = waitpid(pid, &status, 0)) != pid) {
      if (errno != EINTR) {
        perror("waitpid");
        exit(1);
      }
    }
  }
}


x86 assembly (clang, -O0):
<fork_chain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c2 <fork_chain+0x52>
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11e6 <fork_chain+0x76>
cmpl   $0x0,-0x4(%rbp)
jle    11e1 <fork_chain+0x71>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1030 <fork_chain@plt>
jmp    123f <fork_chain+0xcf>
jmp    11eb <fork_chain+0x7b>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmp    -0x8(%rbp),%eax
je     123a <fork_chain+0xca>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
je     1235 <fork_chain+0xc5>
lea    0xdf1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11eb <fork_chain+0x7b>
jmp    123f <fork_chain+0xcf>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_chain>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xe90(%rip),%r14
mov    0x2e59(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    (%r15),%edi
call   1040 <fflush@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11b1 <fork_chain+0x51>
lea    -0x1(%rbx),%eax
test   %ebx,%ebx
mov    %eax,%ebx
jg     1180 <fork_chain+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11f1 <fork_chain+0x91>
lea    0x4(%rsp),%r14
mov    0x2e1c(%rip),%r15
mov    0x2e0d(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    %ebp,%eax
je     11a6 <fork_chain+0x46>
mov    (%r15),%rax
cmp    (%rbx),%rax
je     11d0 <fork_chain+0x70>
lea    0xe28(%rip),%rdi
jmp    11f8 <fork_chain+0x98>
lea    0xe1a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"forking level %d\0A\00"
@stdout = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [8 x i8] c"waitpid\00"
define void @fork_chain(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @stdout
%9 = call i32 @fflush(i32 %8)
%10 = call i32 (...) @fork()
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i32, i32* %2
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* %2
%24 = sub nsw i32 %23, 1
br label %25
25: ; preds = %22, %19
br label %41
26: ; preds = %16
br label %27
27: ; preds = %39, %26
%28 = load i32, i32* %3
%29 = call i32 @waitpid(i32 %28, i32* %5, i32 0)
store i32 %29, i32* %4
%30 = load i32, i32* %3
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %40
32: ; preds = %27
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %32
br label %27
40: ; preds = %27
br label %41
41: ; preds = %40, %25
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"forking level %d\0A\00"
@stdout = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [8 x i8] c"waitpid\00"
define void @fork_chain(i32 %0) {
%2 = alloca i32
br label %3
3: ; preds = %12, %1
%4 = phi i32 [ %0, %1 ], [ %14, %12 ]
%5 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @stdout
%7 = tail call i32 @fflush(i32 %6)
%8 = tail call i32 (...) @fork()
switch i32 %8, label %15 [
i32 -1, label %9
i32 0, label %12
]
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %3
%13 = icmp sgt i32 %4, 0
%14 = add nsw i32 %4, -1
br i1 %13, label %3, label %28
15: ; preds = %3
%16 = bitcast i32* %2 to i8*
br label %17
17: ; preds = %20, %15
%18 = call i32 @waitpid(i32 %8, i32* %2, i32 0)
%19 = icmp eq i32 %18, %8
br i1 %19, label %27, label %20
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %17, label %24
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
br label %28
28: ; preds = %12, %27
ret void
}