#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;
typedef int off_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);
int errno;
scalar_t__ mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/,
                int, int);
int open(char *, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
uint8_t *xh_mmap(off_t phys_addr, size_t length) {
  uint32_t page_offset = phys_addr % sysconf(_SC_PAGESIZE);
  uint8_t *vaddr;
  int fd;
  int __err;
  fd = open("/dev/mem", O_RDONLY);
  if (fd == -1)
    return NULL;
  vaddr = (uint8_t *)mmap(0, page_offset + length, PROT_READ, MAP_PRIVATE, fd,
                          phys_addr - page_offset);
  __err = errno;
  close(fd);
  if (vaddr == MAP_FAILED) {
    errno = __err;
    return NULL;
  }
  return vaddr + page_offset;
}


x86 assembly (clang, -O0):
<xh_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    119d <xh_mmap+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    1224 <xh_mmap+0xe4>
movslq -0x1c(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %eax,%esi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    0x2e09(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0xc(%rbp),%r9d
sub    -0x1c(%rbp),%r9d
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jne    1211 <xh_mmap+0xd1>
mov    -0x30(%rbp),%ecx
mov    0x2dc6(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1224 <xh_mmap+0xe4>
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):
<xh_mmap>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
mov    %edx,%r14d
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11dc <xh_mmap+0x9c>
mov    %eax,%ebx
add    %r14d,%r15d
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
sub    %r14d,%ebp
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebx,%r8d
mov    %ebp,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2e15(%rip),%r15
mov    (%r15),%r12d
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2df4(%rip),%rax
cmp    %rbp,(%rax)
je     11d9 <xh_mmap+0x99>
movslq %r14d,%rax
lea    0x0(,%rax,4),%rax
add    %rbp,%rax
jmp    11de <xh_mmap+0x9e>
mov    %r12d,(%r15)
xor    %eax,%eax
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
@errno = global i32 0
@MAP_FAILED = global i32* null
define i32* @xh_mmap(i32 %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
%10 = load i32, i32* %4
%11 = load i32, i32* @_SC_PAGESIZE
%12 = call i32 @sysconf(i32 %11)
%13 = srem i32 %10, %12
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 i32, i32* %4
%29 = load i32, i32* %6
%30 = sub nsw i32 %28, %29
%31 = call i64 @mmap(i32 0, i32 %24, i32 %25, i32 %26, i32 %27, i32 %30)
%32 = inttoptr i64 %31 to i32*
store i32* %32, i32** %7
%33 = load i32, i32* @errno
store i32 %33, i32* %9
%34 = load i32, i32* %8
%35 = call i32 @close(i32 %34)
%36 = load i32*, i32** %7
%37 = load i32*, i32** @MAP_FAILED
%38 = icmp eq i32* %36, %37
br i1 %38, label %39, label %41
39: ; preds = %19
%40 = load i32, i32* %9
store i32 %40, i32* @errno
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, %39, %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
@errno = global i32 0
@MAP_FAILED = global i32* null
define i32* @xh_mmap(i32 %0, i64 %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = srem i32 %0, %4
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %25, label %9
9: ; preds = %2
%10 = trunc i64 %1 to i32
%11 = add i32 %5, %10
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_PRIVATE
%14 = sub nsw i32 %0, %5
%15 = tail call i64 @mmap(i32 0, i32 %11, i32 %12, i32 %13, i32 %7, i32 %14)
%16 = inttoptr i64 %15 to i32*
%17 = load i32, i32* @errno
%18 = tail call i32 @close(i32 %7)
%19 = load i32*, i32** @MAP_FAILED
%20 = icmp eq i32* %19, %16
br i1 %20, label %21, label %22
21: ; preds = %9
store i32 %17, i32* @errno
br label %25
22: ; preds = %9
%23 = sext i32 %5 to i64
%24 = getelementptr inbounds i32, i32* %16, i64 %23
br label %25
25: ; preds = %2, %22, %21
%26 = phi i32* [ null, %21 ], [ %24, %22 ], [ null, %2 ]
ret i32* %26
}