#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 */

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ errno;
void *mmap(int /*<<< orphan*/, unsigned long, int, int /*<<< orphan*/, int,
           unsigned long);
int /*<<< orphan*/ printf(char *, unsigned long, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void *getVirtualAddress(unsigned long addr, int fd) {
  void *mapBase = mmap(0, 4096UL, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                       (addr & ~(4096UL - 1)));
  if (mapBase == (void *)-1) {
    printf("Memory mapping for %lu failed with: %s\n", addr, strerror(errno));
    return (void *)-1;
  } else
    return mapBase + (addr & (4096UL - 1));
}


x86 assembly (clang, -O0):
<getVirtualAddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2eb2(%rip),%rax
mov    (%rax),%edx
mov    0x2e79(%rip),%rax
or     (%rax),%edx
mov    0x2e90(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x10(%rbp),%r9
and    $0xfffffffffffff000,%r9
xor    %edi,%edi
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    11c2 <getVirtualAddress+0x92>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    11d8 <getVirtualAddress+0xa8>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
and    $0xfff,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getVirtualAddress>:
push   %rbx
mov    0x2ec0(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %esi,%r8d
mov    %rdi,%rbx
mov    0x2e98(%rip),%rax
mov    (%rax),%ecx
mov    %rdi,%r9
and    $0xfffffffffffff000,%r9
mov    $0x1000,%esi
xor    %edi,%edi
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1179 <getVirtualAddress+0x49>
and    $0xfff,%ebx
add    %rbx,%rax
pop    %rbx
ret
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe72(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
mov    $0xffffffffffffffff,%rax
pop    %rbx
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str = [40 x i8] c"Memory mapping for %lu failed with: %s\0A\00"
@errno = global i32 0
define i8* @getVirtualAddress(i64 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
store i64 %0, i64* %4
store i32 %1, i32* %5
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %7, %8
%10 = load i32, i32* @MAP_SHARED
%11 = load i32, i32* %5
%12 = load i64, i64* %4
%13 = and i64 %12, -4096
%14 = call i8* @mmap(i32 0, i64 4096, i32 %9, i32 %10, i32 %11, i64 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, inttoptr (i64 -1 to i8*)
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i64, i64* %4
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i64 %18, i32 %20)
store i8* inttoptr (i64 -1 to i8*), i8** %3
br label %27
22: ; preds = %2
%23 = load i8*, i8** %6
%24 = load i64, i64* %4
%25 = and i64 %24, 4095
%26 = getelementptr i8, i8* %23, i64 %25
store i8* %26, i8** %3
br label %27
27: ; preds = %22, %17
%28 = load i8*, i8** %3
ret i8* %28
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str = [40 x i8] c"Memory mapping for %lu failed with: %s\0A\00"
@errno = global i32 0
define i8* @getVirtualAddress(i64 %0, i32 %1) {
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_SHARED
%7 = and i64 %0, -4096
%8 = tail call i8* @mmap(i32 0, i64 4096, i32 %5, i32 %6, i32 %1, i64 %7)
%9 = icmp eq i8* %8, inttoptr (i64 -1 to i8*)
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i64 %0, i32 %12)
br label %17
14: ; preds = %2
%15 = and i64 %0, 4095
%16 = getelementptr i8, i8* %8, i64 %15
br label %17
17: ; preds = %14, %10
%18 = phi i8* [ inttoptr (i64 -1 to i8*), %10 ], [ %16, %14 ]
ret i8* %18
}