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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
pid_t grab_parent_of_pid(pid_t pid) {
  char statusfile[PATH_MAX + 1];
  FILE *f;
  int ret = 0, s;
  snprintf(statusfile, PATH_MAX, "%s/%d/status", "/proc", pid);
  statusfile[PATH_MAX] = 0;
  f = fopen(statusfile, "r");
  if (!f) {
    return ret;
  }
  s = fscanf(f, "%*s %*s %*s %*s %*s %*s %*s PPid: %d", &ret);
  if (s != 1)
    ret = 0;
  fclose(f);
  return (pid_t)ret;
}


x86 assembly (clang, -O0):
<grab_parent_of_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%r8d
lea    0xe6b(%rip),%rdx
lea    0xe71(%rip),%rcx
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2e44(%rip),%rax
movslq (%rax),%rax
movb   $0x0,(%rdi,%rax,1)
lea    0xe59(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e0 <grab_parent_of_pid+0xa0>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    121e <grab_parent_of_pid+0xde>
mov    -0x20(%rbp),%rdi
lea    0xe2a(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1050 <fscanf@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
je     1208 <grab_parent_of_pid+0xc8>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grab_parent_of_pid>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r8d
mov    0x2e9b(%rip),%r14
mov    (%r14),%esi
lea    0x1(%rsi),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x0,-0x14(%rbp)
lea    0xe86(%rip),%rdx
lea    0xe8c(%rip),%rcx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
movslq (%r14),%rax
movb   $0x0,(%rax,%rbx,1)
lea    0xe7c(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d3 <grab_parent_of_pid+0x93>
mov    %rax,%rbx
lea    0xe67(%rip),%rsi
lea    -0x14(%rbp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     11c6 <grab_parent_of_pid+0x86>
movl   $0x0,-0x14(%rbp)
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
jmp    11d5 <grab_parent_of_pid+0x95>
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"%s/%d/status\00"
@.str.1 = [6 x i8] c"/proc\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [37 x i8] c"%*s %*s %*s %*s %*s %*s %*s PPid: %d\00"
define i32 @grab_parent_of_pid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
%10 = load i32, i32* @PATH_MAX
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %4
%14 = alloca i8, i64 %12
store i64 %12, i64* %5
store i32 0, i32* %7
%15 = load i32, i32* @PATH_MAX
%16 = load i32, i32* %3
%17 = call i32 @snprintf(i8* %14, i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @PATH_MAX
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %14, i64 %19
store i8 0, i8* %20
%21 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp ne i32* %22, null
br i1 %23, label %26, label %24
24: ; preds = %1
%25 = load i32, i32* %7
store i32 %25, i32* %2
store i32 1, i32* %9
br label %36
26: ; preds = %1
%27 = load i32*, i32** %6
%28 = call i32 @fscanf(i32* %27, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32* %7)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp ne i32 %29, 1
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 0, i32* %7
br label %32
32: ; preds = %31, %26
%33 = load i32*, i32** %6
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %7
store i32 %35, i32* %2
store i32 1, i32* %9
br label %36
36: ; preds = %32, %24
%37 = load i8*, i8** %4
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"%s/%d/status\00"
@.str.1 = [6 x i8] c"/proc\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [37 x i8] c"%*s %*s %*s %*s %*s %*s %*s PPid: %d\00"
define i32 @grab_parent_of_pid(i32 %0) {
%2 = alloca i32
%3 = load i32, i32* @PATH_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%8 = call i32 @snprintf(i8* %6, i32 %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%9 = load i32, i32* @PATH_MAX
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %6, i64 %10
store i8 0, i8* %11
%12 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %21, label %14
14: ; preds = %1
%15 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%16 = icmp eq i32 %15, 1
br i1 %16, label %18, label %17
17: ; preds = %14
store i32 0, i32* %2
br label %18
18: ; preds = %17, %14
%19 = call i32 @fclose(i32* %12)
%20 = load i32, i32* %2
br label %21
21: ; preds = %1, %18
%22 = phi i32 [ 0, %1 ], [ %20, %18 ]
ret i32 %22
}