#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;
  int type;
  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,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <munmap@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%eax
mov    $0x38,%ecx
sar    %cl,%eax
and    $0xff,%eax
mov    -0x30(%rbp),%edx
mov    $0x28,%ecx
sar    %cl,%edx
mov    %edx,%ecx
and    $0xff00,%ecx
or     %ecx,%eax
mov    -0x30(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff0000,%ecx
or     %ecx,%eax
mov    -0x30(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff000000,%ecx
or     %ecx,%eax
mov    %eax,%eax
mov    -0x30(%rbp),%ecx
shl    $0x8,%ecx
movslq %ecx,%rcx
movabs $0xff00000000,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    -0x30(%rbp),%ecx
shl    $0x18,%ecx
movslq %ecx,%rcx
movabs $0xff0000000000,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    -0x30(%rbp),%edx
mov    $0x28,%ecx
shl    %cl,%edx
mov    %edx,%ecx
movslq %ecx,%rcx
movabs $0xff000000000000,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    -0x30(%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,-0x30(%rbp)
mov    -0x30(%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
%10 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i8*, i8** %3
%17 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load i32, i32* %5
%21 = call i32 @fstat(i32 %20, %struct.stat* %6)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @PROT_WRITE
%25 = load i32, i32* @PROT_READ
%26 = or i32 %24, %25
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* %5
%29 = call i8* @mmap(i32* null, i32 %23, i32 %26, i32 %27, i32 %28, i32 0)
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %37
33: ; preds = %19
%34 = load i8*, i8** %3
%35 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %19
%38 = load i8*, i8** %7
%39 = load i32, i32* %4
%40 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%41 = load i32, i32* %40
%42 = sub nsw i32 %41, 1
%43 = and i32 %39, %42
%44 = sext i32 %43 to i64
%45 = getelementptr i8, i8* %38, i64 %44
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = bitcast i8* %46 to i32*
%48 = load i32, i32* %47
store i32 %48, i32* %10
%49 = load i8*, i8** %7
%50 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @munmap(i8* %49, i32 %51)
%53 = load i32, i32* %5
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %10
%56 = ashr i32 %55, 56
%57 = and i32 %56, 255
%58 = load i32, i32* %10
%59 = ashr i32 %58, 40
%60 = and i32 %59, 65280
%61 = or i32 %57, %60
%62 = load i32, i32* %10
%63 = ashr i32 %62, 24
%64 = and i32 %63, 16711680
%65 = or i32 %61, %64
%66 = load i32, i32* %10
%67 = ashr i32 %66, 8
%68 = and i32 %67, -16777216
%69 = or i32 %65, %68
%70 = zext i32 %69 to i64
%71 = load i32, i32* %10
%72 = shl i32 %71, 8
%73 = sext i32 %72 to i64
%74 = and i64 %73, 1095216660480
%75 = or i64 %70, %74
%76 = load i32, i32* %10
%77 = shl i32 %76, 24
%78 = sext i32 %77 to i64
%79 = and i64 %78, 280375465082880
%80 = or i64 %75, %79
%81 = load i32, i32* %10
%82 = shl i32 %81, 40
%83 = sext i32 %82 to i64
%84 = and i64 %83, 71776119061217280
%85 = or i64 %80, %84
%86 = load i32, i32* %10
%87 = shl i32 %86, 56
%88 = sext i32 %87 to i64
%89 = and i64 %88, -72057594037927936
%90 = or i64 %85, %89
%91 = trunc i64 %90 to i32
store i32 %91, i32* %10
%92 = load i32, i32* %10
ret i32 %92
}

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
}