#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 /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  printf("[%d] hello world\n", getpid());
  int rv = fork();
  if (rv > 0) {
    wait(NULL);
    printf("[%d] *parent*\n", getpid());
  } else if (rv == 0) {
    sleep(2);
    printf("[%d] *child*\n", getpid());
  } else {
    printf("[%d] *????*\n", getpid());
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe91(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11ab <main+0x5b>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe71(%rip),%rdi
call   1040 <printf@plt>
jmp    11f3 <main+0xa3>
cmpl   $0x0,-0x8(%rbp)
jne    11d9 <main+0x89>
mov    $0x2,%edi
call   1050 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
jmp    11ee <main+0x9e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe46(%rip),%rdi
call   1040 <printf@plt>
jmp    11f3 <main+0xa3>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea1(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
jle    1181 <main+0x31>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xe93(%rip),%rbx
jmp    119d <main+0x4d>
je     118c <main+0x3c>
lea    0xea5(%rip),%rbx
jmp    119d <main+0x4d>
mov    $0x2,%edi
call   1050 <sleep@plt>
lea    0xe84(%rip),%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

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

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