#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 *);
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\n");
    exit(1);
  } else if (rc == 0) {
    printf("hello, I am child (pid:%d)\n", (int)getpid());
  } else {
    int wc = wait(NULL);
    printf("hello, 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    11c0 <main+0x60>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11e6 <main+0x86>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121b <main+0xbb>
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    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1220 <main+0xc0>
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>
test   %eax,%eax
js     11cf <main+0x6f>
je     11af <main+0x4f>
mov    %eax,%ebx
xor    %edi,%edi
call   1070 <wait@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9f(%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    0xe66(%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    0xe37(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"hello world (pid:%d)\0A\00"
@stderr = global i32 0
@.str.1 = [13 x i8] c"fork failed\0A\00"
@.str.2 = [28 x i8] c"hello, I am child (pid:%d)\0A\00"
@.str.3 = [43 x i8] c"hello, 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 ([22 x i8], [22 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 %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%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 ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %32
25: ; preds = %18
%26 = call i32 @wait(i32* null)
store i32 %26, i32* %7
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = call i64 (...) @getpid()
%30 = trunc i64 %29 to i32
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %28, i32 %30)
br label %32
32: ; preds = %25, %21
br label %33
33: ; preds = %32
ret i32 0
}

LLVM IR (-O2):
@.str = [22 x i8] c"hello world (pid:%d)\0A\00"
@stderr = global i32 0
@.str.1 = [13 x i8] c"fork failed\0A\00"
@.str.2 = [28 x i8] c"hello, I am child (pid:%d)\0A\00"
@.str.3 = [43 x i8] c"hello, 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 ([22 x i8], [22 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 ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%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 ([28 x i8], [28 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 ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %6, i32 %19, i32 %21)
br label %23
23: ; preds = %14, %18
ret i32 0
}