#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 fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ sleep(int);
void createChildProccess(int *table, int size, int offset) {
  if (offset == size) {
    return;
  }
  for (int i = 0; i < table[offset]; i++) {
    int pid = fork();
    if (pid < 0) {
      perror("Could not create child.");
    } else if (pid == 0) {
      printf("Child, pid: %d of ppid:%d\n", getpid(), getppid());
      createChildProccess(table, size, offset + 1);
      sleep(2);
    } else {
      sleep(2);
    }
  }
}


x86 assembly (clang, -O0):
<createChildProccess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    1193 <createChildProccess+0x23>
jmp    1242 <createChildProccess+0xd2>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x8(%rbp),%rcx
movslq -0x10(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    1242 <createChildProccess+0xd2>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11d3 <createChildProccess+0x63>
lea    0xe37(%rip),%rdi
call   1060 <perror@plt>
jmp    122f <createChildProccess+0xbf>
cmpl   $0x0,-0x18(%rbp)
jne    1220 <createChildProccess+0xb0>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe1e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
add    $0x1,%edx
call   1050 <createChildProccess@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
jmp    122a <createChildProccess+0xba>
mov    $0x2,%edi
call   1080 <sleep@plt>
jmp    122f <createChildProccess+0xbf>
jmp    1234 <createChildProccess+0xc4>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119a <createChildProccess+0x2a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createChildProccess>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    %esi,%edx
je     1200 <createChildProccess+0x90>
mov    %edx,%r13d
mov    %rdi,%rbp
movslq %edx,%r15
cmpl   $0x0,(%rdi,%r15,4)
jle    1200 <createChildProccess+0x90>
mov    %esi,%r14d
add    $0x1,%r13d
xor    %r12d,%r12d
nopl   (%rax)
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     11f2 <createChildProccess+0x82>
jne    11db <createChildProccess+0x6b>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe54(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %r13d,%edx
call   1050 <createChildProccess@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
add    $0x1,%r12d
cmp    0x0(%rbp,%r15,4),%r12d
jl     11a0 <createChildProccess+0x30>
jmp    1200 <createChildProccess+0x90>
lea    0xe07(%rip),%rdi
call   1060 <perror@plt>
jmp    11e5 <createChildProccess+0x75>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [24 x i8] c"Could not create child.\00"
@.str.1 = [27 x i8] c"Child, pid: %d of ppid:%d\0A\00"
define void @createChildProccess(i32* nocapture %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, %1
br i1 %4, label %31, label %5
5: ; preds = %3
%6 = sext i32 %2 to i64
%7 = getelementptr inbounds i32, i32* %0, i64 %6
%8 = load i32, i32* %7
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %5
%11 = add nsw i32 %2, 1
br label %12
12: ; preds = %10, %27
%13 = phi i32 [ 0, %10 ], [ %28, %27 ]
%14 = tail call i32 (...) @fork()
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %27
18: ; preds = %12
%19 = icmp eq i32 %14, 0
br i1 %19, label %20, label %25
20: ; preds = %18
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (...) @getppid()
%23 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %22)
tail call void @createChildProccess(i32* %0, i32 %1, i32 %11)
%24 = tail call i32 @sleep(i32 2)
br label %27
25: ; preds = %18
%26 = tail call i32 @sleep(i32 2)
br label %27
27: ; preds = %20, %25, %16
%28 = add nuw nsw i32 %13, 1
%29 = load i32, i32* %7
%30 = icmp slt i32 %28, %29
br i1 %30, label %12, label %31
31: ; preds = %27, %5, %3
ret void
}