#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, char *, char *, int *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int getppidex(int pid) {
  int _pid;
  char buf[256];
  char state;
  int ppid;
  sprintf(buf, "/proc/%d/stat", pid);
  FILE *f = fopen(buf, "r");
  if (!f) {
    fprintf(stderr, "can't open %s: %s\n", buf, strerror(errno));
    return -1;
  }
  if (fscanf(f, "%d %s %c %d", &_pid, buf, &state, &ppid) != 4) {
    ppid = -1;
    goto out;
  }
out:
  fclose(f);
  return ppid;
}


x86 assembly (clang, -O0):
<getppidex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe81(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
jne    11ff <getppidex+0x9f>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x12c(%rbp),%edi
mov    -0x128(%rbp),%rdx
mov    %rax,%rcx
lea    0xe22(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <getppidex+0xfd>
mov    -0x120(%rbp),%rdi
lea    -0x110(%rbp),%rcx
lea    0xe0f(%rip),%rsi
lea    -0xc(%rbp),%rdx
lea    -0x111(%rbp),%r8
lea    -0x118(%rbp),%r9
call   1040 <fscanf@plt>
cmp    $0x4,%eax
je     1243 <getppidex+0xe3>
movl   $0xffffffff,-0x118(%rbp)
ff ff ff
jmp    1248 <getppidex+0xe8>
jmp    1248 <getppidex+0xe8>
mov    -0x120(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x118(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getppidex>:
push   %rbx
sub    $0x110,%rsp
mov    %edi,%edx
lea    0xe8f(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d3 <getppidex+0x73>
mov    %rax,%rbx
lea    0xe87(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0x7(%rsp),%r8
lea    0x8(%rsp),%r9
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x4,%eax
je     11c5 <getppidex+0x65>
movl   $0xffffffff,0x8(%rsp)
ff
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%eax
jmp    1205 <getppidex+0xa5>
mov    0x2e06(%rip),%rax
mov    (%rax),%ebx
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe1f(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"can't open %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [12 x i8] c"%d %s %c %d\00"
define i32 @getppidex(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32*
store i32 %0, i32* %3
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, 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 [256 x i8], [256 x i8]* %5, 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** %8
%14 = load i32*, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %22, label %16
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %18, i8* %20)
store i32 -1, i32* %2
br label %33
22: ; preds = %1
%23 = load i32*, i32** %8
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%25 = call i32 @fscanf(i32* %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %4, i8* %24, i8* %6, i32* %7)
%26 = icmp ne i32 %25, 4
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 -1, i32* %7
br label %29
28: ; preds = %22
br label %29
29: ; preds = %28, %27
%30 = load i32*, i32** %8
%31 = call i32 @fclose(i32* %30)
%32 = load i32, i32* %7
store i32 %32, i32* %2
br label %33
33: ; preds = %29, %16
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"can't open %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [12 x i8] c"%d %s %c %d\00"
define i32 @getppidex(i32 %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i8
%5 = alloca i32
%6 = bitcast i32* %2 to i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
%9 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%10 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %7, i8* %15)
br label %24
17: ; preds = %1
%18 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %2, i8* %7, i8* %4, i32* %5)
%19 = icmp eq i32 %18, 4
br i1 %19, label %21, label %20
20: ; preds = %17
store i32 -1, i32* %5
br label %21
21: ; preds = %17, %20
%22 = call i32 @fclose(i32* %10)
%23 = load i32, i32* %5
br label %24
24: ; preds = %21, %12
%25 = phi i32 [ %23, %21 ], [ -1, %12 ]
ret i32 %25
}