#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 /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(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 = 3;
    break;
  }
  for (; n > 0; n--) {
    puts(message);
    sleep(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11a9 <main+0x49>
jmp    1199 <main+0x39>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11bf <main+0x5f>
jmp    11d6 <main+0x76>
lea    0xe67(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe5d(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
jmp    11e8 <main+0x88>
lea    0xe58(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
jmp    11ed <main+0x8d>
cmpl   $0x0,-0x14(%rbp)
jle    1218 <main+0xb8>
mov    -0x10(%rbp),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11ed <main+0x8d>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xed7(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     1185 <main+0x35>
cmp    $0xffffffff,%eax
jne    1193 <main+0x43>
lea    0xe8a(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    $0x5,%ebp
lea    0xe7b(%rip),%rbx
jmp    119f <main+0x4f>
mov    $0x3,%ebp
lea    0xe7f(%rip),%rbx
add    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     11b0 <main+0x60>
xor    %edi,%edi
call   1050 <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"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (...) @fork()
store i32 %6, i32* %2
%7 = load i32, i32* %2
switch i32 %7, label %12 [
i32 -1, label %8
i32 0, label %11
]
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; 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 %13
12: ; 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
br label %13
13: ; preds = %12, %11
br label %14
14: ; preds = %21, %13
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = call i32 @puts(i8* %18)
%20 = call i32 @sleep(i32 1)
br label %21
21: ; preds = %17
%22 = load i32, i32* %4
%23 = add nsw i32 %22, -1
store i32 %23, i32* %4
br label %14
24: ; preds = %14
%25 = 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 = [22 x i8] c"fork program starting\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @fork()
switch i32 %2, label %6 [
i32 -1, label %3
i32 0, label %7
]
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
br label %7
7: ; preds = %0, %6
%8 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), %6 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), %0 ]
%9 = phi i32 [ 3, %6 ], [ 5, %0 ]
br label %10
10: ; preds = %7, %10
%11 = phi i32 [ %9, %7 ], [ %14, %10 ]
%12 = tail call i32 @puts(i8* dereferenceable(1) %8)
%13 = tail call i32 @sleep(i32 1)
%14 = add nsw i32 %11, -1
%15 = icmp ugt i32 %11, 1
br i1 %15, label %10, label %16
16: ; preds = %10
%17 = tail call i32 @exit(i32 0)
unreachable
}