#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 */
typedef int /*<<< orphan*/ caddr_t;

/* Variables and functions */
int MAP_FIXED;
int MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
char *malloc(int);
scalar_t__ mmap(int /*<<< orphan*/, int, int, int, int, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
volatile unsigned *mapRegisterMemory(int base) {
  static int mem_fd = 0;
  char *mem, *map;
  if (!mem_fd) {
    if ((mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
      printf("Ne mogu da pristupim /dev/mem \n");
      exit(-1);
    }
  }
  if ((mem = malloc((4 * 1024) + ((4 * 1024) - 1))) == NULL) {
    printf("Greska u alokaciji \n");
    exit(-1);
  }
  if ((unsigned long)mem % (4 * 1024))
    mem += (4 * 1024) - ((unsigned long)mem % (4 * 1024));
  map = (char *)mmap((caddr_t)mem, (4 * 1024), PROT_READ | PROT_WRITE,
                     MAP_SHARED | MAP_FIXED, mem_fd, base);
  if ((long)map < 0) {
    printf("mmap Greska %d\n", (int)map);
    exit(-1);
  }
  return (volatile unsigned *)map;
}


x86 assembly (clang, -O0):
<mapRegisterMemory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,0x2eea(%rip)
jne    11b2 <mapRegisterMemory+0x62>
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,0x2ec0(%rip)
cmp    $0x0,%eax
jge    11ad <mapRegisterMemory+0x5d>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    11b2 <mapRegisterMemory+0x62>
mov    $0x1fff,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11e2 <mapRegisterMemory+0x92>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
and    $0xfff,%rax
cmp    $0x0,%rax
je     1211 <mapRegisterMemory+0xc1>
mov    -0x10(%rbp),%rcx
and    $0xfff,%rcx
mov    $0x1000,%eax
sub    %rcx,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    0x2d99(%rip),%rax
or     (%rax),%edx
mov    0x2db8(%rip),%rax
mov    (%rax),%ecx
mov    0x2d7f(%rip),%rax
or     (%rax),%ecx
mov    0x2e0a(%rip),%r8d
mov    -0x4(%rbp),%r9d
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    $0x0,%rax
jge    1280 <mapRegisterMemory+0x130>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapRegisterMemory>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ee9(%rip),%ebp
test   %ebp,%ebp
jne    119d <mapRegisterMemory+0x3d>
mov    0x2e62(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
mov    %eax,0x2ebf(%rip)
test   %eax,%eax
js     1228 <mapRegisterMemory+0xc8>
mov    $0x1fff,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1205 <mapRegisterMemory+0xa5>
mov    %eax,%ecx
and    $0xfff,%ecx
mov    $0x1000,%edx
sub    %ecx,%edx
xor    %edi,%edi
test   %rcx,%rcx
cmovne %edx,%edi
add    %eax,%edi
mov    0x2e2c(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0a(%rip),%rax
mov    0x2dd3(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0x1000,%esi
mov    %ebp,%r8d
mov    %ebx,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
js     120e <mapRegisterMemory+0xae>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe0d(%rip),%rdi
jmp    122f <mapRegisterMemory+0xcf>
lea    0xdf4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xdfe(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@mapRegisterMemory.mem_fd = internal global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [32 x i8] c"Ne mogu da pristupim /dev/mem \0A\00"
@.str.2 = [21 x i8] c"Greska u alokaciji \0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FIXED = global i32 0
@.str.3 = [16 x i8] c"mmap Greska %d\0A\00"
define i32* @mapRegisterMemory(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i32 %0, i32* %2
%5 = load i32, i32* @mapRegisterMemory.mem_fd
%6 = icmp ne i32 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %1
%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* @mapRegisterMemory.mem_fd
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %7
br label %17
17: ; preds = %16, %1
%18 = call i8* @malloc(i32 8191)
store i8* %18, i8** %3
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %17
%24 = load i8*, i8** %3
%25 = ptrtoint i8* %24 to i64
%26 = urem i64 %25, 4096
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %23
%29 = load i8*, i8** %3
%30 = ptrtoint i8* %29 to i64
%31 = urem i64 %30, 4096
%32 = sub i64 4096, %31
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i64 %32
store i8* %34, i8** %3
br label %35
35: ; preds = %28, %23
%36 = load i8*, i8** %3
%37 = ptrtoint i8* %36 to i32
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @PROT_WRITE
%40 = or i32 %38, %39
%41 = load i32, i32* @MAP_SHARED
%42 = load i32, i32* @MAP_FIXED
%43 = or i32 %41, %42
%44 = load i32, i32* @mapRegisterMemory.mem_fd
%45 = load i32, i32* %2
%46 = call i64 @mmap(i32 %37, i32 4096, i32 %40, i32 %43, i32 %44, i32 %45)
%47 = inttoptr i64 %46 to i8*
store i8* %47, i8** %4
%48 = load i8*, i8** %4
%49 = ptrtoint i8* %48 to i64
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %35
%52 = load i8*, i8** %4
%53 = ptrtoint i8* %52 to i32
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %53)
%55 = call i32 @exit(i32 -1)
unreachable
56: ; preds = %35
%57 = load i8*, i8** %4
%58 = bitcast i8* %57 to i32*
ret i32* %58
}

LLVM IR (-O2):
@mapRegisterMemory.mem_fd = internal unnamed_addr global i32 0
@.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
@MAP_FIXED = global i32 0
@.str.3 = [16 x i8] c"mmap Greska %d\0A\00"
@str = [20 x i8] c"Greska u alokaciji \00"
@str.4 = [31 x i8] c"Ne mogu da pristupim /dev/mem \00"
define i32* @mapRegisterMemory(i32 %0) {
%2 = load i32, i32* @mapRegisterMemory.mem_fd
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %13
4: ; preds = %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_SYNC
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* @mapRegisterMemory.mem_fd
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.4, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %4, %1
%14 = phi i32 [ %8, %4 ], [ %2, %1 ]
%15 = tail call dereferenceable_or_null(8191) i8* @malloc(i32 8191)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %13
%21 = ptrtoint i8* %15 to i64
%22 = and i64 %21, 4095
%23 = icmp eq i64 %22, 0
%24 = sub nuw nsw i64 4096, %22
%25 = select i1 %23, i64 0, i64 %24
%26 = getelementptr i8, i8* %15, i64 %25
%27 = ptrtoint i8* %26 to i64
%28 = trunc i64 %27 to i32
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %30, %29
%32 = load i32, i32* @MAP_SHARED
%33 = load i32, i32* @MAP_FIXED
%34 = or i32 %33, %32
%35 = tail call i64 @mmap(i32 %28, i32 4096, i32 %31, i32 %34, i32 %14, i32 %0)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %20
%38 = trunc i64 %35 to i32
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = tail call i32 @exit(i32 -1)
unreachable
41: ; preds = %20
%42 = inttoptr i64 %35 to i32*
ret i32* %42
}