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

/* Variables and functions */
int BUFSIZ;
int EOF;
int PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *pfl_strdup(char *);
int snprintf(char *, int, char *, int);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char const *);
char *pfl_getprocenv(int pid, const char *key) {
  char fn[PATH_MAX], buf[BUFSIZ], *t, *p;
  FILE *fp;
  int c;
  if (snprintf(fn, sizeof(fn), "/proc/%d/environ", pid) == -1)
    return (NULL);
  fp = fopen(fn, "r");
  if (fp == NULL)
    return (NULL);
  t = buf;
  p = NULL;
  while ((c = fgetc(fp)) != EOF) {
    if (t < buf + sizeof(buf)) {
      *t++ = c;
      if (c == '\0' && t > buf + 1) {
        if ((p = strchr(buf, '=')) != NULL) {
          *p++ = '\0';
          if (strcmp(buf, key) == 0)
            break;
          p = NULL;
        }
      }
    }
    if (c == '\0')
      t = buf;
  }
  fclose(fp);
  return (p ? pfl_strdup(p) : NULL);
}


x86 assembly (clang, -O0):
<pfl_getprocenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%ecx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x58(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x30(%rbp)
mov    %eax,%esi
mov    -0xc(%rbp),%ecx
lea    0xe20(%rip),%rdx
call   1050 <snprintf@plt>
cmp    $0xffffffff,%eax
jne    1202 <pfl_getprocenv+0x92>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    1360 <pfl_getprocenv+0x1f0>
mov    -0x68(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1235 <pfl_getprocenv+0xc5>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    1360 <pfl_getprocenv+0x1f0>
mov    -0x58(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x48(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x4c(%rbp)
mov    0x2d88(%rip),%rcx
cmp    (%rcx),%eax
je     1320 <pfl_getprocenv+0x1b0>
mov    -0x60(%rbp),%rdx
mov    -0x58(%rbp),%rcx
mov    -0x38(%rbp),%rax
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1309 <pfl_getprocenv+0x199>
mov    -0x4c(%rbp),%eax
mov    %al,%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
cmpl   $0x0,-0x4c(%rbp)
jne    1304 <pfl_getprocenv+0x194>
mov    -0x58(%rbp),%rcx
mov    -0x38(%rbp),%rax
add    $0x1,%rcx
cmp    %rcx,%rax
jbe    1304 <pfl_getprocenv+0x194>
mov    -0x58(%rbp),%rdi
mov    $0x3d,%esi
call   1040 <strchr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     12ff <pfl_getprocenv+0x18f>
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x40(%rbp)
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12f7 <pfl_getprocenv+0x187>
jmp    1320 <pfl_getprocenv+0x1b0>
movq   $0x0,-0x40(%rbp)
jmp    1304 <pfl_getprocenv+0x194>
jmp    1309 <pfl_getprocenv+0x199>
cmpl   $0x0,-0x4c(%rbp)
jne    131b <pfl_getprocenv+0x1ab>
mov    -0x58(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1245 <pfl_getprocenv+0xd5>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x40(%rbp)
je     1346 <pfl_getprocenv+0x1d6>
mov    -0x40(%rbp),%rdi
call   1080 <pfl_strdup@plt>
mov    %rax,-0x70(%rbp)
jmp    1351 <pfl_getprocenv+0x1e1>
xor    %eax,%eax
mov    %rax,-0x70(%rbp)
jmp    1351 <pfl_getprocenv+0x1e1>
mov    -0x70(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pfl_getprocenv>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,-0x38(%rbp)
mov    %edi,%ecx
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
mov    %rax,-0x30(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe37(%rip),%rdx
mov    %rbx,%rdi
call   1050 <snprintf@plt>
cmp    $0xffffffff,%eax
je     120a <pfl_getprocenv+0x9a>
lea    0xe34(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     120a <pfl_getprocenv+0x9a>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebx
mov    0x2de2(%rip),%rax
cmp    (%rax),%ebx
jne    121b <pfl_getprocenv+0xab>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    %r12,-0x30(%rbp)
mov    %r12,%rax
jmp    1251 <pfl_getprocenv+0xe1>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rax,%r14
test   %ebx,%ebx
cmove  %r12,%r14
mov    %r15,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebx
mov    %r14,%rax
mov    0x2d93(%rip),%rcx
cmp    (%rcx),%ebx
je     1202 <pfl_getprocenv+0x92>
cmp    -0x30(%rbp),%rax
jae    1230 <pfl_getprocenv+0xc0>
lea    0x1(%rax),%r14
mov    %bl,(%rax)
test   %ebx,%ebx
jne    1233 <pfl_getprocenv+0xc3>
cmp    %r12,%rax
jbe    1233 <pfl_getprocenv+0xc3>
mov    %r12,%rdi
mov    $0x3d,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1233 <pfl_getprocenv+0xc3>
mov    %rax,%r13
movb   $0x0,(%rax)
mov    %r12,%rdi
mov    -0x38(%rbp),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1233 <pfl_getprocenv+0xc3>
add    $0x1,%r13
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %r13,%rdi
call   1080 <pfl_strdup@plt>
jmp    120c <pfl_getprocenv+0x9c>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@BUFSIZ = global i32 0
@.str = [17 x i8] c"/proc/%d/environ\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
define i8* @pfl_getprocenv(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = load i32, i32* @BUFSIZ
%19 = zext i32 %18 to i64
%20 = alloca i8, i64 %19
store i64 %19, i64* %8
%21 = trunc i64 %15 to i32
%22 = load i32, i32* %4
%23 = call i32 @snprintf(i8* %17, i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %2
store i8* null, i8** %3
store i32 1, i32* %13
br label %81
26: ; preds = %2
%27 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %11
%28 = load i32*, i32** %11
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %31
30: ; preds = %26
store i8* null, i8** %3
store i32 1, i32* %13
br label %81
31: ; preds = %26
store i8* %20, i8** %9
store i8* null, i8** %10
br label %32
32: ; preds = %69, %31
%33 = load i32*, i32** %11
%34 = call i32 @fgetc(i32* %33)
store i32 %34, i32* %12
%35 = load i32, i32* @EOF
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %70
37: ; preds = %32
%38 = load i8*, i8** %9
%39 = getelementptr inbounds i8, i8* %20, i64 %19
%40 = icmp ult i8* %38, %39
br i1 %40, label %41, label %65
41: ; preds = %37
%42 = load i32, i32* %12
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %9
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %9
store i8 %43, i8* %44
%46 = load i32, i32* %12
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %64
48: ; preds = %41
%49 = load i8*, i8** %9
%50 = getelementptr inbounds i8, i8* %20, i64 1
%51 = icmp ugt i8* %49, %50
br i1 %51, label %52, label %64
52: ; preds = %48
%53 = call i8* @strchr(i8* %20, i8 signext 61)
store i8* %53, i8** %10
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %63
55: ; preds = %52
%56 = load i8*, i8** %10
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %10
store i8 0, i8* %56
%58 = load i8*, i8** %5
%59 = call i64 @strcmp(i8* %20, i8* %58)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
br label %70
62: ; preds = %55
store i8* null, i8** %10
br label %63
63: ; preds = %62, %52
br label %64
64: ; preds = %63, %48, %41
br label %65
65: ; preds = %64, %37
%66 = load i32, i32* %12
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %65
store i8* %20, i8** %9
br label %69
69: ; preds = %68, %65
br label %32
70: ; preds = %61, %32
%71 = load i32*, i32** %11
%72 = call i32 @fclose(i32* %71)
%73 = load i8*, i8** %10
%74 = icmp ne i8* %73, null
br i1 %74, label %75, label %78
75: ; preds = %70
%76 = load i8*, i8** %10
%77 = call i8* @pfl_strdup(i8* %76)
br label %79
78: ; preds = %70
br label %79
79: ; preds = %78, %75
%80 = phi i8* [ %77, %75 ], [ null, %78 ]
store i8* %80, i8** %3
store i32 1, i32* %13
br label %81
81: ; preds = %79, %30, %25
%82 = load i8*, i8** %6
%83 = load i8*, i8** %3
ret i8* %83
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@BUFSIZ = global i32 0
@.str = [17 x i8] c"/proc/%d/environ\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
define i8* @pfl_getprocenv(i32 %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = load i32, i32* @BUFSIZ
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %49, label %11
11: ; preds = %2
%12 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %49, label %14
14: ; preds = %11
%15 = call i32 @fgetc(i32* %12)
%16 = load i32, i32* @EOF
%17 = icmp eq i32 %15, %16
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = getelementptr inbounds i8, i8* %8, i64 %7
br label %22
20: ; preds = %38, %14
%21 = call i32 @fclose(i32* %12)
br label %49
22: ; preds = %18, %38
%23 = phi i32 [ %15, %18 ], [ %42, %38 ]
%24 = phi i8* [ %8, %18 ], [ %41, %38 ]
%25 = icmp ult i8* %24, %19
br i1 %25, label %26, label %38
26: ; preds = %22
%27 = trunc i32 %23 to i8
%28 = getelementptr inbounds i8, i8* %24, i64 1
store i8 %27, i8* %24
%29 = icmp eq i32 %23, 0
%30 = icmp ugt i8* %24, %8
%31 = select i1 %29, i1 %30, i1 false
br i1 %31, label %32, label %38
32: ; preds = %26
%33 = call i8* @strchr(i8* %8, i8 signext 61)
%34 = icmp eq i8* %33, null
br i1 %34, label %38, label %35
35: ; preds = %32
store i8 0, i8* %33
%36 = call i64 @strcmp(i8* %8, i8* %1)
%37 = icmp eq i64 %36, 0
br i1 %37, label %45, label %38
38: ; preds = %35, %26, %32, %22
%39 = phi i8* [ %28, %32 ], [ %28, %26 ], [ %24, %22 ], [ %28, %35 ]
%40 = icmp eq i32 %23, 0
%41 = select i1 %40, i8* %8, i8* %39
%42 = call i32 @fgetc(i32* %12)
%43 = load i32, i32* @EOF
%44 = icmp eq i32 %42, %43
br i1 %44, label %20, label %22
45: ; preds = %35
%46 = getelementptr inbounds i8, i8* %33, i64 1
%47 = call i32 @fclose(i32* %12)
%48 = call i8* @pfl_strdup(i8* %46)
br label %49
49: ; preds = %20, %45, %11, %2
%50 = phi i8* [ null, %2 ], [ null, %11 ], [ %48, %45 ], [ null, %20 ]
ret i8* %50
}