#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 fork();
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long, long);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  pid_t pid;
  pid = fork();
  if (pid == -1) {
    perror("failed to fork()");
    return 1;
  } else if (pid == 0) {
    printf("PID %ld: child started, parent is %ld\n", (long)getpid(),
           (long)getppid());
    system("ps f");
  } else {
    printf("PID %ld: started child PID %ld\n", (long)getpid(), (long)pid);
  }
  sleep(1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b2 <main+0x42>
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1222 <main+0xb2>
cmpl   $0x0,-0x14(%rbp)
jne    11f2 <main+0x82>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
lea    0xe50(%rip),%rdi
call   1040 <system@plt>
jmp    120c <main+0x9c>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
movslq -0x14(%rbp),%rdx
lea    0xe36(%rip),%rdi
call   1050 <printf@plt>
jmp    1211 <main+0xa1>
mov    $0x1,%edi
call   1080 <sleep@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     119e <main+0x2e>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11db <main+0x6b>
xor    %eax,%eax
call   1030 <getpid@plt>
movslq %ebx,%rdx
lea    0xea9(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
jmp    11cd <main+0x5d>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe5b(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
lea    0xe70(%rip),%rdi
call   1040 <system@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe1e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"failed to fork()\00"
@.str.1 = [39 x i8] c"PID %ld: child started, parent is %ld\0A\00"
@.str.2 = [5 x i8] c"ps f\00"
@.str.3 = [32 x i8] c"PID %ld: started child PID %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %28
12: ; preds = %2
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = call i64 (...) @getpid()
%17 = call i64 (...) @getppid()
%18 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %16, i64 %17)
%19 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %25
20: ; preds = %12
%21 = call i64 (...) @getpid()
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %21, i64 %23)
br label %25
25: ; preds = %20, %15
br label %26
26: ; preds = %25
%27 = call i32 @sleep(i32 1)
store i32 0, i32* %3
br label %28
28: ; preds = %26, %10
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [17 x i8] c"failed to fork()\00"
@.str.1 = [39 x i8] c"PID %ld: child started, parent is %ld\0A\00"
@.str.2 = [5 x i8] c"ps f\00"
@.str.3 = [32 x i8] c"PID %ld: started child PID %ld\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %11 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %2
%5 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %17
6: ; preds = %2
%7 = tail call i64 (...) @getpid()
%8 = tail call i64 (...) @getppid()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %7, i64 %8)
%10 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %15
11: ; preds = %2
%12 = tail call i64 (...) @getpid()
%13 = sext i32 %3 to i64
%14 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %12, i64 %13)
br label %15
15: ; preds = %6, %11
%16 = tail call i32 @sleep(i32 1)
br label %17
17: ; preds = %15, %4
%18 = phi i32 [ 1, %4 ], [ 0, %15 ]
ret i32 %18
}