#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 /*<<< orphan*/ exit(int);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int);
int /*<<< orphan*/ wait(int *);
int main() {
  int i, ncld, wtime, status;
  pid_t cpid, mypid, parpid;
  mypid = getpid();
  parpid = getppid();
  printf("Parent: PID = %u, PPID = %u\n", mypid, parpid);
  printf("Parent: Number of children = ");
  scanf("%d", &ncld);
  if ((ncld < 0) || (ncld > 10))
    ncld = 5;
  printf("\n");
  for (i = 0; i < ncld; ++i) {
    cpid = fork();
    if (cpid == 0) {
      mypid = getpid();
      parpid = getppid();
      srand(mypid);
      wtime = 1 + rand() % 10;
      printf("Child %d: PID = %u, PPID = %u, work time = %d\n", i, mypid,
             parpid, wtime);
      sleep(wtime);
      printf("\nChild %d: Work done...\n", i);
      exit(i);
    }
  }
  for (i = 0; i < ncld; ++i) {
    wait(&status);
    printf("Parent: Child %d terminates...\n", WEXITSTATUS(status));
  }
  printf("\nParent terminates...\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe3f(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1060 <scanf@plt>
cmpl   $0x0,-0xc(%rbp)
jl     1219 <main+0x69>
cmpl   $0xa,-0xc(%rbp)
jle    1220 <main+0x70>
movl   $0x5,-0xc(%rbp)
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12d5 <main+0x125>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    12c2 <main+0x112>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <srand@plt>
mov    $0x0,%al
call   10d0 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%r8d
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   10a0 <sleep@plt>
mov    -0x8(%rbp),%esi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1090 <exit@plt>
jmp    12c7 <main+0x117>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1235 <main+0x85>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1317 <main+0x167>
lea    -0x14(%rbp),%rdi
call   10b0 <wait@plt>
mov    -0x14(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12dc <main+0x12c>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <getppid@plt>
lea    0xe12(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe1d(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe2d(%rip),%rdi
mov    %rsp,%rsi
call   1080 <scanf@plt>
cmpl   $0xb,(%rsp)
jb     1223 <main+0x53>
movl   $0x5,(%rsp)
mov    $0xa,%edi
call   1030 <putchar@plt>
cmpl   $0x0,(%rsp)
jle    1295 <main+0xc5>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     12a8 <main+0xd8>
add    $0x1,%ebx
mov    (%rsp),%eax
cmp    %eax,%ebx
jl     1240 <main+0x70>
test   %eax,%eax
jle    1295 <main+0xc5>
lea    0x4(%rsp),%r14
lea    0xe21(%rip),%rbp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   10d0 <wait@plt>
mov    0x4(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %rbp,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%ebx
cmp    (%rsp),%ebx
jl     1270 <main+0xa0>
lea    0xe0a(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   10a0 <getppid@plt>
mov    %eax,%r14d
mov    %r15d,%edi
call   1070 <srand@plt>
xor    %eax,%eax
call   10f0 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%ebp
add    $0x1,%ebp
lea    0xd49(%rip),%rdi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %r14d,%ecx
mov    %ebp,%r8d
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   10c0 <sleep@plt>
lea    0xd58(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"Parent: PID = %u, PPID = %u\0A\00"
@.str.1 = [30 x i8] c"Parent: Number of children = \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [47 x i8] c"Child %d: PID = %u, PPID = %u, work time = %d\0A\00"
@.str.5 = [25 x i8] c"\0AChild %d: Work done...\0A\00"
@.str.6 = [32 x i8] c"Parent: Child %d terminates...\0A\00"
@.str.7 = [23 x i8] c"\0AParent terminates...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 (...) @getpid()
store i32 %9, i32* %7
%10 = call i32 (...) @getppid()
store i32 %10, i32* %8
%11 = load i32, i32* %7
%12 = load i32, i32* %8
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %0
%19 = load i32, i32* %3
%20 = icmp sgt i32 %19, 10
br i1 %20, label %21, label %22
21: ; preds = %18, %0
store i32 5, i32* %3
br label %22
22: ; preds = %21, %18
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %24
24: ; preds = %52, %22
%25 = load i32, i32* %2
%26 = load i32, i32* %3
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %55
28: ; preds = %24
%29 = call i32 (...) @fork()
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %51
32: ; preds = %28
%33 = call i32 (...) @getpid()
store i32 %33, i32* %7
%34 = call i32 (...) @getppid()
store i32 %34, i32* %8
%35 = load i32, i32* %7
%36 = call i32 @srand(i32 %35)
%37 = call i32 (...) @rand()
%38 = srem i32 %37, 10
%39 = add nsw i32 1, %38
store i32 %39, i32* %4
%40 = load i32, i32* %2
%41 = load i32, i32* %7
%42 = load i32, i32* %8
%43 = load i32, i32* %4
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %40, i32 %41, i32 %42, i32 %43)
%45 = load i32, i32* %4
%46 = call i32 @sleep(i32 %45)
%47 = load i32, i32* %2
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %2
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %28
br label %52
52: ; preds = %51
%53 = load i32, i32* %2
%54 = add nsw i32 %53, 1
store i32 %54, i32* %2
br label %24
55: ; preds = %24
store i32 0, i32* %2
br label %56
56: ; preds = %65, %55
%57 = load i32, i32* %2
%58 = load i32, i32* %3
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %68
60: ; preds = %56
%61 = call i32 @wait(i32* %5)
%62 = load i32, i32* %5
%63 = call i32 @WEXITSTATUS(i32 %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %60
%66 = load i32, i32* %2
%67 = add nsw i32 %66, 1
store i32 %67, i32* %2
br label %56
68: ; preds = %56
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
%70 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [29 x i8] c"Parent: PID = %u, PPID = %u\0A\00"
@.str.1 = [30 x i8] c"Parent: Number of children = \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.4 = [47 x i8] c"Child %d: PID = %u, PPID = %u, work time = %d\0A\00"
@.str.5 = [25 x i8] c"\0AChild %d: Work done...\0A\00"
@.str.6 = [32 x i8] c"Parent: Child %d terminates...\0A\00"
@str = [22 x i8] c"\0AParent terminates...\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 (...) @getppid()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%10 = load i32, i32* %1
%11 = icmp ugt i32 %10, 10
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 5, i32* %1
br label %13
13: ; preds = %0, %12
%14 = call i32 @putchar(i32 10)
%15 = load i32, i32* %1
%16 = icmp sgt i32 %15, 0
br i1 %16, label %19, label %47
17: ; preds = %34
%18 = icmp sgt i32 %36, 0
br i1 %18, label %38, label %47
19: ; preds = %13, %34
%20 = phi i32 [ %35, %34 ], [ 0, %13 ]
%21 = call i32 (...) @fork()
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %19
%24 = call i32 (...) @getpid()
%25 = call i32 (...) @getppid()
%26 = call i32 @srand(i32 %24)
%27 = call i32 (...) @rand()
%28 = srem i32 %27, 10
%29 = add nsw i32 %28, 1
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %20, i32 %24, i32 %25, i32 %29)
%31 = call i32 @sleep(i32 %29)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %20)
%33 = call i32 @exit(i32 %20)
unreachable
34: ; preds = %19
%35 = add nuw nsw i32 %20, 1
%36 = load i32, i32* %1
%37 = icmp slt i32 %35, %36
br i1 %37, label %19, label %17
38: ; preds = %17, %38
%39 = phi i32 [ %44, %38 ], [ 0, %17 ]
%40 = call i32 @wait(i32* %2)
%41 = load i32, i32* %2
%42 = call i32 @WEXITSTATUS(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i32 %42)
%44 = add nuw nsw i32 %39, 1
%45 = load i32, i32* %1
%46 = icmp slt i32 %44, %45
br i1 %46, label %38, label %47
47: ; preds = %38, %13, %17
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%49 = call i32 @exit(i32 0)
unreachable
}