#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int main(void) {
  pid_t pid;
  char *message;
  int n;
  pid = fork();
  if (pid < 0) {
    perror("fork failed");
    exit(1);
  }
  if (pid == 0) {
    message = "This is the child\n";
    n = 6;
  } else {
    message = "This is the parent\n";
    n = 3;
  }
  for (; n > 0; n--) {
    printf(message);
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    118b <main+0x3b>
lea    0xe84(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11ad <main+0x5d>
lea    0xe6f(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x6,-0x1c(%rbp)
jmp    11bf <main+0x6f>
lea    0xe6b(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x3,-0x1c(%rbp)
jmp    11c4 <main+0x74>
cmpl   $0x0,-0x1c(%rbp)
jle    11ef <main+0x9f>
mov    -0x18(%rbp),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c4 <main+0x74>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11a3 <main+0x53>
lea    0xea6(%rip),%rax
lea    0xeb2(%rip),%rbx
cmove  %rax,%rbx
sete   %al
movzbl %al,%eax
lea    (%rax,%rax,2),%ebp
or     $0x4,%ebp
nopl   (%rax)
mov    %rbx,%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     1180 <main+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe56(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [19 x i8] c"This is the child\0A\00"
@.str.2 = [20 x i8] c"This is the parent\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i64, i64* %2
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i32 6, i32* %4
br label %16
15: ; preds = %11
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8** %3
store i32 3, i32* %4
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 @printf(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
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [19 x i8] c"This is the child\0A\00"
@.str.2 = [20 x i8] c"This is the parent\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
%8 = select i1 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0)
%9 = select i1 %7, i32 6, i32 3
br label %10
10: ; preds = %6, %10
%11 = phi i32 [ %9, %6 ], [ %14, %10 ]
%12 = tail call i32 @printf(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
ret i32 0
}