#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 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*/ err(int, char *);
int getpagesize();
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, unsigned long, ...);
unsigned long readmem(void *, int);
void *strtoul(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int tolower(char);
int /*<<< orphan*/ usage();
unsigned long writemem(void *, unsigned long, int);
int main(int argc, char *argv[]) {
  unsigned long val, rval;
  int fd, access, pagesz;
  off_t target;
  void *mem, *addr;
  if (argc < 2)
    usage();
  target = strtoul(argv[1], 0, 0);
  access = 'w';
  if (argc > 2)
    access = tolower(argv[2][0]);
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd < 0)
    err(1, "open: /dev/mem");
  pagesz = getpagesize();
  mem = mmap(0, pagesz, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
             target & ~(pagesz - 1));
  if (mem == MAP_FAILED)
    err(1, "mmap");
  printf("memory mapped at address %p\n", mem);
  addr = mem + (target & (pagesz - 1));
  val = readmem(addr, access);
  printf("value at address %#zx (%p): %#lx\n", target, addr, val);
  if (argc > 3) {
    val = strtoul(argv[3], 0, 0);
    rval = writemem(addr, val, access);
    printf("written %#lx, readback %#lx\n", val, rval);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d7 <main+0x27>
mov    $0x0,%al
call   10a0 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   10c0 <strtoul@plt>
mov    %eax,-0x30(%rbp)
movl   $0x77,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    120f <main+0x5f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%edi
call   1080 <tolower@plt>
mov    %eax,-0x28(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
or     (%rax),%esi
lea    0xdde(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    124b <main+0x9b>
mov    $0x1,%edi
lea    0xdba(%rip),%rsi
call   1090 <err@plt>
mov    $0x0,%al
call   10d0 <getpagesize@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
mov    0x2d58(%rip),%rax
or     (%rax),%edx
mov    0x2d77(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x30(%rbp),%r9d
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
xor    $0xffffffff,%eax
and    %eax,%r9d
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2d1b(%rip),%rcx
cmp    (%rcx),%rax
jne    12b7 <main+0x107>
mov    $0x1,%edi
lea    0xd5d(%rip),%rsi
call   1090 <err@plt>
mov    -0x38(%rbp),%rsi
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    -0x2c(%rbp),%edx
sub    $0x1,%edx
and    %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1070 <readmem@plt>
mov    %rax,-0x18(%rbp)
movslq -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x3,-0x8(%rbp)
jle    1355 <main+0x1a5>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   10c0 <strtoul@plt>
mov    %rax,-0x18(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1050 <writemem@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xd05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r12d
cmp    $0x1,%edi
jg     11e5 <main+0x35>
xor    %eax,%eax
call   10a0 <usage@plt>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %rax,%rbp
mov    $0x77,%r15d
jmp    1210 <main+0x60>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %rax,%rbp
mov    $0x77,%r15d
cmp    $0x2,%r12d
je     1210 <main+0x60>
mov    0x10(%rbx),%rax
movsbl (%rax),%edi
call   1080 <tolower@plt>
mov    %eax,%r15d
mov    %rbx,(%rsp)
mov    0x2dbd(%rip),%rax
mov    0x2dae(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdd9(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    124a <main+0x9a>
lea    0xdc0(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
xor    %eax,%eax
call   10d0 <getpagesize@plt>
mov    0x2da0(%rip),%rcx
mov    0x2d61(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d7e(%rip),%rcx
mov    (%rcx),%ecx
lea    -0x1(%rax),%r14d
mov    %eax,%r9d
neg    %r9d
and    %ebp,%r9d
xor    %edi,%edi
mov    %eax,%esi
mov    %r13d,%r8d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2d29(%rip),%rax
cmp    (%rax),%rbx
jne    12a5 <main+0xf5>
lea    0xd74(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
lea    0xd68(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
and    %ebp,%r14d
movslq %r14d,%rax
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1070 <readmem@plt>
movslq %ebp,%rsi
lea    0xd5d(%rip),%rdi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x4,%r12d
jl     131d <main+0x16d>
mov    (%rsp),%rax
mov    0x18(%rax),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r15d,%edx
call   1050 <writemem@plt>
lea    0xd43(%rip),%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [15 x i8] c"open: /dev/mem\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [29 x i8] c"memory mapped at address %p\0A\00"
@.str.4 = [34 x i8] c"value at address %#zx (%p): %#lx\0A\00"
@.str.5 = [29 x i8] c"written %#lx, readback %#lx\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (...) @usage()
br label %18
18: ; preds = %16, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i8* @strtoul(i8* %21, i32 0, i32 0)
%23 = ptrtoint i8* %22 to i32
store i32 %23, i32* %11
store i32 119, i32* %9
%24 = load i32, i32* %4
%25 = icmp sgt i32 %24, 2
br i1 %25, label %26, label %33
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = call i32 @tolower(i8 signext %31)
store i32 %32, i32* %9
br label %33
33: ; preds = %26, %18
%34 = load i32, i32* @O_RDWR
%35 = load i32, i32* @O_SYNC
%36 = or i32 %34, %35
%37 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %33
%43 = call i32 (...) @getpagesize()
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = load i32, i32* @PROT_READ
%46 = load i32, i32* @PROT_WRITE
%47 = or i32 %45, %46
%48 = load i32, i32* @MAP_SHARED
%49 = load i32, i32* %8
%50 = load i32, i32* %11
%51 = load i32, i32* %10
%52 = sub nsw i32 %51, 1
%53 = xor i32 %52, -1
%54 = and i32 %50, %53
%55 = call i8* @mmap(i32 0, i32 %44, i32 %47, i32 %48, i32 %49, i32 %54)
store i8* %55, i8** %12
%56 = load i8*, i8** %12
%57 = load i8*, i8** @MAP_FAILED
%58 = icmp eq i8* %56, %57
br i1 %58, label %59, label %61
59: ; preds = %42
%60 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %61
61: ; preds = %59, %42
%62 = load i8*, i8** %12
%63 = ptrtoint i8* %62 to i64
%64 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 %63)
%65 = load i8*, i8** %12
%66 = load i32, i32* %11
%67 = load i32, i32* %10
%68 = sub nsw i32 %67, 1
%69 = and i32 %66, %68
%70 = sext i32 %69 to i64
%71 = getelementptr i8, i8* %65, i64 %70
store i8* %71, i8** %13
%72 = load i8*, i8** %13
%73 = load i32, i32* %9
%74 = call i64 @readmem(i8* %72, i32 %73)
store i64 %74, i64* %6
%75 = load i32, i32* %11
%76 = sext i32 %75 to i64
%77 = load i8*, i8** %13
%78 = load i64, i64* %6
%79 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i64 %76, i8* %77, i64 %78)
%80 = load i32, i32* %4
%81 = icmp sgt i32 %80, 3
br i1 %81, label %82, label %95
82: ; preds = %61
%83 = load i8**, i8*** %5
%84 = getelementptr inbounds i8*, i8** %83, i64 3
%85 = load i8*, i8** %84
%86 = call i8* @strtoul(i8* %85, i32 0, i32 0)
%87 = ptrtoint i8* %86 to i64
store i64 %87, i64* %6
%88 = load i8*, i8** %13
%89 = load i64, i64* %6
%90 = load i32, i32* %9
%91 = call i64 @writemem(i8* %88, i64 %89, i32 %90)
store i64 %91, i64* %7
%92 = load i64, i64* %6
%93 = load i64, i64* %7
%94 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i64 %92, i64 %93)
br label %95
95: ; preds = %82, %61
%96 = load i32, i32* %8
%97 = call i32 @close(i32 %96)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [15 x i8] c"open: /dev/mem\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [29 x i8] c"memory mapped at address %p\0A\00"
@.str.4 = [34 x i8] c"value at address %#zx (%p): %#lx\0A\00"
@.str.5 = [29 x i8] c"written %#lx, readback %#lx\0A\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 (...) @usage()
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i8* @strtoul(i8* %7, i32 0, i32 0)
br label %19
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i8* @strtoul(i8* %11, i32 0, i32 0)
%13 = icmp eq i32 %0, 2
br i1 %13, label %19, label %14
14: ; preds = %9
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = load i8, i8* %16
%18 = tail call i32 @tolower(i8 signext %17)
br label %19
19: ; preds = %4, %14, %9
%20 = phi i8* [ %12, %14 ], [ %12, %9 ], [ %8, %4 ]
%21 = phi i32 [ %18, %14 ], [ 119, %9 ], [ 119, %4 ]
%22 = ptrtoint i8* %20 to i64
%23 = trunc i64 %22 to i32
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_SYNC
%26 = or i32 %25, %24
%27 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %19
%32 = tail call i32 (...) @getpagesize()
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %34, %33
%36 = load i32, i32* @MAP_SHARED
%37 = add nsw i32 %32, -1
%38 = sub i32 0, %32
%39 = and i32 %38, %23
%40 = tail call i8* @mmap(i32 0, i32 %32, i32 %35, i32 %36, i32 %27, i32 %39)
%41 = load i8*, i8** @MAP_FAILED
%42 = icmp eq i8* %40, %41
br i1 %42, label %43, label %45
43: ; preds = %31
%44 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %31
%46 = ptrtoint i8* %40 to i64
%47 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 %46)
%48 = and i32 %37, %23
%49 = sext i32 %48 to i64
%50 = getelementptr i8, i8* %40, i64 %49
%51 = tail call i64 @readmem(i8* %50, i32 %21)
%52 = shl i64 %22, 32
%53 = ashr exact i64 %52, 32
%54 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i64 %53, i8* %50, i64 %51)
%55 = icmp sgt i32 %0, 3
br i1 %55, label %56, label %63
56: ; preds = %45
%57 = getelementptr inbounds i8*, i8** %1, i64 3
%58 = load i8*, i8** %57
%59 = tail call i8* @strtoul(i8* %58, i32 0, i32 0)
%60 = ptrtoint i8* %59 to i64
%61 = tail call i64 @writemem(i8* %50, i64 %60, i32 %21)
%62 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i64 %60, i64 %61)
br label %63
63: ; preds = %56, %45
%64 = tail call i32 @close(i32 %27)
ret i32 0
}