#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*/ linebuf;
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 *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
size_t strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int address_from_efi(char *section, size_t *address) {
  FILE *systab;
  char linebuf[64];
  int ret;
  *address = 0;
  if ((systab = fopen("/sys/firmware/efi/systab", "r")) == NULL &&
      (systab = fopen("/proc/efi/systab", "r")) == NULL) {
    return (-1);
  }
  ret = (-2);
  while ((fgets(linebuf, sizeof(linebuf) - 1, systab)) != NULL) {
    char *addrp = strchr(linebuf, '=');
    if (addrp != NULL) {
      *(addrp++) = '\0';
      if (strcmp(linebuf, section) == 0) {
        *address = strtoul(addrp, NULL, 0);
        ret = 0;
        break;
      }
    }
  }
  fclose(systab);
  return ret;
}


x86 assembly (clang, -O0):
<address_from_efi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
lea    0xe7e(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c9 <address_from_efi+0x69>
lea    0xe78(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c9 <address_from_efi+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <address_from_efi+0x110>
movl   $0xfffffffe,-0x64(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x3f,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1261 <address_from_efi+0x101>
lea    -0x60(%rbp),%rdi
mov    $0x3d,%esi
call   1040 <strchr@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     125c <address_from_efi+0xfc>
mov    -0x70(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x70(%rbp)
movb   $0x0,(%rax)
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1257 <address_from_efi+0xf7>
mov    -0x70(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x64(%rbp)
jmp    1261 <address_from_efi+0x101>
jmp    125c <address_from_efi+0xfc>
jmp    11d0 <address_from_efi+0x70>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<address_from_efi>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,(%rsi)
lea    0xe7e(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11b5 <address_from_efi+0x55>
lea    0xe7e(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
je     124d <address_from_efi+0xed>
mov    %rsp,%rdi
mov    $0x3f,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
mov    $0xfffffffe,%r12d
test   %rax,%rax
je     1233 <address_from_efi+0xd3>
mov    %rsp,%rbx
jmp    11f5 <address_from_efi+0x95>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x3f,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1233 <address_from_efi+0xd3>
mov    %rbx,%rdi
mov    $0x3d,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11e0 <address_from_efi+0x80>
mov    %rax,%rbp
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11e0 <address_from_efi+0x80>
add    $0x1,%rbp
xor    %r12d,%r12d
mov    %rbp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,(%r14)
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %r12d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r12d
jmp    123b <address_from_efi+0xdb>

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

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