#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*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int getpid();
scalar_t__ getppid();
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t pid, old_ppid, new_ppid;
  pid_t child, parent;
  parent = getpid();
  if ((child = fork()) < 0) {
    fprintf(stderr, "%s: fork of child failed: %s\n", argv[0], strerror(errno));
    exit(1);
  } else if (child == 0) {
    old_ppid = getppid();
    sleep(2);
    new_ppid = getppid();
  } else {
    sleep(1);
    exit(0);
  }
  printf("Original parent: %d\n", parent);
  printf("Child: %d\n", getpid());
  printf("Child's old ppid: %d\n", old_ppid);
  printf("Child's new ppid: %d\n", new_ppid);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
cltq
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jge    11fd <main+0x7d>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe12(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x30(%rbp)
jne    122d <main+0xad>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %rax,-0x28(%rbp)
jmp    123e <main+0xbe>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1243 <main+0xc3>
mov    -0x38(%rbp),%rsi
lea    0xdd0(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
movslq %eax,%rsi
lea    0xdcf(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdca(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xdd0(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    11d4 <main+0x54>
mov    0x2e3d(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%rbx
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe43(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jne    1241 <main+0xc1>
movslq %ebp,%rbp
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %rax,%r14
mov    $0x2,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %rax,%rbx
lea    0xe20(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
movslq %eax,%rsi
lea    0xe1c(%rip),%rdi
call   1040 <printf@plt>
lea    0xe1b(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
lea    0xe22(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"%s: fork of child failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"Original parent: %d\0A\00"
@.str.2 = [11 x i8] c"Child: %d\0A\00"
@.str.3 = [22 x i8] c"Child's old ppid: %d\0A\00"
@.str.4 = [22 x i8] c"Child's new ppid: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @getpid()
%12 = sext i32 %11 to i64
store i64 %12, i64* %10
%13 = call i64 (...) @fork()
store i64 %13, i64* %9
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i64, i64* %9
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = call i64 (...) @getppid()
store i64 %28, i64* %7
%29 = call i32 @sleep(i32 2)
%30 = call i64 (...) @getppid()
store i64 %30, i64* %8
br label %34
31: ; preds = %24
%32 = call i32 @sleep(i32 1)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %27
br label %35
35: ; preds = %34
%36 = load i64, i64* %10
%37 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i64 %36)
%38 = call i32 (...) @getpid()
%39 = sext i32 %38 to i64
%40 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %39)
%41 = load i64, i64* %7
%42 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %41)
%43 = load i64, i64* %8
%44 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %43)
%45 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"%s: fork of child failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"Original parent: %d\0A\00"
@.str.2 = [11 x i8] c"Child: %d\0A\00"
@.str.3 = [22 x i8] c"Child's old ppid: %d\0A\00"
@.str.4 = [22 x i8] c"Child's new ppid: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = icmp eq i64 %4, 0
br i1 %14, label %15, label %27
15: ; preds = %13
%16 = sext i32 %3 to i64
%17 = tail call i64 (...) @getppid()
%18 = tail call i32 @sleep(i32 2)
%19 = tail call i64 (...) @getppid()
%20 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i64 %16)
%21 = tail call i32 (...) @getpid()
%22 = sext i32 %21 to i64
%23 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %22)
%24 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %17)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %19)
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %13
%28 = tail call i32 @sleep(i32 1)
%29 = tail call i32 @exit(i32 0)
unreachable
}