#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 /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int);
int wait(int *);
main() {
  if (fork() == 0) {
    int r;
    srand(getpid());
    r = rand() % 10 + 1;
    printf("In Child 1 pid=%d before waiting %d sec\n", getpid(), r);
    sleep(r);
    exit(1);
  } else {
    if (fork() == 0) {
      int r;
      srand(getpid());
      r = rand() % 10 + 1;
      printf("In Child 2 pid=%d before waiting %d sec\n", getpid(), r);
      sleep(r);
      exit(2);
    } else {
      if (fork() == 0) {
        int r;
        srand(getpid());
        r = rand() % 10 + 1;
        printf("In Child 3 pid=%d before waiting %d sec\n", getpid(), r);
        sleep(r);
        exit(3);
      } else {
        int s;
        printf("In Parent ....\n");
        while (wait(&s) != -1) {
          s = s >> 8;
          if (s == 1)
            printf("child 1 exited\n");
          else if (s == 2)
            printf("child 2 exited\n");
          else if (s == 3)
            printf("child 3 exited\n");
        }
        printf("In Parent finished waiting for all child to exit..\n");
        while (1)
          ;
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    11ef <main+0x6f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1050 <srand@plt>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <sleep@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    124f <main+0xcf>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1050 <srand@plt>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0xc(%rbp),%edx
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <sleep@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    12af <main+0x12f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1050 <srand@plt>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <sleep@plt>
mov    $0x3,%edi
call   1060 <exit@plt>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x14(%rbp),%rdi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
je     1339 <main+0x1b9>
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    12f5 <main+0x175>
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1334 <main+0x1b4>
cmpl   $0x2,-0x14(%rbp)
jne    1312 <main+0x192>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132f <main+0x1af>
cmpl   $0x3,-0x14(%rbp)
jne    132a <main+0x1aa>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132f <main+0x1af>
jmp    1334 <main+0x1b4>
jmp    12bd <main+0x13d>
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1347 <main+0x1c7>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1232 <main+0xa2>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1294 <main+0x104>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     12f6 <main+0x166>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
je     1220 <main+0x90>
lea    0xf06(%rip),%r14
lea    0x4(%rsp),%rbx
jmp    11fd <main+0x6d>
nopl   (%rax)
mov    %rbx,%rdi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
je     1220 <main+0x90>
mov    0x4(%rsp),%eax
sar    $0x8,%eax
mov    %eax,0x4(%rsp)
add    $0xffffffff,%eax
cmp    $0x2,%eax
ja     11f0 <main+0x60>
cltq
movslq (%r14,%rax,4),%rdi
add    %r14,%rdi
call   1030 <puts@plt>
jmp    11f0 <main+0x60>
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
nopl   0x0(%rax)
jmp    1230 <main+0xa0>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %eax,%eax
call   10b0 <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),%ebx
add    $0x1,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd88(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %eax,%eax
call   10b0 <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),%ebx
add    $0x1,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd4f(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1080 <sleep@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %eax,%eax
call   10b0 <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),%ebx
add    $0x1,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd16(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1080 <sleep@plt>
mov    $0x3,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"In Child 1 pid=%d before waiting %d sec\0A\00"
@.str.1 = [41 x i8] c"In Child 2 pid=%d before waiting %d sec\0A\00"
@.str.2 = [41 x i8] c"In Child 3 pid=%d before waiting %d sec\0A\00"
@.str.3 = [16 x i8] c"In Parent ....\0A\00"
@.str.4 = [16 x i8] c"child 1 exited\0A\00"
@.str.5 = [16 x i8] c"child 2 exited\0A\00"
@.str.6 = [16 x i8] c"child 3 exited\0A\00"
@.str.7 = [52 x i8] c"In Parent finished waiting for all child to exit..\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %20
8: ; preds = %0
%9 = call i32 (...) @getpid()
%10 = call i32 @srand(i32 %9)
%11 = call i32 (...) @rand()
%12 = srem i32 %11, 10
%13 = add nsw i32 %12, 1
store i32 %13, i32* %2
%14 = call i32 (...) @getpid()
%15 = load i32, i32* %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
%17 = load i32, i32* %2
%18 = call i32 @sleep(i32 %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = call i64 (...) @fork()
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %20
%24 = call i32 (...) @getpid()
%25 = call i32 @srand(i32 %24)
%26 = call i32 (...) @rand()
%27 = srem i32 %26, 10
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
%29 = call i32 (...) @getpid()
%30 = load i32, i32* %3
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30)
%32 = load i32, i32* %3
%33 = call i32 @sleep(i32 %32)
%34 = call i32 @exit(i32 2)
unreachable
35: ; preds = %20
%36 = call i64 (...) @fork()
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %50
38: ; preds = %35
%39 = call i32 (...) @getpid()
%40 = call i32 @srand(i32 %39)
%41 = call i32 (...) @rand()
%42 = srem i32 %41, 10
%43 = add nsw i32 %42, 1
store i32 %43, i32* %4
%44 = call i32 (...) @getpid()
%45 = load i32, i32* %4
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %45)
%47 = load i32, i32* %4
%48 = call i32 @sleep(i32 %47)
%49 = call i32 @exit(i32 3)
unreachable
50: ; preds = %35
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %74, %50
%53 = call i32 @wait(i32* %5)
%54 = icmp ne i32 %53, -1
br i1 %54, label %55, label %75
55: ; preds = %52
%56 = load i32, i32* %5
%57 = ashr i32 %56, 8
store i32 %57, i32* %5
%58 = load i32, i32* %5
%59 = icmp eq i32 %58, 1
br i1 %59, label %60, label %62
60: ; preds = %55
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %74
62: ; preds = %55
%63 = load i32, i32* %5
%64 = icmp eq i32 %63, 2
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %73
67: ; preds = %62
%68 = load i32, i32* %5
%69 = icmp eq i32 %68, 3
br i1 %69, label %70, label %72
70: ; preds = %67
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %72
72: ; preds = %70, %67
br label %73
73: ; preds = %72, %65
br label %74
74: ; preds = %73, %60
br label %52
75: ; preds = %52
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.7, i64 0, i64 0))
br label %77
77: ; preds = %75, %77
br label %77
}

