#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 */
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGE_SIZE;
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int, int);
int sysconf(int /*<<< orphan*/);
size_t map_mem(int fd, void **va, off_t pa, size_t length) {
  off_t pa_aligned = pa & ~(sysconf(_SC_PAGE_SIZE) - 1);
  length = pa - pa_aligned + length;
  *va = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, pa_aligned);
  return length;
}


x86 assembly (clang, -O0):
<map_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x24(%rbp),%eax
cltq
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
mov    0x2e52(%rip),%rax
or     (%rax),%edx
mov    0x2e61(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
mov    -0x24(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_mem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
neg    %eax
and    %ebp,%eax
sub    %eax,%ebp
movslq %ebp,%rbp
add    %rbx,%rbp
mov    0x2ea5(%rip),%rcx
mov    0x2e76(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e83(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r15d,%r8d
mov    %eax,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@_SC_PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i64 @map_mem(i32 %0, i8** nocapture writeonly %1, i32 %2, i64 %3) {
%5 = load i32, i32* @_SC_PAGE_SIZE
%6 = tail call i32 @sysconf(i32 %5)
%7 = sub i32 0, %6
%8 = and i32 %7, %2
%9 = sub nsw i32 %2, %8
%10 = sext i32 %9 to i64
%11 = add i64 %10, %3
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @MAP_SHARED
%16 = tail call i8* @mmap(i32* null, i64 %11, i32 %14, i32 %15, i32 %0, i32 %8)
store i8* %16, i8** %1
ret i64 %11
}