#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 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());
  } 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   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a2 <main+0x42>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1206 <main+0xa6>
cmpl   $0x0,-0x14(%rbp)
jne    11d6 <main+0x76>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
jmp    11f0 <main+0x90>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
movslq -0x14(%rbp),%rdx
lea    0xe4d(%rip),%rdi
call   1040 <printf@plt>
jmp    11f5 <main+0x95>
mov    $0x1,%edi
call   1070 <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   1080 <fork@plt>
test   %eax,%eax
je     1189 <main+0x29>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11ba <main+0x5a>
xor    %eax,%eax
call   1030 <getpid@plt>
movslq %ebx,%rdx
lea    0xeb4(%rip),%rdi
mov    %rax,%rsi
jmp    11a7 <main+0x47>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe70(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe3f(%rip),%rdi
call   1050 <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 = [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 %27
12: ; preds = %2
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
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)
br label %24
19: ; preds = %12
%20 = call i64 (...) @getpid()
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %20, i64 %22)
br label %24
24: ; preds = %19, %15
br label %25
25: ; preds = %24
%26 = call i32 @sleep(i32 1)
store i32 0, i32* %3
br label %27
27: ; preds = %25, %10
%28 = load i32, i32* %3
ret i32 %28
}

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 = [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 %10 [
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 %16
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)
br label %14
10: ; preds = %2
%11 = tail call i64 (...) @getpid()
%12 = sext i32 %3 to i64
%13 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %11, i64 %12)
br label %14
14: ; preds = %6, %10
%15 = tail call i32 @sleep(i32 1)
br label %16
16: ; preds = %14, %4
%17 = phi i32 [ 1, %4 ], [ 0, %14 ]
ret i32 %17
}