#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 /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ sleep(int);
int waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  pid_t pid, vPid[100];
  int n;
  int i, j, tmp;
  printf("Input the number of process n.:\n");
  scanf("%d", &n);
  printf("parent pid : %d\n", getpid());
  for (i = 0; i < n; i++) {
    pid = fork();
    if (pid != 0) {
      vPid[i] = pid;
    } else {
      printf("child pid %d\n", getpid());
      sleep(n - i);
      exit(0);
    }
  }
  for (j = 0; j < n; j++) {
    tmp = waitpid(vPid[j], (int *)0, 0);
    printf("vPid ret %d\n", tmp);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8a(%rip),%rdi
lea    -0x334(%rbp),%rsi
call   1050 <scanf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x338(%rbp)
mov    -0x338(%rbp),%eax
cmp    -0x334(%rbp),%eax
jge    124c <main+0xdc>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1204 <main+0x94>
mov    -0x10(%rbp),%rcx
movslq -0x338(%rbp),%rax
mov    %rcx,-0x330(%rbp,%rax,8)
ff
jmp    1233 <main+0xc3>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x334(%rbp),%edi
sub    -0x338(%rbp),%edi
call   1080 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1238 <main+0xc8>
mov    -0x338(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x338(%rbp)
jmp    11c4 <main+0x54>
movl   $0x0,-0x33c(%rbp)
mov    -0x33c(%rbp),%eax
cmp    -0x334(%rbp),%eax
jge    12b0 <main+0x140>
movslq -0x33c(%rbp),%rax
mov    -0x330(%rbp,%rax,8),%rdi
ff
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x340(%rbp)
mov    -0x340(%rbp),%esi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x33c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x33c(%rbp)
jmp    1256 <main+0xe6>
mov    -0x4(%rbp),%eax
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x338,%rsp
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
lea    0xe63(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1060 <scanf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe4e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x0,0xc(%rsp)
jle    1228 <main+0xa8>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1235 <main+0xb5>
mov    %rax,0x10(%rsp,%rbx,8)
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jl     11d0 <main+0x50>
test   %eax,%eax
jle    1228 <main+0xa8>
lea    0xe28(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    0x10(%rsp,%rbx,8),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jl     1200 <main+0x80>
xor    %eax,%eax
add    $0x338,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdd1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
sub    %ebx,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [17 x i8] c"parent pid : %d\0A\00"
@.str.3 = [14 x i8] c"child pid %d\0A\00"
@.str.4 = [13 x i8] c"vPid ret %d\0A\00"
@str = [32 x i8] c"Input the number of process n.:\00"
define i32 @main() {
%1 = alloca [100 x i64]
%2 = alloca i32
%3 = bitcast [100 x i64]* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%7 = call i32 (...) @getpid()
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %2
%10 = icmp sgt i32 %9, 0
br i1 %10, label %13, label %41
11: ; preds = %17
%12 = icmp sgt i32 %20, 0
br i1 %12, label %31, label %41
13: ; preds = %0, %17
%14 = phi i64 [ %19, %17 ], [ 0, %0 ]
%15 = call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %13
%18 = getelementptr inbounds [100 x i64], [100 x i64]* %1, i64 0, i64 %14
store i64 %15, i64* %18
%19 = add nuw nsw i64 %14, 1
%20 = load i32, i32* %2
%21 = sext i32 %20 to i64
%22 = icmp slt i64 %19, %21
br i1 %22, label %13, label %11
23: ; preds = %13
%24 = trunc i64 %14 to i32
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %2
%28 = sub nsw i32 %27, %24
%29 = call i32 @sleep(i32 %28)
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %11, %31
%32 = phi i64 [ %37, %31 ], [ 0, %11 ]
%33 = getelementptr inbounds [100 x i64], [100 x i64]* %1, i64 0, i64 %32
%34 = load i64, i64* %33
%35 = call i32 @waitpid(i64 %34, i32* null, i32 0)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = add nuw nsw i64 %32, 1
%38 = load i32, i32* %2
%39 = sext i32 %38 to i64
%40 = icmp slt i64 %37, %39
br i1 %40, label %31, label %41
41: ; preds = %31, %0, %11
ret i32 0
}