#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;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/,
                int, int);
int open(char *, int /*<<< orphan*/);
size_t sysconf(int /*<<< orphan*/);
uint8_t *helper_mmap(size_t phys_addr, size_t length) {
  uint32_t page_offset = phys_addr % sysconf(_SC_PAGESIZE);
  uint8_t *addr;
  int fd;
  fd = open("/dev/mem", O_RDONLY);
  if (fd == -1)
    return NULL;
  addr = (uint8_t *)mmap(0, page_offset + length, PROT_READ, MAP_PRIVATE, fd,
                         phys_addr - page_offset);
  close(fd);
  if (addr == MAP_FAILED)
    return NULL;
  return addr + page_offset;
}


x86 assembly (clang, -O0):
<helper_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
xor    %edx,%edx
div    %rcx
mov    %edx,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11a6 <helper_mmap+0x66>
movq   $0x0,-0x8(%rbp)
jmp    121b <helper_mmap+0xdb>
movslq -0x1c(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %eax,%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    0x2e08(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rdi
sub    %rdi,%rax
mov    %eax,%r9d
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    0x2dce(%rip),%rcx
cmp    (%rcx),%rax
jne    1208 <helper_mmap+0xc8>
movq   $0x0,-0x8(%rbp)
jmp    121b <helper_mmap+0xdb>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<helper_mmap>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,%r15
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1195 <helper_mmap+0x55>
mov    %eax,%r12d
mov    %r15,%rax
or     %rbx,%rax
shr    $0x20,%rax
je     1199 <helper_mmap+0x59>
mov    %rbx,%rax
xor    %edx,%edx
div    %r15
mov    %rdx,%r15
jmp    11a3 <helper_mmap+0x63>
xor    %ebp,%ebp
jmp    11eb <helper_mmap+0xab>
mov    %ebx,%eax
xor    %edx,%edx
div    %r15d
mov    %edx,%r15d
add    %r15d,%r14d
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    0x2e12(%rip),%rax
mov    (%rax),%ecx
sub    %r15d,%ebx
xor    %ebp,%ebp
xor    %edi,%edi
mov    %r14d,%esi
mov    %r12d,%r8d
mov    %ebx,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    %r12d,%edi
call   1040 <close@plt>
mov    0x2de1(%rip),%rax
cmp    %rbx,(%rax)
je     11eb <helper_mmap+0xab>
movslq %r15d,%rax
lea    (%rbx,%rax,4),%rbp
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i32* null
define i32* @helper_mmap(i64 %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i64 %0, i64* %4
store i64 %1, i64* %5
%9 = load i64, i64* %4
%10 = load i32, i32* @_SC_PAGESIZE
%11 = call i64 @sysconf(i32 %10)
%12 = urem i64 %9, %11
%13 = trunc i64 %12 to i32
store i32 %13, i32* %6
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %2
store i32* null, i32** %3
br label %46
19: ; preds = %2
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = load i64, i64* %5
%23 = add i64 %21, %22
%24 = trunc i64 %23 to i32
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_PRIVATE
%27 = load i32, i32* %8
%28 = load i64, i64* %4
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = sub i64 %28, %30
%32 = trunc i64 %31 to i32
%33 = call i64 @mmap(i32 0, i32 %24, i32 %25, i32 %26, i32 %27, i32 %32)
%34 = inttoptr i64 %33 to i32*
store i32* %34, i32** %7
%35 = load i32, i32* %8
%36 = call i32 @close(i32 %35)
%37 = load i32*, i32** %7
%38 = load i32*, i32** @MAP_FAILED
%39 = icmp eq i32* %37, %38
br i1 %39, label %40, label %41
40: ; preds = %19
store i32* null, i32** %3
br label %46
41: ; preds = %19
%42 = load i32*, i32** %7
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
store i32* %45, i32** %3
br label %46
46: ; preds = %41, %40, %18
%47 = load i32*, i32** %3
ret i32* %47
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i32* null
define i32* @helper_mmap(i64 %0, i64 %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i64 @sysconf(i32 %3)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %25, label %8
8: ; preds = %2
%9 = urem i64 %0, %4
%10 = add i64 %9, %1
%11 = trunc i64 %10 to i32
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_PRIVATE
%14 = sub i64 %0, %9
%15 = trunc i64 %14 to i32
%16 = tail call i64 @mmap(i32 0, i32 %11, i32 %12, i32 %13, i32 %6, i32 %15)
%17 = inttoptr i64 %16 to i32*
%18 = tail call i32 @close(i32 %6)
%19 = load i32*, i32** @MAP_FAILED
%20 = icmp eq i32* %19, %17
br i1 %20, label %25, label %21
21: ; preds = %8
%22 = shl i64 %9, 32
%23 = ashr exact i64 %22, 32
%24 = getelementptr inbounds i32, i32* %17, i64 %23
br label %25
25: ; preds = %8, %2, %21
%26 = phi i32* [ %24, %21 ], [ null, %2 ], [ null, %8 ]
ret i32* %26
}