#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  pid_t pid;
  pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (pid != 0) {
    printf("Parent (PID=%ld) created child with PID %ld\n", (long)getpid(),
           (long)pid);
    printf("Parent (PID=%ld; PPID=%ld) terminating\n", (long)getpid(),
           (long)getppid());
    exit(EXIT_SUCCESS);
  }
  do {
    usleep(100000);
  } while (getppid() != 1);
  printf("\nChild  (PID=%ld) now an orphan (parent PID=%ld)\n", (long)getpid(),
         (long)getppid());
  sleep(1);
  printf("Child  (PID=%ld) terminating\n", (long)getpid());
  _exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d4 <main+0x44>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
je     122f <main+0x9f>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
movslq -0x14(%rbp),%rdx
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x20(%rbp),%rsi
movslq %eax,%rdx
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1234 <main+0xa4>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
cmp    $0x1,%eax
jne    1234 <main+0xa4>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x28(%rbp),%rsi
movslq %eax,%rdx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     121b <main+0x8b>
mov    %eax,%ebx
test   %eax,%eax
jne    1235 <main+0xa5>
nopw   0x0(%rax,%rax,1)
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
cmp    $0x1,%eax
jne    11b0 <main+0x20>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <getppid@plt>
movslq %eax,%rdx
lea    0xe79(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe89(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
lea    0xdde(%rip),%rdi
call   1060 <perror@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
movslq %ebx,%rdx
lea    0xdbf(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <getppid@plt>
movslq %eax,%rdx
lea    0xdc7(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [45 x i8] c"Parent (PID=%ld) created child with PID %ld\0A\00"
@.str.2 = [40 x i8] c"Parent (PID=%ld; PPID=%ld) terminating\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [50 x i8] c"\0AChild (PID=%ld) now an orphan (parent PID=%ld)\0A\00"
@.str.4 = [30 x i8] c"Child (PID=%ld) terminating\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %14
%18 = call i64 (...) @getpid()
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %18, i64 %20)
%22 = call i64 (...) @getpid()
%23 = call i32 (...) @getppid()
%24 = sext i32 %23 to i64
%25 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i64 %22, i64 %24)
%26 = load i32, i32* @EXIT_SUCCESS
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %14
br label %29
29: ; preds = %31, %28
%30 = call i32 @usleep(i32 100000)
br label %31
31: ; preds = %29
%32 = call i32 (...) @getppid()
%33 = icmp ne i32 %32, 1
br i1 %33, label %29, label %34
34: ; preds = %31
%35 = call i64 (...) @getpid()
%36 = call i32 (...) @getppid()
%37 = sext i32 %36 to i64
%38 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i64 %35, i64 %37)
%39 = call i32 @sleep(i32 1)
%40 = call i64 (...) @getpid()
%41 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 %40)
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @_exit(i32 %42)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [45 x i8] c"Parent (PID=%ld) created child with PID %ld\0A\00"
@.str.2 = [40 x i8] c"Parent (PID=%ld; PPID=%ld) terminating\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [50 x i8] c"\0AChild (PID=%ld) now an orphan (parent PID=%ld)\0A\00"
@.str.4 = [30 x i8] c"Child (PID=%ld) terminating\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %8 [
i32 -1, label %4
i32 0, label %18
]
4: ; preds = %2
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %2
%9 = tail call i64 (...) @getpid()
%10 = sext i32 %3 to i64
%11 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %9, i64 %10)
%12 = tail call i64 (...) @getpid()
%13 = tail call i32 (...) @getppid()
%14 = sext i32 %13 to i64
%15 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i64 %12, i64 %14)
%16 = load i32, i32* @EXIT_SUCCESS
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %2, %18
%19 = tail call i32 @usleep(i32 100000)
%20 = tail call i32 (...) @getppid()
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %18
22: ; preds = %18
%23 = tail call i64 (...) @getpid()
%24 = tail call i32 (...) @getppid()
%25 = sext i32 %24 to i64
%26 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i64 %23, i64 %25)
%27 = tail call i32 @sleep(i32 1)
%28 = tail call i64 (...) @getpid()
%29 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 %28)
%30 = load i32, i32* @EXIT_SUCCESS
%31 = tail call i32 @_exit(i32 %30)
unreachable
}