#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;
typedef int /*<<< orphan*/ file;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *memchr(char *, char, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
pid_t getppid_of(pid_t pid) {
  char file[20];
  char buffer[1024];
  snprintf(file, sizeof(file), "/proc/%d/stat", pid);
  FILE *fd = fopen(file, "r");
  if (fd == NULL)
    return -1;
  if (fgets(buffer, sizeof(buffer), fd) == NULL) {
    fclose(fd);
    return -1;
  }
  fclose(fd);
  char *p = buffer;
  p = memchr(p + 1, ')', sizeof(buffer) - (p - buffer));
  int sp = 2;
  while (sp--)
    p = memchr(p + 1, ' ', sizeof(buffer) - (p - buffer));
  pid_t ppid = atoi(p + 1);
  return ppid;
}


x86 assembly (clang, -O0):
<getppid_of>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x14,%esi
lea    0xe7f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    11b7 <getppid_of+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <getppid_of+0x161>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    11f1 <getppid_of+0x91>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c1 <getppid_of+0x161>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x430(%rbp),%rdi
add    $0x1,%rdi
mov    -0x430(%rbp),%rcx
lea    -0x420(%rbp),%rax
sub    %rax,%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%edx
mov    $0x29,%esi
call   1050 <memchr@plt>
mov    %rax,-0x430(%rbp)
movl   $0x2,-0x434(%rbp)
mov    -0x434(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x434(%rbp)
cmp    $0x0,%eax
je     12a2 <getppid_of+0x142>
mov    -0x430(%rbp),%rdi
add    $0x1,%rdi
mov    -0x430(%rbp),%rcx
lea    -0x420(%rbp),%rax
sub    %rax,%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
mov    %rax,-0x430(%rbp)
jmp    124c <getppid_of+0xec>
mov    -0x430(%rbp),%rdi
add    $0x1,%rdi
call   1080 <atoi@plt>
mov    %eax,-0x438(%rbp)
mov    -0x438(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getppid_of>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %edi,%ecx
lea    0xe8c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x14,%esi
call   1040 <snprintf@plt>
lea    0xe83(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1216 <getppid_of+0xb6>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
je     1216 <getppid_of+0xb6>
lea    0x21(%rsp),%rdi
mov    $0x29,%esi
mov    $0x400,%edx
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
lea    0x20(%rsp),%rbx
mov    %ebx,%edx
sub    %eax,%edx
add    $0x400,%edx
mov    $0x20,%esi
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
sub    %eax,%ebx
add    $0x400,%ebx
mov    $0x20,%esi
mov    %ebx,%edx
call   1050 <memchr@plt>
lea    0x1(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getppid_of(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 @snprintf(i8* %10, i32 20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %60
18: ; preds = %1
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i32* @fgets(i8* %19, i32 1024, i32* %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32*, i32** %6
%25 = call i32 @fclose(i32* %24)
store i32 -1, i32* %2
br label %60
26: ; preds = %18
%27 = load i32*, i32** %6
%28 = call i32 @fclose(i32* %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = load i8*, i8** %7
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = sub i64 1024, %36
%38 = trunc i64 %37 to i32
%39 = call i8* @memchr(i8* %31, i8 signext 41, i32 %38)
store i8* %39, i8** %7
store i32 2, i32* %8
br label %40
40: ; preds = %44, %26
%41 = load i32, i32* %8
%42 = add nsw i32 %41, -1
store i32 %42, i32* %8
%43 = icmp ne i32 %41, 0
br i1 %43, label %44, label %55
44: ; preds = %40
%45 = load i8*, i8** %7
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = load i8*, i8** %7
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%49 = ptrtoint i8* %47 to i64
%50 = ptrtoint i8* %48 to i64
%51 = sub i64 %49, %50
%52 = sub i64 1024, %51
%53 = trunc i64 %52 to i32
%54 = call i8* @memchr(i8* %46, i8 signext 32, i32 %53)
store i8* %54, i8** %7
br label %40
55: ; preds = %40
%56 = load i8*, i8** %7
%57 = getelementptr inbounds i8, i8* %56, i64 1
%58 = call i32 @atoi(i8* %57)
store i32 %58, i32* %9
%59 = load i32, i32* %9
store i32 %59, i32* %2
br label %60
60: ; preds = %55, %23, %17
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getppid_of(i32 %0) {
%2 = alloca [20 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %31, label %9
9: ; preds = %1
%10 = call i32* @fgets(i8* %5, i32 1024, i32* %7)
%11 = icmp eq i32* %10, null
%12 = call i32 @fclose(i32* %7)
br i1 %11, label %31, label %13
13: ; preds = %9
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1
%15 = ptrtoint [1024 x i8]* %3 to i64
%16 = call i8* @memchr(i8* %14, i8 signext 41, i32 1024)
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = ptrtoint i8* %16 to i64
%19 = sub i64 %15, %18
%20 = trunc i64 %19 to i32
%21 = add i32 %20, 1024
%22 = call i8* @memchr(i8* %17, i8 signext 32, i32 %21)
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = ptrtoint i8* %22 to i64
%25 = sub i64 %15, %24
%26 = trunc i64 %25 to i32
%27 = add i32 %26, 1024
%28 = call i8* @memchr(i8* %23, i8 signext 32, i32 %27)
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = call i32 @atoi(i8* %29)
br label %31
31: ; preds = %9, %1, %13
%32 = phi i32 [ %30, %13 ], [ -1, %1 ], [ -1, %9 ]
ret i32 %32
}