#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 */
int WEXITSTATUS(int);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
void create_recursion_process(int process_count) {
  int pid, status;
  if (process_count == 0)
    return;
  pid = fork();
  if (pid == 0) {
    create_recursion_process(process_count - 1);
    exit(process_count - 1);
  } else if (pid == -1) {
    perror("fork error");
    exit(1);
  }
  printf("^start process [id=%d, pid = %d, ppid = %d]\n", process_count,
         getpid(), getppid());
  sleep(1);
  pid = wait(&status);
  printf("$close process [pid = %d, status = %d]\n", pid, WEXITSTATUS(status));
}


x86 assembly (clang, -O0):
<create_recursion_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11ba <create_recursion_process+0x1a>
jmp    126d <create_recursion_process+0xcd>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11e4 <create_recursion_process+0x44>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1060 <create_recursion_process@plt>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0xffffffff,-0x8(%rbp)
jne    1204 <create_recursion_process+0x64>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1209 <create_recursion_process+0x69>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    -0xc(%rbp),%rdi
call   10b0 <wait@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_recursion_process>:
push   %rbp
push   %rbx
push   %rax
test   %edi,%edi
je     120e <create_recursion_process+0x6e>
mov    %edi,%ebx
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     1215 <create_recursion_process+0x75>
test   %eax,%eax
je     122b <create_recursion_process+0x8b>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe3b(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0x4(%rsp),%rdi
call   10b0 <wait@plt>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe35(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xde4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0xffffffff,%ebx
mov    %ebx,%edi
call   1060 <create_recursion_process@plt>
mov    %ebx,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [45 x i8] c"^start process [id=%d, pid = %d, ppid = %d]\0A\00"
@.str.2 = [40 x i8] c"$close process [pid = %d, status = %d]\0A\00"
define void @create_recursion_process(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %1
br label %36
8: ; preds = %1
%9 = call i32 (...) @fork()
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = load i32, i32* %2
%14 = sub nsw i32 %13, 1
%15 = load i32, i32* %2
%16 = sub nsw i32 %15, 1
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %8
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %18
br label %25
25: ; preds = %24
%26 = load i32, i32* %2
%27 = call i32 (...) @getpid()
%28 = call i32 (...) @getppid()
%29 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27, i32 %28)
%30 = call i32 @sleep(i32 1)
%31 = call i32 @wait(i32* %4)
store i32 %31, i32* %3
%32 = load i32, i32* %3
%33 = load i32, i32* %4
%34 = call i32 @WEXITSTATUS(i32 %33)
%35 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %34)
br label %36
36: ; preds = %25, %7
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [45 x i8] c"^start process [id=%d, pid = %d, ppid = %d]\0A\00"
@.str.2 = [40 x i8] c"$close process [pid = %d, status = %d]\0A\00"
define void @create_recursion_process(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = icmp eq i32 %0, 0
br i1 %4, label %22, label %5
5: ; preds = %1
%6 = tail call i32 (...) @fork()
switch i32 %6, label %13 [
i32 0, label %7
i32 -1, label %10
]
7: ; preds = %5
%8 = add nsw i32 %0, -1
tail call void @create_recursion_process(i32 %8)
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %5
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 (...) @getppid()
%16 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %14, i32 %15)
%17 = tail call i32 @sleep(i32 1)
%18 = call i32 @wait(i32* %2)
%19 = load i32, i32* %2
%20 = call i32 @WEXITSTATUS(i32 %19)
%21 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %20)
br label %22
22: ; preds = %1, %13
ret void
}