#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 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 {
    printf("I am parent of %d pid:%d\n", rc, (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    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b3 <main+0x63>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe70(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11d9 <main+0x89>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f9 <main+0xa9>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11fe <main+0xae>
xor    %eax,%eax
add    $0x20,%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
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11a8 <main+0x58>
xor    %eax,%eax
call   1030 <getpid@plt>
test   %ebx,%ebx
je     1194 <main+0x44>
lea    0xebc(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11a4 <main+0x54>
lea    0xe95(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%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 = [26 x i8] c"I am parent of %d pid:%d\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 i64 (...) @getpid()
%8 = trunc i64 %7 to i32
%9 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 (...) @fork()
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* %6
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = call i64 (...) @getpid()
%23 = trunc i64 %22 to i32
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %30
25: ; preds = %18
%26 = load i32, i32* %6
%27 = call i64 (...) @getpid()
%28 = trunc i64 %27 to i32
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %28)
br label %30
30: ; preds = %25, %21
br label %31
31: ; preds = %30
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 = [26 x i8] c"I am parent of %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
%14 = tail call i64 (...) @getpid()
%15 = trunc i64 %14 to i32
br i1 %13, label %16, label %18
16: ; preds = %12
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %15)
br label %20
18: ; preds = %12
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %6, i32 %15)
br label %20
20: ; preds = %16, %18
ret i32 0
}