#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*/ uint8_t;
typedef unsigned long off_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
void *mmap(int /*<<< orphan*/, unsigned long, int, int /*<<< orphan*/, int,
           unsigned long);
int /*<<< orphan*/ munmap(void *, unsigned long);
int open(char *, int);
int /*<<< orphan*/ read8(void *);
unsigned int read_mem8(off_t target, uint8_t *val) {
  int fd;
  void *map_base, *virt_addr;
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd < 0)
    return (unsigned int)1;
  map_base = mmap(0, 4096UL, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                  target & ~(4096UL - 1));
  if (map_base == (void *)-1)
    return (unsigned int)2;
  virt_addr = map_base + (target & (4096UL - 1));
  *val = read8(virt_addr);
  munmap(map_base, 4096UL);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<read_mem8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
lea    0xe87(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1197 <read_mem8+0x47>
movl   $0x1,-0x4(%rbp)
jmp    1232 <read_mem8+0xe2>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
mov    0x2e19(%rip),%rax
or     (%rax),%edx
mov    0x2e38(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x10(%rbp),%r9
and    $0xfffffffffffff000,%r9
xor    %edi,%edi
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    11ee <read_mem8+0x9e>
movl   $0x2,-0x4(%rbp)
jmp    1232 <read_mem8+0xe2>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
and    $0xfff,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <read8@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <munmap@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_mem8>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e73(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe89(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11ee <read_mem8+0x9e>
mov    %eax,%r12d
mov    0x2e6e(%rip),%rax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%r9
and    $0xfffffffffffff000,%r9
xor    %r15d,%r15d
mov    $0x1000,%esi
xor    %edi,%edi
mov    %r12d,%r8d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11f6 <read_mem8+0xa6>
mov    %rax,%rbp
and    $0xfff,%ebx
add    %rax,%rbx
mov    %rbx,%rdi
call   1040 <read8@plt>
mov    %eax,(%r14)
mov    $0x1000,%esi
mov    %rbp,%rdi
call   1060 <munmap@plt>
mov    %r12d,%edi
call   1050 <close@plt>
jmp    11fc <read_mem8+0xac>
mov    $0x1,%r15d
jmp    11fc <read_mem8+0xac>
mov    $0x2,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i32 @read_mem8(i64 %0, i32* nocapture writeonly %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %2
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_SHARED
%13 = and i64 %0, -4096
%14 = tail call i8* @mmap(i32 0, i64 4096, i32 %11, i32 %12, i32 %6, i64 %13)
%15 = icmp eq i8* %14, inttoptr (i64 -1 to i8*)
br i1 %15, label %22, label %16
16: ; preds = %8
%17 = and i64 %0, 4095
%18 = getelementptr i8, i8* %14, i64 %17
%19 = tail call i32 @read8(i8* %18)
store i32 %19, i32* %1
%20 = tail call i32 @munmap(i8* %14, i64 4096)
%21 = tail call i32 @close(i32 %6)
br label %22
22: ; preds = %8, %2, %16
%23 = phi i32 [ 0, %16 ], [ 1, %2 ], [ 2, %8 ]
ret i32 %23
}