#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 off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
scalar_t__ close(int);
unsigned int getpagesize();
void *mmap(int /*<<< orphan*/, size_t, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int regio_rd32(unsigned int addr, unsigned int *data, int verbose) {
  int fd;
  unsigned int page_size, mapped_pages;
  void *pmem;
  void *vaddr;
  volatile unsigned int *reg_addr;
  unsigned int reg_data;
  off_t phy_addr;
  size_t phy_length;
  if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
    printf("ERROR : failed to open /dev/mem\n");
    return (-1);
  }
  phy_addr = 0x43c00000;
  phy_length = (16 * 64 * 1024);
  page_size = getpagesize();
  mapped_pages = phy_length / page_size;
  if (verbose) {
    printf("off_t size            : %lu\n", (long unsigned int)sizeof(off_t));
    printf("phy_addr & map_mask   : 0x%llx\n",
           (unsigned long long)(phy_addr & ~((off_t)((16 * 64 * 1024) - 1))));
    printf("page size             : 0x%x (%d)\n", page_size, page_size);
    printf("num pages             : %d\n", mapped_pages);
  }
  pmem = mmap(0, phy_length, (PROT_READ | PROT_WRITE), MAP_SHARED, fd,
              phy_addr & ~((off_t)((16 * 64 * 1024) - 1)));
  if ((pmem == MAP_FAILED) || (pmem == NULL)) {
    printf("ERROR: failed to mmap /dev/mem\n");
    return (-1);
  } else {
    if (verbose) {
      printf("pmem                  : 0x%lx\n", (long unsigned int)pmem);
      printf("pmem (ptr)            : %p\n", pmem);
    }
  }
  if (close(fd)) {
    printf("ERROR: failed to close /dev/mem\n");
    return (-1);
  }
  vaddr = (char *)pmem + (phy_addr & ((off_t)((16 * 64 * 1024) - 1)));
  reg_addr = ((volatile unsigned int *)((char *)vaddr + addr));
  reg_data = *reg_addr;
  *data = reg_data;
  if (verbose) {
    printf("reg_addr              : 0x%08x\n", (unsigned int)reg_addr);
    printf("reg_data              : 0x%08x\n", reg_data);
  }
  munmap(pmem, phy_length);
  return (0);
}


