#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_acpi_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_process_rsdp_mmap(struct xh_acpi_ctx *, int /*<<< orphan*/ *);
int /*<<< orphan*/ xh_unmmap(int /*<<< orphan*/ *, int);
int xh_locate_acpi_mmap(struct xh_acpi_ctx *acctx) {
  size_t loc = 0;
  uint8_t *vaddr;
  int rc = -1;
  int __err;
  rc = xh_efi_locate("ACPI20", 6, &loc);
  if ((rc == 0) && (loc != 0)) {
    vaddr = xh_mmap(loc, 0x24);
    if (vaddr == NULL)
      return -1;
    rc = xh_process_rsdp_mmap(acctx, vaddr);
    __err = errno;
    xh_unmmap(vaddr, 0x24);
    errno = __err;
    return rc;
  }
  vaddr = xh_mmap(0xF0000, 0x10000);
  if (vaddr == NULL)
    return -1;
  for (loc = 0; loc <= (0x10000 - 0x24); loc += 16) {
    if (memcmp(vaddr + loc, "RSD PTR ", 8) == 0) {
      rc = xh_process_rsdp_mmap(acctx, vaddr + loc);
      if (rc == 0)
        break;
    }
  }
  __err = errno;
  xh_unmmap(vaddr, 0x10000);
  errno = __err;
  return rc;
}


