#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__ fork_ok();
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ wait(int /*<<< orphan*/);
void new_level(int level, int n) {
  if (level > n)
    return;
  if (fork_ok() == 0 || fork_ok() == 0)
    new_level(level + 1, n);
  else {
    printf("i am %d, my parent is %d\n", (int)getpid(), (int)getppid());
    while (wait(0) > 0)
      ;
    printf("Exit %d\n", (int)getpid());
  }
}


x86 assembly (clang, -O0):
<new_level>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    117f <new_level+0x1f>
jmp    1205 <new_level+0xa5>
mov    $0x0,%al
call   1050 <fork_ok@plt>
cmp    $0x0,%rax
je     11a1 <new_level+0x41>
mov    $0x0,%al
call   1050 <fork_ok@plt>
cmp    $0x0,%rax
jne    11b4 <new_level+0x54>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    -0x8(%rbp),%esi
call   1060 <new_level@plt>
jmp    1205 <new_level+0xa5>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <wait@plt>
cmp    $0x0,%rax
jle    11ee <new_level+0x8e>
jmp    11d8 <new_level+0x78>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_level>:
push   %rbx
cmp    %esi,%edi
jle    1157 <new_level+0x7>
pop    %rbx
ret
mov    %esi,%ebx
sub    %edi,%ebx
add    $0x1,%ebx
jmp    1165 <new_level+0x15>
add    $0xffffffff,%ebx
je     1155 <new_level+0x5>
xor    %eax,%eax
call   1050 <fork_ok@plt>
test   %rax,%rax
je     1160 <new_level+0x10>
xor    %eax,%eax
call   1050 <fork_ok@plt>
test   %rax,%rax
je     1160 <new_level+0x10>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe6b(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <wait@plt>
test   %rax,%rax
jg     11a0 <new_level+0x50>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe60(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
pop    %rbx
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"i am %d, my parent is %d\0A\00"
@.str.1 = [9 x i8] c"Exit %d\0A\00"
define void @new_level(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%5 = load i32, i32* %3
%6 = load i32, i32* %4
%7 = icmp sgt i32 %5, %6
br i1 %7, label %8, label %9
8: ; preds = %2
br label %33
9: ; preds = %2
%10 = call i64 (...) @fork_ok()
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = call i64 (...) @fork_ok()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12, %9
%16 = load i32, i32* %3
%17 = add nsw i32 %16, 1
%18 = load i32, i32* %4
br label %33
19: ; preds = %12
%20 = call i64 (...) @getpid()
%21 = trunc i64 %20 to i32
%22 = call i64 (...) @getppid()
%23 = trunc i64 %22 to i32
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %23)
br label %25
25: ; preds = %28, %19
%26 = call i64 @wait(i32 0)
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25
br label %25
29: ; preds = %25
%30 = call i64 (...) @getpid()
%31 = trunc i64 %30 to i32
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %8, %29, %15
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"i am %d, my parent is %d\0A\00"
@.str.1 = [9 x i8] c"Exit %d\0A\00"
define void @new_level(i32 %0, i32 %1) {
%3 = icmp sgt i32 %0, %1
br i1 %3, label %27, label %4
4: ; preds = %2, %11
%5 = phi i32 [ %12, %11 ], [ %0, %2 ]
%6 = tail call i64 (...) @fork_ok()
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i64 (...) @fork_ok()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8, %4
%12 = add i32 %5, 1
%13 = icmp eq i32 %5, %1
br i1 %13, label %27, label %4
14: ; preds = %8
%15 = tail call i64 (...) @getpid()
%16 = trunc i64 %15 to i32
%17 = tail call i64 (...) @getppid()
%18 = trunc i64 %17 to i32
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %18)
br label %20
20: ; preds = %20, %14
%21 = tail call i64 @wait(i32 0)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %20, label %23
23: ; preds = %20
%24 = tail call i64 (...) @getpid()
%25 = trunc i64 %24 to i32
%26 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %11, %2, %23
ret void
}