#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_SUCCESS;
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ run_child();
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  pid_t pid;
  printf("original process, pid = %d\n", getpid());
  if ((pid = fork()) < 0) {
    err_sys("fork error");
  } else if (pid == 0) {
    run_child();
  }
  if (waitpid(pid, NULL, 0) != pid) {
    err_sys("waitpid error");
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe71(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11ba <main+0x4a>
lea    0xe6c(%rip),%rdi
call   1030 <err_sys@plt>
jmp    11d1 <main+0x61>
cmpq   $0x0,-0x10(%rbp)
jne    11cc <main+0x5c>
mov    $0x0,%al
call   1070 <run_child@plt>
jmp    11d1 <main+0x61>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    -0x10(%rbp),%rax
je     11f6 <main+0x86>
lea    0xe36(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe81(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11a3 <main+0x33>
lea    0xe80(%rip),%rdi
call   1030 <err_sys@plt>
jmp    11ac <main+0x3c>
jne    11ac <main+0x3c>
xor    %eax,%eax
call   1070 <run_child@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %rbx,%rax
je     11c9 <main+0x59>
lea    0xe63(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"original process, pid = %d\0A\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [14 x i8] c"waitpid error\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i32 (...) @getpid()
%4 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %15
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 (...) @run_child()
br label %14
14: ; preds = %12, %9
br label %15
15: ; preds = %14, %7
%16 = load i64, i64* %2
%17 = call i64 @waitpid(i64 %16, i32* null, i32 0)
%18 = load i64, i64* %2
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
}

LLVM IR (-O2):
@.str = [28 x i8] c"original process, pid = %d\0A\00"
@.str.1 = [11 x i8] c"fork error\00"
@.str.2 = [14 x i8] c"waitpid error\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %11
7: ; preds = %0
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 (...) @run_child()
br label %11
11: ; preds = %7, %9, %5
%12 = tail call i64 @waitpid(i64 %3, i32* null, i32 0)
%13 = icmp eq i64 %12, %3
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = load i32, i32* @EXIT_SUCCESS
%18 = tail call i32 @exit(i32 %17)
unreachable
}