#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*/ sleep(int);
int main() {
  pid_t pid;
  char *message;
  int n;
  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;
    break;
  default:
    message = "This is the parent speaking\n";
    n = 3;
    break;
  }
  for (; n > 0; n--) {
    printf("#%d %s\n", n, message);
    sleep(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x19(%rbp)
call   1030 <printf@plt>
mov    -0x19(%rbp),%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     119f <main+0x4f>
jmp    118f <main+0x3f>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11b5 <main+0x65>
jmp    11cc <main+0x7c>
lea    0xe6e(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
lea    0xe67(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
jmp    11de <main+0x8e>
lea    0xe6c(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
jmp    11e3 <main+0x93>
cmpl   $0x0,-0x14(%rbp)
jle    121a <main+0xca>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11e3 <main+0x93>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xee4(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1197 <main+0x37>
cmp    $0xffffffff,%eax
jne    11a5 <main+0x45>
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0x5,%ebx
lea    0xe6c(%rip),%r14
jmp    11b1 <main+0x51>
mov    $0x3,%ebx
lea    0xe7a(%rip),%r14
lea    0xe90(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
ja     11c0 <main+0x60>
xor    %edi,%edi
call   1060 <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"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 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 ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; 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
br label %13
12: ; 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
br label %13
13: ; preds = %12, %11
br label %14
14: ; preds = %22, %13
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i32, i32* %4
%19 = load i8*, i8** %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %18, i8* %19)
%21 = call i32 @sleep(i32 1)
br label %22
22: ; preds = %17
%23 = load i32, i32* %4
%24 = add nsw i32 %23, -1
store i32 %24, i32* %4
br label %14
25: ; preds = %14
%26 = call i32 @exit(i32 0)
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 = [19 x i8] c"Fork program start\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 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 ([15 x i8], [15 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 ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), %6 ], [ getelementptr inbounds ([28 x i8], [28 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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %11, i8* %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
}