#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 open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  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, (0x00000000));
  printf("virtualBase = 0x%x\n", (unsigned int)virtualBase);
  int *memIntPtr = (int *)virtualBase;
  int i;
  int range = 128;
  for (i = 0; i < range; i++) {
    int *currPtr = memIntPtr + i;
    *currPtr = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
or     (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11b1 <main+0x61>
lea    0xe71(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <main+0x10b>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    0x2df1(%rip),%rax
or     (%rax),%edx
mov    0x2e10(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x80,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1254 <main+0x104>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    121d <main+0xcd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e5e(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe74(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11fd <main+0x8d>
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e4e(%rip),%rax
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
lea    0xe45(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x200,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe05(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
jmp    11f4 <main+0x84>

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"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%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* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %3
br label %50
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %24, %25
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* %6
%29 = call i64 @mmap(i32* null, i32 4096, i32 %26, i32 %27, i32 %28, i32 0)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = ptrtoint i8* %31 to i32
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = load i8*, i8** %7
%35 = bitcast i8* %34 to i32*
store i32* %35, i32** %8
store i32 128, i32* %10
store i32 0, i32* %9
br label %36
36: ; preds = %46, %22
%37 = load i32, i32* %9
%38 = load i32, i32* %10
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %49
40: ; preds = %36
%41 = load i32*, i32** %8
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
store i32* %44, i32** %11
%45 = load i32*, i32** %11
store i32 0, i32* %45
br label %46
46: ; preds = %40
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 1
store i32 %48, i32* %9
br label %36
49: ; preds = %36
store i32 0, i32* %3
br label %50
50: ; preds = %49, %18
%51 = load i32, i32* %3
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 = [30 x i8] c"/dev/mem opened successfully!\00"
define i32 @main(i32 %0, i8** nocapture readnone %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 slt i32 %6, 1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @close(i32 %6)
br label %21
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i64 @mmap(i32* null, i32 4096, i32 %15, i32 %16, i32 %6, i32 0)
%18 = trunc i64 %17 to i32
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = inttoptr i64 %17 to i8*
br label %21
21: ; preds = %11, %8
%22 = phi i32 [ -1, %8 ], [ 0, %11 ]
ret i32 %22
}