#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);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(int *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int fd = open("/dev/mem", (O_RDWR | O_SYNC));
  if (fd < 1) {
    perror("error opening /dev/mem\n");
    return -1;
  }
  printf("/dev/mem opened successfully!\n");
  int *virtualBase = (int *)mmap(NULL, (0x00001000), (PROT_READ | PROT_WRITE),
                                 MAP_SHARED, fd, (0x100000a00));
  printf("virtualBase = 0x%x\n", (unsigned int)virtualBase);
  int *sramStart = (int *)(virtualBase);
  printf("reading from PR ID:\n");
  int result = *sramStart;
  printf("result = %x\n", result);
  munmap(virtualBase, (0x00001000));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    0x2e51(%rip),%rax
or     (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jge    11b2 <main+0x52>
lea    0xe68(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <main+0xf8>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    0x2df0(%rip),%rax
or     (%rax),%edx
mov    0x2e0f(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
mov    $0xa00,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e5d(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe73(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
jle    1226 <main+0xb6>
mov    %eax,%ebp
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e49(%rip),%rax
mov    0x2e0a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e27(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebp,%r8d
mov    $0xa00,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
lea    0xe3c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%esi
lea    0xe32(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xddc(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%r14d
jmp    121e <main+0xae>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [24 x i8] c"error opening /dev/mem\0A\00"
@.str.2 = [31 x i8] c"/dev/mem opened successfully!\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [20 x i8] c"virtualBase = 0x%x\0A\00"
@.str.4 = [21 x i8] c"reading from PR ID:\0A\00"
@.str.5 = [13 x i8] c"result = %x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_SYNC
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %36
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%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* %2
%21 = call i64 @mmap(i32* null, i32 4096, i32 %18, i32 %19, i32 %20, i32 2560)
%22 = inttoptr i64 %21 to i32*
store i32* %22, i32** %3
%23 = load i32*, i32** %3
%24 = ptrtoint i32* %23 to i32
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = load i32*, i32** %3
store i32* %26, i32** %4
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%28 = load i32*, i32** %4
%29 = load i32, i32* %28
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = load i32*, i32** %3
%33 = call i32 @munmap(i32* %32, i32 4096)
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
store i32 0, i32* %1
br label %36
36: ; preds = %14, %12
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [24 x i8] c"error opening /dev/mem\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [20 x i8] c"virtualBase = 0x%x\0A\00"
@.str.5 = [13 x i8] c"result = %x\0A\00"
@str = [30 x i8] c"/dev/mem opened successfully!\00"
@str.6 = [20 x i8] c"reading from PR ID:\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_SYNC
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %23
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i64 @mmap(i32* null, i32 4096, i32 %12, i32 %13, i32 %4, i32 2560)
%15 = inttoptr i64 %14 to i32*
%16 = trunc i64 %14 to i32
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
%19 = load i32, i32* %15
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%21 = tail call i32 @munmap(i32* %15, i32 4096)
%22 = tail call i32 @close(i32 %4)
br label %23
23: ; preds = %8, %6
%24 = phi i32 [ -1, %6 ], [ 0, %8 ]
ret i32 %24
}