#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 uint32_t;
typedef int off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int, int);
scalar_t__ munmap(void *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  if (argc < 2) {
    printf("Usage:\n");
    printf("%s byte_addr [write_data]\n", argv[0]);
    exit(-1);
  }
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd < 0) {
    perror("open");
    exit(-1);
  }
  void *map_page_addr, *map_byte_addr;
  off_t byte_addr;
  byte_addr = strtoul(argv[1], NULL, 0);
  map_page_addr = mmap(0, (4096), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                       byte_addr & ~((4096) - 1));
  if (map_page_addr == MAP_FAILED) {
    perror("mmap");
    exit(-1);
  }
  map_byte_addr = map_page_addr + (byte_addr & ((4096) - 1));
  uint32_t data;
  if (argc > 2) {
    data = strtoul(argv[2], NULL, 0);
    *((uint32_t *)map_byte_addr) = data;
  } else {
    data = *((uint32_t *)map_byte_addr);
    printf("data = 0x%08x\n", data);
  }
  if (munmap(map_page_addr, (4096))) {
    perror("munmap");
    exit(-1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11cd <main+0x4d>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
or     (%rax),%esi
lea    0xe3d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120e <main+0x8e>
lea    0xe2d(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
mov    0x2d8c(%rip),%rax
or     (%rax),%edx
mov    0x2dab(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x2c(%rbp),%r9d
and    $0xfffff000,%r9d
xor    %edi,%edi
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d4f(%rip),%rcx
cmp    (%rcx),%rax
jne    1288 <main+0x108>
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
and    $0xfff,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    12cd <main+0x14d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    12e7 <main+0x167>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <munmap@plt>
cmp    $0x0,%rax
je     1315 <main+0x195>
lea    0xd3f(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
cmp    $0x1,%edi
jle    127c <main+0xec>
mov    %edi,%r15d
mov    0x2e2a(%rip),%rax
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12a3 <main+0x113>
mov    %eax,%r12d
mov    0x8(%r14),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtoul@plt>
mov    0x2e14(%rip),%rcx
mov    0x2dd5(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %eax,%ebx
mov    0x2df0(%rip),%rax
mov    (%rax),%ecx
mov    %ebx,%r9d
and    $0xfffff000,%r9d
xor    %edi,%edi
mov    $0x1000,%esi
mov    %r12d,%r8d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2d9b(%rip),%rax
cmp    (%rax),%rbp
je     12ac <main+0x11c>
and    $0xfff,%ebx
cmp    $0x3,%r15d
jb     1245 <main+0xb5>
mov    0x10(%r14),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtoul@plt>
mov    %eax,0x0(%rbp,%rbx,1)
jmp    1257 <main+0xc7>
mov    0x0(%rbp,%rbx,1),%esi
lea    0xdde(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
test   %rax,%rax
jne    12b5 <main+0x125>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc1(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%rsi
lea    0xd6e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xd7a(%rip),%rdi
jmp    12bc <main+0x12c>
lea    0xd76(%rip),%rdi
jmp    12bc <main+0x12c>
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"Usage:\0A\00"
@.str.1 = [27 x i8] c"%s byte_addr [write_data]\0A\00"
@.str.2 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.3 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [15 x i8] c"data = 0x%08x\0A\00"
@.str.6 = [7 x i8] c"munmap\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_SYNC
%23 = or i32 %21, %22
%24 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %20
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @strtoul(i8* %33, i32* null, i32 0)
store i32 %34, i32* %9
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @PROT_WRITE
%37 = or i32 %35, %36
%38 = load i32, i32* @MAP_SHARED
%39 = load i32, i32* %6
%40 = load i32, i32* %9
%41 = and i32 %40, -4096
%42 = call i8* @mmap(i32 0, i32 4096, i32 %37, i32 %38, i32 %39, i32 %41)
store i8* %42, i8** %7
%43 = load i8*, i8** %7
%44 = load i8*, i8** @MAP_FAILED
%45 = icmp eq i8* %43, %44
br i1 %45, label %46, label %49
46: ; preds = %30
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %30
%50 = load i8*, i8** %7
%51 = load i32, i32* %9
%52 = and i32 %51, 4095
%53 = sext i32 %52 to i64
%54 = getelementptr i8, i8* %50, i64 %53
store i8* %54, i8** %8
%55 = load i32, i32* %4
%56 = icmp sgt i32 %55, 2
br i1 %56, label %57, label %65
57: ; preds = %49
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 2
%60 = load i8*, i8** %59
%61 = call i32 @strtoul(i8* %60, i32* null, i32 0)
store i32 %61, i32* %10
%62 = load i32, i32* %10
%63 = load i8*, i8** %8
%64 = bitcast i8* %63 to i32*
store i32 %62, i32* %64
br label %71
65: ; preds = %49
%66 = load i8*, i8** %8
%67 = bitcast i8* %66 to i32*
%68 = load i32, i32* %67
store i32 %68, i32* %10
%69 = load i32, i32* %10
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %65, %57
%72 = load i8*, i8** %7
%73 = call i64 @munmap(i8* %72, i32 4096)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %71
%76 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%77 = call i32 @exit(i32 -1)
unreachable
78: ; preds = %71
%79 = load i32, i32* %6
%80 = call i32 @close(i32 %79)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [27 x i8] c"%s byte_addr [write_data]\0A\00"
@.str.2 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.3 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [15 x i8] c"data = 0x%08x\0A\00"
@.str.6 = [7 x i8] c"munmap\00"
@str = [7 x i8] c"Usage:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_SYNC
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %9
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @strtoul(i8* nocapture %20, i32* null, i32 0)
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %23, %22
%25 = load i32, i32* @MAP_SHARED
%26 = and i32 %21, -4096
%27 = tail call i8* @mmap(i32 0, i32 4096, i32 %24, i32 %25, i32 %13, i32 %26)
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %33
30: ; preds = %18
%31 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%32 = tail call i32 @exit(i32 -1)
unreachable
33: ; preds = %18
%34 = and i32 %21, 4095
%35 = zext i32 %34 to i64
%36 = getelementptr i8, i8* %27, i64 %35
%37 = icmp ugt i32 %0, 2
br i1 %37, label %38, label %43
38: ; preds = %33
%39 = getelementptr inbounds i8*, i8** %1, i64 2
%40 = load i8*, i8** %39
%41 = tail call i32 @strtoul(i8* nocapture %40, i32* null, i32 0)
%42 = bitcast i8* %36 to i32*
store i32 %41, i32* %42
br label %47
43: ; preds = %33
%44 = bitcast i8* %36 to i32*
%45 = load i32, i32* %44
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %43, %38
%48 = tail call i64 @munmap(i8* %27, i32 4096)
%49 = icmp eq i64 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %47
%51 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%52 = tail call i32 @exit(i32 -1)
unreachable
53: ; preds = %47
%54 = tail call i32 @close(i32 %13)
ret i32 0
}