#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*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main() {
  pid_t pid;
  char *message;
  int n;
  int exit_code;
  printf("fork program starting\n");
  pid = fork();
  switch (pid) {
  case -1:
    exit(1);
  case 0:
    message = "This is the child";
    n = 5;
    exit_code = 37;
    break;
  default:
    message = "This is the parent";
    n = 3;
    exit_code = 0;
    break;
  }
  for (; n > 0; n--) {
    puts(message);
    sleep(1);
  }
  if (pid) {
    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 terminated 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   1040 <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     11d9 <main+0x59>
jmp    11f7 <main+0x77>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe37(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
movl   $0x25,-0x18(%rbp)
jmp    1210 <main+0x90>
lea    0xe2b(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
jmp    1215 <main+0x95>
cmpl   $0x0,-0x14(%rbp)
jle    1240 <main+0xc0>
mov    -0x10(%rbp),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1215 <main+0x95>
cmpl   $0x0,-0x8(%rbp)
je     12a9 <main+0x129>
lea    -0x1c(%rbp),%rdi
call   1090 <wait@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1296 <main+0x116>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12a4 <main+0x124>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12a9 <main+0x129>
mov    -0x18(%rbp),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xed0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%r15d
test   %eax,%eax
je     11b1 <main+0x31>
cmp    $0xffffffff,%r15d
jne    11c5 <main+0x45>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe48(%rip),%rbx
mov    $0x25,%r14d
mov    $0x5,%ebp
jmp    11d4 <main+0x54>
lea    0xe46(%rip),%rbx
xor    %r14d,%r14d
mov    $0x3,%ebp
add    $0x1,%ebp
nopw   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   %r15d,%r15d
je     1233 <main+0xb3>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
lea    0xe15(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
jne    123b <main+0xbb>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1070 <exit@plt>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xdf8(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"fork program starting\0A\00"
@.str.1 = [18 x i8] c"This is the child\00"
@.str.2 = [19 x i8] c"This is the parent\00"
@.str.3 = [30 x i8] c"Child has finished: PID = %d\0A\00"
@.str.4 = [27 x i8] c"Child exited with code %d\0A\00"
@.str.5 = [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
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 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 %14 [
i32 -1, label %11
i32 0, label %13
]
11: ; preds = %0
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i32 5, i32* %4
store i32 37, i32* %5
br label %15
14: ; preds = %0
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %3
store i32 3, i32* %4
store i32 0, i32* %5
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 @puts(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 %43
29: ; preds = %26
%30 = call i32 @wait(i32* %6)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %6
%34 = call i64 @WIFEXITED(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i32, i32* %6
%38 = call i32 @WEXITSTATUS(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %38)
br label %42
40: ; preds = %29
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
br label %43
43: ; preds = %42, %26
%44 = load i32, i32* %5
%45 = call i32 @exit(i32 %44)
unreachable
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"This is the child\00"
@.str.2 = [19 x i8] c"This is the parent\00"
@.str.3 = [30 x i8] c"Child has finished: PID = %d\0A\00"
@.str.4 = [27 x i8] c"Child exited with code %d\0A\00"
@str = [22 x i8] c"fork program starting\00"
@str.6 = [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 %6 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %0
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
br label %7
7: ; preds = %0, %6
%8 = phi i32 [ 3, %6 ], [ 5, %0 ]
%9 = phi i32 [ 0, %6 ], [ 37, %0 ]
%10 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), %6 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %0 ]
br label %11
11: ; preds = %7, %11
%12 = phi i32 [ %8, %7 ], [ %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 %33, 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 ([30 x i8], [30 x i8]* @.str.3, 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 %30, label %26
26: ; preds = %19
%27 = load i32, i32* %1
%28 = call i32 @WEXITSTATUS(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %28)
br label %32
30: ; preds = %19
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
br label %33
33: ; preds = %32, %17
%34 = call i32 @exit(i32 %9)
unreachable
}