#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 fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int is_auxv_64_file(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int is_auxv_64(const char *pid, bool *result) {
  char filename[64];
  sprintf(filename, "/proc/%s/exe", pid);
  FILE *f = fopen(filename, "rb");
  if (!f) {
    perror(0);
    return -1;
  }
  int r = is_auxv_64_file(f, result);
  if (r) {
    fclose(f);
    return r;
  }
  r = fclose(f);
  if (r) {
    perror(0);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_auxv_64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe91(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    11a6 <is_auxv_64+0x56>
xor    %edi,%edi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <is_auxv_64+0xb4>
mov    -0x68(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <is_auxv_64_file@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
je     11d4 <is_auxv_64+0x84>
mov    -0x68(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <is_auxv_64+0xb4>
mov    -0x68(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
je     11fd <is_auxv_64+0xad>
xor    %edi,%edi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <is_auxv_64+0xb4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_auxv_64>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
mov    %rdi,%rdx
lea    0xe9b(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe96(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a4 <is_auxv_64+0x54>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <is_auxv_64_file@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %ebp,%ebp
jne    11b0 <is_auxv_64+0x60>
test   %eax,%eax
je     11bb <is_auxv_64+0x6b>
xor    %edi,%edi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %ebp,%ebp
jmp    11b0 <is_auxv_64+0x60>

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/%s/exe\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @is_auxv_64(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [64 x i8]
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%10 = load i8*, i8** %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = call i32 @perror(i32 0)
store i32 -1, i32* %3
br label %36
18: ; preds = %2
%19 = load i32*, i32** %7
%20 = load i32*, i32** %5
%21 = call i32 @is_auxv_64_file(i32* %19, i32* %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = load i32*, i32** %7
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %8
store i32 %27, i32* %3
br label %36
28: ; preds = %18
%29 = load i32*, i32** %7
%30 = call i32 @fclose(i32* %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @perror(i32 0)
store i32 -1, i32* %3
br label %36
35: ; preds = %28
store i32 0, i32* %3
br label %36
36: ; preds = %35, %33, %24, %16
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%s/exe\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @is_auxv_64(i8* %0, i32* %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = tail call i32 @is_auxv_64_file(i32* %6, i32* %1)
%10 = icmp eq i32 %9, 0
%11 = tail call i32 @fclose(i32* %6)
br i1 %10, label %12, label %16
12: ; preds = %8
%13 = icmp eq i32 %11, 0
br i1 %13, label %16, label %14
14: ; preds = %12, %2
%15 = tail call i32 @perror(i32 0)
br label %16
16: ; preds = %14, %8, %12
%17 = phi i32 [ 0, %12 ], [ %9, %8 ], [ -1, %14 ]
ret i32 %17
}