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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ strncmp(char *, char *, int);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
pid_t get_parent(pid_t process) {
  pid_t parent = (pid_t)-1;
  char tmp[32];
  FILE *status;
  if (snprintf(tmp, sizeof(tmp), "/proc/%u/status", process) < 32) {
    status = fopen(tmp, "rt");
    if (status) {
      while (fgets(tmp, sizeof(tmp), status)) {
        if (!strncmp(tmp, "PPid:\t", 6)) {
          parent = (pid_t)strtol(tmp + 6, NULL, 10);
        }
      }
      fclose(status);
    }
  }
  return parent;
}


x86 assembly (clang, -O0):
<get_parent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x20,%esi
lea    0xe7b(%rip),%rdx
call   1050 <snprintf@plt>
cmp    $0x20,%eax
jge    121d <get_parent+0xbd>
lea    -0x30(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1218 <get_parent+0xb8>
jmp    11b7 <get_parent+0x57>
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x20,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     120f <get_parent+0xaf>
lea    -0x30(%rbp),%rdi
lea    0xe35(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    120a <get_parent+0xaa>
lea    -0x30(%rbp),%rdi
add    $0x6,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x8(%rbp)
jmp    11b7 <get_parent+0x57>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
jmp    121d <get_parent+0xbd>
mov    -0x8(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_parent>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ecx
lea    0xe8a(%rip),%rdx
mov    %rsp,%rdi
mov    $0x20,%esi
call   1050 <snprintf@plt>
mov    $0xffffffff,%r14d
cmp    $0x1f,%eax
jg     1225 <get_parent+0xc5>
lea    0xe77(%rip),%rsi
mov    %rsp,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1225 <get_parent+0xc5>
mov    %rax,%r13
mov    %rsp,%rdi
mov    $0x20,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     121d <get_parent+0xbd>
lea    0x6(%rsp),%r15
lea    0xe3f(%rip),%r12
mov    %rsp,%rbx
jmp    11f5 <get_parent+0x95>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x20,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     121d <get_parent+0xbd>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11e0 <get_parent+0x80>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,%r14
jmp    11e0 <get_parent+0x80>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/%u/status\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [7 x i8] c"PPid:\09\00"
define i32 @get_parent(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [32 x i8]
%5 = alloca i32*
store i32 %0, i32* %2
store i32 -1, i32* %3
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* %2
%8 = call i32 @snprintf(i8* %6, i32 32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 32
br i1 %9, label %10, label %35
10: ; preds = %1
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %34
15: ; preds = %10
br label %16
16: ; preds = %30, %15
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%18 = load i32*, i32** %5
%19 = call i64 @fgets(i8* %17, i32 32, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %16
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%23 = call i32 @strncmp(i8* %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%24 = icmp ne i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %21
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%27 = getelementptr inbounds i8, i8* %26, i64 6
%28 = call i64 @strtol(i8* %27, i32* null, i32 10)
%29 = trunc i64 %28 to i32
store i32 %29, i32* %3
br label %30
30: ; preds = %25, %21
br label %16
31: ; preds = %16
%32 = load i32*, i32** %5
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %31, %10
br label %35
35: ; preds = %34, %1
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/%u/status\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [7 x i8] c"PPid:\09\00"
define i32 @get_parent(i32 %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = icmp slt i32 %4, 32
br i1 %5, label %6, label %28
6: ; preds = %1
%7 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %28, label %9
9: ; preds = %6
%10 = call i64 @fgets(i8* %3, i32 32, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %9
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 6
br label %14
14: ; preds = %12, %21
%15 = phi i32 [ -1, %12 ], [ %22, %21 ]
%16 = call i32 @strncmp(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i64 @strtol(i8* nocapture %13, i32* null, i32 10)
%20 = trunc i64 %19 to i32
br label %21
21: ; preds = %18, %14
%22 = phi i32 [ %15, %14 ], [ %20, %18 ]
%23 = call i64 @fgets(i8* %3, i32 32, i32* %7)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %14
25: ; preds = %21, %9
%26 = phi i32 [ -1, %9 ], [ %22, %21 ]
%27 = call i32 @fclose(i32* %7)
br label %28
28: ; preds = %6, %25, %1
%29 = phi i32 [ %26, %25 ], [ -1, %6 ], [ -1, %1 ]
ret i32 %29
}