#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*/ 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:
    perror("fork failed");
    exit(1);
  case 0:
    message = "This is the child";
    n = 5;
    exit_code = 37;
    break;
  default:
    message = "This is the prrent";
    n = 3;
    exit_code = 0;
    break;
  }
  if (pid) {
    int stat_val;
    pid_t child_pid;
    child_pid = wait(&stat_val);
    printf("Child process 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");
    }
  }
  for (; n > 0; n--) {
    puts(message);
    sleep(1);
  }
  exit(exit_code);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x25(%rbp)
call   1040 <printf@plt>
mov    -0x25(%rbp),%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0xffffffff,%eax
je     11df <main+0x4f>
jmp    11cf <main+0x3f>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     11f5 <main+0x65>
jmp    1213 <main+0x83>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe27(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
movl   $0x25,-0x18(%rbp)
jmp    122c <main+0x9c>
lea    0xe1b(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1295 <main+0x105>
lea    -0x1c(%rbp),%rdi
call   10a0 <wait@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1282 <main+0xf2>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1290 <main+0x100>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1295 <main+0x105>
jmp    129a <main+0x10a>
cmpl   $0x0,-0x14(%rbp)
jle    12c5 <main+0x135>
mov    -0x10(%rbp),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    129a <main+0x10a>
mov    -0x18(%rbp),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xed1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11ca <main+0x3a>
cmp    $0xffffffff,%eax
jne    11de <main+0x4e>
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x5,%ebp
mov    $0x25,%r14d
lea    0xe30(%rip),%rbx
jmp    123c <main+0xac>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
lea    0xe42(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
jne    1214 <main+0x84>
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
jmp    122d <main+0x9d>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xe31(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3,%ebp
xor    %r14d,%r14d
lea    0xde2(%rip),%rbx
add    $0x1,%ebp
nop
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     1240 <main+0xb0>
mov    %r14d,%edi
call   1080 <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 prrent\00"
@.str.4 = [36 x i8] c"Child process has finished: PID=%d\0A\00"
@.str.5 = [27 x i8] c"Child exited with code %d\0A\00"
@.str.6 = [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 %15 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; 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
store i32 37, i32* %5
br label %16
15: ; preds = %0
store i8* getelementptr inbounds ([19 x i8], [19 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
%17 = load i32, i32* %2
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %16
%20 = call i32 @wait(i32* %6)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %6
%24 = call i64 @WIFEXITED(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = load i32, i32* %6
%28 = call i32 @WEXITSTATUS(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %28)
br label %32
30: ; preds = %19
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
br label %33
33: ; preds = %32, %16
br label %34
34: ; preds = %41, %33
%35 = load i32, i32* %4
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %34
%38 = load i8*, i8** %3
%39 = call i32 @puts(i8* %38)
%40 = call i32 @sleep(i32 1)
br label %41
41: ; preds = %37
%42 = load i32, i32* %4
%43 = add nsw i32 %42, -1
store i32 %43, i32* %4
br label %34
44: ; preds = %34
%45 = load i32, i32* %5
%46 = call i32 @exit(i32 %45)
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 prrent\00"
@.str.4 = [36 x i8] c"Child process has finished: PID=%d\0A\00"
@.str.5 = [27 x i8] c"Child exited with code %d\0A\00"
@str = [22 x i8] c"fork program starting\00"
@str.7 = [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 %21
]
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
%8 = bitcast i32* %1 to i8*
%9 = call i32 @wait(i32* %1)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %1
%12 = call i64 @WIFEXITED(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %7
%15 = load i32, i32* %1
%16 = call i32 @WEXITSTATUS(i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %16)
br label %20
18: ; preds = %7
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
br label %21
21: ; preds = %0, %20
%22 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), %20 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), %0 ]
%23 = phi i32 [ 0, %20 ], [ 37, %0 ]
%24 = phi i32 [ 3, %20 ], [ 5, %0 ]
br label %25
25: ; preds = %21, %25
%26 = phi i32 [ %24, %21 ], [ %29, %25 ]
%27 = call i32 @puts(i8* dereferenceable(1) %22)
%28 = call i32 @sleep(i32 1)
%29 = add nsw i32 %26, -1
%30 = icmp ugt i32 %26, 1
br i1 %30, label %25, label %31
31: ; preds = %25
%32 = call i32 @exit(i32 %23)
unreachable
}