#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 */

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ exit(int);
void *mmap(int /*<<< orphan*/ *, unsigned int, int, int /*<<< orphan*/, int,
           unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
unsigned int strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
unsigned int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  int fd;
  void *ptr;
  unsigned val;
  unsigned addr, page_addr, page_offset;
  unsigned page_size = sysconf(_SC_PAGESIZE);
  fd = open("/dev/mem", O_RDWR);
  if (fd < 1) {
    perror(argv[0]);
    exit(-1);
  }
  if (argc != 3) {
    usage(argv[0]);
    exit(-1);
  }
  addr = strtoul(argv[1], NULL, 0);
  val = strtoul(argv[2], NULL, 0);
  page_addr = (addr & ~(page_size - 1));
  page_offset = addr - page_addr;
  ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
             (addr & ~(page_size - 1)));
  if ((int)ptr == -1) {
    perror(argv[0]);
    exit(-1);
  }
  *((unsigned *)(ptr + page_offset)) = val;
  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)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0x34(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11cf <main+0x5f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
je     11ef <main+0x7f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <usage@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x34(%rbp),%esi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
mov    0x2d78(%rip),%rax
or     (%rax),%edx
mov    0x2d97(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x28(%rbp),%r9d
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
xor    $0xffffffff,%eax
and    %eax,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    $0xffffffff,%eax
jne    1297 <main+0x127>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    %edx,(%rax,%rcx,1)
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,%r14
mov    %edi,%ebx
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,%r15d
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
jle    1223 <main+0xb3>
cmp    $0x3,%ebx
jne    1235 <main+0xc5>
mov    %eax,%r13d
mov    0x8(%r14),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,%ebp
mov    0x10(%r14),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,%r12d
mov    %r15d,%ebx
neg    %ebx
and    %ebp,%ebx
mov    0x2e15(%rip),%rax
mov    0x2dd6(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df3(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %r13d,%r8d
mov    %ebx,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffff,%eax
je     1223 <main+0xb3>
sub    %ebx,%ebp
mov    %r12d,(%rax,%rbp,1)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    (%r14),%rdi
call   1040 <usage@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @perror(i8* %9)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = icmp eq i32 %0, 3
br i1 %13, label %18, label %14
14: ; preds = %12
%15 = load i8*, i8** %1
%16 = tail call i32 @usage(i8* %15)
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %12
%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 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = tail call i32 @strtoul(i8* nocapture %23, i32* null, i32 0)
%25 = sub i32 0, %4
%26 = and i32 %21, %25
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %28, %27
%30 = load i32, i32* @MAP_SHARED
%31 = tail call i8* @mmap(i32* null, i32 %4, i32 %29, i32 %30, i32 %6, i32 %26)
%32 = ptrtoint i8* %31 to i64
%33 = trunc i64 %32 to i32
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %18
%36 = load i8*, i8** %1
%37 = tail call i32 @perror(i8* %36)
%38 = tail call i32 @exit(i32 -1)
unreachable
39: ; preds = %18
%40 = sub i32 %21, %26
%41 = zext i32 %40 to i64
%42 = getelementptr i8, i8* %31, i64 %41
%43 = bitcast i8* %42 to i32*
store i32 %24, i32* %43
ret i32 0
}