#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*/ uint8_t;
struct xh_smbios_ctx {
  int dummy;
};

/* Variables and functions */
int errno;
scalar_t__ memcmp(int /*<<< orphan*/ *, char *, int);
int xh_efi_locate(char *, int, size_t *);
int /*<<< orphan*/ *xh_mmap(int, int);
int xh_smbios_entry_point_mmap(struct xh_smbios_ctx *, int /*<<< orphan*/ *,
                               int);
int /*<<< orphan*/ xh_unmmap(int /*<<< orphan*/ *, int);
int xh_locate_smbios_mmap(struct xh_smbios_ctx *smctx) {
  size_t loc = 0;
  uint8_t *vaddr;
  int rc = -1;
  int __err;
  rc = xh_efi_locate("SMBIOS", 6, &loc);
  if ((rc == 0) && (loc != 0)) {
    vaddr = xh_mmap(loc, 0x20);
    if (vaddr == NULL)
      return -1;
    rc = xh_smbios_entry_point_mmap(smctx, vaddr, 1);
    __err = errno;
    xh_unmmap(vaddr, 0x20);
    errno = __err;
    return rc;
  }
  vaddr = xh_mmap(0xF0000, 0x10000);
  if (vaddr == NULL)
    return -1;
  for (loc = 0; loc <= (0x10000 - 0x20); loc += 16) {
    if (memcmp(vaddr + loc, "_SM_", 4) == 0) {
      rc = xh_smbios_entry_point_mmap(smctx, vaddr + loc, 1);
      if (rc == 0)
        break;
    } else if (memcmp(vaddr + loc, "_DMI_", 5) == 0) {
      rc = xh_smbios_entry_point_mmap(smctx, vaddr + loc, 0);
      if (rc == 0)
        break;
    }
  }
  __err = errno;
  xh_unmmap(vaddr, 0x10000);
  errno = __err;
  return rc;
}


