#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 getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(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";
    printf("child PID:%d Child PPID:%d\n", getpid(), getppid());
    n = 3;
    break;
  default:
    message = "This is the parent";
    printf("Parent PID:%d Parent PPID:%d\n", getpid(), getppid());
    n = 5;
    break;
  }
  for (; n > 0; n--) {
    printf("\n %s", message);
    sleep(3);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x19(%rbp)
call   1040 <printf@plt>
mov    -0x19(%rbp),%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11bf <main+0x4f>
jmp    11af <main+0x3f>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11d5 <main+0x65>
jmp    1210 <main+0xa0>
lea    0xe51(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe47(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x3,-0x14(%rbp)
jmp    1246 <main+0xd6>
lea    0xe3a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x5,-0x14(%rbp)
jmp    124b <main+0xdb>
cmpl   $0x0,-0x14(%rbp)
jle    127f <main+0x10f>
mov    -0x10(%rbp),%rsi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   1080 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    124b <main+0xdb>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11bc <main+0x3c>
cmp    $0xffffffff,%eax
je     122c <main+0xac>
mov    $0x5,%ebp
lea    0xe87(%rip),%rbx
lea    0xe93(%rip),%r14
jmp    11cf <main+0x4f>
mov    $0x3,%ebp
lea    0xe44(%rip),%rbx
lea    0xe4f(%rip),%r14
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %r14,%rdi
mov    %r15d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
lea    0xe72(%rip),%r14
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     1200 <main+0x80>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcd(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <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 = [28 x i8] c"child PID:%d Child PPID:%d\0A\00"
@.str.4 = [19 x i8] c"This is the parent\00"
@.str.5 = [30 x i8] c"Parent PID:%d Parent PPID:%d\0A\00"
@.str.6 = [5 x i8] c"\0A %s\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 ([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 %15 [
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
%12 = call i32 (...) @getpid()
%13 = call i32 (...) @getppid()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %12, i32 %13)
store i32 3, i32* %4
br label %19
15: ; preds = %0
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8** %3
%16 = call i32 (...) @getpid()
%17 = call i32 (...) @getppid()
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %16, i32 %17)
store i32 5, i32* %4
br label %19
19: ; preds = %15, %11
br label %20
20: ; preds = %27, %19
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i8*, i8** %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %24)
%26 = call i32 @sleep(i32 3)
br label %27
27: ; preds = %23
%28 = load i32, i32* %4
%29 = add nsw i32 %28, -1
store i32 %29, i32* %4
br label %20
30: ; preds = %20
%31 = load i32, i32* %1
ret i32 %31
}

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 = [28 x i8] c"child PID:%d Child PPID:%d\0A\00"
@.str.4 = [19 x i8] c"This is the parent\00"
@.str.5 = [30 x i8] c"Parent PID:%d Parent PPID:%d\0A\00"
@.str.6 = [5 x i8] c"\0A %s\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 ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), %6 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), %0 ]
%9 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), %6 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), %0 ]
%10 = phi i32 [ 5, %6 ], [ 3, %0 ]
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (...) @getppid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %8, i32 %11, i32 %12)
br label %14
14: ; preds = %7, %14
%15 = phi i32 [ %10, %7 ], [ %18, %14 ]
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %9)
%17 = tail call i32 @sleep(i32 3)
%18 = add nsw i32 %15, -1
%19 = icmp ugt i32 %15, 1
br i1 %19, label %14, label %20
20: ; preds = %14
ret i32 0
}