#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ _SC_PAGESIZE;
int close(int);
int /*<<< orphan*/ memcpy(void *, char *, size_t);
char *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int);
int munmap(char *, size_t);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
int mem_read(void *buffer, off_t ptr, size_t len) {
  int errno;
  off_t mapped_offset = ptr & ~(sysconf(_SC_PAGESIZE) - 1);
  size_t mapped_len = len + ptr - mapped_offset;
  int fd = open("/dev/mem", O_RDONLY, 0);
  if (fd == -1)
    return -1;
  char *mapped =
      mmap(NULL, mapped_len, PROT_READ, MAP_SHARED, fd, mapped_offset);
  if (mapped == MAP_FAILED)
    return -1;
  memcpy(buffer, mapped + ptr - mapped_offset, len);
  errno = munmap(mapped, mapped_len);
  if (errno)
    return errno;
  errno = close(fd);
  if (errno)
    return errno;
  return 0;
}


x86 assembly (clang, -O0):
<mem_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
movslq -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11dd <mem_read+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <mem_read+0x138>
mov    -0x30(%rbp),%rsi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    0x2df7(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x28(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <mem_read+0xc8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <mem_read+0x138>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
movslq -0x28(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <munmap@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1271 <mem_read+0x111>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1298 <mem_read+0x138>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1291 <mem_read+0x131>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1298 <mem_read+0x138>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r15
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ebp
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    $0xffffffff,%r13d
cmp    $0xffffffff,%eax
je     1224 <mem_read+0xc4>
mov    %eax,%r12d
mov    %r15,0x8(%rsp)
neg    %ebp
and    %ebx,%ebp
movslq %ebx,%rbx
mov    %r14,0x10(%rsp)
lea    (%rbx,%r14,1),%r15
movslq %ebp,%r14
sub    %r14,%r15
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    0x2e12(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %r12d,%r8d
mov    %r14d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2dce(%rip),%rax
cmp    (%rax),%rbp
je     1224 <mem_read+0xc4>
neg    %r14
add    %rbp,%rbx
add    %r14,%rbx
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
mov    0x10(%rsp),%rdx
call   1050 <memcpy@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <munmap@plt>
mov    %eax,%r13d
test   %eax,%eax
je     1236 <mem_read+0xd6>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12d,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @mem_read(i8* %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%13 = load i32, i32* %6
%14 = load i32, i32* @_SC_PAGESIZE
%15 = call i32 @sysconf(i32 %14)
%16 = sub nsw i32 %15, 1
%17 = xor i32 %16, -1
%18 = and i32 %13, %17
store i32 %18, i32* %9
%19 = load i64, i64* %7
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = add i64 %19, %21
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = sub i64 %22, %24
store i64 %25, i64* %10
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %26, i32 0)
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %3
store i32 -1, i32* %4
br label %69
31: ; preds = %3
%32 = load i64, i64* %10
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @MAP_SHARED
%35 = load i32, i32* %11
%36 = load i32, i32* %9
%37 = call i8* @mmap(i32* null, i64 %32, i32 %33, i32 %34, i32 %35, i32 %36)
store i8* %37, i8** %12
%38 = load i8*, i8** %12
%39 = load i8*, i8** @MAP_FAILED
%40 = icmp eq i8* %38, %39
br i1 %40, label %41, label %42
41: ; preds = %31
store i32 -1, i32* %4
br label %69
42: ; preds = %31
%43 = load i8*, i8** %5
%44 = load i8*, i8** %12
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = sub i64 0, %49
%51 = getelementptr inbounds i8, i8* %47, i64 %50
%52 = load i64, i64* %7
%53 = call i32 @memcpy(i8* %43, i8* %51, i64 %52)
%54 = load i8*, i8** %12
%55 = load i64, i64* %10
%56 = call i32 @munmap(i8* %54, i64 %55)
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %42
%60 = load i32, i32* %8
store i32 %60, i32* %4
br label %69
61: ; preds = %42
%62 = load i32, i32* %11
%63 = call i32 @close(i32 %62)
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = load i32, i32* %8
store i32 %67, i32* %4
br label %69
68: ; preds = %61
store i32 0, i32* %4
br label %69
69: ; preds = %68, %66, %59, %41, %30
%70 = load i32, i32* %4
ret i32 %70
}

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