#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);
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, exit_code;
  printf("Fork program start\n");
  pid = fork();
  switch (pid) {
  case -1:
    perror("Unable to fork");
    exit(1);
  case 0:
    message = "This is the child speaking\n";
    n = 5;
    exit_code = 37;
    break;
  default:
    message = "This is the parent speaking\n";
    n = 3;
    exit_code = 0;
    break;
  }
  for (; n > 0; n--) {
    printf("#%d %s\n", n, message);
    sleep(1);
  }
  if (pid != 0) {
    int stat_val;
    pid_t child_pid;
    child_pid = wait(&stat_val);
    printf("Child has finished PID = %d\n", child_pid);
    if (WIFEXITED(stat_val))
      printf("Child exited with code %d\n", WEXITSTATUS(stat_val));
    else
      printf("Child exited with abnormally\n");
  }
  exit(exit_code);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x25(%rbp)
call   1030 <printf@plt>
mov    -0x25(%rbp),%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0xffffffff,%eax
je     11cf <main+0x4f>
jmp    11bf <main+0x3f>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     11e5 <main+0x65>
jmp    1203 <main+0x83>
lea    0xe3e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe37(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
movl   $0x25,-0x18(%rbp)
jmp    121c <main+0x9c>
lea    0xe35(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
jmp    1221 <main+0xa1>
cmpl   $0x0,-0x14(%rbp)
jle    1258 <main+0xd8>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1221 <main+0xa1>
cmpl   $0x0,-0x8(%rbp)
je     12c1 <main+0x141>
lea    -0x1c(%rbp),%rdi
call   1090 <wait@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12ae <main+0x12e>
mov    -0x1c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12bc <main+0x13c>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c1 <main+0x141>
mov    -0x18(%rbp),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xee5(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%r15d
test   %eax,%eax
je     11d2 <main+0x42>
cmp    $0xffffffff,%r15d
jne    11e6 <main+0x56>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe36(%rip),%rbx
mov    $0x25,%r14d
mov    $0x5,%ebp
jmp    11f5 <main+0x65>
lea    0xe3e(%rip),%rbx
xor    %r14d,%r14d
mov    $0x3,%ebp
lea    0xe4c(%rip),%r12
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    -0x1(%rbp),%eax
cmp    $0x1,%ebp
mov    %eax,%ebp
ja     1200 <main+0x70>
test   %r15d,%r15d
je     125c <main+0xcc>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
lea    0xe17(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
jne    1264 <main+0xd4>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1080 <exit@plt>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xdf9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
call   1080 <exit@plt>

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

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