#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*/ execlp(char *, char *, char *, ...);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int parent_pid = getpid();
  printf("parent PID %d\n", parent_pid);
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork Failed");
    exit(-1);
  } else if (pid == 0) {
    int kid_pid = getpid();
    printf("kid 1 PID %d\n", kid_pid);
    execlp("/bin/echo", "echo", "'hola'", NULL);
  }
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork Failed");
    exit(-1);
  } else if (pid == 0) {
    int kid_kid_pid = getpid();
    printf("kid 2 PID %d\n", kid_kid_pid);
    execlp("/bin/ls", "ls", NULL);
  }
  wait(NULL);
  printf("Child Complete\n");
  exit(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,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11cf <main+0x5f>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1215 <main+0xa5>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2d(%rip),%rdi
lea    0xe2b(%rip),%rsi
lea    0xe29(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   1070 <execlp@plt>
jmp    121a <main+0xaa>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    124f <main+0xdf>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    128e <main+0x11e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdcc(%rip),%rdi
lea    0xdca(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   1070 <execlp@plt>
jmp    1293 <main+0x123>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe71(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11e7 <main+0x67>
jne    11db <main+0x5b>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe67(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe65(%rip),%rdi
lea    0xe63(%rip),%rsi
lea    0xe61(%rip),%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1080 <execlp@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    1206 <main+0x86>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jne    1236 <main+0xb6>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe24(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe22(%rip),%rdi
lea    0xe20(%rip),%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1080 <execlp@plt>
xor    %edi,%edi
call   1090 <wait@plt>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"parent PID %d\0A\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c"Fork Failed\00"
@.str.2 = [14 x i8] c"kid 1 PID %d\0A\00"
@.str.3 = [10 x i8] c"/bin/echo\00"
@.str.4 = [5 x i8] c"echo\00"
@.str.5 = [7 x i8] c"'hola'\00"
@.str.6 = [14 x i8] c"kid 2 PID %d\0A\00"
@.str.7 = [8 x i8] c"/bin/ls\00"
@.str.8 = [3 x i8] c"ls\00"
@.str.9 = [16 x i8] c"Child Complete\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (...) @getpid()
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i64 (...) @fork()
store i64 %9, i64* %3
%10 = load i64, i64* %3
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = load i64, i64* %3
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = call i32 (...) @getpid()
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* null)
br label %24
24: ; preds = %19, %16
br label %25
25: ; preds = %24
%26 = call i64 (...) @fork()
store i64 %26, i64* %3
%27 = load i64, i64* %3
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %25
%34 = load i64, i64* %3
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = call i32 (...) @getpid()
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %38)
%40 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* null)
br label %41
41: ; preds = %36, %33
br label %42
42: ; preds = %41
%43 = call i32 @wait(i32* null)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%45 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [15 x i8] c"parent PID %d\0A\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c"Fork Failed\00"
@.str.2 = [14 x i8] c"kid 1 PID %d\0A\00"
@.str.3 = [10 x i8] c"/bin/echo\00"
@.str.4 = [5 x i8] c"echo\00"
@.str.5 = [7 x i8] c"'hola'\00"
@.str.6 = [14 x i8] c"kid 2 PID %d\0A\00"
@.str.7 = [8 x i8] c"/bin/ls\00"
@.str.8 = [3 x i8] c"ls\00"
@str = [15 x i8] c"Child Complete\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = icmp eq i64 %3, 0
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = tail call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* null)
br label %15
15: ; preds = %9, %11
%16 = tail call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 -1)
unreachable
22: ; preds = %15
%23 = icmp eq i64 %16, 0
br i1 %23, label %24, label %28
24: ; preds = %22
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %25)
%27 = tail call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* null)
br label %28
28: ; preds = %22, %24
%29 = tail call i32 @wait(i32* null)
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%31 = tail call i32 @exit(i32 0)
unreachable
}