#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*/ scanf(char *, int *);
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 = "\tThis is the child";
    n = 5;
    break;
  default:
    message = "This is the parent";
    printf("Enter number of repetitions for the message display:");
    scanf("%d", &n);
    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    0xe7a(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11b9 <main+0x49>
jmp    11a9 <main+0x39>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11cf <main+0x5f>
jmp    11e6 <main+0x76>
lea    0xe58(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe4e(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
jmp    120d <main+0x9d>
lea    0xe4a(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
lea    0xe7b(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1050 <scanf@plt>
jmp    1212 <main+0xa2>
cmpl   $0x0,-0x14(%rbp)
jle    123d <main+0xcd>
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    1212 <main+0xa2>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xeee(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11a7 <main+0x37>
cmp    $0xffffffff,%eax
jne    11b8 <main+0x48>
lea    0xe68(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x5,0xc(%rsp)
lea    0xe56(%rip),%rbx
jmp    11f0 <main+0x80>
lea    0xe73(%rip),%rdi
call   1040 <printf@plt>
lea    0xe9c(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
jle    1212 <main+0xa2>
lea    0xe3c(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    0xc(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0xc(%rsp)
cmp    $0x1,%eax
jg     11f0 <main+0x80>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c" fork program starting\0A\00"
@.str.1 = [12 x i8] c"fork failed\00"
@.str.2 = [19 x i8] c"\09This is the child\00"
@.str.3 = [19 x i8] c"This is the parent\00"
@.str.4 = [53 x i8] c"Enter number of repetitions for the message display:\00"
@.str.5 = [3 x i8] c"%d\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 ([24 x i8], [24 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 ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %3
store i32 5, i32* %4
br label %15
12: ; preds = %0
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8** %3
%13 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0))
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %4)
br label %15
15: ; preds = %12, %11
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 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"fork failed\00"
@.str.2 = [19 x i8] c"\09This is the child\00"
@.str.3 = [19 x i8] c"This is the parent\00"
@.str.4 = [53 x i8] c"Enter number of repetitions for the message display:\00"
@.str.5 = [3 x i8] c"%d\00"
@str = [23 x i8] c" fork program starting\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @fork()
switch i32 %4, label %9 [
i32 -1, label %5
i32 0, label %8
]
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
store i32 5, i32* %1
br label %14
9: ; preds = %0
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %1)
%12 = load i32, i32* %1
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %8, %9
%15 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), %9 ]
br label %16
16: ; preds = %14, %16
%17 = call i32 @puts(i8* dereferenceable(1) %15)
%18 = call i32 @sleep(i32 1)
%19 = load i32, i32* %1
%20 = add nsw i32 %19, -1
store i32 %20, i32* %1
%21 = icmp sgt i32 %19, 1
br i1 %21, label %16, label %22
22: ; preds = %16, %9
%23 = call i32 @exit(i32 0)
unreachable
}