#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 */
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main() {
  pid_t pid;
  char *message;
  int n;
  printf("fork program starting\n");
  pid = fork();
  switch (pid) {
  case -1:
    perror("fork failed");
    exit(1);
  case 0:
    message = "This is the child";
    n = 5;
    break;
  default:
    message = "This is the parent";
    n = 10;
    break;
  }
  for (; n > 0; n--) {
    printf("%s\n", message);
    sleep(1);
  }
  if (pid != 0) {
    int start_val;
    pid_t child_pid;
    child_pid = wait(&start_val);
    printf("Child has finished:PID = %d\n", child_pid);
    if (WIFEXITED(start_val))
      printf("Child exited with code %d\n", child_pid);
    else
      printf("Child terminated abnormally\n");
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x21(%rbp)
call   1030 <printf@plt>
mov    -0x21(%rbp),%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0xffffffff,%eax
je     11bf <main+0x4f>
jmp    11af <main+0x3f>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     11d5 <main+0x65>
jmp    11ec <main+0x7c>
lea    0xe51(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe47(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
jmp    11fe <main+0x8e>
lea    0xe42(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0xa,-0x14(%rbp)
jmp    1203 <main+0x93>
cmpl   $0x0,-0x14(%rbp)
jle    1237 <main+0xc7>
mov    -0x10(%rbp),%rsi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1203 <main+0x93>
cmpl   $0x0,-0x8(%rbp)
je     1299 <main+0x129>
lea    -0x18(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1286 <main+0x116>
mov    -0x1c(%rbp),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1294 <main+0x124>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1299 <main+0x129>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeda(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%r14d
test   %eax,%eax
je     11be <main+0x3e>
cmp    $0xffffffff,%r14d
jne    11cc <main+0x4c>
lea    0xe51(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe47(%rip),%rbx
mov    $0x5,%ebp
jmp    11d8 <main+0x58>
lea    0xe4b(%rip),%rbx
mov    $0xa,%ebp
add    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     11e0 <main+0x60>
test   %r14d,%r14d
je     1235 <main+0xb5>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
mov    %eax,%ebx
lea    0xe1f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
jne    123c <main+0xbc>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe0b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"fork program starting\0A\00"
@.str.1 = [12 x i8] c"fork failed\00"
@.str.2 = [18 x i8] c"This is the child\00"
@.str.3 = [19 x i8] c"This is the parent\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [29 x i8] c"Child has finished:PID = %d\0A\00"
@.str.6 = [27 x i8] c"Child exited with code %d\0A\00"
@.str.7 = [29 x i8] c"Child terminated abnormally\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (...) @fork()
store i32 %8, i32* %2
%9 = load i32, i32* %2
switch i32 %9, label %14 [
i32 -1, label %10
i32 0, label %13
]
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8** %3
store i32 5, i32* %4
br label %15
14: ; preds = %0
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8** %3
store i32 10, i32* %4
br label %15
15: ; preds = %14, %13
br label %16
16: ; preds = %23, %15
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i8*, i8** %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %20)
%22 = call i32 @sleep(i32 1)
br label %23
23: ; preds = %19
%24 = load i32, i32* %4
%25 = add nsw i32 %24, -1
store i32 %25, i32* %4
br label %16
26: ; preds = %16
%27 = load i32, i32* %2
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %42
29: ; preds = %26
%30 = call i32 @wait(i32* %5)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %5
%34 = call i64 @WIFEXITED(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = load i32, i32* %6
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %37)
br label %41
39: ; preds = %29
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
br label %42
42: ; preds = %41, %26
%43 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"fork failed\00"
@.str.2 = [18 x i8] c"This is the child\00"
@.str.3 = [19 x i8] c"This is the parent\00"
@.str.5 = [29 x i8] c"Child has finished:PID = %d\0A\00"
@.str.6 = [27 x i8] c"Child exited with code %d\0A\00"
@str = [22 x i8] c"fork program starting\00"
@str.8 = [28 x i8] c"Child terminated abnormally\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 (...) @fork()
switch i32 %3, label %7 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
br label %8
8: ; preds = %0, %7
%9 = phi i32 [ 10, %7 ], [ 5, %0 ]
%10 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), %7 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), %0 ]
br label %11
11: ; preds = %8, %11
%12 = phi i32 [ %9, %8 ], [ %15, %11 ]
%13 = tail call i32 @puts(i8* dereferenceable(1) %10)
%14 = tail call i32 @sleep(i32 1)
%15 = add nsw i32 %12, -1
%16 = icmp ugt i32 %12, 1
br i1 %16, label %11, label %17
17: ; preds = %11
%18 = icmp eq i32 %3, 0
br i1 %18, label %31, label %19
19: ; preds = %17
%20 = bitcast i32* %1 to i8*
%21 = call i32 @wait(i32* %1)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %1
%24 = call i64 @WIFEXITED(i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %19
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %21)
br label %30
28: ; preds = %19
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.8, i64 0, i64 0))
br label %30
30: ; preds = %28, %26
br label %31
31: ; preds = %30, %17
%32 = call i32 @exit(i32 0)
unreachable
}