#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*/ EINVAL;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
pid_t getppid(void) {
  int n, f;
  char ppidbuf[15];
  f = open("#c/ppid", 0);
  n = read(f, ppidbuf, sizeof ppidbuf);
  if (n < 0)
    errno = EINVAL;
  else
    n = atoi(ppidbuf);
  close(f);
  return n;
}


x86 assembly (clang, -O0):
<getppid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x17(%rbp),%rsi
mov    $0xf,%edx
call   1040 <read@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    118e <getppid+0x4e>
mov    0x2e5a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e59(%rip),%rax
mov    %ecx,(%rax)
jmp    119a <getppid+0x5a>
lea    -0x17(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getppid>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0xeb3(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,%ebx
lea    0x9(%rsp),%rsi
mov    %eax,%edi
mov    $0xf,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1179 <getppid+0x39>
lea    0x9(%rsp),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebp
jmp    118d <getppid+0x4d>
mov    %eax,%ebp
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    0x2e55(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"#c/ppid\00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @getppid() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [15 x i8]
%4 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%7 = call i32 @read(i32 %5, i8* %6, i32 15)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %15
12: ; preds = %0
%13 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%14 = call i32 @atoi(i8* %13)
store i32 %14, i32* %1
br label %15
15: ; preds = %12, %10
%16 = load i32, i32* %2
%17 = call i32 @close(i32 %16)
%18 = load i32, i32* %1
ret i32 %18
}

LLVM IR (-O2):
@.str = [8 x i8] c"#c/ppid\00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @getppid() {
%1 = alloca [15 x i8]
%2 = getelementptr inbounds [15 x i8], [15 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = call i32 @read(i32 %3, i8* %2, i32 15)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = load i32, i32* @EINVAL
store i32 %7, i32* @errno
br label %10
8: ; preds = %0
%9 = call i32 @atoi(i8* %2)
br label %10
10: ; preds = %8, %6
%11 = phi i32 [ %4, %6 ], [ %9, %8 ]
%12 = tail call i32 @close(i32 %3)
ret i32 %11
}