#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 /*<<< orphan*/ printf(char *, int, int, int, int);
int /*<<< orphan*/ sleep(int);
void rescure(int deep, int *ptr, int n) {
  if (deep == 0) {
    ptr[n - deep] = getpid();
    printf("the tree pid:%u  %u  %u  %u\n", ptr[0], ptr[1], ptr[2], ptr[3]);
    exit(0);
  } else {
    ptr[n - deep] = getpid();
    pid_t fpid = fork();
    if (fpid == 0) {
      rescure(deep - 1, ptr, n);
      sleep(100);
      return;
    } else {
      pid_t pid = fork();
      if (pid == 0) {
        rescure(deep - 1, ptr, n);
        sleep(100);
        return;
      } else {
        sleep(100);
        return;
      }
    }
  }
}


x86 assembly (clang, -O0):
<rescure>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11c4 <rescure+0x64>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    -0x4(%rbp),%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%r8d
lea    0xe48(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    -0x4(%rbp),%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1214 <rescure+0xb4>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <rescure@plt>
mov    $0x64,%edi
call   1070 <sleep@plt>
jmp    1255 <rescure+0xf5>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    124b <rescure+0xeb>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <rescure@plt>
mov    $0x64,%edi
call   1070 <sleep@plt>
jmp    1255 <rescure+0xf5>
mov    $0x64,%edi
call   1070 <sleep@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rescure>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
test   %ebx,%ebx
je     11b6 <rescure+0x56>
mov    %ebp,%ecx
sub    %ebx,%ecx
movslq %ecx,%rcx
mov    %eax,(%r14,%rcx,4)
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     1199 <rescure+0x39>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    11a8 <rescure+0x48>
add    $0xffffffff,%ebx
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <rescure@plt>
mov    $0x64,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <sleep@plt>
movslq %ebp,%rcx
mov    %eax,(%r14,%rcx,4)
mov    (%r14),%esi
mov    0x4(%r14),%edx
mov    0x8(%r14),%ecx
mov    0xc(%r14),%r8d
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"the tree pid:%u %u %u %u\0A\00"
define void @rescure(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %33
11: ; preds = %3
%12 = call i32 (...) @getpid()
%13 = load i32*, i32** %5
%14 = load i32, i32* %6
%15 = load i32, i32* %4
%16 = sub nsw i32 %14, %15
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i32, i32* %13, i64 %17
store i32 %12, i32* %18
%19 = load i32*, i32** %5
%20 = getelementptr inbounds i32, i32* %19, i64 0
%21 = load i32, i32* %20
%22 = load i32*, i32** %5
%23 = getelementptr inbounds i32, i32* %22, i64 1
%24 = load i32, i32* %23
%25 = load i32*, i32** %5
%26 = getelementptr inbounds i32, i32* %25, i64 2
%27 = load i32, i32* %26
%28 = load i32*, i32** %5
%29 = getelementptr inbounds i32, i32* %28, i64 3
%30 = load i32, i32* %29
%31 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %24, i32 %27, i32 %30)
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %3
%34 = call i32 (...) @getpid()
%35 = load i32*, i32** %5
%36 = load i32, i32* %6
%37 = load i32, i32* %4
%38 = sub nsw i32 %36, %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %35, i64 %39
store i32 %34, i32* %40
%41 = call i64 (...) @fork()
store i64 %41, i64* %7
%42 = load i64, i64* %7
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %33
%45 = load i32, i32* %4
%46 = sub nsw i32 %45, 1
%47 = load i32*, i32** %5
%48 = load i32, i32* %6
%49 = call i32 @sleep(i32 100)
br label %62
50: ; preds = %33
%51 = call i64 (...) @fork()
store i64 %51, i64* %8
%52 = load i64, i64* %8
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %50
%55 = load i32, i32* %4
%56 = sub nsw i32 %55, 1
%57 = load i32*, i32** %5
%58 = load i32, i32* %6
%59 = call i32 @sleep(i32 100)
br label %62
60: ; preds = %50
%61 = call i32 @sleep(i32 100)
br label %62
62: ; preds = %60, %54, %44
ret void
}

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