#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 uint8_t;
typedef int volatile uint64_t;
typedef int uint32_t;
typedef int uint16_t;
typedef int off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDONLY;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
unsigned int atol(char *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
unsigned int getpagesize();
scalar_t__ isdigit(char);
void *mmap(int /*<<< orphan*/ *, unsigned int, int, int /*<<< orphan*/, int,
           int);
int munmap(void *, unsigned int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strchrnul(char const *, int);
void *strtoull(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int volatile writeval;
int main(int argc, char **argv) {
  void *map_base, *virt_addr;
  uint64_t read_result;
  uint64_t writeval = writeval;
  off_t target;
  unsigned page_size, mapped_size, offset_in_page;
  int fd;
  unsigned width = 8 * sizeof(int);
  if (!argv[1])
    printf("failed! \n");
  errno = 0;
  target = strtoull(argv[1], NULL, 0);
  if (argv[2]) {
    if (isdigit(argv[2][0]) || argv[2][1])
      width = atol(argv[2]);
    else {
      static const char bhwl[] = "bhwl";
      static const uint8_t sizes[] = {8 * sizeof(char), 8 * sizeof(short),
                                      8 * sizeof(int), 8 * sizeof(long), 0};
      width =
          (char *)strchrnul(bhwl, ((int)argv[2][0] | 0x20)) - (char *)&bhwl[0];
      width = sizes[width];
    }
    if (argv[3])
      writeval = strtoull(argv[3], NULL, 0);
  } else {
    argv--;
  }
  if (errno)
    printf("errno\n");
  fd = open("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC));
  mapped_size = page_size = getpagesize();
  offset_in_page = (unsigned)target & (page_size - 1);
  if (offset_in_page + width > page_size) {
    mapped_size *= 2;
  }
  map_base =
      mmap(NULL, mapped_size, argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ,
           MAP_SHARED, fd, target & ~(off_t)(page_size - 1));
  if (map_base == MAP_FAILED) {
    printf("memmap failed\n");
    return 0;
  }
  virt_addr = (char *)map_base + offset_in_page;
  if (!argv[3]) {
    switch (width) {
    case 8:
      read_result = *(volatile uint8_t *)virt_addr;
      break;
    case 16:
      read_result = *(volatile uint16_t *)virt_addr;
      break;
    case 32:
      read_result = *(volatile uint32_t *)virt_addr;
      break;
    case 64:
      read_result = *(volatile uint64_t *)virt_addr;
      break;
    default:
      printf("bad width\n");
      return 0;
    }
    printf("0x%0*llX\n", (width >> 2), (unsigned long long)read_result);
  } else {
    switch (width) {
    case 8:
      *(volatile uint8_t *)virt_addr = writeval;
      break;
    case 16:
      *(volatile uint16_t *)virt_addr = writeval;
      break;
    case 32:
      *(volatile uint32_t *)virt_addr = writeval;
      break;
    case 64:
      *(volatile uint64_t *)virt_addr = writeval;
      break;
    default:
      printf("bad width\n");
      return 0;
    }
  }
  if (1) {
    if (munmap(map_base, mapped_size) == -1) {
      printf("munmap failed\n");
      return 0;
    }
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x20,-0x40(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11e0 <main+0x40>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2dd1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtoull@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     12ba <main+0x11a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
jne    1242 <main+0xa2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     1257 <main+0xb7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atol@plt>
mov    %eax,-0x40(%rbp)
jmp    1290 <main+0xf0>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%esi
or     $0x20,%esi
lea    0xde4(%rip),%rdi
call   1040 <strchrnul@plt>
lea    0xdd8(%rip),%rcx
sub    %rcx,%rax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,%ecx
lea    0xdd6(%rip),%rax
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     12b5 <main+0x115>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtoull@plt>
mov    %eax,-0x28(%rbp)
jmp    12c6 <main+0x126>
mov    -0x10(%rbp),%rax
add    $0xfffffffffffffff8,%rax
mov    %rax,-0x10(%rbp)
mov    0x2ceb(%rip),%rax
cmpq   $0x0,(%rax)
je     12e5 <main+0x145>
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     130e <main+0x16e>
mov    0x2cdd(%rip),%rax
mov    (%rax),%eax
mov    0x2ccc(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x44(%rbp)
jmp    1323 <main+0x183>
mov    0x2cb3(%rip),%rax
mov    (%rax),%eax
mov    0x2cb2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%esi
lea    0xce4(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   10c0 <getpagesize@plt>
mov    %eax,-0x30(%rbp)
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    -0x40(%rbp),%eax
cmp    -0x30(%rbp),%eax
jbe    1368 <main+0x1c8>
mov    -0x34(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1397 <main+0x1f7>
mov    0x2c74(%rip),%rax
mov    (%rax),%eax
mov    0x2c23(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x4c(%rbp)
jmp    13a3 <main+0x203>
mov    0x2c5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%esi
mov    -0x4c(%rbp),%edx
mov    0x2c38(%rip),%rax
mov    (%rax),%ecx
mov    -0x3c(%rbp),%r8d
mov    -0x2c(%rbp),%r9d
mov    -0x30(%rbp),%eax
sub    $0x1,%eax
xor    $0xffffffff,%eax
and    %eax,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1050 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2bca(%rip),%rcx
cmp    (%rcx),%rax
jne    1401 <main+0x261>
lea    0xc2c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    15b2 <main+0x412>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
jne    14d6 <main+0x336>
mov    -0x40(%rbp),%eax
mov    %eax,-0x50(%rbp)
sub    $0x8,%eax
je     1465 <main+0x2c5>
jmp    1432 <main+0x292>
mov    -0x50(%rbp),%eax
sub    $0x10,%eax
je     1473 <main+0x2d3>
jmp    1443 <main+0x2a3>
mov    -0x50(%rbp),%eax
sub    $0x20,%eax
je     1481 <main+0x2e1>
jmp    1454 <main+0x2b4>
mov    -0x50(%rbp),%eax
sub    $0x40,%eax
je     148f <main+0x2ef>
jmp    149d <main+0x2fd>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    14b7 <main+0x317>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    14b7 <main+0x317>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    14b7 <main+0x317>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    14b7 <main+0x317>
lea    0xb85(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    15b2 <main+0x412>
mov    -0x40(%rbp),%esi
shr    $0x2,%esi
mov    -0x24(%rbp),%eax
movslq %eax,%rdx
lea    0xb6a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1574 <main+0x3d4>
mov    -0x40(%rbp),%eax
mov    %eax,-0x54(%rbp)
sub    $0x8,%eax
je     151d <main+0x37d>
jmp    14ea <main+0x34a>
mov    -0x54(%rbp),%eax
sub    $0x10,%eax
je     152b <main+0x38b>
jmp    14fb <main+0x35b>
mov    -0x54(%rbp),%eax
sub    $0x20,%eax
je     1539 <main+0x399>
jmp    150c <main+0x36c>
mov    -0x54(%rbp),%eax
sub    $0x40,%eax
je     1547 <main+0x3a7>
jmp    1555 <main+0x3b5>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    156f <main+0x3cf>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    156f <main+0x3cf>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    156f <main+0x3cf>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    156f <main+0x3cf>
lea    0xacd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    15b2 <main+0x412>
jmp    1574 <main+0x3d4>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1090 <munmap@plt>
cmp    $0xffffffff,%eax
jne    15a3 <main+0x403>
lea    0xaae(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    15b2 <main+0x412>
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    (%rsp),%eax
mov    %eax,(%rsp)
mov    0x8(%rsi),%rdi
test   %rdi,%rdi
jne    11dd <main+0x2d>
lea    0xea3(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rbx),%rdi
mov    0x2dd4(%rip),%rbp
movq   $0x0,0x0(%rbp)
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtoull@plt>
mov    %rax,%r15
mov    0x10(%rbx),%rax
test   %rax,%rax
je     131a <main+0x16a>
movsbl (%rax),%edi
call   1030 <isdigit@plt>
mov    0x10(%rbx),%rdi
test   %rax,%rax
jne    1220 <main+0x70>
cmpb   $0x0,0x1(%rdi)
je     13b6 <main+0x206>
call   10b0 <atol@plt>
mov    %eax,%r14d
mov    0x18(%rbx),%rdi
test   %rdi,%rdi
je     123d <main+0x8d>
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtoull@plt>
mov    %eax,(%rsp)
cmpq   $0x0,0x0(%rbp)
je     1250 <main+0xa0>
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
cmpq   $0x0,0x18(%rbx)
jne    1260 <main+0xb0>
mov    0x2d6a(%rip),%rax
jmp    1267 <main+0xb7>
mov    0x2d71(%rip),%rax
mov    0x2d62(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdeb(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   10d0 <getpagesize@plt>
lea    -0x1(%rax),%ebp
and    %r15d,%ebp
lea    (%r14,%rbp,1),%ecx
cmp    %eax,%ecx
seta   %cl
mov    %eax,%r13d
shl    %cl,%r13d
cmpq   $0x0,0x18(%rbx)
mov    0x2d4f(%rip),%rcx
mov    $0x0,%edx
je     12b9 <main+0x109>
mov    0x2cf9(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d26(%rip),%rcx
mov    (%rcx),%ecx
neg    %eax
and    %r15d,%eax
xor    %edi,%edi
mov    %r13d,%esi
mov    %r12d,%r8d
mov    %eax,%r9d
call   1060 <mmap@plt>
mov    %rax,%r15
mov    0x2cc5(%rip),%rax
cmp    (%rax),%r15
je     1334 <main+0x184>
lea    -0x8(%r14),%eax
rol    $0x1d,%eax
cmpq   $0x0,0x18(%rbx)
je     133d <main+0x18d>
cmp    $0x7,%eax
ja     1399 <main+0x1e9>
mov    %eax,%eax
lea    0xcf8(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    (%rsp),%eax
mov    %eax,(%r15,%rbp,1)
jmp    1376 <main+0x1c6>
add    $0xfffffffffffffff8,%rbx
mov    $0x20,%r14d
cmpq   $0x0,0x0(%rbp)
jne    1244 <main+0x94>
jmp    1250 <main+0xa0>
lea    0xd5d(%rip),%rdi
jmp    13a0 <main+0x1f0>
cmp    $0x7,%eax
ja     1399 <main+0x1e9>
mov    %eax,%eax
lea    0xcd5(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    (%r15,%rbp,1),%eax
mov    %eax,0x4(%rsp)
shr    $0x2,%r14d
movslq 0x4(%rsp),%rdx
lea    0xd01(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
mov    %r13d,%esi
call   10a0 <munmap@plt>
cmp    $0xffffffff,%eax
je     1390 <main+0x1e0>
mov    %r12d,%edi
call   1080 <close@plt>
jmp    13a5 <main+0x1f5>
lea    0xce9(%rip),%rdi
jmp    13a0 <main+0x1f0>
lea    0xcee(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movsbl (%rdi),%esi
or     $0x20,%esi
lea    0xc7d(%rip),%r14
mov    %r14,%rdi
call   1050 <strchrnul@plt>
sub    %r14d,%eax
lea    0xc7b(%rip),%rcx
mov    (%rcx,%rax,4),%r14d
jmp    1228 <main+0x78>

LLVM IR (-O0):
@.str = [10 x i8] c"failed! \0A\00"
@errno = global i64 0
@main.bhwl = internal constant [5 x i8] c"bhwl\00"
@main.sizes = internal constant [5 x i32] [i32 8, i32 16, i32 32, i32 64, i32 0]
@.str.1 = [7 x i8] c"errno\0A\00"
@.str.2 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [15 x i8] c"memmap failed\0A\00"
@.str.4 = [11 x i8] c"bad width\0A\00"
@.str.5 = [10 x i8] c"0x%0*llX\0A\00"
@.str.6 = [15 x i8] c"munmap failed\0A\00"
@writeval = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load volatile i32, i32* %9
store volatile i32 %16, i32* %9
store i32 32, i32* %15
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %2
store i64 0, i64* @errno
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i8* @strtoull(i8* %26, i32* null, i32 0)
%28 = ptrtoint i8* %27 to i32
store i32 %28, i32* %10
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %83
33: ; preds = %23
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = call i64 @isdigit(i8 signext %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %49, label %41
41: ; preds = %33
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %41, %33
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 2
%52 = load i8*, i8** %51
%53 = call i32 @atol(i8* %52)
store i32 %53, i32* %15
br label %71
54: ; preds = %41
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = getelementptr inbounds i8, i8* %57, i64 0
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = or i32 %60, 32
%62 = call i64 @strchrnul(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @main.bhwl, i64 0, i64 0), i32 %61)
%63 = inttoptr i64 %62 to i8*
%64 = ptrtoint i8* %63 to i64
%65 = sub i64 %64, ptrtoint ([5 x i8]* @main.bhwl to i64)
%66 = trunc i64 %65 to i32
store i32 %66, i32* %15
%67 = load i32, i32* %15
%68 = zext i32 %67 to i64
%69 = getelementptr inbounds [5 x i32], [5 x i32]* @main.sizes, i64 0, i64 %68
%70 = load i32, i32* %69
store i32 %70, i32* %15
br label %71
71: ; preds = %54, %49
%72 = load i8**, i8*** %5
%73 = getelementptr inbounds i8*, i8** %72, i64 3
%74 = load i8*, i8** %73
%75 = icmp ne i8* %74, null
br i1 %75, label %76, label %82
76: ; preds = %71
%77 = load i8**, i8*** %5
%78 = getelementptr inbounds i8*, i8** %77, i64 3
%79 = load i8*, i8** %78
%80 = call i8* @strtoull(i8* %79, i32* null, i32 0)
%81 = ptrtoint i8* %80 to i32
store volatile i32 %81, i32* %9
br label %82
82: ; preds = %76, %71
br label %86
83: ; preds = %23
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i32 -1
store i8** %85, i8*** %5
br label %86
86: ; preds = %83, %82
%87 = load i64, i64* @errno
%88 = icmp ne i64 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %86
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %91
91: ; preds = %89, %86
%92 = load i8**, i8*** %5
%93 = getelementptr inbounds i8*, i8** %92, i64 3
%94 = load i8*, i8** %93
%95 = icmp ne i8* %94, null
br i1 %95, label %96, label %100
96: ; preds = %91
%97 = load i32, i32* @O_RDWR
%98 = load i32, i32* @O_SYNC
%99 = or i32 %97, %98
br label %104
100: ; preds = %91
%101 = load i32, i32* @O_RDONLY
%102 = load i32, i32* @O_SYNC
%103 = or i32 %101, %102
br label %104
104: ; preds = %100, %96
%105 = phi i32 [ %99, %96 ], [ %103, %100 ]
%106 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %105)
store i32 %106, i32* %14
%107 = call i32 (...) @getpagesize()
store i32 %107, i32* %11
store i32 %107, i32* %12
%108 = load i32, i32* %10
%109 = load i32, i32* %11
%110 = sub i32 %109, 1
%111 = and i32 %108, %110
store i32 %111, i32* %13
%112 = load i32, i32* %13
%113 = load i32, i32* %15
%114 = add i32 %112, %113
%115 = load i32, i32* %11
%116 = icmp ugt i32 %114, %115
br i1 %116, label %117, label %120
117: ; preds = %104
%118 = load i32, i32* %12
%119 = mul i32 %118, 2
store i32 %119, i32* %12
br label %120
120: ; preds = %117, %104
%121 = load i32, i32* %12
%122 = load i8**, i8*** %5
%123 = getelementptr inbounds i8*, i8** %122, i64 3
%124 = load i8*, i8** %123
%125 = icmp ne i8* %124, null
br i1 %125, label %126, label %130
126: ; preds = %120
%127 = load i32, i32* @PROT_READ
%128 = load i32, i32* @PROT_WRITE
%129 = or i32 %127, %128
br label %132
130: ; preds = %120
%131 = load i32, i32* @PROT_READ
br label %132
132: ; preds = %130, %126
%133 = phi i32 [ %129, %126 ], [ %131, %130 ]
%134 = load i32, i32* @MAP_SHARED
%135 = load i32, i32* %14
%136 = load i32, i32* %10
%137 = load i32, i32* %11
%138 = sub i32 %137, 1
%139 = xor i32 %138, -1
%140 = and i32 %136, %139
%141 = call i8* @mmap(i32* null, i32 %121, i32 %133, i32 %134, i32 %135, i32 %140)
store i8* %141, i8** %6
%142 = load i8*, i8** %6
%143 = load i8*, i8** @MAP_FAILED
%144 = icmp eq i8* %142, %143
br i1 %144, label %145, label %147
145: ; preds = %132
%146 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %213
147: ; preds = %132
%148 = load i8*, i8** %6
%149 = load i32, i32* %13
%150 = zext i32 %149 to i64
%151 = getelementptr inbounds i8, i8* %148, i64 %150
store i8* %151, i8** %7
%152 = load i8**, i8*** %5
%153 = getelementptr inbounds i8*, i8** %152, i64 3
%154 = load i8*, i8** %153
%155 = icmp ne i8* %154, null
br i1 %155, label %182, label %156
156: ; preds = %147
%157 = load i32, i32* %15
switch i32 %157, label %174 [
i32 8, label %158
i32 16, label %162
i32 32, label %166
i32 64, label %170
]
158: ; preds = %156
%159 = load i8*, i8** %7
%160 = bitcast i8* %159 to i32*
%161 = load volatile i32, i32* %160
store volatile i32 %161, i32* %8
br label %176
162: ; preds = %156
%163 = load i8*, i8** %7
%164 = bitcast i8* %163 to i32*
%165 = load volatile i32, i32* %164
store volatile i32 %165, i32* %8
br label %176
166: ; preds = %156
%167 = load i8*, i8** %7
%168 = bitcast i8* %167 to i32*
%169 = load volatile i32, i32* %168
store volatile i32 %169, i32* %8
br label %176
170: ; preds = %156
%171 = load i8*, i8** %7
%172 = bitcast i8* %171 to i32*
%173 = load volatile i32, i32* %172
store volatile i32 %173, i32* %8
br label %176
174: ; preds = %156
%175 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %213
176: ; preds = %170, %166, %162, %158
%177 = load i32, i32* %15
%178 = lshr i32 %177, 2
%179 = load volatile i32, i32* %8
%180 = sext i32 %179 to i64
%181 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %178, i64 %180)
br label %203
182: ; preds = %147
%183 = load i32, i32* %15
switch i32 %183, label %200 [
i32 8, label %184
i32 16, label %188
i32 32, label %192
i32 64, label %196
]
184: ; preds = %182
%185 = load volatile i32, i32* %9
%186 = load i8*, i8** %7
%187 = bitcast i8* %186 to i32*
store volatile i32 %185, i32* %187
br label %202
188: ; preds = %182
%189 = load volatile i32, i32* %9
%190 = load i8*, i8** %7
%191 = bitcast i8* %190 to i32*
store volatile i32 %189, i32* %191
br label %202
192: ; preds = %182
%193 = load volatile i32, i32* %9
%194 = load i8*, i8** %7
%195 = bitcast i8* %194 to i32*
store volatile i32 %193, i32* %195
br label %202
196: ; preds = %182
%197 = load volatile i32, i32* %9
%198 = load i8*, i8** %7
%199 = bitcast i8* %198 to i32*
store volatile i32 %197, i32* %199
br label %202
200: ; preds = %182
%201 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %213
202: ; preds = %196, %192, %188, %184
br label %203
203: ; preds = %202, %176
%204 = load i8*, i8** %6
%205 = load i32, i32* %12
%206 = call i32 @munmap(i8* %204, i32 %205)
%207 = icmp eq i32 %206, -1
br i1 %207, label %208, label %210
208: ; preds = %203
%209 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %3
br label %213
210: ; preds = %203
%211 = load i32, i32* %14
%212 = call i32 @close(i32 %211)
store i32 0, i32* %3
br label %213
213: ; preds = %210, %208, %200, %174, %145
%214 = load i32, i32* %3
ret i32 %214
}

LLVM IR (-O2):
@errno = global i64 0
@main.bhwl = internal constant [5 x i8] c"bhwl\00"
@main.sizes = internal unnamed_addr constant [5 x i32] [i32 8, i32 16, i32 32, i32 64, i32 0]
@.str.2 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [10 x i8] c"0x%0*llX\0A\00"
@writeval = global i32 0
@str = [9 x i8] c"failed! \00"
@str.7 = [14 x i8] c"munmap failed\00"
@str.9 = [10 x i8] c"bad width\00"
@str.10 = [14 x i8] c"memmap failed\00"
@str.11 = [6 x i8] c"errno\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load volatile i32, i32* %4
store volatile i32 %7, i32* %4
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%13 = load i8*, i8** %8
br label %14
14: ; preds = %11, %2
%15 = phi i8* [ %13, %11 ], [ %9, %2 ]
store i64 0, i64* @errno
%16 = tail call i8* @strtoull(i8* nocapture %15, i32* null, i32 0)
%17 = ptrtoint i8* %16 to i64
%18 = trunc i64 %17 to i32
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = icmp eq i8* %20, null
br i1 %21, label %51, label %22
22: ; preds = %14
%23 = load i8, i8* %20
%24 = tail call i64 @isdigit(i8 signext %23)
%25 = icmp eq i64 %24, 0
%26 = load i8*, i8** %19
br i1 %25, label %27, label %31
27: ; preds = %22
%28 = getelementptr inbounds i8, i8* %26, i64 1
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %22, %27
%32 = tail call i32 @atol(i8* %26)
br label %42
33: ; preds = %27
%34 = load i8, i8* %26
%35 = or i8 %34, 32
%36 = sext i8 %35 to i32
%37 = tail call i64 @strchrnul(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @main.bhwl, i64 0, i64 0), i32 %36)
%38 = sub i64 %37, zext (i32 ptrtoint ([5 x i8]* @main.bhwl to i32) to i64)
%39 = and i64 %38, 4294967295
%40 = getelementptr inbounds [5 x i32], [5 x i32]* @main.sizes, i64 0, i64 %39
%41 = load i32, i32* %40
br label %42
42: ; preds = %33, %31
%43 = phi i32 [ %32, %31 ], [ %41, %33 ]
%44 = getelementptr inbounds i8*, i8** %1, i64 3
%45 = load i8*, i8** %44
%46 = icmp eq i8* %45, null
br i1 %46, label %53, label %47
47: ; preds = %42
%48 = tail call i8* @strtoull(i8* nocapture %45, i32* null, i32 0)
%49 = ptrtoint i8* %48 to i64
%50 = trunc i64 %49 to i32
store volatile i32 %50, i32* %4
br label %53
51: ; preds = %14
%52 = getelementptr inbounds i8*, i8** %1, i64 -1
br label %53
53: ; preds = %42, %47, %51
%54 = phi i8** [ %1, %47 ], [ %1, %42 ], [ %52, %51 ]
%55 = phi i32 [ %43, %47 ], [ %43, %42 ], [ 32, %51 ]
%56 = load i64, i64* @errno
%57 = icmp eq i64 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %53
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.11, i64 0, i64 0))
br label %60
60: ; preds = %58, %53
%61 = getelementptr inbounds i8*, i8** %54, i64 3
%62 = load i8*, i8** %61
%63 = icmp eq i8* %62, null
%64 = load i32, i32* @O_RDONLY
%65 = load i32, i32* @O_RDWR
%66 = select i1 %63, i32 %64, i32 %65
%67 = load i32, i32* @O_SYNC
%68 = or i32 %67, %66
%69 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %68)
%70 = tail call i32 (...) @getpagesize()
%71 = add i32 %70, -1
%72 = and i32 %71, %18
%73 = add i32 %72, %55
%74 = icmp ugt i32 %73, %70
%75 = zext i1 %74 to i32
%76 = shl i32 %70, %75
%77 = load i8*, i8** %61
%78 = icmp eq i8* %77, null
%79 = load i32, i32* @PROT_READ
%80 = load i32, i32* @PROT_WRITE
%81 = select i1 %78, i32 0, i32 %80
%82 = or i32 %81, %79
%83 = load i32, i32* @MAP_SHARED
%84 = sub i32 0, %70
%85 = and i32 %84, %18
%86 = tail call i8* @mmap(i32* null, i32 %76, i32 %82, i32 %83, i32 %69, i32 %85)
%87 = load i8*, i8** @MAP_FAILED
%88 = icmp eq i8* %86, %87
br i1 %88, label %89, label %91
89: ; preds = %60
%90 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.10, i64 0, i64 0))
br label %140
91: ; preds = %60
%92 = zext i32 %72 to i64
%93 = getelementptr inbounds i8, i8* %86, i64 %92
%94 = load i8*, i8** %61
%95 = icmp eq i8* %94, null
%96 = add i32 %55, -8
%97 = tail call i32 @llvm.fshl.i32(i32 %96, i32 %96, i32 29)
br i1 %95, label %98, label %118
98: ; preds = %91
switch i32 %97, label %111 [
i32 0, label %99
i32 1, label %102
i32 3, label %105
i32 7, label %108
]
99: ; preds = %98
%100 = bitcast i8* %93 to i32*
%101 = load volatile i32, i32* %100
store volatile i32 %101, i32* %3
br label %113
102: ; preds = %98
%103 = bitcast i8* %93 to i32*
%104 = load volatile i32, i32* %103
store volatile i32 %104, i32* %3
br label %113
105: ; preds = %98
%106 = bitcast i8* %93 to i32*
%107 = load volatile i32, i32* %106
store volatile i32 %107, i32* %3
br label %113
108: ; preds = %98
%109 = bitcast i8* %93 to i32*
%110 = load volatile i32, i32* %109
store volatile i32 %110, i32* %3
br label %113
111: ; preds = %98
%112 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.9, i64 0, i64 0))
br label %140
113: ; preds = %108, %105, %102, %99
%114 = lshr i32 %55, 2
%115 = load volatile i32, i32* %3
%116 = sext i32 %115 to i64
%117 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %114, i64 %116)
br label %133
118: ; preds = %91
switch i32 %97, label %131 [
i32 0, label %119
i32 1, label %122
i32 3, label %125
i32 7, label %128
]
119: ; preds = %118
%120 = load volatile i32, i32* %4
%121 = bitcast i8* %93 to i32*
store volatile i32 %120, i32* %121
br label %133
122: ; preds = %118
%123 = load volatile i32, i32* %4
%124 = bitcast i8* %93 to i32*
store volatile i32 %123, i32* %124
br label %133
125: ; preds = %118
%126 = load volatile i32, i32* %4
%127 = bitcast i8* %93 to i32*
store volatile i32 %126, i32* %127
br label %133
128: ; preds = %118
%129 = load volatile i32, i32* %4
%130 = bitcast i8* %93 to i32*
store volatile i32 %129, i32* %130
br label %133
131: ; preds = %118
%132 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.9, i64 0, i64 0))
br label %140
133: ; preds = %119, %122, %125, %128, %113
%134 = tail call i32 @munmap(i8* %86, i32 %76)
%135 = icmp eq i32 %134, -1
br i1 %135, label %136, label %138
136: ; preds = %133
%137 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
br label %140
138: ; preds = %133
%139 = tail call i32 @close(i32 %69)
br label %140
140: ; preds = %138, %136, %131, %111, %89
ret i32 0
}