#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 */
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int main() {
  pid_t pid;
  pid = fork();
  if (pid == 0) {
    while (getppid() > 1)
      ;
    printf("Child exiting: pid=%d ppid=%d\n", getpid(), getppid());
    return 0;
  } else if (pid > 0) {
    printf("Parent exiting: pid=%d ppid=%d\n", getpid(), getppid());
    return 0;
  }
  perror("Could not create child");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bd <main+0x6d>
jmp    117a <main+0x2a>
mov    $0x0,%al
call   1060 <getppid@plt>
cmp    $0x1,%eax
jle    118f <main+0x3f>
jmp    117a <main+0x2a>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    120e <main+0xbe>
cmpq   $0x0,-0x10(%rbp)
jle    11f6 <main+0xa6>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    120e <main+0xbe>
jmp    11fb <main+0xab>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     1180 <main+0x30>
jle    11b7 <main+0x67>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xea5(%rip),%rdi
jmp    11a5 <main+0x55>
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <getppid@plt>
cmp    $0x1,%eax
jg     1180 <main+0x30>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe5b(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%ebx
jmp    11ae <main+0x5e>

LLVM IR (-O0):
@.str = [31 x i8] c"Child exiting: pid=%d ppid=%d\0A\00"
@.str.1 = [32 x i8] c"Parent exiting: pid=%d ppid=%d\0A\00"
@.str.2 = [23 x i8] c"Could not create child\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = load i64, i64* %2
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %15
6: ; preds = %0
br label %7
7: ; preds = %10, %6
%8 = call i32 (...) @getppid()
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %7
br label %7
11: ; preds = %7
%12 = call i32 (...) @getpid()
%13 = call i32 (...) @getppid()
%14 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
store i32 0, i32* %1
br label %25
15: ; preds = %0
%16 = load i64, i64* %2
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getppid()
%21 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %20)
store i32 0, i32* %1
br label %25
22: ; preds = %15
br label %23
23: ; preds = %22
%24 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %25
25: ; preds = %23, %18, %11
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [31 x i8] c"Child exiting: pid=%d ppid=%d\0A\00"
@.str.1 = [32 x i8] c"Parent exiting: pid=%d ppid=%d\0A\00"
@.str.2 = [23 x i8] c"Could not create child\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %10
3: ; preds = %0, %3
%4 = tail call i32 (...) @getppid()
%5 = icmp sgt i32 %4, 1
br i1 %5, label %3, label %6
6: ; preds = %3
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (...) @getppid()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
br label %18
10: ; preds = %0
%11 = icmp sgt i64 %1, 0
br i1 %11, label %12, label %16
12: ; preds = %10
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (...) @getppid()
%15 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
br label %18
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %12, %6
%19 = phi i32 [ 0, %6 ], [ 0, %12 ], [ 1, %16 ]
ret i32 %19
}