#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_write(char resource_file[], uint64_t offset, uint64_t val64) {
  int priv2;
  struct stat sb;
  void *memblk, *adr;
  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 = ((val64 >> 56) & 0xff) | ((val64 >> 40) & 0xff00) |
          ((val64 >> 24) & 0xff0000) | ((val64 >> 8) & 0xff000000) |
          ((val64 << 8) & 0xff00000000) | ((val64 << 24) & 0xff0000000000) |
          ((val64 << 40) & 0xff000000000000) |
          ((val64 << 56) & 0xff00000000000000);
  *((uint64_t *)adr) = val64;
  munmap(memblk, sb.st_size);
  close(priv2);
  return 0;
}


x86 assembly (clang, -O0):
<mmio_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11ba <mmio_write+0x4a>
mov    -0x8(%rbp),%rsi
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x18(%rbp),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
mov    0x2e1f(%rip),%rax
or     (%rax),%edx
mov    0x2e06(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
jne    1226 <mmio_write+0xb6>
mov    -0x8(%rbp),%rsi
lea    0xdfa(%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    -0x10(%rbp),%eax
mov    $0x38,%ecx
sar    %cl,%eax
and    $0xff,%eax
mov    -0x10(%rbp),%edx
mov    $0x28,%ecx
sar    %cl,%edx
mov    %edx,%ecx
and    $0xff00,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff0000,%ecx
or     %ecx,%eax
mov    -0x10(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff000000,%ecx
or     %ecx,%eax
mov    %eax,%eax
mov    -0x10(%rbp),%ecx
shl    $0x8,%ecx
movslq %ecx,%rcx
movabs $0xff00000000,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    -0x10(%rbp),%ecx
shl    $0x18,%ecx
movslq %ecx,%rcx
movabs $0xff0000000000,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    -0x10(%rbp),%edx
mov    $0x28,%ecx
shl    %cl,%edx
mov    %edx,%ecx
movslq %ecx,%rcx
movabs $0xff000000000000,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    -0x10(%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,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmio_write>:
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     11f1 <mmio_write+0x81>
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     11fa <mmio_write+0x8a>
mov    %rax,%rdi
mov    %ebx,%esi
call   1060 <munmap@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe08(%rip),%rdi
jmp    1201 <mmio_write+0x91>
lea    0xe10(%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_write(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i8*, i8** %4
%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 = %3
%20 = load i32, i32* %7
%21 = call i32 @fstat(i32 %20, %struct.stat* %8)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %8, 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* %7
%29 = call i8* @mmap(i32* null, i32 %23, i32 %26, i32 %27, i32 %28, i32 0)
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%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** %4
%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** %9
%39 = load i32, i32* %5
%40 = getelementptr inbounds %struct.stat, %struct.stat* %8, 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** %10
%46 = load i32, i32* %6
%47 = ashr i32 %46, 56
%48 = and i32 %47, 255
%49 = load i32, i32* %6
%50 = ashr i32 %49, 40
%51 = and i32 %50, 65280
%52 = or i32 %48, %51
%53 = load i32, i32* %6
%54 = ashr i32 %53, 24
%55 = and i32 %54, 16711680
%56 = or i32 %52, %55
%57 = load i32, i32* %6
%58 = ashr i32 %57, 8
%59 = and i32 %58, -16777216
%60 = or i32 %56, %59
%61 = zext i32 %60 to i64
%62 = load i32, i32* %6
%63 = shl i32 %62, 8
%64 = sext i32 %63 to i64
%65 = and i64 %64, 1095216660480
%66 = or i64 %61, %65
%67 = load i32, i32* %6
%68 = shl i32 %67, 24
%69 = sext i32 %68 to i64
%70 = and i64 %69, 280375465082880
%71 = or i64 %66, %70
%72 = load i32, i32* %6
%73 = shl i32 %72, 40
%74 = sext i32 %73 to i64
%75 = and i64 %74, 71776119061217280
%76 = or i64 %71, %75
%77 = load i32, i32* %6
%78 = shl i32 %77, 56
%79 = sext i32 %78 to i64
%80 = and i64 %79, -72057594037927936
%81 = or i64 %76, %80
%82 = trunc i64 %81 to i32
store i32 %82, i32* %6
%83 = load i32, i32* %6
%84 = load i8*, i8** %10
%85 = bitcast i8* %84 to i32*
store i32 %83, i32* %85
%86 = load i8*, i8** %9
%87 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%88 = load i32, i32* %87
%89 = call i32 @munmap(i8* %86, i32 %88)
%90 = load i32, i32* %7
%91 = call i32 @close(i32 %90)
ret i32 0
}

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_write(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %3
%13 = call i32 @fstat(i32 %7, %struct.stat* %4)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @PROT_WRITE
%17 = load i32, i32* @PROT_READ
%18 = or i32 %17, %16
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i8* @mmap(i32* null, i32 %15, i32 %18, i32 %19, i32 %7, i32 0)
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %12
%27 = tail call i32 @munmap(i8* %20, i32 %15)
%28 = tail call i32 @close(i32 %7)
ret i32 0
}