#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 */
scalar_t__ calloc(int, int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ wait();
int main() {
  int height = 0;
  pid_t pid;
  pid_t *pid_array;
  int id = 10000;
  printf("gimme height\n");
  scanf("%d", &height);
  pid_array = (pid_t *)calloc(height, sizeof(pid_t));
  for (size_t i = 0; i < (size_t)height; i++) {
    pid = fork();
    if (pid == 0) {
      id += i;
      printf("i am son = %d\n", id);
      if (pid > 0) {
        wait();
      }
    } else {
      printf("waiting for %d", id);
      wait();
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x2710,-0x1c(%rbp)
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xea7(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jae    122f <main+0xdf>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1201 <main+0xb1>
mov    -0x28(%rbp),%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11fc <main+0xac>
mov    $0x0,%al
call   1060 <wait@plt>
jmp    1219 <main+0xc9>
mov    -0x1c(%rbp),%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <wait@plt>
jmp    121e <main+0xce>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11a4 <main+0x54>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
lea    0xe99(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x4(%rsp),%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
cmpl   $0x0,0x4(%rsp)
je     11fd <main+0x9d>
mov    $0x2710,%ebp
lea    0xe53(%rip),%r14
lea    0xe5b(%rip),%r15
xor    %ebx,%ebx
jmp    11e1 <main+0x81>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <wait@plt>
add    $0x1,%rbx
movslq 0x4(%rsp),%rax
cmp    %rax,%rbx
jae    11fd <main+0x9d>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    11c0 <main+0x60>
add    %ebx,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d3 <main+0x73>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"gimme height\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [15 x i8] c"i am son = %d\0A\00"
@.str.3 = [15 x i8] c"waiting for %d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %2
store i32 10000, i32* %5
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = load i32, i32* %2
%10 = call i64 @calloc(i32 %9, i32 8)
%11 = inttoptr i64 %10 to i64*
store i64* %11, i64** %4
store i64 0, i64* %6
br label %12
12: ; preds = %39, %0
%13 = load i64, i64* %6
%14 = load i32, i32* %2
%15 = sext i32 %14 to i64
%16 = icmp ult i64 %13, %15
br i1 %16, label %17, label %42
17: ; preds = %12
%18 = call i64 (...) @fork()
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %17
%22 = load i64, i64* %6
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = add i64 %24, %22
%26 = trunc i64 %25 to i32
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = load i64, i64* %3
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 (...) @wait()
br label %33
33: ; preds = %31, %21
br label %38
34: ; preds = %17
%35 = load i32, i32* %5
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = call i32 (...) @wait()
br label %38
38: ; preds = %34, %33
br label %39
39: ; preds = %38
%40 = load i64, i64* %6
%41 = add i64 %40, 1
store i64 %41, i64* %6
br label %12
42: ; preds = %12
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [15 x i8] c"i am son = %d\0A\00"
@.str.3 = [15 x i8] c"waiting for %d\00"
@str = [13 x i8] c"gimme height\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = call i64 @calloc(i32 %5, i32 8)
%7 = load i32, i32* %1
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %22, %0
ret i32 0
10: ; preds = %0, %22
%11 = phi i64 [ %24, %22 ], [ 0, %0 ]
%12 = phi i32 [ %23, %22 ], [ 10000, %0 ]
%13 = call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = trunc i64 %11 to i32
%17 = add i32 %12, %16
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %22
19: ; preds = %10
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%21 = call i32 (...) @wait()
br label %22
22: ; preds = %15, %19
%23 = phi i32 [ %17, %15 ], [ %12, %19 ]
%24 = add nuw i64 %11, 1
%25 = load i32, i32* %1
%26 = sext i32 %25 to i64
%27 = icmp ult i64 %24, %26
br i1 %27, label %10, label %9
}