#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 */

/* Variables and functions */
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int *);
int main() {
  printf("Hello from process: %d with parent: %d\n", getpid(), getppid());
  int pid = fork();
  if (pid == 0) {
    printf("Hello world from child process!\n");
    int i;
    for (i = 0; i < (1 << 30); i++)
      ;
    for (i = 0; i < (1 << 30); i++)
      ;
    for (i = 0; i < (1 << 30); i++)
      ;
    for (i = 0; i < (1 << 30); i++)
      ;
    for (i = 0; i < (1 << 30); i++)
      ;
    printf("Bye from child!\n");
    perror("execl()");
  } else if (pid > 0) {
    printf("Hello form parent process!\n");
    int status;
    printf("Waiting for child...\n");
    wait(&status);
    printf("Child finished: status = %d\n", status);
  } else {
    printf("fork();\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1297 <main+0x137>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x40000000,-0xc(%rbp)
jge    11dc <main+0x7c>
jmp    11ce <main+0x6e>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11bc <main+0x5c>
movl   $0x0,-0xc(%rbp)
cmpl   $0x40000000,-0xc(%rbp)
jge    1203 <main+0xa3>
jmp    11f5 <main+0x95>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11e3 <main+0x83>
movl   $0x0,-0xc(%rbp)
cmpl   $0x40000000,-0xc(%rbp)
jge    122a <main+0xca>
jmp    121c <main+0xbc>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    120a <main+0xaa>
movl   $0x0,-0xc(%rbp)
cmpl   $0x40000000,-0xc(%rbp)
jge    1251 <main+0xf1>
jmp    1243 <main+0xe3>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1231 <main+0xd1>
movl   $0x0,-0xc(%rbp)
cmpl   $0x40000000,-0xc(%rbp)
jge    1278 <main+0x118>
jmp    126a <main+0x10a>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1258 <main+0xf8>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdcd(%rip),%rdi
call   1050 <perror@plt>
jmp    12ef <main+0x18f>
cmpl   $0x0,-0x8(%rbp)
jle    12dc <main+0x17c>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x10(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x10(%rbp),%esi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ea <main+0x18a>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ef <main+0x18f>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe74(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11ee <main+0x7e>
jle    11da <main+0x6a>
lea    0xeaa(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb9(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
mov    0xc(%rsp),%esi
lea    0xe5f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11e6 <main+0x76>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe90(%rip),%rdi
call   1030 <puts@plt>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
lea    0xe1b(%rip),%rdi
call   1060 <perror@plt>
jmp    11e6 <main+0x76>

LLVM IR (-O0):
@.str = [40 x i8] c"Hello from process: %d with parent: %d\0A\00"
@.str.1 = [33 x i8] c"Hello world from child process!\0A\00"
@.str.2 = [17 x i8] c"Bye from child!\0A\00"
@.str.3 = [8 x i8] c"execl()\00"
@.str.4 = [28 x i8] c"Hello form parent process!\0A\00"
@.str.5 = [22 x i8] c"Waiting for child...\0A\00"
@.str.6 = [29 x i8] c"Child finished: status = %d\0A\00"
@.str.7 = [9 x i8] c"fork();\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @getpid()
%6 = call i32 (...) @getppid()
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = call i32 (...) @fork()
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %55
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %13
13: ; preds = %17, %11
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 1073741824
br i1 %15, label %16, label %20
16: ; preds = %13
br label %17
17: ; preds = %16
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
store i32 %19, i32* %3
br label %13
20: ; preds = %13
store i32 0, i32* %3
br label %21
21: ; preds = %25, %20
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 1073741824
br i1 %23, label %24, label %28
24: ; preds = %21
br label %25
25: ; preds = %24
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
store i32 %27, i32* %3
br label %21
28: ; preds = %21
store i32 0, i32* %3
br label %29
29: ; preds = %33, %28
%30 = load i32, i32* %3
%31 = icmp slt i32 %30, 1073741824
br i1 %31, label %32, label %36
32: ; preds = %29
br label %33
33: ; preds = %32
%34 = load i32, i32* %3
%35 = add nsw i32 %34, 1
store i32 %35, i32* %3
br label %29
36: ; preds = %29
store i32 0, i32* %3
br label %37
37: ; preds = %41, %36
%38 = load i32, i32* %3
%39 = icmp slt i32 %38, 1073741824
br i1 %39, label %40, label %44
40: ; preds = %37
br label %41
41: ; preds = %40
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 1
store i32 %43, i32* %3
br label %37
44: ; preds = %37
store i32 0, i32* %3
br label %45
45: ; preds = %49, %44
%46 = load i32, i32* %3
%47 = icmp slt i32 %46, 1073741824
br i1 %47, label %48, label %52
48: ; preds = %45
br label %49
49: ; preds = %48
%50 = load i32, i32* %3
%51 = add nsw i32 %50, 1
store i32 %51, i32* %3
br label %45
52: ; preds = %45
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%54 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %67
55: ; preds = %0
%56 = load i32, i32* %2
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %55
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%61 = call i32 @wait(i32* %4)
%62 = load i32, i32* %4
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %62)
br label %66
64: ; preds = %55
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
br label %66
66: ; preds = %64, %58
br label %67
67: ; preds = %66, %52
ret i32 0
}

LLVM IR (-O2):
@.str = [40 x i8] c"Hello from process: %d with parent: %d\0A\00"
@.str.3 = [8 x i8] c"execl()\00"
@.str.6 = [29 x i8] c"Child finished: status = %d\0A\00"
@str = [8 x i8] c"fork();\00"
@str.8 = [27 x i8] c"Hello form parent process!\00"
@str.9 = [21 x i8] c"Waiting for child...\00"
@str.10 = [32 x i8] c"Hello world from child process!\00"
@str.11 = [16 x i8] c"Bye from child!\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @getpid()
%3 = tail call i32 (...) @getppid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3)
%5 = tail call i32 (...) @fork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.10, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.11, i64 0, i64 0))
%10 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %22
11: ; preds = %0
%12 = icmp sgt i32 %5, 0
br i1 %12, label %13, label %20
13: ; preds = %11
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
%15 = bitcast i32* %1 to i8*
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
%17 = call i32 @wait(i32* %1)
%18 = load i32, i32* %1
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %18)
br label %22
20: ; preds = %11
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %13, %20, %7
ret i32 0
}