x86 assembly (clang, -O0):
<xh_locate_smbios_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
lea    0xe8e(%rip),%rdi
mov    $0x6,%esi
lea    -0x18(%rbp),%rdx
call   1070 <xh_efi_locate@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1209 <xh_locate_smbios_mmap+0xb9>
cmpq   $0x0,-0x18(%rbp)
je     1209 <xh_locate_smbios_mmap+0xb9>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    $0x20,%esi
call   1060 <xh_mmap@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c3 <xh_locate_smbios_mmap+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <xh_locate_smbios_mmap+0x1f9>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <xh_smbios_entry_point_mmap@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x20,%esi
call   1050 <xh_unmmap@plt>
mov    -0x28(%rbp),%ecx
mov    0x2de4(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1349 <xh_locate_smbios_mmap+0x1f9>
mov    $0xf0000,%edi
mov    $0x10000,%esi
call   1060 <xh_mmap@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1233 <xh_locate_smbios_mmap+0xe3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <xh_locate_smbios_mmap+0x1f9>
movq   $0x0,-0x18(%rbp)
cmpq   $0xffe0,-0x18(%rbp)
ja     131d <xh_locate_smbios_mmap+0x1cd>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
lea    0xda8(%rip),%rsi
mov    $0x4,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    12a7 <xh_locate_smbios_mmap+0x157>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <xh_smbios_entry_point_mmap@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    12a2 <xh_locate_smbios_mmap+0x152>
jmp    131d <xh_locate_smbios_mmap+0x1cd>
jmp    1307 <xh_locate_smbios_mmap+0x1b7>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
lea    0xd4f(%rip),%rsi
mov    $0x5,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    1302 <xh_locate_smbios_mmap+0x1b2>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
xor    %edx,%edx
call   1030 <xh_smbios_entry_point_mmap@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    12fd <xh_locate_smbios_mmap+0x1ad>
jmp    131d <xh_locate_smbios_mmap+0x1cd>
jmp    1302 <xh_locate_smbios_mmap+0x1b2>
jmp    1307 <xh_locate_smbios_mmap+0x1b7>
jmp    130c <xh_locate_smbios_mmap+0x1bc>
mov    -0x18(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x18(%rbp)
jmp    123b <xh_locate_smbios_mmap+0xeb>
mov    0x2cbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x10000,%esi
call   1050 <xh_unmmap@plt>
mov    -0x28(%rbp),%ecx
mov    0x2c9f(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xh_locate_smbios_mmap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
movq   $0x0,(%rsp)
lea    0xe93(%rip),%rdi
mov    %rsp,%rdx
mov    $0x6,%esi
call   1070 <xh_efi_locate@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11c8 <xh_locate_smbios_mmap+0x78>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     11c8 <xh_locate_smbios_mmap+0x78>
mov    $0x20,%esi
call   1060 <xh_mmap@plt>
test   %rax,%rax
je     1291 <xh_locate_smbios_mmap+0x141>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1030 <xh_smbios_entry_point_mmap@plt>
mov    %eax,%ebp
mov    0x2e28(%rip),%rax
mov    (%rax),%r14d
mov    %rbx,%rdi
mov    $0x20,%esi
jmp    12ac <xh_locate_smbios_mmap+0x15c>
mov    $0xf0000,%edi
mov    $0x10000,%esi
call   1060 <xh_mmap@plt>
test   %rax,%rax
je     1291 <xh_locate_smbios_mmap+0x141>
mov    %rax,%r13
movq   $0x0,(%rsp)
xor    %eax,%eax
lea    0xe13(%rip),%r15
lea    0xe11(%rip),%r12
jmp    122d <xh_locate_smbios_mmap+0xdd>
nopl   (%rax)
mov    %r14,%rdi
mov    $0x1,%edx
call   1030 <xh_smbios_entry_point_mmap@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1298 <xh_locate_smbios_mmap+0x148>
mov    (%rsp),%rax
add    $0x10,%rax
mov    %rax,(%rsp)
mov    %ebx,%ebp
cmp    $0xffe1,%rax
jae    129a <xh_locate_smbios_mmap+0x14a>
lea    0x0(,%rax,4),%rdi
add    %r13,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1040 <memcmp@plt>
mov    (%rsp),%rcx
lea    0x0(,%rcx,4),%rsi
add    %r13,%rsi
test   %rax,%rax
je     1200 <xh_locate_smbios_mmap+0xb0>
mov    %rsi,%rdi
mov    %r12,%rsi
mov    $0x5,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
jne    1217 <xh_locate_smbios_mmap+0xc7>
mov    (%rsp),%rax
lea    0x0(,%rax,4),%rsi
add    %r13,%rsi
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %edx,%edx
call   1030 <xh_smbios_entry_point_mmap@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1217 <xh_locate_smbios_mmap+0xc7>
jmp    129a <xh_locate_smbios_mmap+0x14a>
mov    $0xffffffff,%ebp
jmp    12bb <xh_locate_smbios_mmap+0x16b>
xor    %ebp,%ebp
mov    0x2d3f(%rip),%rax
mov    (%rax),%r14d
mov    %r13,%rdi
mov    $0x10000,%esi
call   1050 <xh_unmmap@plt>
mov    0x2d28(%rip),%rax
mov    %r14d,(%rax)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.xh_smbios_ctx = type { i32 }
@.str = [7 x i8] c"SMBIOS\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"_SM_\00"
@.str.2 = [6 x i8] c"_DMI_\00"
define i32 @xh_locate_smbios_mmap(%struct.xh_smbios_ctx* %0) {
%2 = alloca i32
%3 = alloca %struct.xh_smbios_ctx*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store %struct.xh_smbios_ctx* %0, %struct.xh_smbios_ctx** %3
store i64 0, i64* %4
store i32 -1, i32* %6
%8 = call i32 @xh_efi_locate(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6, i64* %4)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %30
11: ; preds = %1
%12 = load i64, i64* %4
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = load i64, i64* %4
%16 = trunc i64 %15 to i32
%17 = call i32* @xh_mmap(i32 %16, i32 32)
store i32* %17, i32** %5
%18 = load i32*, i32** %5
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 -1, i32* %2
br label %82
21: ; preds = %14
%22 = load %struct.xh_smbios_ctx*, %struct.xh_smbios_ctx** %3
%23 = load i32*, i32** %5
%24 = call i32 @xh_smbios_entry_point_mmap(%struct.xh_smbios_ctx* %22, i32* %23, i32 1)
store i32 %24, i32* %6
%25 = load i32, i32* @errno
store i32 %25, i32* %7
%26 = load i32*, i32** %5
%27 = call i32 @xh_unmmap(i32* %26, i32 32)
%28 = load i32, i32* %7
store i32 %28, i32* @errno
%29 = load i32, i32* %6
store i32 %29, i32* %2
br label %82
30: ; preds = %11, %1
%31 = call i32* @xh_mmap(i32 983040, i32 65536)
store i32* %31, i32** %5
%32 = load i32*, i32** %5
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -1, i32* %2
br label %82
35: ; preds = %30
store i64 0, i64* %4
br label %36
36: ; preds = %73, %35
%37 = load i64, i64* %4
%38 = icmp ule i64 %37, 65504
br i1 %38, label %39, label %76
39: ; preds = %36
%40 = load i32*, i32** %5
%41 = load i64, i64* %4
%42 = getelementptr inbounds i32, i32* %40, i64 %41
%43 = call i64 @memcmp(i32* %42, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %39
%46 = load %struct.xh_smbios_ctx*, %struct.xh_smbios_ctx** %3
%47 = load i32*, i32** %5
%48 = load i64, i64* %4
%49 = getelementptr inbounds i32, i32* %47, i64 %48
%50 = call i32 @xh_smbios_entry_point_mmap(%struct.xh_smbios_ctx* %46, i32* %49, i32 1)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %45
br label %76
54: ; preds = %45
br label %72
55: ; preds = %39
%56 = load i32*, i32** %5
%57 = load i64, i64* %4
%58 = getelementptr inbounds i32, i32* %56, i64 %57
%59 = call i64 @memcmp(i32* %58, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %71
61: ; preds = %55
%62 = load %struct.xh_smbios_ctx*, %struct.xh_smbios_ctx** %3
%63 = load i32*, i32** %5
%64 = load i64, i64* %4
%65 = getelementptr inbounds i32, i32* %63, i64 %64
%66 = call i32 @xh_smbios_entry_point_mmap(%struct.xh_smbios_ctx* %62, i32* %65, i32 0)
store i32 %66, i32* %6
%67 = load i32, i32* %6
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %61
br label %76
70: ; preds = %61
br label %71
71: ; preds = %70, %55
br label %72
72: ; preds = %71, %54
br label %73
73: ; preds = %72
%74 = load i64, i64* %4
%75 = add i64 %74, 16
store i64 %75, i64* %4
br label %36
76: ; preds = %69, %53, %36
%77 = load i32, i32* @errno
store i32 %77, i32* %7
%78 = load i32*, i32** %5
%79 = call i32 @xh_unmmap(i32* %78, i32 65536)
%80 = load i32, i32* %7
store i32 %80, i32* @errno
%81 = load i32, i32* %6
store i32 %81, i32* %2
br label %82
82: ; preds = %76, %34, %21, %20
%83 = load i32, i32* %2
ret i32 %83
}

LLVM IR (-O2):
%struct.xh_smbios_ctx = type { i32 }
@.str = [7 x i8] c"SMBIOS\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"_SM_\00"
@.str.2 = [6 x i8] c"_DMI_\00"
define i32 @xh_locate_smbios_mmap(%struct.xh_smbios_ctx* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%4 = call i32 @xh_efi_locate(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6, i64* %2)
%5 = icmp eq i32 %4, 0
%6 = load i64, i64* %2
%7 = icmp ne i64 %6, 0
%8 = select i1 %5, i1 %7, i1 false
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = trunc i64 %6 to i32
%11 = call i32* @xh_mmap(i32 %10, i32 32)
%12 = icmp eq i32* %11, null
br i1 %12, label %52, label %13
13: ; preds = %9
%14 = call i32 @xh_smbios_entry_point_mmap(%struct.xh_smbios_ctx* %0, i32* %11, i32 1)
%15 = load i32, i32* @errno
%16 = call i32 @xh_unmmap(i32* %11, i32 32)
br label %49
17: ; preds = %1
%18 = call i32* @xh_mmap(i32 983040, i32 65536)
%19 = icmp eq i32* %18, null
br i1 %19, label %52, label %20
20: ; preds = %17
store i64 0, i64* %2
br label %21
21: ; preds = %20, %40
%22 = phi i32 [ %4, %20 ], [ %41, %40 ]
%23 = phi i64 [ 0, %20 ], [ %43, %40 ]
%24 = getelementptr inbounds i32, i32* %18, i64 %23
%25 = call i64 @memcmp(i32* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%26 = icmp eq i64 %25, 0
%27 = load i64, i64* %2
%28 = getelementptr inbounds i32, i32* %18, i64 %27
br i1 %26, label %29, label %32
29: ; preds = %21
%30 = call i32 @xh_smbios_entry_point_mmap(%struct.xh_smbios_ctx* %0, i32* %28, i32 1)
%31 = icmp eq i32 %30, 0
br i1 %31, label %45, label %40
32: ; preds = %21
%33 = call i64 @memcmp(i32* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i64, i64* %2
%37 = getelementptr inbounds i32, i32* %18, i64 %36
%38 = call i32 @xh_smbios_entry_point_mmap(%struct.xh_smbios_ctx* %0, i32* %37, i32 0)
%39 = icmp eq i32 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %29, %35, %32
%41 = phi i32 [ %30, %29 ], [ %38, %35 ], [ %22, %32 ]
%42 = load i64, i64* %2
%43 = add i64 %42, 16
store i64 %43, i64* %2
%44 = icmp ult i64 %43, 65505
br i1 %44, label %21, label %45
45: ; preds = %35, %29, %40
%46 = phi i32 [ 0, %29 ], [ 0, %35 ], [ %41, %40 ]
%47 = load i32, i32* @errno
%48 = call i32 @xh_unmmap(i32* %18, i32 65536)
br label %49
49: ; preds = %13, %45
%50 = phi i32 [ %47, %45 ], [ %15, %13 ]
%51 = phi i32 [ %46, %45 ], [ %14, %13 ]
store i32 %50, i32* @errno
br label %52
52: ; preds = %49, %17, %9
%53 = phi i32 [ -1, %9 ], [ -1, %17 ], [ %51, %49 ]
ret i32 %53
}