#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 O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int, unsigned int);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
unsigned int rareg(int mode, unsigned int addr, long long int new_value) {
  int fd;
  unsigned int round;
  void *start;
  volatile unsigned int *v_addr;
  unsigned int rc;
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd < 0) {
    printf("open file /dev/mem error. %s\n", strerror(errno));
    exit(-1);
  }
  round = addr;
  addr = (addr / 0x1000) * 0x1000;
  round = round - addr;
  start = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr);
  if ((int)start == -1) {
    printf("mmap() failed at phsical address:%d %s\n", addr, strerror(errno));
    close(fd);
    exit(-1);
  }
  v_addr = (void *)start + round;
  addr = addr + round;
  if (mode == 0x1) {
    *v_addr = new_value;
    usleep(10 * 1000);
  }
  rc = *v_addr;
  munmap(start, 0x1000);
  close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<rareg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
lea    0xe55(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e6 <rareg+0x66>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
shr    $0xc,%eax
shl    $0xc,%eax
mov    %eax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
mov    0x2da7(%rip),%rax
or     (%rax),%edx
mov    0x2dce(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x8(%rbp),%r9d
xor    %edi,%edi
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    $0xffffffff,%eax
jne    127b <rareg+0xfb>
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%esi
mov    %rax,%rdx
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    12b2 <rareg+0x132>
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    $0x2710,%edi
call   10a0 <usleep@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rareg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r13d
mov    %edi,%r15d
mov    0x2e3d(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe53(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1240 <rareg+0xc0>
mov    %eax,%r12d
mov    %r13d,%ebp
and    $0xfffff000,%ebp
mov    0x2e2b(%rip),%rax
mov    0x2de4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e09(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %r12d,%r8d
mov    %ebp,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
cmp    $0xffffffff,%ebx
je     1269 <rareg+0xe9>
and    $0xfff,%r13d
cmp    $0x1,%r15d
jne    1216 <rareg+0x96>
mov    %r14d,(%rbx,%r13,1)
mov    $0x2710,%edi
call   10a0 <usleep@plt>
mov    (%rbx,%r13,1),%ebp
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <munmap@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdb4(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xda9(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [30 x i8] c"open file /dev/mem error. %s\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [40 x i8] c"mmap() failed at phsical address:%d %s\0A\00"
define i32 @rareg(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_SYNC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %3
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %3
%24 = load i32, i32* %5
store i32 %24, i32* %8
%25 = load i32, i32* %5
%26 = udiv i32 %25, 4096
%27 = mul i32 %26, 4096
store i32 %27, i32* %5
%28 = load i32, i32* %8
%29 = load i32, i32* %5
%30 = sub i32 %28, %29
store i32 %30, i32* %8
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %31, %32
%34 = load i32, i32* @MAP_SHARED
%35 = load i32, i32* %7
%36 = load i32, i32* %5
%37 = call i8* @mmap(i32 0, i32 4096, i32 %33, i32 %34, i32 %35, i32 %36)
store i8* %37, i8** %9
%38 = load i8*, i8** %9
%39 = ptrtoint i8* %38 to i32
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %49
41: ; preds = %23
%42 = load i32, i32* %5
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %42, i8* %44)
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %23
%50 = load i8*, i8** %9
%51 = load i32, i32* %8
%52 = zext i32 %51 to i64
%53 = getelementptr i8, i8* %50, i64 %52
%54 = bitcast i8* %53 to i32*
store i32* %54, i32** %10
%55 = load i32, i32* %5
%56 = load i32, i32* %8
%57 = add i32 %55, %56
store i32 %57, i32* %5
%58 = load i32, i32* %4
%59 = icmp eq i32 %58, 1
br i1 %59, label %60, label %65
60: ; preds = %49
%61 = load i64, i64* %6
%62 = trunc i64 %61 to i32
%63 = load i32*, i32** %10
store volatile i32 %62, i32* %63
%64 = call i32 @usleep(i32 10000)
br label %65
65: ; preds = %60, %49
%66 = load i32*, i32** %10
%67 = load volatile i32, i32* %66
store i32 %67, i32* %11
%68 = load i8*, i8** %9
%69 = call i32 @munmap(i8* %68, i32 4096)
%70 = load i32, i32* %7
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %11
ret i32 %72
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [30 x i8] c"open file /dev/mem error. %s\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [40 x i8] c"mmap() failed at phsical address:%d %s\0A\00"
define i32 @rareg(i32 %0, i32 %1, i64 %2) {
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_SYNC
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %3
%15 = and i32 %1, -4096
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %17, %16
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i8* @mmap(i32 0, i32 4096, i32 %18, i32 %19, i32 %7, i32 %15)
%21 = ptrtoint i8* %20 to i64
%22 = trunc i64 %21 to i32
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %14
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %15, i8* %26)
%28 = tail call i32 @close(i32 %7)
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %14
%31 = and i32 %1, 4095
%32 = zext i32 %31 to i64
%33 = getelementptr i8, i8* %20, i64 %32
%34 = bitcast i8* %33 to i32*
%35 = icmp eq i32 %0, 1
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = trunc i64 %2 to i32
store volatile i32 %37, i32* %34
%38 = tail call i32 @usleep(i32 10000)
br label %39
39: ; preds = %36, %30
%40 = load volatile i32, i32* %34
%41 = tail call i32 @munmap(i8* %20, i32 4096)
%42 = tail call i32 @close(i32 %7)
ret i32 %40
}