LLVM IR (-O2):
@.str = [41 x i8] c"In Child 1 pid=%d before waiting %d sec\0A\00"
@.str.1 = [41 x i8] c"In Child 2 pid=%d before waiting %d sec\0A\00"
@.str.2 = [41 x i8] c"In Child 3 pid=%d before waiting %d sec\0A\00"
@str = [15 x i8] c"In Parent ....\00"
@str.8 = [51 x i8] c"In Parent finished waiting for all child to exit..\00"
@str.9 = [15 x i8] c"child 3 exited\00"
@str.10 = [15 x i8] c"child 2 exited\00"
@str.11 = [15 x i8] c"child 1 exited\00"
@reltable.main = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([15 x i8]* @str.11 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([15 x i8]* @str.10 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([15 x i8]* @str.9 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32)]
define i32 @main() {
%1 = alloca i32
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %14
4: ; preds = %0
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 @srand(i32 %5)
%7 = tail call i32 (...) @rand()
%8 = srem i32 %7, 10
%9 = add nsw i32 %8, 1
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %9)
%12 = tail call i32 @sleep(i32 %9)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = tail call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %14
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 @srand(i32 %18)
%20 = tail call i32 (...) @rand()
%21 = srem i32 %20, 10
%22 = add nsw i32 %21, 1
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %22)
%25 = tail call i32 @sleep(i32 %22)
%26 = tail call i32 @exit(i32 2)
unreachable
27: ; preds = %14
%28 = tail call i64 (...) @fork()
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %27
%31 = tail call i32 (...) @getpid()
%32 = tail call i32 @srand(i32 %31)
%33 = tail call i32 (...) @rand()
%34 = srem i32 %33, 10
%35 = add nsw i32 %34, 1
%36 = tail call i32 (...) @getpid()
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %35)
%38 = tail call i32 @sleep(i32 %35)
%39 = tail call i32 @exit(i32 3)
unreachable
40: ; preds = %27
%41 = bitcast i32* %1 to i8*
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%43 = call i32 @wait(i32* %1)
%44 = icmp eq i32 %43, -1
br i1 %44, label %58, label %45
45: ; preds = %40, %55
%46 = load i32, i32* %1
%47 = ashr i32 %46, 8
store i32 %47, i32* %1
%48 = add nsw i32 %47, -1
%49 = icmp ult i32 %48, 3
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = sext i32 %48 to i64
%52 = shl i64 %51, 2
%53 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.main to i8*), i64 %52)
%54 = call i32 @puts(i8* dereferenceable(1) %53)
br label %55
55: ; preds = %45, %50
%56 = call i32 @wait(i32* %1)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %45
58: ; preds = %55, %40
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.8, i64 0, i64 0))
br label %60
60: ; preds = %60, %58
br label %60
}