#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*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  int id;
  id = fork();
  if (id != 0) {
    printf("+Start parent work\n");
    wait(NULL);
    printf("+Finish parent work\n");
    return 0;
  } else {
    printf("+Start child work\n");
    printf("PID: %d\n", getpid());
    printf("PPID: %d\n", getppid());
    sleep(3);
    printf("+Finish child work\n");
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11b4 <main+0x54>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    120f <main+0xaf>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   1060 <sleep@plt>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1198 <main+0x28>
lea    0xeac(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <wait@plt>
lea    0xeac(%rip),%rdi
jmp    11e3 <main+0x73>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe4f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe37(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   1070 <sleep@plt>
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [20 x i8] c"+Start parent work\0A\00"
@.str.1 = [21 x i8] c"+Finish parent work\0A\00"
@.str.2 = [19 x i8] c"+Start child work\0A\00"
@.str.3 = [9 x i8] c"PID: %d\0A\00"
@.str.4 = [10 x i8] c"PPID: %d\0A\00"
@.str.5 = [20 x i8] c"+Finish child work\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (...) @fork()
store i32 %3, i32* %2
%4 = load i32, i32* %2
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @wait(i32* null)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %18
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 (...) @getpid()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = call i32 (...) @getppid()
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%16 = call i32 @sleep(i32 3)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %18
18: ; preds = %10, %6
%19 = load i32, i32* %1
ret i32 %19
}

LLVM IR (-O2):
@.str.3 = [9 x i8] c"PID: %d\0A\00"
@.str.4 = [10 x i8] c"PPID: %d\0A\00"
@str = [18 x i8] c"+Start child work\00"
@str.6 = [19 x i8] c"+Finish child work\00"
@str.7 = [19 x i8] c"+Start parent work\00"
@str.8 = [20 x i8] c"+Finish parent work\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, 0
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.7, i64 0, i64 0))
%5 = tail call i32 @wait(i32* null)
br label %13
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%10 = tail call i32 (...) @getppid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %10)
%12 = tail call i32 @sleep(i32 3)
br label %13
13: ; preds = %6, %3
%14 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0), %6 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0), %3 ]
%15 = tail call i32 @puts(i8* dereferenceable(1) %14)
ret i32 0
}