#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
unsigned long rareg(int, int, unsigned long);
void *strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usage(char *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ wait_pciephy_busy();
int main(int argc, char *argv[]) {
  char rwmode = 'r';
  unsigned long addr, value = 0;
  unsigned long reg_value = 0x0;
  long addr_end = -1;
  if (argc < 3 || argc > 4) {
    usage(argv[0]);
    exit(0);
  }
  rwmode = argv[1][0];
  if (rwmode == 'r') {
    if (argc != 3) {
      usage(argv[0]);
      exit(0);
    }
  } else {
    if (argc != 4) {
      usage(argv[0]);
      exit(0);
    }
  }
  addr = strtol(argv[2], NULL, 16);
  if (rwmode == 'w')
    value = strtol(argv[3], NULL, 16);
  if (rwmode == 'd') {
    addr_end = strtol(argv[3], NULL, 16);
    if (addr > addr_end) {
      printf("Error: wrong range.\n");
      exit(0);
    }
  }
  if (addr > 255 || addr_end > 255 || value > 255) {
    printf("Error: too large: Addr=0x%x, value=0x%x\n", addr, value);
    exit(0);
  }
  do {
    wait_pciephy_busy();
    reg_value = 0x0;
    if (rwmode == 'w') {
      reg_value |= (1UL << 23);
      reg_value |= (value) << 0;
    }
    reg_value |= (addr) << 8;
    rareg(0x1, 0x10140090, reg_value);
    usleep(1000);
    wait_pciephy_busy();
    if (rwmode == 'r' || rwmode == 'd') {
      reg_value = rareg(0x0, 0x10140090, 0);
      printf("[%02x]=0x%02x\n", addr, reg_value & 0xff);
    }
    addr++;
  } while (addr_end >= 0 && addr <= addr_end);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x72,-0x11(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0xffffffffffffffff,-0x38(%rbp)
ff
cmpl   $0x3,-0x8(%rbp)
jl     11b6 <main+0x46>
cmpl   $0x4,-0x8(%rbp)
jle    11c9 <main+0x59>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <usage@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x72,%eax
jne    1205 <main+0x95>
cmpl   $0x3,-0x8(%rbp)
je     1200 <main+0x90>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <usage@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1227 <main+0xb7>
cmpl   $0x4,-0x8(%rbp)
je     1222 <main+0xb2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <usage@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1227 <main+0xb7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,-0x20(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x77,%eax
jne    1268 <main+0xf8>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,-0x28(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x64,%eax
jne    12b7 <main+0x147>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x38(%rbp),%rax
jbe    12b2 <main+0x142>
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    12b7 <main+0x147>
cmpq   $0xff,-0x20(%rbp)
ja     12e1 <main+0x171>
cmpq   $0xff,-0x38(%rbp)
jg     12e1 <main+0x171>
cmpq   $0xff,-0x28(%rbp)
jbe    12fe <main+0x18e>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1303 <main+0x193>
mov    $0x0,%al
call   1040 <wait_pciephy_busy@plt>
movq   $0x0,-0x30(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x77,%eax
jne    133d <main+0x1cd>
mov    -0x30(%rbp),%rax
or     $0x800000,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
or     -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x8,%rax
or     -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdx
mov    $0x1,%edi
mov    $0x10140090,%esi
call   1090 <rareg@plt>
mov    $0x3e8,%edi
call   1080 <usleep@plt>
mov    $0x0,%al
call   1040 <wait_pciephy_busy@plt>
movsbl -0x11(%rbp),%eax
cmp    $0x72,%eax
je     138b <main+0x21b>
movsbl -0x11(%rbp),%eax
cmp    $0x64,%eax
jne    13bc <main+0x24c>
xor    %edi,%edi
mov    $0x10140090,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <rareg@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
and    $0xff,%rdx
lea    0xc89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x38(%rbp)
mov    %al,-0x39(%rbp)
jl     13e6 <main+0x276>
mov    -0x20(%rbp),%rax
cmp    -0x38(%rbp),%rax
setbe  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1303 <main+0x193>
mov    -0x4(%rbp),%eax
add    $0x40,%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,%r12
lea    -0x5(%rdi),%eax
cmp    $0xfffffffd,%eax
jbe    130d <main+0x18d>
mov    0x8(%r12),%rax
mov    (%rax),%r13b
cmp    $0x72,%r13b
jne    11d1 <main+0x51>
cmp    $0x3,%edi
jne    130d <main+0x18d>
mov    0x10(%r12),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %rax,%rbp
mov    $0xffffffffffffffff,%r15
jmp    123c <main+0xbc>
cmp    $0x4,%edi
jne    130d <main+0x18d>
mov    0x10(%r12),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %rax,%rbp
cmp    $0x64,%r13b
je     121c <main+0x9c>
mov    $0xffffffffffffffff,%r15
cmp    $0x77,%r13b
jne    123c <main+0xbc>
mov    0x18(%r12),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %rax,%r14
mov    $0x1,%al
jmp    123e <main+0xbe>
mov    0x18(%r12),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
cmp    %rax,%rbp
ja     1338 <main+0x1b8>
mov    %rax,%r15
xor    %eax,%eax
cmp    $0xff,%rbp
ja     131d <main+0x19d>
cmp    $0xff,%r15
jg     131d <main+0x19d>
cmp    $0xff,%r14
ja     131d <main+0x19d>
or     $0x800000,%r14
xor    %ecx,%ecx
test   %al,%al
cmove  %rcx,%r14
mov    %rbp,%rbx
shl    $0x8,%rbx
lea    0xda7(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <wait_pciephy_busy@plt>
mov    %rbx,%rdx
or     %r14,%rdx
mov    $0x1,%edi
mov    $0x10140090,%esi
call   10a0 <rareg@plt>
mov    $0x3e8,%edi
call   1090 <usleep@plt>
xor    %eax,%eax
call   1050 <wait_pciephy_busy@plt>
cmp    $0x72,%r13b
je     12c9 <main+0x149>
cmp    $0x64,%r13b
jne    12e7 <main+0x167>
xor    %edi,%edi
mov    $0x10140090,%esi
xor    %edx,%edx
call   10a0 <rareg@plt>
movzbl %al,%edx
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r15,%r15
js     12fc <main+0x17c>
add    $0x1,%rbp
add    $0x100,%rbx
cmp    %r15,%rbp
jbe    1290 <main+0x110>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%rdi
call   1070 <usage@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xcdc(%rip),%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xcf9(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"Error: wrong range.\0A\00"
@.str.1 = [41 x i8] c"Error: too large: Addr=0x%x, value=0x%x\0A\00"
@.str.2 = [15 x i8] c"[%02x]=0x%02x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 114, i8* %6
store i64 0, i64* %8
store i64 0, i64* %9
store i64 -1, i64* %10
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 4
br i1 %15, label %16, label %22
16: ; preds = %13, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @usage(i8* %19)
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %13
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
store i8 %27, i8* %6
%28 = load i8, i8* %6
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 114
br i1 %30, label %31, label %41
31: ; preds = %22
%32 = load i32, i32* %4
%33 = icmp ne i32 %32, 3
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = call i32 @usage(i8* %37)
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %31
br label %51
41: ; preds = %22
%42 = load i32, i32* %4
%43 = icmp ne i32 %42, 4
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 0
%47 = load i8*, i8** %46
%48 = call i32 @usage(i8* %47)
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %41
br label %51
51: ; preds = %50, %40
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 2
%54 = load i8*, i8** %53
%55 = call i8* @strtol(i8* %54, i32* null, i32 16)
%56 = ptrtoint i8* %55 to i64
store i64 %56, i64* %7
%57 = load i8, i8* %6
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 119
br i1 %59, label %60, label %66
60: ; preds = %51
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 3
%63 = load i8*, i8** %62
%64 = call i8* @strtol(i8* %63, i32* null, i32 16)
%65 = ptrtoint i8* %64 to i64
store i64 %65, i64* %8
br label %66
66: ; preds = %60, %51
%67 = load i8, i8* %6
%68 = sext i8 %67 to i32
%69 = icmp eq i32 %68, 100
br i1 %69, label %70, label %83
70: ; preds = %66
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i64 3
%73 = load i8*, i8** %72
%74 = call i8* @strtol(i8* %73, i32* null, i32 16)
%75 = ptrtoint i8* %74 to i64
store i64 %75, i64* %10
%76 = load i64, i64* %7
%77 = load i64, i64* %10
%78 = icmp ugt i64 %76, %77
br i1 %78, label %79, label %82
79: ; preds = %70
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%81 = call i32 @exit(i32 0)
unreachable
82: ; preds = %70
br label %83
83: ; preds = %82, %66
%84 = load i64, i64* %7
%85 = icmp ugt i64 %84, 255
br i1 %85, label %92, label %86
86: ; preds = %83
%87 = load i64, i64* %10
%88 = icmp sgt i64 %87, 255
br i1 %88, label %92, label %89
89: ; preds = %86
%90 = load i64, i64* %8
%91 = icmp ugt i64 %90, 255
br i1 %91, label %92, label %97
92: ; preds = %89, %86, %83
%93 = load i64, i64* %7
%94 = load i64, i64* %8
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i64 %93, i64 %94)
%96 = call i32 @exit(i32 0)
unreachable
97: ; preds = %89
br label %98
98: ; preds = %142, %97
%99 = call i32 (...) @wait_pciephy_busy()
store i64 0, i64* %9
%100 = load i8, i8* %6
%101 = sext i8 %100 to i32
%102 = icmp eq i32 %101, 119
br i1 %102, label %103, label %110
103: ; preds = %98
%104 = load i64, i64* %9
%105 = or i64 %104, 8388608
store i64 %105, i64* %9
%106 = load i64, i64* %8
%107 = shl i64 %106, 0
%108 = load i64, i64* %9
%109 = or i64 %108, %107
store i64 %109, i64* %9
br label %110
110: ; preds = %103, %98
%111 = load i64, i64* %7
%112 = shl i64 %111, 8
%113 = load i64, i64* %9
%114 = or i64 %113, %112
store i64 %114, i64* %9
%115 = load i64, i64* %9
%116 = call i64 @rareg(i32 1, i32 269746320, i64 %115)
%117 = call i32 @usleep(i32 1000)
%118 = call i32 (...) @wait_pciephy_busy()
%119 = load i8, i8* %6
%120 = sext i8 %119 to i32
%121 = icmp eq i32 %120, 114
br i1 %121, label %126, label %122
122: ; preds = %110
%123 = load i8, i8* %6
%124 = sext i8 %123 to i32
%125 = icmp eq i32 %124, 100
br i1 %125, label %126, label %132
126: ; preds = %122, %110
%127 = call i64 @rareg(i32 0, i32 269746320, i64 0)
store i64 %127, i64* %9
%128 = load i64, i64* %7
%129 = load i64, i64* %9
%130 = and i64 %129, 255
%131 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i64 %128, i64 %130)
br label %132
132: ; preds = %126, %122
%133 = load i64, i64* %7
%134 = add i64 %133, 1
store i64 %134, i64* %7
br label %135
135: ; preds = %132
%136 = load i64, i64* %10
%137 = icmp sge i64 %136, 0
br i1 %137, label %138, label %142
138: ; preds = %135
%139 = load i64, i64* %7
%140 = load i64, i64* %10
%141 = icmp ule i64 %139, %140
br label %142
142: ; preds = %138, %135
%143 = phi i1 [ false, %135 ], [ %141, %138 ]
br i1 %143, label %98, label %144
144: ; preds = %142
%145 = load i32, i32* %3
ret i32 %145
}

LLVM IR (-O2):
@.str.1 = [41 x i8] c"Error: too large: Addr=0x%x, value=0x%x\0A\00"
@.str.2 = [15 x i8] c"[%02x]=0x%02x\0A\00"
@str = [20 x i8] c"Error: wrong range.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -5
%4 = icmp ult i32 %3, -2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @usage(i8* %6)
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 114
br i1 %13, label %14, label %24
14: ; preds = %9
%15 = icmp eq i32 %0, 3
br i1 %15, label %16, label %20
16: ; preds = %14
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i8* @strtol(i8* nocapture %18, i32* null, i32 16)
br label %48
20: ; preds = %14
%21 = load i8*, i8** %1
%22 = tail call i32 @usage(i8* %21)
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %9
%25 = icmp eq i32 %0, 4
br i1 %25, label %30, label %26
26: ; preds = %24
%27 = load i8*, i8** %1
%28 = tail call i32 @usage(i8* %27)
%29 = tail call i32 @exit(i32 0)
unreachable
30: ; preds = %24
%31 = getelementptr inbounds i8*, i8** %1, i64 2
%32 = load i8*, i8** %31
%33 = tail call i8* @strtol(i8* nocapture %32, i32* null, i32 16)
switch i8 %12, label %48 [
i8 119, label %34
i8 100, label %39
]
34: ; preds = %30
%35 = getelementptr inbounds i8*, i8** %1, i64 3
%36 = load i8*, i8** %35
%37 = tail call i8* @strtol(i8* nocapture %36, i32* null, i32 16)
%38 = ptrtoint i8* %37 to i64
br label %48
39: ; preds = %30
%40 = getelementptr inbounds i8*, i8** %1, i64 3
%41 = load i8*, i8** %40
%42 = tail call i8* @strtol(i8* nocapture %41, i32* null, i32 16)
%43 = ptrtoint i8* %42 to i64
%44 = icmp ugt i8* %33, %42
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%47 = tail call i32 @exit(i32 0)
unreachable
48: ; preds = %30, %34, %16, %39
%49 = phi i64 [ 0, %39 ], [ 0, %16 ], [ %38, %34 ], [ 0, %30 ]
%50 = phi i8* [ %33, %39 ], [ %19, %16 ], [ %33, %34 ], [ %33, %30 ]
%51 = phi i1 [ false, %39 ], [ false, %16 ], [ true, %34 ], [ false, %30 ]
%52 = phi i64 [ %43, %39 ], [ -1, %16 ], [ -1, %34 ], [ -1, %30 ]
%53 = ptrtoint i8* %50 to i64
%54 = icmp ugt i8* %50, inttoptr (i64 255 to i8*)
%55 = icmp sgt i64 %52, 255
%56 = select i1 %54, i1 true, i1 %55
%57 = icmp ugt i64 %49, 255
%58 = select i1 %56, i1 true, i1 %57
br i1 %58, label %63, label %59
59: ; preds = %48
%60 = or i64 %49, 8388608
%61 = select i1 %51, i64 %60, i64 0
%62 = icmp sgt i64 %52, -1
br label %66
63: ; preds = %48
%64 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i64 %53, i64 %49)
%65 = tail call i32 @exit(i32 0)
unreachable
66: ; preds = %59, %78
%67 = phi i64 [ %79, %78 ], [ %53, %59 ]
%68 = tail call i32 (...) @wait_pciephy_busy()
%69 = shl i64 %67, 8
%70 = or i64 %69, %61
%71 = tail call i64 @rareg(i32 1, i32 269746320, i64 %70)
%72 = tail call i32 @usleep(i32 1000)
%73 = tail call i32 (...) @wait_pciephy_busy()
switch i8 %12, label %78 [
i8 114, label %74
i8 100, label %74
]
74: ; preds = %66, %66
%75 = tail call i64 @rareg(i32 0, i32 269746320, i64 0)
%76 = and i64 %75, 255
%77 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i64 %67, i64 %76)
br label %78
78: ; preds = %66, %74
%79 = add i64 %67, 1
%80 = icmp ule i64 %79, %52
%81 = select i1 %62, i1 %80, i1 false
br i1 %81, label %66, label %82
82: ; preds = %78
ret i32 0
}