#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*/ off_t;
typedef int /*<<< orphan*/ caddr_t;

/* Variables and functions */
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int getpagesize();
void *mmap(int /*<<< orphan*/, unsigned long, int, int, int,
           int /*<<< orphan*/);
void *map_dev_mem(int fd, unsigned long Base, unsigned long Size) {
  void *base;
  int mapflags = MAP_SHARED;
  unsigned long realBase, alignOff;
  realBase = Base & ~(getpagesize() - 1);
  alignOff = Base - realBase;
  base = mmap((caddr_t)0, Size + alignOff, PROT_READ | PROT_WRITE, mapflags, fd,
              (off_t)realBase);
  return (void *)((char *)base + alignOff);
}


x86 assembly (clang, -O0):
<map_dev_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
movslq %ecx,%rcx
and    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
mov    0x2e48(%rip),%rax
or     (%rax),%edx
mov    -0x24(%rbp),%ecx
mov    -0x4(%rbp),%r8d
mov    -0x30(%rbp),%rax
mov    %eax,%r9d
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_dev_mem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb1(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1040 <getpagesize@plt>
neg    %eax
movslq %eax,%r9
and    %rbx,%r9
sub    %r9,%rbx
add    %rbx,%r14
mov    0x2ea3(%rip),%rax
mov    0x2e74(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebp,%ecx
mov    %r15d,%r8d
call   1030 <mmap@plt>
add    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAP_SHARED = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i8* @map_dev_mem(i32 %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%11 = load i32, i32* @MAP_SHARED
store i32 %11, i32* %8
%12 = load i64, i64* %5
%13 = call i32 (...) @getpagesize()
%14 = sub nsw i32 %13, 1
%15 = xor i32 %14, -1
%16 = sext i32 %15 to i64
%17 = and i64 %12, %16
store i64 %17, i64* %9
%18 = load i64, i64* %5
%19 = load i64, i64* %9
%20 = sub i64 %18, %19
store i64 %20, i64* %10
%21 = load i64, i64* %6
%22 = load i64, i64* %10
%23 = add i64 %21, %22
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %24, %25
%27 = load i32, i32* %8
%28 = load i32, i32* %4
%29 = load i64, i64* %9
%30 = trunc i64 %29 to i32
%31 = call i8* @mmap(i32 0, i64 %23, i32 %26, i32 %27, i32 %28, i32 %30)
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = load i64, i64* %10
%34 = getelementptr inbounds i8, i8* %32, i64 %33
ret i8* %34
}

LLVM IR (-O2):
@MAP_SHARED = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i8* @map_dev_mem(i32 %0, i64 %1, i64 %2) {
%4 = load i32, i32* @MAP_SHARED
%5 = tail call i32 (...) @getpagesize()
%6 = sub i32 0, %5
%7 = sext i32 %6 to i64
%8 = and i64 %7, %1
%9 = sub i64 %1, %8
%10 = add i64 %9, %2
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = trunc i64 %8 to i32
%15 = tail call i8* @mmap(i32 0, i64 %10, i32 %13, i32 %4, i32 %0, i32 %14)
%16 = getelementptr inbounds i8, i8* %15, i64 %9
ret i8* %16
}