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


x86 assembly (clang, -O0):
<run_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
lea    0xe5b(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jge    11d0 <run_child+0x50>
lea    0xe53(%rip),%rdi
call   1030 <err_sys@plt>
jmp    11e7 <run_child+0x67>
cmpq   $0x0,-0x8(%rbp)
jne    11e2 <run_child+0x62>
mov    $0x0,%al
call   1090 <run_grandchild@plt>
jmp    11e7 <run_child+0x67>
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    -0x8(%rbp),%rax
je     120c <run_child+0x8c>
lea    0xe1d(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<run_child>:
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe68(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11be <run_child+0x3e>
lea    0xe62(%rip),%rdi
call   1030 <err_sys@plt>
jmp    11c7 <run_child+0x47>
jne    11c7 <run_child+0x47>
xor    %eax,%eax
call   1090 <run_grandchild@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %rbx,%rax
je     11e4 <run_child+0x64>
lea    0xe45(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"child = %d, parent = %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 void @run_child() {
%1 = alloca i64
%2 = call i32 (...) @getpid()
%3 = call i32 (...) @getppid()
%4 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3)
%5 = call i64 (...) @fork()
store i64 %5, i64* %1
%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* %1
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 (...) @run_grandchild()
br label %14
14: ; preds = %12, %9
br label %15
15: ; preds = %14, %7
%16 = load i64, i64* %1
%17 = call i64 @waitpid(i64 %16, i32* null, i32 0)
%18 = load i64, i64* %1
%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 = [25 x i8] c"child = %d, parent = %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 void @run_child() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (...) @getppid()
%3 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %12
8: ; preds = %0
%9 = icmp eq i64 %4, 0
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i32 (...) @run_grandchild()
br label %12
12: ; preds = %8, %10, %6
%13 = tail call i64 @waitpid(i64 %4, i32* null, i32 0)
%14 = icmp eq i64 %13, %4
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* @EXIT_SUCCESS
%19 = tail call i32 @exit(i32 %18)
unreachable
}