x86 assembly (clang, -O0):
<regio_rd32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11b6 <regio_rd32+0x56>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <regio_rd32+0x224>
movl   $0x43c00000,-0x40(%rbp)
movq   $0x100000,-0x48(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x1c(%rbp),%ecx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1239 <regio_rd32+0xd9>
lea    0xe3b(%rip),%rdi
mov    $0x4,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%eax
and    $0xfff00000,%eax
movslq %eax,%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%esi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%rsi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
mov    0x2d73(%rip),%rax
or     (%rax),%edx
mov    0x2d92(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
mov    -0x40(%rbp),%r9d
and    $0xfff00000,%r9d
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d3b(%rip),%rcx
cmp    (%rcx),%rax
je     1291 <regio_rd32+0x131>
cmpq   $0x0,-0x28(%rbp)
jne    12ab <regio_rd32+0x14b>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <regio_rd32+0x224>
cmpl   $0x0,-0x14(%rbp)
je     12d9 <regio_rd32+0x179>
mov    -0x28(%rbp),%rsi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12de <regio_rd32+0x17e>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     130a <regio_rd32+0x1aa>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1384 <regio_rd32+0x224>
mov    -0x28(%rbp),%rax
mov    -0x40(%rbp),%ecx
and    $0xfffff,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x8(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x14(%rbp)
je     1370 <regio_rd32+0x210>
mov    -0x38(%rbp),%rax
mov    %eax,%esi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x3c(%rbp),%esi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1060 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<regio_rd32>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e4d(%rip),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xf71(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     128f <regio_rd32+0x11f>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <getpagesize@plt>
test   %r12d,%r12d
je     120f <regio_rd32+0x9f>
mov    %eax,%ebx
mov    $0x100000,%eax
xor    %edx,%edx
div    %ebx
mov    %eax,%r13d
lea    0xe33(%rip),%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe3d(%rip),%rdi
mov    $0x43c00000,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe4a(%rip),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe5b(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2de2(%rip),%rax
mov    0x2da3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dc0(%rip),%rax
mov    (%rax),%ecx
mov    $0x100000,%esi
xor    %edi,%edi
mov    %ebp,%r8d
mov    $0x43c00000,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
je     1298 <regio_rd32+0x128>
mov    %rax,%rbx
mov    0x2d6a(%rip),%rax
cmp    (%rax),%rbx
je     1298 <regio_rd32+0x128>
test   %r12d,%r12d
je     127a <regio_rd32+0x10a>
lea    0xe1d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe2b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
test   %rax,%rax
je     12b8 <regio_rd32+0x148>
lea    0xe6a(%rip),%rdi
jmp    129f <regio_rd32+0x12f>
lea    0xea0(%rip),%rdi
jmp    129f <regio_rd32+0x12f>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%eax
mov    (%rbx,%rax,1),%ebp
mov    %ebp,(%r14)
test   %r12d,%r12d
je     12ea <regio_rd32+0x17a>
mov    %rbx,%rsi
add    %rax,%rsi
lea    0xde4(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdf6(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x100000,%esi
mov    %rbx,%rdi
call   1070 <munmap@plt>
xor    %eax,%eax
jmp    12a9 <regio_rd32+0x139>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [33 x i8] c"ERROR : failed to open /dev/mem\0A\00"
@.str.2 = [29 x i8] c"off_t size : %lu\0A\00"
@.str.3 = [32 x i8] c"phy_addr & map_mask : 0x%llx\0A\00"
@.str.4 = [35 x i8] c"page size : 0x%x (%d)\0A\00"
@.str.5 = [28 x i8] c"num pages : %d\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.6 = [32 x i8] c"ERROR: failed to mmap /dev/mem\0A\00"
@.str.7 = [31 x i8] c"pmem : 0x%lx\0A\00"
@.str.8 = [28 x i8] c"pmem (ptr) : %p\0A\00"
@.str.9 = [33 x i8] c"ERROR: failed to close /dev/mem\0A\00"
@.str.10 = [32 x i8] c"reg_addr : 0x%08x\0A\00"
@.str.11 = [32 x i8] c"reg_data : 0x%08x\0A\00"
define i32 @regio_rd32(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i64
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_SYNC
%19 = or i32 %17, %18
%20 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 %20, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %105
24: ; preds = %3
store i32 1136656384, i32* %15
store i64 1048576, i64* %16
%25 = call i32 (...) @getpagesize()
store i32 %25, i32* %9
%26 = load i64, i64* %16
%27 = load i32, i32* %9
%28 = zext i32 %27 to i64
%29 = udiv i64 %26, %28
%30 = trunc i64 %29 to i32
store i32 %30, i32* %10
%31 = load i32, i32* %7
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %24
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 4)
%35 = load i32, i32* %15
%36 = and i32 %35, -1048576
%37 = sext i32 %36 to i64
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %37)
%39 = load i32, i32* %9
%40 = load i32, i32* %9
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %39, i32 %40)
%42 = load i32, i32* %10
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %33, %24
%45 = load i64, i64* %16
%46 = load i32, i32* @PROT_READ
%47 = load i32, i32* @PROT_WRITE
%48 = or i32 %46, %47
%49 = load i32, i32* @MAP_SHARED
%50 = load i32, i32* %8
%51 = load i32, i32* %15
%52 = and i32 %51, -1048576
%53 = call i8* @mmap(i32 0, i64 %45, i32 %48, i32 %49, i32 %50, i32 %52)
store i8* %53, i8** %11
%54 = load i8*, i8** %11
%55 = load i8*, i8** @MAP_FAILED
%56 = icmp eq i8* %54, %55
br i1 %56, label %60, label %57
57: ; preds = %44
%58 = load i8*, i8** %11
%59 = icmp eq i8* %58, null
br i1 %59, label %60, label %62
60: ; preds = %57, %44
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %4
br label %105
62: ; preds = %57
%63 = load i32, i32* %7
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %71
65: ; preds = %62
%66 = load i8*, i8** %11
%67 = ptrtoint i8* %66 to i64
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i64 %67)
%69 = load i8*, i8** %11
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %69)
br label %71
71: ; preds = %65, %62
br label %72
72: ; preds = %71
%73 = load i32, i32* %8
%74 = call i64 @close(i32 %73)
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %72
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %4
br label %105
78: ; preds = %72
%79 = load i8*, i8** %11
%80 = load i32, i32* %15
%81 = and i32 %80, 1048575
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %79, i64 %82
store i8* %83, i8** %12
%84 = load i8*, i8** %12
%85 = load i32, i32* %5
%86 = zext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %84, i64 %86
%88 = bitcast i8* %87 to i32*
store i32* %88, i32** %13
%89 = load i32*, i32** %13
%90 = load volatile i32, i32* %89
store i32 %90, i32* %14
%91 = load i32, i32* %14
%92 = load i32*, i32** %6
store i32 %91, i32* %92
%93 = load i32, i32* %7
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %101
95: ; preds = %78
%96 = load i32*, i32** %13
%97 = ptrtoint i32* %96 to i32
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.10, i64 0, i64 0), i32 %97)
%99 = load i32, i32* %14
%100 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.11, i64 0, i64 0), i32 %99)
br label %101
101: ; preds = %95, %78
%102 = load i8*, i8** %11
%103 = load i64, i64* %16
%104 = call i32 @munmap(i8* %102, i64 %103)
store i32 0, i32* %4
br label %105
105: ; preds = %101, %76, %60, %22
%106 = load i32, i32* %4
ret i32 %106
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [29 x i8] c"off_t size : %lu\0A\00"
@.str.3 = [32 x i8] c"phy_addr & map_mask : 0x%llx\0A\00"
@.str.4 = [35 x i8] c"page size : 0x%x (%d)\0A\00"
@.str.5 = [28 x i8] c"num pages : %d\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.7 = [31 x i8] c"pmem : 0x%lx\0A\00"
@.str.8 = [28 x i8] c"pmem (ptr) : %p\0A\00"
@.str.10 = [32 x i8] c"reg_addr : 0x%08x\0A\00"
@.str.11 = [32 x i8] c"reg_data : 0x%08x\0A\00"
@str = [32 x i8] c"ERROR: failed to close /dev/mem\00"
@str.12 = [31 x i8] c"ERROR: failed to mmap /dev/mem\00"
@str.13 = [32 x i8] c"ERROR : failed to open /dev/mem\00"
define i32 @regio_rd32(i32 %0, i32* nocapture writeonly %1, i32 %2) {
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_SYNC
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.13, i64 0, i64 0))
br label %54
11: ; preds = %3
%12 = tail call i32 (...) @getpagesize()
%13 = icmp eq i32 %2, 0
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = udiv i32 1048576, %12
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 4)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 1136656384)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %12, i32 %12)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %15)
br label %20
20: ; preds = %14, %11
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %22, %21
%24 = load i32, i32* @MAP_SHARED
%25 = tail call i8* @mmap(i32 0, i64 1048576, i32 %23, i32 %24, i32 %7, i32 1136656384)
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
%28 = icmp eq i8* %25, null
%29 = or i1 %28, %27
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.12, i64 0, i64 0))
br label %54
32: ; preds = %20
br i1 %13, label %37, label %33
33: ; preds = %32
%34 = ptrtoint i8* %25 to i64
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i64 %34)
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %25)
br label %37
37: ; preds = %32, %33
%38 = tail call i64 @close(i32 %7)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %54
42: ; preds = %37
%43 = zext i32 %0 to i64
%44 = getelementptr inbounds i8, i8* %25, i64 %43
%45 = bitcast i8* %44 to i32*
%46 = load volatile i32, i32* %45
store i32 %46, i32* %1
br i1 %13, label %52, label %47
47: ; preds = %42
%48 = ptrtoint i8* %44 to i64
%49 = trunc i64 %48 to i32
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.10, i64 0, i64 0), i32 %49)
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.11, i64 0, i64 0), i32 %46)
br label %52
52: ; preds = %47, %42
%53 = tail call i32 @munmap(i8* %25, i64 1048576)
br label %54
54: ; preds = %52, %40, %30, %9
%55 = phi i32 [ -1, %9 ], [ -1, %30 ], [ -1, %40 ], [ 0, %52 ]
ret i32 %55
}