#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*/ exit(int);
void *mmap(int /*<<< orphan*/, unsigned long, int, int /*<<< orphan*/, int,
           unsigned long);
int open(char *, int);
void reg_write(unsigned long addr, unsigned long dat) {
  int memfd;
  void *map_base, *virt_addr;
  if ((memfd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
    exit(1);
  map_base = mmap(0, 4096UL, PROT_READ | PROT_WRITE, MAP_SHARED, memfd,
                  addr & ~(4096UL - 1));
  if (map_base == (void *)-1)
    exit(1);
  virt_addr = map_base + (addr & (4096UL - 1));
  *((unsigned long *)virt_addr) = dat;
  close(memfd);
}


x86 assembly (clang, -O0):
<reg_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
lea    0xe97(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1184 <reg_write+0x44>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    0x2e2c(%rip),%rax
or     (%rax),%edx
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x8(%rbp),%r9
and    $0xfffffffffffff000,%r9
xor    %edi,%edi
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    11d9 <reg_write+0x99>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
and    $0xfff,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reg_write>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e87(%rip),%rax
mov    0x2e78(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11be <reg_write+0x7e>
mov    %eax,%ebp
mov    0x2e82(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e60(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%r9
and    $0xfffffffffffff000,%r9
mov    $0x1000,%esi
xor    %edi,%edi
mov    %ebp,%r8d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11be <reg_write+0x7e>
and    $0xfff,%ebx
mov    %r14,(%rax,%rbx,1)
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <close@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define void @reg_write(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i64 %0, i64* %3
store i64 %1, i64* %4
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_SYNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %16, %17
%19 = load i32, i32* @MAP_SHARED
%20 = load i32, i32* %5
%21 = load i64, i64* %3
%22 = and i64 %21, -4096
%23 = call i8* @mmap(i32 0, i64 4096, i32 %18, i32 %19, i32 %20, i64 %22)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = icmp eq i8* %24, inttoptr (i64 -1 to i8*)
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %15
%29 = load i8*, i8** %6
%30 = load i64, i64* %3
%31 = and i64 %30, 4095
%32 = getelementptr i8, i8* %29, i64 %31
store i8* %32, i8** %7
%33 = load i64, i64* %4
%34 = load i8*, i8** %7
%35 = bitcast i8* %34 to i64*
store i64 %33, i64* %35
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define void @reg_write(i64 %0, i64 %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = and i64 %0, -4096
%16 = tail call i8* @mmap(i32 0, i64 4096, i32 %13, i32 %14, i32 %6, i64 %15)
%17 = icmp eq i8* %16, inttoptr (i64 -1 to i8*)
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = and i64 %0, 4095
%22 = getelementptr i8, i8* %16, i64 %21
%23 = bitcast i8* %22 to i64*
store i64 %1, i64* %23
%24 = tail call i32 @close(i32 %6)
ret void
}