#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 uint64_t;
struct stat {
  int st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
uint64_t mmio_read(char resource_file[], uint64_t offset) {
  int priv2;
  struct stat sb;
  void *memblk, *adr;
  uint64_t val64;
  if ((priv2 = open(resource_file, O_RDWR)) < 0) {
    printf("Can not open %s\n", resource_file);
    exit(-1);
  }
  fstat(priv2, &sb);
  memblk = mmap(NULL, sb.st_size, PROT_WRITE | PROT_READ, MAP_SHARED, priv2, 0);
  if (memblk == MAP_FAILED) {
    printf("Can not mmap %s\n", resource_file);
    exit(-1);
  }
  adr = memblk + (offset & (sb.st_size - 1));
  val64 = *((uint64_t *)adr);
  munmap(memblk, sb.st_size);
  close(priv2);
  val64 = ((val64 >> 56) & 0xff) | ((val64 >> 40) & 0xff00) |
          ((val64 >> 24) & 0xff0000) | ((val64 >> 8) & 0xff000000) |
          ((val64 << 8) & 0xff00000000) | ((val64 << 24) & 0xff0000000000) |
          ((val64 << 40) & 0xff000000000000) |
          ((val64 << 56) & 0xff00000000000000);
  return val64;
}


x86 assembly (clang, -O0):
<mmio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11b7 <mmio_read+0x47>
mov    -0x8(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x18(%rbp),%esi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
mov    0x2e22(%rip),%rax
or     (%rax),%edx
mov    0x2e09(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dc0(%rip),%rcx
cmp    (%rcx),%rax
jne    1223 <mmio_read+0xb3>
mov    -0x8(%rbp),%rsi
lea    0xdfd(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
and    %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <munmap@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%eax
mov    $0x38,%ecx
sar    %cl,%eax
and    $0xff,%eax
mov    -0x2c(%rbp),%edx
mov    $0x28,%ecx
sar    %cl,%edx
mov    %edx,%ecx
and    $0xff00,%ecx
or     %ecx,%eax
mov    -0x2c(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff0000,%ecx
or     %ecx,%eax
mov    -0x2c(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff000000,%ecx
or     %ecx,%eax
mov    %eax,%eax
mov    -0x2c(%rbp),%ecx
shl    $0x8,%ecx
movslq %ecx,%rcx
movabs $0xff00000000,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    -0x2c(%rbp),%ecx
shl    $0x18,%ecx
movslq %ecx,%rcx
movabs $0xff0000000000,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    -0x2c(%rbp),%edx
mov    $0x28,%ecx
shl    %cl,%edx
mov    %edx,%ecx
movslq %ecx,%rcx
movabs $0xff000000000000,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    -0x2c(%rbp),%edx
mov    $0x38,%ecx
shl    %cl,%edx
mov    %edx,%ecx
movslq %ecx,%rcx
movabs $0xff00000000000000,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmio_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11ee <mmio_read+0x7e>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    0x8(%rsp),%ebx
mov    0x2e22(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%rax
je     11f7 <mmio_read+0x87>
mov    %rax,%rdi
mov    %ebx,%esi
call   1060 <munmap@plt>
mov    %ebp,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <close@plt>
lea    0xe0b(%rip),%rdi
jmp    11fe <mmio_read+0x8e>
lea    0xe13(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@.str = [17 x i8] c"Can not open %s\0A\00"
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [17 x i8] c"Can not mmap %s\0A\00"
define i32 @mmio_read(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %5
%20 = call i32 @fstat(i32 %19, %struct.stat* %6)
%21 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* @PROT_WRITE
%24 = load i32, i32* @PROT_READ
%25 = or i32 %23, %24
%26 = load i32, i32* @MAP_SHARED
%27 = load i32, i32* %5
%28 = call i8* @mmap(i32* null, i32 %22, i32 %25, i32 %26, i32 %27, i32 0)
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %36
32: ; preds = %18
%33 = load i8*, i8** %3
%34 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %18
%37 = load i8*, i8** %7
%38 = load i32, i32* %4
%39 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%40 = load i32, i32* %39
%41 = sub nsw i32 %40, 1
%42 = and i32 %38, %41
%43 = sext i32 %42 to i64
%44 = getelementptr i8, i8* %37, i64 %43
store i8* %44, i8** %8
%45 = load i8*, i8** %8
%46 = bitcast i8* %45 to i32*
%47 = load i32, i32* %46
store i32 %47, i32* %9
%48 = load i8*, i8** %7
%49 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @munmap(i8* %48, i32 %50)
%52 = load i32, i32* %5
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %9
%55 = ashr i32 %54, 56
%56 = and i32 %55, 255
%57 = load i32, i32* %9
%58 = ashr i32 %57, 40
%59 = and i32 %58, 65280
%60 = or i32 %56, %59
%61 = load i32, i32* %9
%62 = ashr i32 %61, 24
%63 = and i32 %62, 16711680
%64 = or i32 %60, %63
%65 = load i32, i32* %9
%66 = ashr i32 %65, 8
%67 = and i32 %66, -16777216
%68 = or i32 %64, %67
%69 = zext i32 %68 to i64
%70 = load i32, i32* %9
%71 = shl i32 %70, 8
%72 = sext i32 %71 to i64
%73 = and i64 %72, 1095216660480
%74 = or i64 %69, %73
%75 = load i32, i32* %9
%76 = shl i32 %75, 24
%77 = sext i32 %76 to i64
%78 = and i64 %77, 280375465082880
%79 = or i64 %74, %78
%80 = load i32, i32* %9
%81 = shl i32 %80, 40
%82 = sext i32 %81 to i64
%83 = and i64 %82, 71776119061217280
%84 = or i64 %79, %83
%85 = load i32, i32* %9
%86 = shl i32 %85, 56
%87 = sext i32 %86 to i64
%88 = and i64 %87, -72057594037927936
%89 = or i64 %84, %88
%90 = trunc i64 %89 to i32
store i32 %90, i32* %9
%91 = load i32, i32* %9
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@.str = [17 x i8] c"Can not open %s\0A\00"
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [17 x i8] c"Can not mmap %s\0A\00"
define i32 @mmio_read(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = call i32 @fstat(i32 %6, %struct.stat* %3)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @PROT_WRITE
%16 = load i32, i32* @PROT_READ
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_SHARED
%19 = tail call i8* @mmap(i32* null, i32 %14, i32 %17, i32 %18, i32 %6, i32 0)
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %25
22: ; preds = %11
%23 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %11
%26 = tail call i32 @munmap(i8* %19, i32 %14)
%27 = tail call i32 @close(i32 %6)
ret i32 poison
}