#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 stating\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 parent";
    n = 3;
    exit_code = 0;
    break;
  }
  for (; n > 0; n--) {
    puts(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 terminated abnormally\n");
    }
  }
  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    0xe30(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe26(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
movl   $0x25,-0x18(%rbp)
jmp    122c <main+0x9c>
lea    0xe1a(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
jmp    1231 <main+0xa1>
cmpl   $0x0,-0x14(%rbp)
jle    125c <main+0xcc>
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    1231 <main+0xa1>
cmpl   $0x0,-0x8(%rbp)
je     12c5 <main+0x135>
lea    -0x1c(%rbp),%rdi
call   10a0 <wait@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12b2 <main+0x122>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c0 <main+0x130>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c5 <main+0x135>
mov    -0x18(%rbp),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%r15d
test   %eax,%eax
je     11cd <main+0x3d>
cmp    $0xffffffff,%r15d
jne    11e1 <main+0x51>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe38(%rip),%rbx
mov    $0x25,%r14d
mov    $0x5,%ebp
jmp    11f0 <main+0x60>
lea    0xe36(%rip),%rbx
xor    %r14d,%r14d
mov    $0x3,%ebp
add    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     1200 <main+0x70>
test   %r15d,%r15d
je     1253 <main+0xc3>
lea    0x4(%rsp),%rdi
call   10a0 <wait@plt>
lea    0xe01(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
jne    125b <main+0xcb>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1080 <exit@plt>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
lea    0xde4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"fork program stating\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 = [30 x i8] c"child 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 ([22 x i8], [22 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
br label %17
17: ; preds = %24, %16
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i8*, i8** %3
%22 = call i32 @puts(i8* %21)
%23 = call i32 @sleep(i32 1)
br label %24
24: ; preds = %20
%25 = load i32, i32* %4
%26 = add nsw i32 %25, -1
store i32 %26, i32* %4
br label %17
27: ; preds = %17
%28 = load i32, i32* %2
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %27
%31 = call i32 @wait(i32* %6)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %6
%35 = call i64 @WIFEXITED(i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %30
%38 = load i32, i32* %6
%39 = call i32 @WEXITSTATUS(i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %39)
br label %43
41: ; preds = %30
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
br label %44
44: ; preds = %43, %27
%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 parent\00"
@.str.4 = [30 x i8] c"child has finished: PID = %d\0A\00"
@.str.5 = [27 x i8] c"child exited with code %d\0A\00"
@str = [21 x i8] c"fork program stating\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 ([21 x i8], [21 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 [ 3, %7 ], [ 5, %0 ]
%10 = phi i32 [ 0, %7 ], [ 37, %0 ]
%11 = 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 %12
12: ; preds = %8, %12
%13 = phi i32 [ %9, %8 ], [ %16, %12 ]
%14 = tail call i32 @puts(i8* dereferenceable(1) %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 ([30 x i8], [30 x i8]* @.str.4, 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.5, i64 0, i64 0), i32 %29)
br label %33
31: ; preds = %20
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, 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
}