#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 */
unsigned int *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int getpagesize();
unsigned int *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                   unsigned int);
int /*<<< orphan*/ munmap(unsigned int *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int wt(unsigned reg, unsigned val) {
  int argc = 3;
  if (argc != 3) {
    printf("insufficient arguments\n");
    return -1;
  }
  int fd;
  unsigned int *map_base;
  unsigned int PHYADDR, newval;
  int pagesize = getpagesize();
  PHYADDR = reg;
  newval = val;
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd == -1) {
    printf("open /dev/mem failed\n");
    return -1;
  }
  map_base = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                  PHYADDR & 0xfffff000);
  if (map_base == MAP_FAILED) {
    perror("mmap");
  }
  int pgoffset = (PHYADDR & 0x00000fff) >> 2;
  map_base[pgoffset] = newval;
  printf("Value at 0x%08X: 0x%08X\n", PHYADDR,
         (unsigned int)map_base[pgoffset]);
  close(fd);
  munmap(map_base, pagesize);
  return 0;
}


x86 assembly (clang, -O0):
<wt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x3,-0x10(%rbp)
cmpl   $0x3,-0x10(%rbp)
je     11a9 <wt+0x39>
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <wt+0x141>
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
lea    0xe40(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1204 <wt+0x94>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <wt+0x141>
mov    -0x2c(%rbp),%esi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
mov    0x2da9(%rip),%rax
or     (%rax),%edx
mov    0x2dc8(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x24(%rbp),%r9d
and    $0xfffff000,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d6f(%rip),%rcx
cmp    (%rcx),%rax
jne    125e <wt+0xee>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%eax
and    $0xfff,%eax
shr    $0x2,%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%edx
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1060 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%r13d
xor    %eax,%eax
call   10a0 <getpagesize@plt>
mov    %eax,%r14d
mov    0x2e36(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     123b <wt+0xbb>
mov    %eax,%ebp
mov    0x2e31(%rip),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0f(%rip),%rax
mov    (%rax),%ecx
mov    %r13d,%r9d
and    $0xfffff000,%r9d
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2db9(%rip),%rax
cmp    (%rax),%rbx
je     125f <wt+0xdf>
mov    %r13d,%eax
shr    $0x2,%eax
and    $0x3ff,%eax
mov    %r15d,(%rbx,%rax,4)
lea    0xdf4(%rip),%rdi
mov    %r13d,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <munmap@plt>
jmp    124d <wt+0xcd>
lea    0xde5(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda3(%rip),%rdi
call   1090 <perror@plt>
jmp    1204 <wt+0x84>

LLVM IR (-O0):
@.str = [24 x i8] c"insufficient arguments\0A\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [22 x i8] c"open /dev/mem failed\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [25 x i8] c"Value at 0x%08X: 0x%08X\0A\00"
define i32 @wt(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 3, i32* %6
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
17: ; preds = %2
%18 = call i32 (...) @getpagesize()
store i32 %18, i32* %11
%19 = load i32, i32* %4
store i32 %19, i32* %9
%20 = load i32, i32* %5
store i32 %20, i32* %10
%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.1, i64 0, i64 0), i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
29: ; preds = %17
%30 = load i32, i32* %11
%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* %9
%37 = and i32 %36, -4096
%38 = call i32* @mmap(i32* null, i32 %30, i32 %33, i32 %34, i32 %35, i32 %37)
store i32* %38, i32** %8
%39 = load i32*, i32** %8
%40 = load i32*, i32** @MAP_FAILED
%41 = icmp eq i32* %39, %40
br i1 %41, label %42, label %44
42: ; preds = %29
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %29
%45 = load i32, i32* %9
%46 = and i32 %45, 4095
%47 = lshr i32 %46, 2
store i32 %47, i32* %12
%48 = load i32, i32* %10
%49 = load i32*, i32** %8
%50 = load i32, i32* %12
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
store i32 %48, i32* %52
%53 = load i32, i32* %9
%54 = load i32*, i32** %8
%55 = load i32, i32* %12
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %54, i64 %56
%58 = load i32, i32* %57
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %53, i32 %58)
%60 = load i32, i32* %7
%61 = call i32 @close(i32 %60)
%62 = load i32*, i32** %8
%63 = load i32, i32* %11
%64 = call i32 @munmap(i32* %62, i32 %63)
store i32 0, i32* %3
br label %65
65: ; preds = %44, %27, %15
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str.1 = [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
@MAP_FAILED = global i32* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [25 x i8] c"Value at 0x%08X: 0x%08X\0A\00"
@str = [21 x i8] c"open /dev/mem failed\00"
define i32 @wt(i32 %0, i32 %1) {
%3 = tail call i32 (...) @getpagesize()
%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.1, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %30
11: ; preds = %2
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @MAP_SHARED
%16 = and i32 %0, -4096
%17 = tail call i32* @mmap(i32* null, i32 %3, i32 %14, i32 %15, i32 %7, i32 %16)
%18 = load i32*, i32** @MAP_FAILED
%19 = icmp eq i32* %17, %18
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %11
%23 = lshr i32 %0, 2
%24 = and i32 %23, 1023
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %17, i64 %25
store i32 %1, i32* %26
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %0, i32 %1)
%28 = tail call i32 @close(i32 %7)
%29 = tail call i32 @munmap(i32* %17, i32 %3)
br label %30
30: ; preds = %9, %22
%31 = phi i32 [ -1, %9 ], [ 0, %22 ]
ret i32 %31
}