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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x19(%rbp)
call   1050 <printf@plt>
mov    -0x19(%rbp),%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11cf <main+0x4f>
jmp    11bf <main+0x3f>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11e5 <main+0x65>
jmp    1220 <main+0xa0>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe37(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x5,-0x14(%rbp)
jmp    1256 <main+0xd6>
lea    0xe2a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x3,-0x14(%rbp)
jmp    125b <main+0xdb>
cmpl   $0x0,-0x14(%rbp)
jle    1286 <main+0x106>
mov    -0x10(%rbp),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    125b <main+0xdb>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xedd(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11b9 <main+0x39>
cmp    $0xffffffff,%eax
jne    11ce <main+0x4e>
lea    0xe56(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x5,%ebp
lea    0xe47(%rip),%rbx
lea    0xe52(%rip),%r14
jmp    11e1 <main+0x61>
mov    $0x3,%ebp
lea    0xe60(%rip),%rbx
lea    0xe6c(%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
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     1210 <main+0x90>
xor    %edi,%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"
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 5, 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 3, 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 @puts(i8* %24)
%26 = call i32 @sleep(i32 1)
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 = 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 = [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 = [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 [ 3, %6 ], [ 5, %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 @puts(i8* dereferenceable(1) %9)
%17 = tail call i32 @sleep(i32 1)
%18 = add nsw i32 %15, -1
%19 = icmp ugt i32 %15, 1
br i1 %19, label %14, label %20
20: ; preds = %14
%21 = tail call i32 @exit(i32 0)
unreachable
}