#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, scalar_t__, ...);
int main(int argc, char **argv) {
  pid_t child_id;
  pid_t my_id;
  my_id = getpid();
  printf("pid: %d -- I am the parent about to call fork\n", (int)my_id);
  child_id = fork();
  if (child_id != 0) {
    my_id = getpid();
    printf("pid: %d -- I just forked a child with id %d\n", (int)my_id,
           (int)child_id);
  } else {
    my_id = getpid();
    printf("pid: %d -- I am the child\n", my_id);
  }
  printf("pid: %d -- I am exiting\n", my_id);
  exit(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    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq %eax,%rsi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b7 <main+0x77>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq %eax,%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d4 <main+0x94>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
movslq %eax,%rsi
lea    0xeab(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r14
test   %rbx,%rbx
jne    1188 <main+0x48>
lea    0xee0(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    119b <main+0x5b>
movslq %r14d,%rsi
lea    0xe9d(%rip),%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed5(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [47 x i8] c"pid: %d -- I am the parent about to call fork\0A\00"
@.str.1 = [45 x i8] c"pid: %d -- I just forked a child with id %d\0A\00"
@.str.2 = [27 x i8] c"pid: %d -- I am the child\0A\00"
@.str.3 = [25 x i8] c"pid: %d -- I am exiting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @getpid()
store i64 %8, i64* %7
%9 = load i64, i64* %7
%10 = trunc i64 %9 to i32
%11 = sext i32 %10 to i64
%12 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i64 %11)
%13 = call i64 (...) @fork()
store i64 %13, i64* %6
%14 = load i64, i64* %6
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %2
%17 = call i64 (...) @getpid()
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = trunc i64 %18 to i32
%20 = sext i32 %19 to i64
%21 = load i64, i64* %6
%22 = trunc i64 %21 to i32
%23 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %20, i32 %22)
br label %28
24: ; preds = %2
%25 = call i64 (...) @getpid()
store i64 %25, i64* %7
%26 = load i64, i64* %7
%27 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i64 %26)
br label %28
28: ; preds = %24, %16
%29 = load i64, i64* %7
%30 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %29)
%31 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [47 x i8] c"pid: %d -- I am the parent about to call fork\0A\00"
@.str.1 = [45 x i8] c"pid: %d -- I just forked a child with id %d\0A\00"
@.str.2 = [27 x i8] c"pid: %d -- I am the child\0A\00"
@.str.3 = [25 x i8] c"pid: %d -- I am exiting\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @getpid()
%4 = shl i64 %3, 32
%5 = ashr exact i64 %4, 32
%6 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i64 %5)
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
%9 = tail call i64 (...) @getpid()
br i1 %8, label %15, label %10
10: ; preds = %2
%11 = shl i64 %9, 32
%12 = ashr exact i64 %11, 32
%13 = trunc i64 %7 to i32
%14 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %12, i32 %13)
br label %17
15: ; preds = %2
%16 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i64 %9)
br label %17
17: ; preds = %15, %10
%18 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %9)
%19 = tail call i32 @exit(i32 0)
unreachable
}