#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
char *string(char *, int);
int strlen(char *);
char *strrchr(char *, char);
int strtol(char *, char **, int);
int getparent(pid_t child) {
  char *end, *line = NULL, *path, *start;
  pid_t parent = -1;
  size_t size;
  FILE *file;
  path = string("/proc/%u/stat", child);
  file = fopen(path, "r");
  free(path);
  if (file && getline(&line, &size, file) >= 0)
    if ((start = strrchr(line, ')')) && strlen(start) >= 4) {
      parent = strtol(start + 4, &end, 10);
      if (end == start || *end != ' ')
        parent = -1;
    }
  if (file)
    fclose(file);
  if (line)
    free(line);
  return parent;
}


x86 assembly (clang, -O0):
<getparent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe5c(%rip),%rdi
call   1070 <string@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x40(%rbp)
je     1266 <getparent+0xe6>
mov    -0x40(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10a0 <getline@plt>
cmp    $0x0,%rax
jl     1266 <getparent+0xe6>
mov    -0x18(%rbp),%rdi
mov    $0x29,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1261 <getparent+0xe1>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jl     1261 <getparent+0xe1>
mov    -0x28(%rbp),%rdi
add    $0x4,%rdi
lea    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1255 <getparent+0xd5>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
je     125c <getparent+0xdc>
movl   $0xffffffff,-0x2c(%rbp)
jmp    1261 <getparent+0xe1>
jmp    1266 <getparent+0xe6>
cmpq   $0x0,-0x40(%rbp)
je     127a <getparent+0xfa>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     128e <getparent+0x10e>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getparent>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%esi
movq   $0x0,0x8(%rsp)
lea    0xe66(%rip),%rdi
call   1070 <string@plt>
mov    %rax,%rbp
lea    0xe65(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     124a <getparent+0xca>
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     1231 <getparent+0xb1>
mov    0x8(%rsp),%rdi
mov    $0x29,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     1231 <getparent+0xb1>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jl     1231 <getparent+0xb1>
lea    0x4(%rbp),%rdi
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,%r14d
mov    0x10(%rsp),%rax
cmp    %rbp,%rax
je     122b <getparent+0xab>
cmpb   $0x20,(%rax)
je     1231 <getparent+0xb1>
mov    $0xffffffff,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1250 <getparent+0xd0>
call   1030 <free@plt>
jmp    1250 <getparent+0xd0>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%u/stat\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getparent(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32*
store i32 %0, i32* %2
store i8* null, i8** %4
store i32 -1, i32* %7
%10 = load i32, i32* %2
%11 = call i8* @string(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%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** %9
%14 = load i8*, i8** %5
%15 = call i32 @free(i8* %14)
%16 = load i32*, i32** %9
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %45
18: ; preds = %1
%19 = load i32*, i32** %9
%20 = call i64 @getline(i8** %4, i64* %8, i32* %19)
%21 = icmp sge i64 %20, 0
br i1 %21, label %22, label %45
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = call i8* @strrchr(i8* %23, i8 signext 41)
store i8* %24, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %44
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = call i32 @strlen(i8* %27)
%29 = icmp sge i32 %28, 4
br i1 %29, label %30, label %44
30: ; preds = %26
%31 = load i8*, i8** %6
%32 = getelementptr inbounds i8, i8* %31, i64 4
%33 = call i32 @strtol(i8* %32, i8** %3, i32 10)
store i32 %33, i32* %7
%34 = load i8*, i8** %3
%35 = load i8*, i8** %6
%36 = icmp eq i8* %34, %35
br i1 %36, label %42, label %37
37: ; preds = %30
%38 = load i8*, i8** %3
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 32
br i1 %41, label %42, label %43
42: ; preds = %37, %30
store i32 -1, i32* %7
br label %43
43: ; preds = %42, %37
br label %44
44: ; preds = %43, %26, %22
br label %45
45: ; preds = %44, %18, %1
%46 = load i32*, i32** %9
%47 = icmp ne i32* %46, null
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32*, i32** %9
%50 = call i32 @fclose(i32* %49)
br label %51
51: ; preds = %48, %45
%52 = load i8*, i8** %4
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i8*, i8** %4
%56 = call i32 @free(i8* %55)
br label %57
57: ; preds = %54, %51
%58 = load i32, i32* %7
ret i32 %58
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%u/stat\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getparent(i32 %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %2 to i8*
%6 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%7 = bitcast i64* %4 to i8*
%8 = tail call i8* @string(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @free(i8* %8)
%11 = icmp eq i32* %9, null
br i1 %11, label %38, label %12
12: ; preds = %1
%13 = call i64 @getline(i8** %3, i64* %4, i32* %9)
%14 = icmp sgt i64 %13, -1
br i1 %14, label %15, label %31
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = call i8* @strrchr(i8* dereferenceable(1) %16, i8 signext 41)
%18 = icmp eq i8* %17, null
br i1 %18, label %31, label %19
19: ; preds = %15
%20 = call i32 @strlen(i8* %17)
%21 = icmp sgt i32 %20, 3
br i1 %21, label %22, label %31
22: ; preds = %19
%23 = getelementptr inbounds i8, i8* %17, i64 4
%24 = call i32 @strtol(i8* %23, i8** %2, i32 10)
%25 = load i8*, i8** %2
%26 = icmp eq i8* %25, %17
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = load i8, i8* %25
%29 = icmp eq i8 %28, 32
br i1 %29, label %31, label %30
30: ; preds = %27, %22
br label %31
31: ; preds = %12, %27, %30, %19, %15
%32 = phi i32 [ -1, %12 ], [ -1, %15 ], [ -1, %19 ], [ %24, %27 ], [ -1, %30 ]
%33 = call i32 @fclose(i32* %9)
%34 = load i8*, i8** %3
%35 = icmp eq i8* %34, null
br i1 %35, label %38, label %36
36: ; preds = %31
%37 = call i32 @free(i8* %34)
br label %38
38: ; preds = %1, %36, %31
%39 = phi i32 [ %32, %36 ], [ %32, %31 ], [ -1, %1 ]
ret i32 %39
}