#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*/ sleep(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());
    sleep(10);
  } 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    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d0 <main+0x60>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1200 <main+0x90>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
jmp    1235 <main+0xc5>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <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    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123a <main+0xca>
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    0xe7f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     11e9 <main+0x79>
je     11bf <main+0x4f>
mov    %eax,%ebx
xor    %edi,%edi
call   1080 <wait@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8f(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11e0 <main+0x70>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe56(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%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 %26
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)
%25 = call i32 @sleep(i32 10)
br label %33
26: ; preds = %18
%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 ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %29, i32 %31)
br label %33
33: ; preds = %26, %21
br label %34
34: ; preds = %33
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 %19
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)
%18 = tail call i32 @sleep(i32 10)
br label %24
19: ; preds = %12
%20 = tail call i32 @wait(i32* null)
%21 = tail call i64 (...) @getpid()
%22 = trunc i64 %21 to i32
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %6, i32 %20, i32 %22)
br label %24
24: ; preds = %14, %19
ret i32 0
}