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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void *map_memory(off_t addr, size_t len) {
  int fd;
  void *mem;
  fd = open("/dev/mem", O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Failed to open /dev/mem : %s\n", strerror(errno));
    return NULL;
  }
  mem = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr);
  if (mem == MAP_FAILED) {
    fprintf(stderr, "Failed to mmap memory at 0x%08lx : %s\n", addr,
            strerror(errno));
    return NULL;
  }
  return mem;
}


x86 assembly (clang, -O0):
<map_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ac <map_memory+0x6c>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1243 <map_memory+0x103>
mov    -0x18(%rbp),%rsi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    0x2df8(%rip),%rax
or     (%rax),%edx
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0xc(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
jne    123b <map_memory+0xfb>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
movslq -0xc(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1243 <map_memory+0x103>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_memory>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11d2 <map_memory+0x92>
mov    0x2e8f(%rip),%rcx
mov    0x2e48(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %eax,%r8d
mov    %ebp,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e19(%rip),%rax
cmp    (%rax),%rbx
jne    11fe <map_memory+0xbe>
mov    0x2e25(%rip),%rax
mov    (%rax),%r14d
movslq %ebp,%rbp
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe69(%rip),%rsi
xor    %ebx,%ebx
mov    %r14d,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11fe <map_memory+0xbe>
mov    0x2def(%rip),%rax
mov    (%rax),%ebp
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe19(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [30 x i8] c"Failed to open /dev/mem : %s\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [39 x i8] c"Failed to mmap memory at 0x%08lx : %s\0A\00"
define i8* @map_memory(i32 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %4
store i64 %1, i64* %5
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i8* null, i8** %3
br label %39
17: ; preds = %2
%18 = load i64, i64* %5
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %6
%24 = load i32, i32* %4
%25 = call i8* @mmap(i32* null, i64 %18, i32 %21, i32 %22, i32 %23, i32 %24)
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp eq i8* %26, %27
br i1 %28, label %29, label %37
29: ; preds = %17
%30 = load i32, i32* @stderr
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = inttoptr i64 %32 to i8*
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %33, i8* %35)
store i8* null, i8** %3
br label %39
37: ; preds = %17
%38 = load i8*, i8** %7
store i8* %38, i8** %3
br label %39
39: ; preds = %37, %29, %12
%40 = load i8*, i8** %3
ret i8* %40
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [30 x i8] c"Failed to open /dev/mem : %s\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [39 x i8] c"Failed to mmap memory at 0x%08lx : %s\0A\00"
define i8* @map_memory(i32 %0, i64 %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %26
11: ; preds = %2
%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 %1, i32 %14, i32 %15, i32 %4, i32 %0)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %26
19: ; preds = %11
%20 = load i32, i32* @stderr
%21 = sext i32 %0 to i64
%22 = inttoptr i64 %21 to i8*
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %22, i8* %24)
br label %26
26: ; preds = %11, %19, %6
%27 = phi i8* [ null, %6 ], [ null, %19 ], [ %16, %11 ]
ret i8* %27
}