#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 int pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
int atoi(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
void spawn(int poziom, char *argv[]) {
  int i;
  pid_t pid;
  int status;
  if (poziom == 0)
    return;
  if (fork() == 0) {
    spawn(poziom - 1, argv);
    exit(poziom - 1);
  }
  for (i = 0; i < atoi(argv[poziom]); i++) {
    if (poziom == 1) {
      printf("[main] step %d\n", i);
    } else {
      printf("[child %d] step %d\n", poziom, i);
    }
    sleep(1);
  }
  pid = wait(&status);
  printf("pid = %d\nstatus = %d\n", pid, WEXITSTATUS(status));
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    119e <spawn+0x1e>
jmp    126d <spawn+0xed>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    11c9 <spawn+0x49>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
mov    -0x10(%rbp),%rsi
call   1040 <spawn@plt>
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <atoi@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    1240 <spawn+0xc0>
cmpl   $0x1,-0x4(%rbp)
jne    1214 <spawn+0x94>
mov    -0x14(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1228 <spawn+0xa8>
mov    -0x4(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d0 <spawn+0x50>
lea    -0x1c(%rbp),%rdi
call   1090 <wait@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
je     1232 <spawn+0xb2>
mov    %rsi,%r14
mov    %edi,%ebx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1241 <spawn+0xc1>
movslq %ebx,%r13
mov    (%r14,%r13,8),%rdi
call   1060 <atoi@plt>
test   %eax,%eax
jle    120b <spawn+0x8b>
lea    0xe41(%rip),%r15
lea    0xe4a(%rip),%r12
xor    %ebp,%ebp
jmp    11f8 <spawn+0x78>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
add    $0x1,%ebp
mov    (%r14,%r13,8),%rdi
call   1060 <atoi@plt>
cmp    %eax,%ebp
jge    120b <spawn+0x8b>
cmp    $0x1,%ebx
jne    11d0 <spawn+0x50>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11de <spawn+0x5e>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xdfd(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0xffffffff,%ebx
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <spawn@plt>
mov    %ebx,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"[main] step %d\0A\00"
@.str.1 = [20 x i8] c"[child %d] step %d\0A\00"
@.str.2 = [22 x i8] c"pid = %d\0Astatus = %d\0A\00"
define void @spawn(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
br label %52
11: ; preds = %2
%12 = call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = load i32, i32* %3
%16 = sub nsw i32 %15, 1
%17 = load i8**, i8*** %4
%18 = load i32, i32* %3
%19 = sub nsw i32 %18, 1
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %11
store i32 0, i32* %5
br label %22
22: ; preds = %43, %21
%23 = load i32, i32* %5
%24 = load i8**, i8*** %4
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %24, i64 %26
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
%30 = icmp slt i32 %23, %29
br i1 %30, label %31, label %46
31: ; preds = %22
%32 = load i32, i32* %3
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* %5
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %41
37: ; preds = %31
%38 = load i32, i32* %3
%39 = load i32, i32* %5
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %39)
br label %41
41: ; preds = %37, %34
%42 = call i32 @sleep(i32 1)
br label %43
43: ; preds = %41
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
br label %22
46: ; preds = %22
%47 = call i32 @wait(i32* %7)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = load i32, i32* %7
%50 = call i32 @WEXITSTATUS(i32 %49)
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %48, i32 %50)
br label %52
52: ; preds = %46, %10
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"[main] step %d\0A\00"
@.str.1 = [20 x i8] c"[child %d] step %d\0A\00"
@.str.2 = [22 x i8] c"pid = %d\0Astatus = %d\0A\00"
define void @spawn(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 0
br i1 %5, label %37, label %6
6: ; preds = %2
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %6
%10 = sext i32 %0 to i64
%11 = getelementptr inbounds i8*, i8** %1, i64 %10
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %9
%16 = icmp eq i32 %0, 1
br label %20
17: ; preds = %6
%18 = add nsw i32 %0, -1
tail call void @spawn(i32 %18, i8** %1)
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %15, %26
%21 = phi i32 [ 0, %15 ], [ %28, %26 ]
br i1 %16, label %22, label %24
22: ; preds = %20
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %26
24: ; preds = %20
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %21)
br label %26
26: ; preds = %24, %22
%27 = tail call i32 @sleep(i32 1)
%28 = add nuw nsw i32 %21, 1
%29 = load i8*, i8** %11
%30 = tail call i32 @atoi(i8* %29)
%31 = icmp slt i32 %28, %30
br i1 %31, label %20, label %32
32: ; preds = %26, %9
%33 = call i32 @wait(i32* %3)
%34 = load i32, i32* %3
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %35)
br label %37
37: ; preds = %2, %32
ret void
}