#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(void *, 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");
    close(fd);
    return -1;
  }
  printf("/dev/mem opened successfully!\n");
  void *virtualBase = (void *)mmap(NULL, (0x00001000), (PROT_READ | PROT_WRITE),
                                   MAP_SHARED, fd, (0xff200000));
  printf("virtualBase = 0x%x\n", (unsigned int)virtualBase);
  printf("This is for pr_region_alternate!\n");
  printf("read sys id:\n");
  int *sysIDPtr = (int *)(virtualBase + (0x00000800) + (0x00000200));
  printf("0x%x\n", *sysIDPtr);
  printf("This is for pr_region_default!\n");
  printf("read sys id:\n");
  int *sysIDPtrOrig = (int *)(virtualBase + (0x00000800) + (0x00000000));
  printf("0x%x\n", *sysIDPtrOrig);
  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    11ba <main+0x5a>
lea    0xe68(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <main+0x152>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
mov    0x2de8(%rip),%rax
or     (%rax),%edx
mov    0x2e07(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
mov    $0xff200000,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x800,%rax
add    $0x200,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xdc0(%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   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e59(%rip),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    126a <main+0xfa>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e44(%rip),%rax
mov    0x2e05(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x1000,%esi
mov    %r12d,%r8d
mov    $0xff200000,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
lea    0xe37(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe59(%rip),%rdi
call   1030 <puts@plt>
lea    0xe8d(%rip),%r15
mov    %r15,%rdi
call   1030 <puts@plt>
mov    0xa00(%rbx),%esi
lea    0xe14(%rip),%rbp
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    0x800(%rbx),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <munmap@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd98(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1256 <main+0xe6>

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 = [34 x i8] c"This is for pr_region_alternate!\0A\00"
@.str.5 = [14 x i8] c"read sys id:\0A\00"
@.str.6 = [6 x i8] c"0x%x\0A\00"
@.str.7 = [32 x i8] c"This is for pr_region_default!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%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 %16
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* %2
%15 = call i32 @close(i32 %14)
store i32 -1, i32* %1
br label %50
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %18, %19
%21 = load i32, i32* @MAP_SHARED
%22 = load i32, i32* %2
%23 = call i64 @mmap(i32* null, i32 4096, i32 %20, i32 %21, i32 %22, i32 -14680064)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %3
%25 = load i8*, i8** %3
%26 = ptrtoint i8* %25 to i32
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%30 = load i8*, i8** %3
%31 = getelementptr i8, i8* %30, i64 2048
%32 = getelementptr i8, i8* %31, i64 512
%33 = bitcast i8* %32 to i32*
store i32* %33, i32** %4
%34 = load i32*, i32** %4
%35 = load i32, i32* %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0))
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%39 = load i8*, i8** %3
%40 = getelementptr i8, i8* %39, i64 2048
%41 = getelementptr i8, i8* %40, i64 0
%42 = bitcast i8* %41 to i32*
store i32* %42, i32** %5
%43 = load i32*, i32** %5
%44 = load i32, i32* %43
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %44)
%46 = load i8*, i8** %3
%47 = call i32 @munmap(i8* %46, i32 4096)
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
store i32 0, i32* %1
br label %50
50: ; preds = %16, %12
%51 = load i32, i32* %1
ret i32 %51
}

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.6 = [6 x i8] c"0x%x\0A\00"
@str = [30 x i8] c"/dev/mem opened successfully!\00"
@str.8 = [33 x i8] c"This is for pr_region_alternate!\00"
@str.10 = [31 x i8] c"This is for pr_region_default!\00"
@str.11 = [13 x i8] c"read sys 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 %31
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 -14680064)
%15 = inttoptr i64 %14 to i8*
%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 ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0))
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.11, i64 0, i64 0))
%20 = getelementptr i8, i8* %15, i64 2048
%21 = getelementptr i8, i8* %15, i64 2560
%22 = bitcast i8* %21 to i32*
%23 = load i32, i32* %22
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %23)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.11, i64 0, i64 0))
%27 = bitcast i8* %20 to i32*
%28 = load i32, i32* %27
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %28)
%30 = tail call i32 @munmap(i8* %15, i32 4096)
br label %31
31: ; preds = %8, %6
%32 = phi i32 [ -1, %6 ], [ 0, %8 ]
%33 = tail call i32 @close(i32 %4)
ret i32 %32
}