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