#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 */
scalar_t__ fork();
int /*<<< orphan*/ generate_random();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int main(void) {
  printf("\nIn this program, the parent process controls\n"
         "the output of the program status messages.\n"
         "Random number generation is handled by the\n"
         "child process.\n\n"
         "------- Key -------\n"
         "p = parent process\n"
         "c = child process\n"
         "-------------------\n\n");
  pid_t pid = fork();
  if (pid == 0) {
    int i;
    for (i = 0; i <= 5; i++) {
      generate_random();
      if (i < 5) {
        sleep(1);
      }
    }
    printf("\n");
  } else if (pid > 0) {
    printf("p: Generating random numbers...\n\n");
    int returnStatus;
    waitpid(0, &returnStatus, 0);
    if (returnStatus == 0) {
      printf("p: Random number generation complete\n\n");
    } else {
      printf("ERROR: Child process encountered an error\n\n");
    }
  } else {
    printf("ERROR: System call \"fork()\" failed\n\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d1 <main+0x81>
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jg     11c0 <main+0x70>
mov    $0x0,%al
call   1050 <generate_random@plt>
cmpl   $0x5,-0x14(%rbp)
jge    11ad <main+0x5d>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11b2 <main+0x62>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1188 <main+0x38>
lea    0xf1a(%rip),%rdi
call   1030 <printf@plt>
jmp    123e <main+0xee>
cmpq   $0x0,-0x10(%rbp)
jle    1221 <main+0xd1>
lea    0xf00(%rip),%rdi
call   1030 <printf@plt>
xor    %edx,%edx
lea    -0x18(%rbp),%rsi
mov    %edx,%edi
call   1040 <waitpid@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1210 <main+0xc0>
lea    0xeff(%rip),%rdi
call   1030 <printf@plt>
jmp    121c <main+0xcc>
lea    0xf15(%rip),%rdi
call   1030 <printf@plt>
jmp    1239 <main+0xe9>
lea    0xf30(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1245 <main+0xf5>
jmp    123e <main+0xee>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe94(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11bf <main+0x5f>
jle    1227 <main+0xc7>
lea    0xf7a(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rsi
xor    %ebx,%ebx
xor    %edi,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmpl   $0x0,0xc(%rsp)
lea    0xfa5(%rip),%rax
lea    0xf73(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
jmp    1238 <main+0xd8>
xor    %eax,%eax
call   1060 <generate_random@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
call   1060 <generate_random@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
call   1060 <generate_random@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
call   1060 <generate_random@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
call   1060 <generate_random@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
call   1060 <generate_random@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1238 <main+0xd8>
lea    0xeb4(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [227 x i8] c"\0AIn this program, the parent process controls\0Athe output of the program status messages.\0ARandom number generation is handled by the\0Achild process.\0A\0A------- Key -------\0Ap = parent process\0Ac = child process\0A-------------------\0A\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [34 x i8] c"p: Generating random numbers...\0A\0A\00"
@.str.3 = [39 x i8] c"p: Random number generation complete\0A\0A\00"
@.str.4 = [44 x i8] c"ERROR: Child process encountered an error\0A\0A\00"
@.str.5 = [37 x i8] c"ERROR: System call \22fork()\22 failed\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @printf(i8* getelementptr inbounds ([227 x i8], [227 x i8]* @.str, i64 0, i64 0))
%6 = call i64 (...) @fork()
store i64 %6, i64* %2
%7 = load i64, i64* %2
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %25
9: ; preds = %0
store i32 0, i32* %3
br label %10
10: ; preds = %20, %9
%11 = load i32, i32* %3
%12 = icmp sle i32 %11, 5
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = call i32 (...) @generate_random()
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 5
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @sleep(i32 1)
br label %19
19: ; preds = %17, %13
br label %20
20: ; preds = %19
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %10
23: ; preds = %10
%24 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %41
25: ; preds = %0
%26 = load i64, i64* %2
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %25
%29 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @waitpid(i32 0, i32* %4, i32 0)
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %37
35: ; preds = %28
%36 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %35, %33
br label %40
38: ; preds = %25
%39 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %42
40: ; preds = %37
br label %41
41: ; preds = %40, %23
store i32 0, i32* %1
br label %42
42: ; preds = %41, %38
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@str = [226 x i8] c"\0AIn this program, the parent process controls\0Athe output of the program status messages.\0ARandom number generation is handled by the\0Achild process.\0A\0A------- Key -------\0Ap = parent process\0Ac = child process\0A-------------------\0A\00"
@str.6 = [36 x i8] c"ERROR: System call \22fork()\22 failed\0A\00"
@str.7 = [33 x i8] c"p: Generating random numbers...\0A\00"
@str.8 = [43 x i8] c"ERROR: Child process encountered an error\0A\00"
@str.9 = [38 x i8] c"p: Random number generation complete\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([226 x i8], [226 x i8]* @str, i64 0, i64 0))
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %18
5: ; preds = %0
%6 = tail call i32 (...) @generate_random()
%7 = tail call i32 @sleep(i32 1)
%8 = tail call i32 (...) @generate_random()
%9 = tail call i32 @sleep(i32 1)
%10 = tail call i32 (...) @generate_random()
%11 = tail call i32 @sleep(i32 1)
%12 = tail call i32 (...) @generate_random()
%13 = tail call i32 @sleep(i32 1)
%14 = tail call i32 (...) @generate_random()
%15 = tail call i32 @sleep(i32 1)
%16 = tail call i32 (...) @generate_random()
%17 = tail call i32 @putchar(i32 10)
br label %30
18: ; preds = %0
%19 = icmp sgt i64 %3, 0
br i1 %19, label %20, label %28
20: ; preds = %18
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.7, i64 0, i64 0))
%22 = bitcast i32* %1 to i8*
%23 = call i32 @waitpid(i32 0, i32* %1, i32 0)
%24 = load i32, i32* %1
%25 = icmp eq i32 %24, 0
%26 = select i1 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @str.9, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @str.8, i64 0, i64 0)
%27 = call i32 @puts(i8* dereferenceable(1) %26)
br label %30
28: ; preds = %18
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.6, i64 0, i64 0))
br label %30
30: ; preds = %5, %20, %28
%31 = phi i32 [ 1, %28 ], [ 0, %20 ], [ 0, %5 ]
ret i32 %31
}