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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *memchr(char *, char, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char const *, int);
size_t strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int helper_efi_locate(const char *efi_entry, uint32_t length,
                      size_t *location) {
  FILE *systab = NULL;
  char efiline[64];
  char *val;
  off_t loc = 0;
  *location = 0;
  systab = fopen("/sys/firmware/efi/systab", "r");
  if (systab != NULL) {
    while ((fgets(efiline, 64 - 1, systab)) != NULL) {
      if (strncmp(efiline, efi_entry, 6) == 0) {
        val = memchr(efiline, '=', strlen(efiline)) + 1;
        loc = strtol(val, NULL, 0);
        break;
      }
    }
    fclose(systab);
    if (loc != 0) {
      *location = loc;
      return 0;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<helper_efi_locate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x80(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
lea    0xe58(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     127c <helper_efi_locate+0x10c>
jmp    11c8 <helper_efi_locate+0x58>
lea    -0x70(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x3f,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     124c <helper_efi_locate+0xdc>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1247 <helper_efi_locate+0xd7>
lea    -0x70(%rbp),%rax
mov    %rax,-0x88(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x88(%rbp),%rdi
mov    %eax,%edx
mov    $0x3d,%esi
call   1060 <memchr@plt>
add    $0x1,%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %rax,-0x80(%rbp)
jmp    124c <helper_efi_locate+0xdc>
jmp    11c8 <helper_efi_locate+0x58>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x80(%rbp)
je     1277 <helper_efi_locate+0x107>
mov    -0x80(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1283 <helper_efi_locate+0x113>
jmp    127c <helper_efi_locate+0x10c>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<helper_efi_locate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%r14
mov    %rdi,%rbp
movq   $0x0,(%rdx)
lea    0xe70(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1090 <fopen@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1221 <helper_efi_locate+0xb1>
mov    %rax,%r12
mov    %rsp,%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x3f,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1219 <helper_efi_locate+0xa9>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11b0 <helper_efi_locate+0x40>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    $0x3d,%esi
mov    %eax,%edx
call   1060 <memchr@plt>
lea    0x1(%rax),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <fclose@plt>
test   %rbx,%rbx
je     1221 <helper_efi_locate+0xb1>
mov    %rbx,(%r14)
xor    %r15d,%r15d
jmp    1221 <helper_efi_locate+0xb1>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"/sys/firmware/efi/systab\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @helper_efi_locate(i8* %0, i32 %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca [64 x i8]
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i64* %2, i64** %7
store i32* null, i32** %8
store i64 0, i64* %11
%12 = load i64*, i64** %7
store i64 0, i64* %12
%13 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), 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 %16, label %45
16: ; preds = %3
br label %17
17: ; preds = %35, %16
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%19 = load i32*, i32** %8
%20 = call i32* @fgets(i8* %18, i32 63, i32* %19)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %36
22: ; preds = %17
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%24 = load i8*, i8** %5
%25 = call i64 @strncmp(i8* %23, i8* %24, i32 6)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %22
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = call i8* @memchr(i8* %28, i8 signext 61, i32 %30)
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8* %32, i8** %10
%33 = load i8*, i8** %10
%34 = call i64 @strtol(i8* %33, i32* null, i32 0)
store i64 %34, i64* %11
br label %36
35: ; preds = %22
br label %17
36: ; preds = %27, %17
%37 = load i32*, i32** %8
%38 = call i32 @fclose(i32* %37)
%39 = load i64, i64* %11
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i64, i64* %11
%43 = load i64*, i64** %7
store i64 %42, i64* %43
store i32 0, i32* %4
br label %46
44: ; preds = %36
br label %45
45: ; preds = %44, %3
store i32 -1, i32* %4
br label %46
46: ; preds = %45, %41
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@.str = [25 x i8] c"/sys/firmware/efi/systab\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @helper_efi_locate(i8* %0, i32 %1, i64* nocapture writeonly %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
store i64 0, i64* %2
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %24, label %8
8: ; preds = %3, %13
%9 = call i32* @fgets(i8* %5, i32 63, i32* %6)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @fclose(i32* %6)
br label %24
13: ; preds = %8
%14 = call i64 @strncmp(i8* %5, i8* %0, i32 6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %8
16: ; preds = %13
%17 = call i32 @strlen(i8* %5)
%18 = call i8* @memchr(i8* %5, i8 signext 61, i32 %17)
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = call i64 @strtol(i8* nocapture %19, i32* null, i32 0)
%21 = call i32 @fclose(i32* %6)
%22 = icmp eq i64 %20, 0
br i1 %22, label %24, label %23
23: ; preds = %16
store i64 %20, i64* %2
br label %24
24: ; preds = %11, %3, %16, %23
%25 = phi i32 [ 0, %23 ], [ -1, %16 ], [ -1, %3 ], [ -1, %11 ]
ret i32 %25
}