#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*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ mmap(int /*<<< orphan*/, unsigned int, int, int, int, unsigned long);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int posix_memalign(void **, long, unsigned int);
long sysconf(int /*<<< orphan*/);
volatile unsigned long *mapMem(unsigned long armAddr, unsigned int memSize) {
  char *mem, *map;
  long pageSize = sysconf(_SC_PAGE_SIZE);
  int memFD = open("/dev/mem", O_RDWR | O_SYNC);
  if (memFD < 0) {
    perror("can't open /dev/mem");
    exit(1);
  }
  int status = posix_memalign((void **)&mem, pageSize, memSize);
  if (status != 0) {
    perror("Can't get aligned memory");
    exit(2);
  }
  map = (unsigned char *)mmap((caddr_t)mem, memSize, PROT_READ | PROT_WRITE,
                              MAP_SHARED | MAP_FIXED, memFD, armAddr);
  if ((long)map < 0) {
    perror("Can't map memory");
    exit(1);
  }
  close(memFD);
  return (volatile unsigned long *)map;
}


x86 assembly (clang, -O0):
<mapMem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
lea    0xe61(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11d2 <mapMem+0x62>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1090 <posix_memalign@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1205 <mapMem+0x95>
lea    0xe1e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    -0xc(%rbp),%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
mov    0x2da2(%rip),%rax
or     (%rax),%edx
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
mov    0x2d80(%rip),%rax
or     (%rax),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x8(%rbp),%r9
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    $0x0,%rax
jge    1267 <mapMem+0xf7>
lea    0xdd5(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapMem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,%rbp
mov    0x2e43(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe64(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1216 <mapMem+0xa6>
mov    %eax,%ebx
mov    %rsp,%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1090 <posix_memalign@plt>
test   %eax,%eax
jne    121f <mapMem+0xaf>
mov    (%rsp),%edi
mov    0x2e2a(%rip),%rax
mov    0x2deb(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e08(%rip),%rax
mov    0x2dc9(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %r15d,%esi
mov    %ebx,%r8d
mov    %r14,%r9
call   1030 <mmap@plt>
test   %rax,%rax
js     1235 <mapMem+0xc5>
mov    %rax,%rbp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde3(%rip),%rdi
jmp    123c <mapMem+0xcc>
lea    0xdee(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xdf1(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@_SC_PAGE_SIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [20 x i8] c"can't open /dev/mem\00"
@.str.2 = [25 x i8] c"Can't get aligned memory\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FIXED = global i32 0
@.str.3 = [17 x i8] c"Can't map memory\00"
define i64* @mapMem(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i64 %0, i64* %3
store i32 %1, i32* %4
%10 = load i32, i32* @_SC_PAGE_SIZE
%11 = call i64 @sysconf(i32 %10)
store i64 %11, i64* %7
%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* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i64, i64* %7
%23 = load i32, i32* %4
%24 = call i32 @posix_memalign(i8** %5, i64 %22, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 2)
unreachable
30: ; preds = %21
%31 = load i8*, i8** %5
%32 = ptrtoint i8* %31 to i32
%33 = load i32, i32* %4
%34 = load i32, i32* @PROT_READ
%35 = load i32, i32* @PROT_WRITE
%36 = or i32 %34, %35
%37 = load i32, i32* @MAP_SHARED
%38 = load i32, i32* @MAP_FIXED
%39 = or i32 %37, %38
%40 = load i32, i32* %8
%41 = load i64, i64* %3
%42 = call i64 @mmap(i32 %32, i32 %33, i32 %36, i32 %39, i32 %40, i64 %41)
%43 = inttoptr i64 %42 to i8*
store i8* %43, i8** %6
%44 = load i8*, i8** %6
%45 = ptrtoint i8* %44 to i64
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %30
%48 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %30
%51 = load i32, i32* %8
%52 = call i32 @close(i32 %51)
%53 = load i8*, i8** %6
%54 = bitcast i8* %53 to i64*
ret i64* %54
}

LLVM IR (-O2):
@_SC_PAGE_SIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [20 x i8] c"can't open /dev/mem\00"
@.str.2 = [25 x i8] c"Can't get aligned memory\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FIXED = global i32 0
@.str.3 = [17 x i8] c"Can't map memory\00"
define i64* @mapMem(i64 %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = load i32, i32* @_SC_PAGE_SIZE
%6 = tail call i64 @sysconf(i32 %5)
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_SYNC
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32 @posix_memalign(i8** %3, i64 %6, i32 %1)
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 2)
unreachable
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = ptrtoint i8* %22 to i64
%24 = trunc i64 %23 to i32
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %26, %25
%28 = load i32, i32* @MAP_SHARED
%29 = load i32, i32* @MAP_FIXED
%30 = or i32 %29, %28
%31 = call i64 @mmap(i32 %24, i32 %1, i32 %27, i32 %30, i32 %10, i64 %0)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %21
%34 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %21
%37 = call i32 @close(i32 %10)
%38 = inttoptr i64 %31 to i64*
ret i64* %38
}