#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*/ 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 *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strtok(char *, char *);
pid_t get_ppid_of(pid_t pid) {
  char statfile[20];
  char buffer[1024];
  sprintf(statfile, "/proc/%d/stat", pid);
  FILE *fd = fopen(statfile, "r");
  if (fd == NULL)
    return -1;
  if (fgets(buffer, sizeof(buffer), fd) == NULL) {
    fclose(fd);
    return -1;
  }
  fclose(fd);
  char *token = strtok(buffer, " ");
  int i;
  for (i = 0; i < 3; i++)
    token = strtok(NULL, " ");
  return atoi(token);
}


x86 assembly (clang, -O0):
<get_ppid_of>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe84(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x20(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    11b2 <get_ppid_of+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <get_ppid_of+0x103>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11ec <get_ppid_of+0x8c>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <get_ppid_of+0x103>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x420(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x430(%rbp)
movl   $0x0,-0x434(%rbp)
cmpl   $0x3,-0x434(%rbp)
jge    1254 <get_ppid_of+0xf4>
xor    %eax,%eax
mov    %eax,%edi
lea    0xddc(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x430(%rbp)
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    121c <get_ppid_of+0xbc>
mov    -0x430(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ppid_of>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %edi,%edx
lea    0xe8c(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11f9 <get_ppid_of+0x99>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
je     11f9 <get_ppid_of+0x99>
lea    0xe4c(%rip),%rbx
lea    0x20(%rsp),%rdi
mov    %rbx,%rsi
call   1060 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <strtok@plt>
mov    %rax,%rdi
call   1070 <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"
@.str.2 = [2 x i8] c" \00"
define i32 @get_ppid_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
store i32 %0, i32* %3
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
br label %41
17: ; preds = %1
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = load i32*, i32** %6
%20 = call i32* @fgets(i8* %18, i32 1024, i32* %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32*, i32** %6
%24 = call i32 @fclose(i32* %23)
store i32 -1, i32* %2
br label %41
25: ; preds = %17
%26 = load i32*, i32** %6
%27 = call i32 @fclose(i32* %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = call i8* @strtok(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %29, i8** %7
store i32 0, i32* %8
br label %30
30: ; preds = %35, %25
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 3
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %34, i8** %7
br label %35
35: ; preds = %33
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %30
38: ; preds = %30
%39 = load i8*, i8** %7
%40 = call i32 @atoi(i8* %39)
store i32 %40, i32* %2
br label %41
41: ; preds = %38, %22, %16
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c" \00"
define i32 @get_ppid_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 @sprintf(i8* %4, i8* dereferenceable(1) 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 %19, 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 %19, label %13
13: ; preds = %9
%14 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @atoi(i8* %17)
br label %19
19: ; preds = %9, %1, %13
%20 = phi i32 [ %18, %13 ], [ -1, %1 ], [ -1, %9 ]
ret i32 %20
}