#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 off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int getpagesize();
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int, int);
int open(char *, int);
int map_mem(void **mem_virt, off_t phys_address, int size) {
  off_t PageOffset, PageAddress;
  int PagesSize;
  int mem_dev;
  mem_dev = open("/dev/mem", O_RDWR | O_SYNC);
  if (mem_dev < 0)
    return mem_dev;
  PageOffset = phys_address % getpagesize();
  PageAddress = phys_address - PageOffset;
  PagesSize = (((size - 1) / getpagesize()) + 1) * getpagesize();
  *mem_virt = mmap(NULL, (size_t)PagesSize, PROT_READ | PROT_WRITE, MAP_SHARED,
                   mem_dev, PageAddress);
  if (*mem_virt == MAP_FAILED)
    return -1;
  *mem_virt += PageOffset;
  return 0;
}


x86 assembly (clang, -O0):
<map_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    0x2e7e(%rip),%rax
or     (%rax),%esi
lea    0xea5(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1178 <map_mem+0x48>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <map_mem+0x110>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1050 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1050 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1050 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rsi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    0x2de3(%rip),%rax
or     (%rax),%edx
mov    0x2e02(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%r8d
mov    -0x20(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2da7(%rip),%rcx
cmp    (%rcx),%rax
jne    1226 <map_mem+0xf6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <map_mem+0x110>
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
movslq %edx,%rdx
add    %rdx,%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_mem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e8f(%rip),%rax
mov    0x2e80(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea5(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%r13d
test   %eax,%eax
js     11e2 <map_mem+0xb2>
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <getpagesize@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
mov    %edx,%r12d
sub    %edx,%ebp
add    $0xffffffff,%ebx
xor    %eax,%eax
call   1050 <getpagesize@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
mov    %eax,%ebx
add    $0x1,%ebx
xor    %eax,%eax
call   1050 <getpagesize@plt>
imul   %ebx,%eax
movslq %eax,%rsi
mov    0x2e51(%rip),%rax
mov    0x2e12(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r13d,%r8d
mov    %ebp,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
je     11e7 <map_mem+0xb7>
movslq %r12d,%rcx
add    %rcx,%rax
mov    %rax,(%r14)
jmp    11ed <map_mem+0xbd>
mov    %r13d,%r15d
jmp    11ed <map_mem+0xbd>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @map_mem(i8** %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_SYNC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i32, i32* %11
store i32 %19, i32* %4
br label %55
20: ; preds = %3
%21 = load i32, i32* %6
%22 = call i32 (...) @getpagesize()
%23 = srem i32 %21, %22
store i32 %23, i32* %8
%24 = load i32, i32* %6
%25 = load i32, i32* %8
%26 = sub nsw i32 %24, %25
store i32 %26, i32* %9
%27 = load i32, i32* %7
%28 = sub nsw i32 %27, 1
%29 = call i32 (...) @getpagesize()
%30 = sdiv i32 %28, %29
%31 = add nsw i32 %30, 1
%32 = call i32 (...) @getpagesize()
%33 = mul nsw i32 %31, %32
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @PROT_WRITE
%38 = or i32 %36, %37
%39 = load i32, i32* @MAP_SHARED
%40 = load i32, i32* %11
%41 = load i32, i32* %9
%42 = call i8* @mmap(i32* null, i64 %35, i32 %38, i32 %39, i32 %40, i32 %41)
%43 = load i8**, i8*** %5
store i8* %42, i8** %43
%44 = load i8**, i8*** %5
%45 = load i8*, i8** %44
%46 = load i8*, i8** @MAP_FAILED
%47 = icmp eq i8* %45, %46
br i1 %47, label %48, label %49
48: ; preds = %20
store i32 -1, i32* %4
br label %55
49: ; preds = %20
%50 = load i32, i32* %8
%51 = load i8**, i8*** %5
%52 = load i8*, i8** %51
%53 = sext i32 %50 to i64
%54 = getelementptr i8, i8* %52, i64 %53
store i8* %54, i8** %51
store i32 0, i32* %4
br label %55
55: ; preds = %49, %48, %18
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @map_mem(i8** nocapture writeonly %0, i32 %1, i32 %2) {
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_SYNC
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %30, label %9
9: ; preds = %3
%10 = tail call i32 (...) @getpagesize()
%11 = srem i32 %1, %10
%12 = sub nsw i32 %1, %11
%13 = add nsw i32 %2, -1
%14 = tail call i32 (...) @getpagesize()
%15 = sdiv i32 %13, %14
%16 = add nsw i32 %15, 1
%17 = tail call i32 (...) @getpagesize()
%18 = mul nsw i32 %16, %17
%19 = sext i32 %18 to i64
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %21, %20
%23 = load i32, i32* @MAP_SHARED
%24 = tail call i8* @mmap(i32* null, i64 %19, i32 %22, i32 %23, i32 %7, i32 %12)
store i8* %24, i8** %0
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %30, label %27
27: ; preds = %9
%28 = sext i32 %11 to i64
%29 = getelementptr i8, i8* %24, i64 %28
store i8* %29, i8** %0
br label %30
30: ; preds = %9, %3, %27
%31 = phi i32 [ 0, %27 ], [ %7, %3 ], [ -1, %9 ]
ret i32 %31
}