#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 /*<<< orphan*/ 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");
        wait(&s);
        s = s >> 8;
        if (s == 2) {
          printf("child 2 exited\n");
          exit(0);
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    11e8 <main+0x68>
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,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%edx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
call   1070 <sleep@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    1248 <main+0xc8>
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    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <sleep@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    12a8 <main+0x128>
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    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <sleep@plt>
mov    $0x3,%edi
call   1060 <exit@plt>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x10(%rbp),%rdi
call   1080 <wait@plt>
mov    -0x10(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x2,-0x10(%rbp)
jne    12e7 <main+0x167>
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
jmp    12ec <main+0x16c>
jmp    12f1 <main+0x171>
jmp    12f6 <main+0x176>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11f3 <main+0x63>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1255 <main+0xc5>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     12b7 <main+0x127>
lea    0xeb3(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
mov    0xc(%rsp),%eax
sar    $0x8,%eax
mov    %eax,0xc(%rsp)
cmp    $0x2,%eax
je     1319 <main+0x189>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
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    0xdc7(%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    0xd8e(%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    0xd55(%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>
lea    0xd6a(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%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 2 exited\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %19
7: ; preds = %0
%8 = call i32 (...) @getpid()
%9 = call i32 @srand(i32 %8)
%10 = call i32 (...) @rand()
%11 = srem i32 %10, 10
%12 = add nsw i32 %11, 1
store i32 %12, i32* %1
%13 = call i32 (...) @getpid()
%14 = load i32, i32* %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
%16 = load i32, i32* %1
%17 = call i32 @sleep(i32 %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = call i64 (...) @fork()
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %19
%23 = call i32 (...) @getpid()
%24 = call i32 @srand(i32 %23)
%25 = call i32 (...) @rand()
%26 = srem i32 %25, 10
%27 = add nsw i32 %26, 1
store i32 %27, i32* %2
%28 = call i32 (...) @getpid()
%29 = load i32, i32* %2
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29)
%31 = load i32, i32* %2
%32 = call i32 @sleep(i32 %31)
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %19
%35 = call i64 (...) @fork()
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %34
%38 = call i32 (...) @getpid()
%39 = call i32 @srand(i32 %38)
%40 = call i32 (...) @rand()
%41 = srem i32 %40, 10
%42 = add nsw i32 %41, 1
store i32 %42, i32* %3
%43 = call i32 (...) @getpid()
%44 = load i32, i32* %3
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %43, i32 %44)
%46 = load i32, i32* %3
%47 = call i32 @sleep(i32 %46)
%48 = call i32 @exit(i32 3)
unreachable
49: ; preds = %34
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @wait(i32* %4)
%52 = load i32, i32* %4
%53 = ashr i32 %52, 8
store i32 %53, i32* %4
%54 = load i32, i32* %4
%55 = icmp eq i32 %54, 2
br i1 %55, label %56, label %59
56: ; preds = %49
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 @exit(i32 0)
unreachable
59: ; preds = %49
br label %60
60: ; preds = %59
br label %61
61: ; preds = %60
br label %62
62: ; preds = %61
ret i32 0
}

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.5 = [15 x i8] c"child 2 exited\00"
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 = load i32, i32* %1
%45 = ashr i32 %44, 8
store i32 %45, i32* %1
%46 = icmp eq i32 %45, 2
br i1 %46, label %47, label %50
47: ; preds = %40
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.5, i64 0, i64 0))
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %40
ret i32 0
}