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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int safe_int(char *, int *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strncmp(char *, char const *, size_t);
char *trim_whitespace_in_place(char *);
pid_t get_pid_from_fdinfo_file(int pidfd, const char *key, size_t keylen) {
  int ret;
  char path[512];
  FILE *f;
  size_t n = 0;
  pid_t result = -1;
  char *line = NULL;
  snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", pidfd);
  f = fopen(path, "re");
  if (!f)
    return -1;
  while (getline(&line, &n, f) != -1) {
    char *numstr;
    if (strncmp(line, key, keylen))
      continue;
    numstr = trim_whitespace_in_place(line + 4);
    ret = safe_int(numstr, &result);
    if (ret < 0)
      goto out;
    break;
  }
out:
  free(line);
  fclose(f);
  return result;
}


x86 assembly (clang, -O0):
<get_pid_from_fdinfo_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x230(%rbp)
movl   $0xffffffff,-0x234(%rbp)
ff ff ff
movq   $0x0,-0x240(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x200,%esi
lea    0xe34(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xe36(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jne    1205 <get_pid_from_fdinfo_file+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <get_pid_from_fdinfo_file+0x137>
jmp    120a <get_pid_from_fdinfo_file+0x8a>
mov    -0x228(%rbp),%rdx
lea    -0x240(%rbp),%rdi
lea    -0x230(%rbp),%rsi
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
je     1291 <get_pid_from_fdinfo_file+0x111>
mov    -0x240(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     1250 <get_pid_from_fdinfo_file+0xd0>
jmp    120a <get_pid_from_fdinfo_file+0x8a>
mov    -0x240(%rbp),%rdi
add    $0x4,%rdi
call   1070 <trim_whitespace_in_place@plt>
mov    %rax,-0x248(%rbp)
mov    -0x248(%rbp),%rdi
lea    -0x234(%rbp),%rsi
call   10a0 <safe_int@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    128c <get_pid_from_fdinfo_file+0x10c>
jmp    1296 <get_pid_from_fdinfo_file+0x116>
jmp    1291 <get_pid_from_fdinfo_file+0x111>
jmp    1296 <get_pid_from_fdinfo_file+0x116>
mov    -0x240(%rbp),%rdi
call   1030 <free@plt>
mov    -0x228(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x234(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pid_from_fdinfo_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x220,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ecx
movq   $0x0,0x18(%rsp)
movl   $0xffffffff,0xc(%rsp)
ff
movq   $0x0,0x10(%rsp)
lea    0xe47(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1060 <snprintf@plt>
lea    0xe43(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     124b <get_pid_from_fdinfo_file+0xcb>
mov    %rax,%rbx
lea    0x10(%rsp),%r12
lea    0x18(%rsp),%r13
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
je     1233 <get_pid_from_fdinfo_file+0xb3>
mov    0x10(%rsp),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    11f0 <get_pid_from_fdinfo_file+0x70>
mov    0x10(%rsp),%rdi
add    $0x4,%rdi
call   1070 <trim_whitespace_in_place@plt>
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
call   10a0 <safe_int@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    1250 <get_pid_from_fdinfo_file+0xd0>
mov    $0xffffffff,%eax
add    $0x220,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/proc/self/fdinfo/%d\00"
@.str.1 = [3 x i8] c"re\00"
define i32 @get_pid_from_fdinfo_file(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca [512 x i8]
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %11
store i32 -1, i32* %12
store i8* null, i8** %13
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%16 = load i32, i32* %5
%17 = call i32 @snprintf(i8* %15, i32 512, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %10
%20 = load i32*, i32** %10
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %3
store i32 -1, i32* %4
br label %52
23: ; preds = %3
br label %24
24: ; preds = %34, %23
%25 = load i32*, i32** %10
%26 = call i32 @getline(i8** %13, i64* %11, i32* %25)
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %45
28: ; preds = %24
%29 = load i8*, i8** %13
%30 = load i8*, i8** %6
%31 = load i64, i64* %7
%32 = call i64 @strncmp(i8* %29, i8* %30, i64 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
br label %24
35: ; preds = %28
%36 = load i8*, i8** %13
%37 = getelementptr inbounds i8, i8* %36, i64 4
%38 = call i8* @trim_whitespace_in_place(i8* %37)
store i8* %38, i8** %14
%39 = load i8*, i8** %14
%40 = call i32 @safe_int(i8* %39, i32* %12)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %35
br label %46
44: ; preds = %35
br label %45
45: ; preds = %44, %24
br label %46
46: ; preds = %45, %43
%47 = load i8*, i8** %13
%48 = call i32 @free(i8* %47)
%49 = load i32*, i32** %10
%50 = call i32 @fclose(i32* %49)
%51 = load i32, i32* %12
store i32 %51, i32* %4
br label %52
52: ; preds = %46, %22
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@.str = [21 x i8] c"/proc/self/fdinfo/%d\00"
@.str.1 = [3 x i8] c"re\00"
define i32 @get_pid_from_fdinfo_file(i32 %0, i8* %1, i64 %2) {
%4 = alloca [512 x i8]
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%9 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%10 = bitcast i32* %6 to i8*
store i32 -1, i32* %6
%11 = bitcast i8** %7 to i8*
store i8* null, i8** %7
%12 = call i32 @snprintf(i8* %8, i32 512, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0)
%13 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %32, label %15
15: ; preds = %3, %18
%16 = call i32 @getline(i8** %7, i64* %5, i32* %13)
%17 = icmp eq i32 %16, -1
br i1 %17, label %27, label %18
18: ; preds = %15
%19 = load i8*, i8** %7
%20 = call i64 @strncmp(i8* %19, i8* %1, i64 %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %15
22: ; preds = %18
%23 = load i8*, i8** %7
%24 = getelementptr inbounds i8, i8* %23, i64 4
%25 = call i8* @trim_whitespace_in_place(i8* %24)
%26 = call i32 @safe_int(i8* %25, i32* %6)
br label %27
27: ; preds = %15, %22
%28 = load i8*, i8** %7
%29 = call i32 @free(i8* %28)
%30 = call i32 @fclose(i32* %13)
%31 = load i32, i32* %6
br label %32
32: ; preds = %3, %27
%33 = phi i32 [ %31, %27 ], [ -1, %3 ]
ret i32 %33
}