#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 /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
void rescure(int deep) {
  if (deep == 0) {
    printf("the leaf pid:%u\n", getpid());
    exit(0);
  } else {
    pid_t fpid = fork();
    if (fpid == 0) {
      printf("the left child pid:%u\n", getpid());
      printf("%u's parent pid is:%u\n", getpid(), getppid());
      rescure(deep - 1);
      sleep(100);
      return;
    } else {
      printf("the current pid:%u\n", getpid());
      pid_t pid = fork();
      if (pid == 0) {
        printf("the right child pid:%u\n", getpid());
        printf("%u's parent pid is:%u\n", getpid(), getppid());
        rescure(deep - 1);
        sleep(100);
        return;
      } else {
        sleep(100);
        return;
      }
    }
  }
}


x86 assembly (clang, -O0):
<rescure>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11a3 <rescure+0x33>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    120e <rescure+0x9e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1050 <rescure@plt>
mov    $0x64,%edi
call   1080 <sleep@plt>
jmp    129a <rescure+0x12a>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1290 <rescure+0x120>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1050 <rescure@plt>
mov    $0x64,%edi
call   1080 <sleep@plt>
jmp    129a <rescure+0x12a>
mov    $0x64,%edi
call   1080 <sleep@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rescure>:
push   %rbp
push   %rbx
push   %rax
test   %edi,%edi
je     120b <rescure+0x9b>
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
test   %rbx,%rbx
je     11bf <rescure+0x4f>
lea    0xea5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11fb <rescure+0x8b>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe96(%rip),%rdi
jmp    11c6 <rescure+0x56>
lea    0xe4b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe42(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%ebp
mov    %ebp,%edi
call   1050 <rescure@plt>
mov    $0x64,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xde7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"the leaf pid:%u\0A\00"
@.str.1 = [23 x i8] c"the left child pid:%u\0A\00"
@.str.2 = [23 x i8] c"%u's parent pid is:%u\0A\00"
@.str.3 = [20 x i8] c"the current pid:%u\0A\00"
@.str.4 = [24 x i8] c"the right child pid:%u\0A\00"
define void @rescure(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = call i32 (...) @getpid()
%9 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %1
%12 = call i64 (...) @fork()
store i64 %12, i64* %3
%13 = load i64, i64* %3
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %11
%16 = call i32 (...) @getpid()
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = call i32 (...) @getpid()
%19 = call i32 (...) @getppid()
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %19)
%21 = load i32, i32* %2
%22 = sub nsw i32 %21, 1
%23 = call i32 @sleep(i32 100)
br label %41
24: ; preds = %11
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i64 (...) @fork()
store i64 %27, i64* %4
%28 = load i64, i64* %4
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %24
%31 = call i32 (...) @getpid()
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = call i32 (...) @getpid()
%34 = call i32 (...) @getppid()
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %34)
%36 = load i32, i32* %2
%37 = sub nsw i32 %36, 1
%38 = call i32 @sleep(i32 100)
br label %41
39: ; preds = %24
%40 = call i32 @sleep(i32 100)
br label %41
41: ; preds = %39, %30, %15
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"the leaf pid:%u\0A\00"
@.str.1 = [23 x i8] c"the left child pid:%u\0A\00"
@.str.2 = [23 x i8] c"%u's parent pid is:%u\0A\00"
@.str.3 = [20 x i8] c"the current pid:%u\0A\00"
@.str.4 = [24 x i8] c"the right child pid:%u\0A\00"
define void @rescure(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %7
3: ; preds = %1
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %1
%8 = tail call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
%10 = tail call i32 (...) @getpid()
br i1 %9, label %18, label %20
11: ; preds = %18, %24
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %13)
%15 = add nsw i32 %0, -1
tail call void @rescure(i32 %15)
br label %16
16: ; preds = %11, %20
%17 = tail call i32 @sleep(i32 100)
ret void
18: ; preds = %7
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %11
20: ; preds = %7
%21 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%22 = tail call i64 (...) @fork()
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %16
24: ; preds = %20
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %25)
br label %11
}