x86 assembly (clang, -O0):
<xh_locate_acpi_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   1060 <xh_efi_locate@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1204 <xh_locate_acpi_mmap+0xb4>
cmpq   $0x0,-0x18(%rbp)
je     1204 <xh_locate_acpi_mmap+0xb4>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    $0x24,%esi
call   1050 <xh_mmap@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c3 <xh_locate_acpi_mmap+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <xh_locate_acpi_mmap+0x18f>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <xh_process_rsdp_mmap@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x24,%esi
call   1040 <xh_unmmap@plt>
mov    -0x28(%rbp),%ecx
mov    0x2de9(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12df <xh_locate_acpi_mmap+0x18f>
mov    $0xf0000,%edi
mov    $0x10000,%esi
call   1050 <xh_mmap@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122e <xh_locate_acpi_mmap+0xde>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <xh_locate_acpi_mmap+0x18f>
movq   $0x0,-0x18(%rbp)
cmpq   $0xffdc,-0x18(%rbp)
ja     12b3 <xh_locate_acpi_mmap+0x163>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
lea    0xdad(%rip),%rsi
mov    $0x8,%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
jne    129d <xh_locate_acpi_mmap+0x14d>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1070 <xh_process_rsdp_mmap@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1298 <xh_locate_acpi_mmap+0x148>
jmp    12b3 <xh_locate_acpi_mmap+0x163>
jmp    129d <xh_locate_acpi_mmap+0x14d>
jmp    12a2 <xh_locate_acpi_mmap+0x152>
mov    -0x18(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x18(%rbp)
jmp    1236 <xh_locate_acpi_mmap+0xe6>
mov    0x2d26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x10000,%esi
call   1040 <xh_unmmap@plt>
mov    -0x28(%rbp),%ecx
mov    0x2d09(%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_acpi_mmap>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
movq   $0x0,(%rsp)
lea    0xe97(%rip),%rdi
mov    %rsp,%rdx
mov    $0x6,%esi
call   1060 <xh_efi_locate@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11bf <xh_locate_acpi_mmap+0x6f>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     11bf <xh_locate_acpi_mmap+0x6f>
mov    $0x24,%esi
call   1050 <xh_mmap@plt>
test   %rax,%rax
je     1255 <xh_locate_acpi_mmap+0x105>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1070 <xh_process_rsdp_mmap@plt>
mov    %eax,%ebp
mov    0x2e31(%rip),%rax
mov    (%rax),%r14d
mov    %rbx,%rdi
mov    $0x24,%esi
jmp    1244 <xh_locate_acpi_mmap+0xf4>
mov    $0xf0000,%edi
mov    $0x10000,%esi
call   1050 <xh_mmap@plt>
test   %rax,%rax
je     1255 <xh_locate_acpi_mmap+0x105>
mov    %rax,%rbx
movq   $0x0,(%rsp)
xor    %eax,%eax
lea    0xe1c(%rip),%r15
jmp    1204 <xh_locate_acpi_mmap+0xb4>
nopl   (%rax)
mov    (%rsp),%rax
add    $0x10,%rax
mov    %rax,(%rsp)
cmp    $0xffdd,%rax
jae    1232 <xh_locate_acpi_mmap+0xe2>
lea    (%rbx,%rax,4),%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
jne    11f0 <xh_locate_acpi_mmap+0xa0>
mov    (%rsp),%rax
lea    (%rbx,%rax,4),%rsi
mov    %r14,%rdi
call   1070 <xh_process_rsdp_mmap@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11f0 <xh_locate_acpi_mmap+0xa0>
xor    %ebp,%ebp
mov    0x2da7(%rip),%rax
mov    (%rax),%r14d
mov    %rbx,%rdi
mov    $0x10000,%esi
call   1040 <xh_unmmap@plt>
mov    0x2d90(%rip),%rax
mov    %r14d,(%rax)
jmp    125a <xh_locate_acpi_mmap+0x10a>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.xh_acpi_ctx = type { i32 }
@.str = [7 x i8] c"ACPI20\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"RSD PTR \00"
define i32 @xh_locate_acpi_mmap(%struct.xh_acpi_ctx* %0) {
%2 = alloca i32
%3 = alloca %struct.xh_acpi_ctx*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store %struct.xh_acpi_ctx* %0, %struct.xh_acpi_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 36)
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 %65
21: ; preds = %14
%22 = load %struct.xh_acpi_ctx*, %struct.xh_acpi_ctx** %3
%23 = load i32*, i32** %5
%24 = call i32 @xh_process_rsdp_mmap(%struct.xh_acpi_ctx* %22, i32* %23)
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 36)
%28 = load i32, i32* %7
store i32 %28, i32* @errno
%29 = load i32, i32* %6
store i32 %29, i32* %2
br label %65
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 %65
35: ; preds = %30
store i64 0, i64* %4
br label %36
36: ; preds = %56, %35
%37 = load i64, i64* %4
%38 = icmp ule i64 %37, 65500
br i1 %38, label %39, label %59
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 ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %39
%46 = load %struct.xh_acpi_ctx*, %struct.xh_acpi_ctx** %3
%47 = load i32*, i32** %5
%48 = load i64, i64* %4
%49 = getelementptr inbounds i32, i32* %47, i64 %48
%50 = call i32 @xh_process_rsdp_mmap(%struct.xh_acpi_ctx* %46, i32* %49)
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 %59
54: ; preds = %45
br label %55
55: ; preds = %54, %39
br label %56
56: ; preds = %55
%57 = load i64, i64* %4
%58 = add i64 %57, 16
store i64 %58, i64* %4
br label %36
59: ; preds = %53, %36
%60 = load i32, i32* @errno
store i32 %60, i32* %7
%61 = load i32*, i32** %5
%62 = call i32 @xh_unmmap(i32* %61, i32 65536)
%63 = load i32, i32* %7
store i32 %63, i32* @errno
%64 = load i32, i32* %6
store i32 %64, i32* %2
br label %65
65: ; preds = %59, %34, %21, %20
%66 = load i32, i32* %2
ret i32 %66
}

LLVM IR (-O2):
%struct.xh_acpi_ctx = type { i32 }
@.str = [7 x i8] c"ACPI20\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"RSD PTR \00"
define i32 @xh_locate_acpi_mmap(%struct.xh_acpi_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 36)
%12 = icmp eq i32* %11, null
br i1 %12, label %44, label %13
13: ; preds = %9
%14 = call i32 @xh_process_rsdp_mmap(%struct.xh_acpi_ctx* %0, i32* %11)
%15 = load i32, i32* @errno
%16 = call i32 @xh_unmmap(i32* %11, i32 36)
br label %41
17: ; preds = %1
%18 = call i32* @xh_mmap(i32 983040, i32 65536)
%19 = icmp eq i32* %18, null
br i1 %19, label %44, label %20
20: ; preds = %17
store i64 0, i64* %2
br label %21
21: ; preds = %20, %32
%22 = phi i32 [ %4, %20 ], [ %33, %32 ]
%23 = phi i64 [ 0, %20 ], [ %35, %32 ]
%24 = getelementptr inbounds i32, i32* %18, i64 %23
%25 = call i64 @memcmp(i32* %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 8)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = load i64, i64* %2
%29 = getelementptr inbounds i32, i32* %18, i64 %28
%30 = call i32 @xh_process_rsdp_mmap(%struct.xh_acpi_ctx* %0, i32* %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %21, %27
%33 = phi i32 [ %30, %27 ], [ %22, %21 ]
%34 = load i64, i64* %2
%35 = add i64 %34, 16
store i64 %35, i64* %2
%36 = icmp ult i64 %35, 65501
br i1 %36, label %21, label %37
37: ; preds = %27, %32
%38 = phi i32 [ 0, %27 ], [ %33, %32 ]
%39 = load i32, i32* @errno
%40 = call i32 @xh_unmmap(i32* %18, i32 65536)
br label %41
41: ; preds = %13, %37
%42 = phi i32 [ %39, %37 ], [ %15, %13 ]
%43 = phi i32 [ %38, %37 ], [ %14, %13 ]
store i32 %42, i32* @errno
br label %44
44: ; preds = %41, %17, %9
%45 = phi i32 [ -1, %9 ], [ -1, %17 ], [ %43, %41 ]
ret i32 %45
}