#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*/ execl(char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  printf("Process %d, child of process %d started...\n", getpid(), getppid());
  pid_t pid = fork();
  if (pid < 0) {
    printf("Failed fork()\n");
    return 1;
  } else if (pid == 0) {
    printf("Process %d, child of process %d started...\n", getpid(), getppid());
    execl("/usr/bin/top", "top", NULL);
    printf("Process %d, child of process %d stopped...\n", getpid(), getppid());
  } else {
    wait(NULL);
  }
  printf("Process %d, child of process %d stopped...\n", getpid(), getppid());
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11ca <main+0x6a>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1272 <main+0x112>
cmpq   $0x0,-0x18(%rbp)
jne    1239 <main+0xd9>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe3b(%rip),%rdi
lea    0xe3d(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <execl@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1242 <main+0xe2>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
jmp    1247 <main+0xe7>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x28(%rbp),%esi
mov    %eax,%edx
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe76(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11ac <main+0x3c>
je     11bf <main+0x4f>
xor    %edi,%edi
call   1080 <wait@plt>
jmp    1218 <main+0xa8>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    123c <main+0xcc>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe2a(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe44(%rip),%rdi
lea    0xe46(%rip),%rsi
xor    %edx,%edx
call   1070 <execl@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe2c(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe08(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"Process %d, child of process %d started...\0A\00"
@.str.1 = [15 x i8] c"Failed fork()\0A\00"
@.str.2 = [13 x i8] c"/usr/bin/top\00"
@.str.3 = [4 x i8] c"top\00"
@.str.4 = [44 x i8] c"Process %d, child of process %d stopped...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @getpid()
%8 = call i32 (...) @getppid()
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = call i64 (...) @fork()
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %33
15: ; preds = %2
%16 = load i64, i64* %6
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getppid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %20)
%22 = call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* null)
%23 = call i32 (...) @getpid()
%24 = call i32 (...) @getppid()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %23, i32 %24)
br label %28
26: ; preds = %15
%27 = call i32 @wait(i32* null)
br label %28
28: ; preds = %26, %18
br label %29
29: ; preds = %28
%30 = call i32 (...) @getpid()
%31 = call i32 (...) @getppid()
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %30, i32 %31)
store i32 0, i32* %3
br label %33
33: ; preds = %29, %13
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [44 x i8] c"Process %d, child of process %d started...\0A\00"
@.str.2 = [13 x i8] c"/usr/bin/top\00"
@.str.3 = [4 x i8] c"top\00"
@.str.4 = [44 x i8] c"Process %d, child of process %d stopped...\0A\00"
@str = [14 x i8] c"Failed fork()\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (...) @getppid()
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%6 = tail call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %26
10: ; preds = %2
%11 = icmp eq i64 %6, 0
br i1 %11, label %12, label %20
12: ; preds = %10
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (...) @getppid()
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
%16 = tail call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* null)
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (...) @getppid()
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %17, i32 %18)
br label %22
20: ; preds = %10
%21 = tail call i32 @wait(i32* null)
br label %22
22: ; preds = %12, %20
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (...) @getppid()
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %23, i32 %24)
br label %26
26: ; preds = %22, %8
%27 = phi i32 [ 1, %8 ], [ 0, %22 ]
ret i32 %27
}