#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 unsigned long off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int getpagesize();
int /*<<< orphan*/ lseek(int, unsigned long, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           unsigned long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned long);
int read(int, void *, size_t);
int read_mem(off_t offset, void *buf, size_t count) {
  int fd;
  int n;
  int page_size;
  void *map_base;
  unsigned long value;
  printf("/dev/mem: the offset is %lx\n", offset);
  fd = open("/dev/mem", O_RDONLY);
  if (fd < 0) {
    perror("open /dev/mem failed");
    return -1;
  }
  if (1) {
    page_size = getpagesize();
    printf("the page size = %d\n", page_size);
    map_base = mmap(0, page_size, PROT_READ, MAP_SHARED, fd, offset);
    if (map_base == MAP_FAILED) {
      perror("mmap");
      exit(1);
    }
    value = *(volatile unsigned long *)(map_base);
    printf("/dev/mem: the value is %ld\n", value);
    buf = (unsigned long *)map_base;
  }
  if (0) {
    lseek(fd, offset, SEEK_SET);
    n = read(fd, buf, count);
    if (n != count)
      perror("/dev/mem read failed");
    else
      printf("/dev/mem read buf = %ld\n", *(unsigned long *)buf);
  }
  close(fd);
  return n;
}


x86 assembly (clang, -O0):
<read_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe71(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ce <read_mem+0x5e>
lea    0xe69(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <read_mem+0xfb>
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
mov    -0x2c(%rbp),%esi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    0x2ded(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x10(%rbp),%r9
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%rax
jne    123a <read_mem+0xca>
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xe04(%rip),%rdi
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_mem>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe82(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1207 <read_mem+0x97>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <getpagesize@plt>
movslq %eax,%rbx
lea    0xe89(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
mov    0x2e1e(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
mov    %r14,%r9
call   1030 <mmap@plt>
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
je     1215 <read_mem+0xa5>
mov    (%rax),%rsi
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
jmp    11fd <read_mem+0x8d>
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"/dev/mem: the offset is %lx\0A\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.2 = [21 x i8] c"open /dev/mem failed\00"
@.str.3 = [20 x i8] c"the page size = %d\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [28 x i8] c"/dev/mem: the value is %ld\0A\00"
@SEEK_SET = global i32 0
define i32 @read_mem(i64 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
store i64 %0, i64* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%13 = load i64, i64* %5
%14 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i64 %13)
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %50
21: ; preds = %3
%22 = call i32 (...) @getpagesize()
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %24)
%26 = load i32, i32* %10
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @MAP_SHARED
%29 = load i32, i32* %8
%30 = load i64, i64* %5
%31 = call i8* @mmap(i32 0, i32 %26, i32 %27, i32 %28, i32 %29, i64 %30)
store i8* %31, i8** %11
%32 = load i8*, i8** %11
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %38
35: ; preds = %21
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %21
%39 = load i8*, i8** %11
%40 = bitcast i8* %39 to i64*
%41 = load volatile i64, i64* %40
store i64 %41, i64* %12
%42 = load i64, i64* %12
%43 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i64 %42)
%44 = load i8*, i8** %11
%45 = bitcast i8* %44 to i64*
%46 = bitcast i64* %45 to i8*
store i8* %46, i8** %6
%47 = load i32, i32* %8
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %9
store i32 %49, i32* %4
br label %50
50: ; preds = %38, %19
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@.str = [29 x i8] c"/dev/mem: the offset is %lx\0A\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.2 = [21 x i8] c"open /dev/mem failed\00"
@.str.3 = [20 x i8] c"the page size = %d\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [28 x i8] c"/dev/mem: the value is %ld\0A\00"
@SEEK_SET = global i32 0
define i32 @read_mem(i64 %0, i8* nocapture readnone %1, i64 %2) {
%4 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i64 %0)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %27
10: ; preds = %3
%11 = tail call i32 (...) @getpagesize()
%12 = sext i32 %11 to i64
%13 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %12)
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @MAP_SHARED
%16 = tail call i8* @mmap(i32 0, i32 %11, i32 %14, i32 %15, i32 %6, i64 %0)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %10
%23 = bitcast i8* %16 to i64*
%24 = load volatile i64, i64* %23
%25 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i64 %24)
%26 = tail call i32 @close(i32 %6)
br label %27
27: ; preds = %22, %8
ret i32 -1
}