#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 wait(int *);
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 = wait((int *)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   1070 <sleep@plt>
xor    %edi,%edi
call   1060 <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    129f <main+0x12f>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
mov    %eax,-0x340(%rbp)
mov    -0x340(%rbp),%esi
lea    0xdbf(%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   %rbp
push   %rbx
push   %rax
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
lea    0xe6a(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1060 <scanf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe55(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x0,0x4(%rsp)
jle    120c <main+0x8c>
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1215 <main+0x95>
add    $0x1,%ebx
mov    0x4(%rsp),%eax
cmp    %eax,%ebx
jl     11c0 <main+0x40>
test   %eax,%eax
jle    120c <main+0x8c>
lea    0xe40(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
call   1090 <wait@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
cmp    0x4(%rsp),%ebp
jl     11f0 <main+0x70>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdf1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
sub    %ebx,%edi
call   1080 <sleep@plt>
xor    %edi,%edi
call   1070 <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 = %46, %37
%39 = load i32, i32* %6
%40 = load i32, i32* %4
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %49
42: ; preds = %38
%43 = call i32 @wait(i32* null)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %42
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
br label %38
49: ; preds = %38
%50 = load i32, i32* %1
ret i32 %50
}

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 i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 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 = call i32 (...) @getpid()
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%7 = load i32, i32* %1
%8 = icmp sgt i32 %7, 0
br i1 %8, label %11, label %33
9: ; preds = %15
%10 = icmp sgt i32 %17, 0
br i1 %10, label %26, label %33
11: ; preds = %0, %15
%12 = phi i32 [ %16, %15 ], [ 0, %0 ]
%13 = call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = add nuw nsw i32 %12, 1
%17 = load i32, i32* %1
%18 = icmp slt i32 %16, %17
br i1 %18, label %11, label %9
19: ; preds = %11
%20 = call i32 (...) @getpid()
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %1
%23 = sub nsw i32 %22, %12
%24 = call i32 @sleep(i32 %23)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %9, %26
%27 = phi i32 [ %30, %26 ], [ 0, %9 ]
%28 = call i32 @wait(i32* null)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = add nuw nsw i32 %27, 1
%31 = load i32, i32* %1
%32 = icmp slt i32 %30, %31
br i1 %32, label %26, label %33
33: ; preds = %26, %0, %9
ret i32 0
}