#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 /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ stderr;
int wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  printf("hello world pid:%d\n", (int)getpid());
  int rc = fork();
  if (rc < 0) {
    fprintf(stderr, "fork failed return code:%d\n", rc);
    exit(1);
  } else if (rc == 0) {
    printf("I am child pid:%d\n", (int)getpid());
  } else {
    int wc = wait(NULL);
    printf("I am parent of %d wc:%d pid:%d\n", rc, wc, (int)getpid());
  }
  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   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c3 <main+0x63>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11e9 <main+0x89>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121e <main+0xbe>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1223 <main+0xc3>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11cf <main+0x6f>
je     11af <main+0x4f>
xor    %edi,%edi
call   1070 <wait@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea3(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c6 <main+0x66>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe73(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
mov    %ebx,%edx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"hello world pid:%d\0A\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"fork failed return code:%d\0A\00"
@.str.2 = [19 x i8] c"I am child pid:%d\0A\00"
@.str.3 = [32 x i8] c"I am parent of %d wc:%d pid:%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @getpid()
%9 = trunc i64 %8 to i32
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 (...) @fork()
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* %6
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = call i64 (...) @getpid()
%24 = trunc i64 %23 to i32
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %33
26: ; preds = %19
%27 = call i32 @wait(i32* null)
store i32 %27, i32* %7
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = call i64 (...) @getpid()
%31 = trunc i64 %30 to i32
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %29, i32 %31)
br label %33
33: ; preds = %26, %22
br label %34
34: ; preds = %33
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"hello world pid:%d\0A\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"fork failed return code:%d\0A\00"
@.str.2 = [19 x i8] c"I am child pid:%d\0A\00"
@.str.3 = [32 x i8] c"I am parent of %d wc:%d pid:%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @getpid()
%4 = trunc i64 %3 to i32
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 (...) @fork()
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = icmp eq i32 %6, 0
br i1 %13, label %14, label %18
14: ; preds = %12
%15 = tail call i64 (...) @getpid()
%16 = trunc i64 %15 to i32
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %23
18: ; preds = %12
%19 = tail call i32 @wait(i32* null)
%20 = tail call i64 (...) @getpid()
%21 = trunc i64 %20 to i32
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %6, i32 %19, i32 %21)
br label %23
23: ; preds = %14, %18
ret i32 0
}