#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int getpagesize();
void *mmap(int /*<<< orphan*/, unsigned int, int, int /*<<< orphan*/, int,
           unsigned long);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
volatile void *ioremap(unsigned long physaddr, unsigned size) {
  static int axs_mem_fd = -1;
  unsigned long page_addr, ofs_addr, reg, pgmask;
  void *reg_mem = NULL;
  pgmask = getpagesize() - 1;
  page_addr = physaddr & ~pgmask;
  ofs_addr = physaddr & pgmask;
  if (axs_mem_fd == -1) {
    axs_mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
    if (axs_mem_fd < 0) {
      perror("AXS: can't open /dev/mem");
      return NULL;
    }
  }
  reg_mem = mmap((caddr_t)reg_mem, size + ofs_addr, PROT_READ | PROT_WRITE,
                 MAP_SHARED, axs_mem_fd, page_addr);
  if (reg_mem == MAP_FAILED) {
    perror("AXS: mmap error");
    close(axs_mem_fd);
    return NULL;
  }
  reg = (unsigned long)reg_mem + ofs_addr;
  return (volatile void *)reg;
}


x86 assembly (clang, -O0):
<ioremap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x40(%rbp)
mov    $0x0,%al
call   1070 <getpagesize@plt>
sub    $0x1,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
and    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0xffffffff,0x2eab(%rip)
jne    11f2 <ioremap+0xa2>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
lea    0xe54(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x2e81(%rip)
cmpl   $0x0,0x2e7a(%rip)
jge    11ed <ioremap+0x9d>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    127c <ioremap+0x12c>
jmp    11f2 <ioremap+0xa2>
mov    -0x40(%rbp),%rax
mov    %eax,%edi
mov    -0x14(%rbp),%eax
add    -0x28(%rbp),%rax
mov    %eax,%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
mov    0x2daf(%rip),%rax
or     (%rax),%edx
mov    0x2dce(%rip),%rax
mov    (%rax),%ecx
mov    0x2e25(%rip),%r8d
mov    -0x20(%rbp),%r9
call   1030 <mmap@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2d7d(%rip),%rcx
cmp    (%rcx),%rax
jne    1268 <ioremap+0x118>
lea    0xdce(%rip),%rdi
call   1060 <perror@plt>
mov    0x2df2(%rip),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    127c <ioremap+0x12c>
mov    -0x40(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ioremap>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %eax,%ebx
mov    0x2edf(%rip),%r8d
cmp    $0xffffffff,%r8d
jne    119a <ioremap+0x4a>
mov    0x2e62(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe88(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%r8d
mov    %eax,0x2eb2(%rip)
test   %eax,%eax
js     1204 <ioremap+0xb4>
add    $0xffffffff,%ebx
movslq %ebx,%rbx
mov    %rbx,%r9
not    %r9
and    %r14,%r9
and    %r14,%rbx
add    %ebx,%ebp
mov    0x2e43(%rip),%rax
mov    0x2e04(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e21(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
call   1030 <mmap@plt>
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%rax
je     11e9 <ioremap+0x99>
add    %rax,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe29(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e4d(%rip),%edi
call   1040 <close@plt>
xor    %ebx,%ebx
jmp    11e1 <ioremap+0x91>
lea    0xdf5(%rip),%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
jmp    11e1 <ioremap+0x91>

LLVM IR (-O0):
@ioremap.axs_mem_fd = internal global i32 -1
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [25 x i8] c"AXS: can't open /dev/mem\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [16 x i8] c"AXS: mmap error\00"
define i8* @ioremap(i64 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i64 %0, i64* %4
store i32 %1, i32* %5
store i8* null, i8** %10
%11 = call i32 (...) @getpagesize()
%12 = sub nsw i32 %11, 1
%13 = sext i32 %12 to i64
store i64 %13, i64* %9
%14 = load i64, i64* %4
%15 = load i64, i64* %9
%16 = xor i64 %15, -1
%17 = and i64 %14, %16
store i64 %17, i64* %6
%18 = load i64, i64* %4
%19 = load i64, i64* %9
%20 = and i64 %18, %19
store i64 %20, i64* %7
%21 = load i32, i32* @ioremap.axs_mem_fd
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %33
23: ; preds = %2
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_SYNC
%26 = or i32 %24, %25
%27 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 %27, i32* @ioremap.axs_mem_fd
%28 = load i32, i32* @ioremap.axs_mem_fd
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %62
32: ; preds = %23
br label %33
33: ; preds = %32, %2
%34 = load i8*, i8** %10
%35 = ptrtoint i8* %34 to i32
%36 = load i32, i32* %5
%37 = zext i32 %36 to i64
%38 = load i64, i64* %7
%39 = add i64 %37, %38
%40 = trunc i64 %39 to i32
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @PROT_WRITE
%43 = or i32 %41, %42
%44 = load i32, i32* @MAP_SHARED
%45 = load i32, i32* @ioremap.axs_mem_fd
%46 = load i64, i64* %6
%47 = call i8* @mmap(i32 %35, i32 %40, i32 %43, i32 %44, i32 %45, i64 %46)
store i8* %47, i8** %10
%48 = load i8*, i8** %10
%49 = load i8*, i8** @MAP_FAILED
%50 = icmp eq i8* %48, %49
br i1 %50, label %51, label %55
51: ; preds = %33
%52 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%53 = load i32, i32* @ioremap.axs_mem_fd
%54 = call i32 @close(i32 %53)
store i8* null, i8** %3
br label %62
55: ; preds = %33
%56 = load i8*, i8** %10
%57 = ptrtoint i8* %56 to i64
%58 = load i64, i64* %7
%59 = add i64 %57, %58
store i64 %59, i64* %8
%60 = load i64, i64* %8
%61 = inttoptr i64 %60 to i8*
store i8* %61, i8** %3
br label %62
62: ; preds = %55, %51, %30
%63 = load i8*, i8** %3
ret i8* %63
}

LLVM IR (-O2):
@ioremap.axs_mem_fd = internal unnamed_addr global i32 -1
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [25 x i8] c"AXS: can't open /dev/mem\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [16 x i8] c"AXS: mmap error\00"
define i8* @ioremap(i64 %0, i32 %1) {
%3 = tail call i32 (...) @getpagesize()
%4 = add nsw i32 %3, -1
%5 = sext i32 %4 to i64
%6 = xor i64 %5, -1
%7 = and i64 %6, %0
%8 = and i64 %5, %0
%9 = load i32, i32* @ioremap.axs_mem_fd
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_SYNC
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* @ioremap.axs_mem_fd
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %38
19: ; preds = %11, %2
%20 = phi i32 [ %15, %11 ], [ %9, %2 ]
%21 = trunc i64 %8 to i32
%22 = add i32 %21, %1
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %24, %23
%26 = load i32, i32* @MAP_SHARED
%27 = tail call i8* @mmap(i32 0, i32 %22, i32 %25, i32 %26, i32 %20, i64 %7)
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %34
30: ; preds = %19
%31 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @ioremap.axs_mem_fd
%33 = tail call i32 @close(i32 %32)
br label %38
34: ; preds = %19
%35 = ptrtoint i8* %27 to i64
%36 = add i64 %8, %35
%37 = inttoptr i64 %36 to i8*
br label %38
38: ; preds = %34, %30, %17
%39 = phi i8* [ null, %17 ], [ null, %30 ], [ %37, %34 ]
ret i8* %39
}