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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int getopt(int, char **, char *);
int /*<<< orphan*/ intel_get_pci_device();
int /*<<< orphan*/ intel_register_access_fini();
int /*<<< orphan*/ intel_register_access_init(int /*<<< orphan*/,
                                              int /*<<< orphan*/);
scalar_t__ iopl(int);
scalar_t__ optind;
int /*<<< orphan*/ printf(char *, int, int);
int read_reg(int, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ usage(char const *);
int main(int argc, char *argv[]) {
  bool use_mmio = false;
  int ret = 0;
  uint32_t reg;
  int i, ch;
  const char *cmdname = argv[0];
  while ((ch = getopt(argc, argv, "m")) != -1) {
    switch (ch) {
    case 'm':
      use_mmio = true;
      break;
    default:
      break;
    }
  }
  argc -= optind;
  argv += optind;
  if (argc < 1) {
    usage(cmdname);
    return 1;
  }
  if (use_mmio)
    intel_register_access_init(intel_get_pci_device(), 0);
  else
    assert(iopl(3) == 0);
  for (i = 0; i < argc; i++) {
    sscanf(argv[i], "0x%x", &reg);
    printf("0x%X : 0x%X\n", reg, read_reg(reg, use_mmio));
  }
  if (use_mmio)
    intel_register_access_fini();
  else
    iopl(0);
  return ret;
}


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)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe23(%rip),%rdx
call   10b0 <getopt@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1215 <main+0x75>
mov    -0x24(%rbp),%eax
sub    $0x6d,%eax
jne    120b <main+0x6b>
jmp    11ff <main+0x5f>
movl   $0x1,-0x14(%rbp)
jmp    1210 <main+0x70>
jmp    1210 <main+0x70>
jmp    11cf <main+0x2f>
mov    0x2dcc(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jge    125e <main+0xbe>
mov    -0x30(%rbp),%rdi
call   1090 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <main+0x17a>
cmpl   $0x0,-0x14(%rbp)
je     127d <main+0xdd>
mov    $0x0,%al
call   10c0 <intel_get_pci_device@plt>
mov    %eax,%edi
xor    %esi,%esi
call   10a0 <intel_register_access_init@plt>
jmp    1298 <main+0xf8>
mov    $0x3,%edi
call   1040 <iopl@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12f7 <main+0x157>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xd44(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1060 <read_reg@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
lea    0xd23(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    129f <main+0xff>
cmpl   $0x0,-0x14(%rbp)
je     130d <main+0x16d>
mov    $0x0,%al
call   1050 <intel_register_access_fini@plt>
jmp    1314 <main+0x174>
xor    %edi,%edi
call   1040 <iopl@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
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
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%r15
xor    %r14d,%r14d
mov    $0x1,%r13b
lea    0xe3d(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10b0 <getopt@plt>
cmp    $0x6d,%eax
je     11e9 <main+0x49>
cmp    $0xffffffff,%eax
jne    11d0 <main+0x30>
jmp    11f4 <main+0x54>
mov    $0x1,%r14d
xor    %r13d,%r13d
jmp    11d0 <main+0x30>
mov    0x2ded(%rip),%rax
mov    (%rax),%r12
sub    %r12d,%ebp
test   %ebp,%ebp
jle    122f <main+0x8f>
test   $0x1,%r13b
mov    %r14d,0x10(%rsp)
mov    %r13d,0xc(%rsp)
je     1241 <main+0xa1>
mov    $0x3,%edi
call   1040 <iopl@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
jmp    1251 <main+0xb1>
mov    %r15,%rdi
call   1090 <usage@plt>
mov    $0x1,%ebx
jmp    12c4 <main+0x124>
xor    %eax,%eax
call   10c0 <intel_get_pci_device@plt>
mov    %eax,%edi
xor    %esi,%esi
call   10a0 <intel_register_access_init@plt>
lea    (%rbx,%r12,8),%r13
mov    %ebp,%r14d
xor    %r15d,%r15d
lea    0xda5(%rip),%rbx
mov    0x10(%rsp),%r12d
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13,%r15,8),%rdi
lea    0xd86(%rip),%rsi
lea    0x14(%rsp),%rdx
call   1080 <sscanf@plt>
mov    0x14(%rsp),%ebp
mov    %ebp,%edi
mov    %r12d,%esi
call   1060 <read_reg@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1030 <printf@plt>
add    $0x1,%r15
cmp    %r15,%r14
jne    1270 <main+0xd0>
testb  $0x1,0xc(%rsp)
je     12bb <main+0x11b>
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <iopl@plt>
jmp    12c4 <main+0x124>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <intel_register_access_fini@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"m\00"
@optind = global i64 0
@.str.1 = [5 x i8] c"0x%x\00"
@.str.2 = [13 x i8] c"0x%X : 0x%X\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
store i8* %14, i8** %11
br label %15
15: ; preds = %24, %2
%16 = load i32, i32* %4
%17 = load i8**, i8*** %5
%18 = call i32 @getopt(i32 %16, i8** %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %18, i32* %10
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i32, i32* %10
switch i32 %21, label %23 [
i32 109, label %22
]
22: ; preds = %20
store i32 1, i32* %6
br label %24
23: ; preds = %20
br label %24
24: ; preds = %23, %22
br label %15
25: ; preds = %15
%26 = load i64, i64* @optind
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = sub nsw i64 %28, %26
%30 = trunc i64 %29 to i32
store i32 %30, i32* %4
%31 = load i64, i64* @optind
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 %31
store i8** %33, i8*** %5
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 1
br i1 %35, label %36, label %39
36: ; preds = %25
%37 = load i8*, i8** %11
%38 = call i32 @usage(i8* %37)
store i32 1, i32* %3
br label %79
39: ; preds = %25
%40 = load i32, i32* %6
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = call i32 (...) @intel_get_pci_device()
%44 = call i32 @intel_register_access_init(i32 %43, i32 0)
br label %50
45: ; preds = %39
%46 = call i64 @iopl(i32 3)
%47 = icmp eq i64 %46, 0
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
br label %50
50: ; preds = %45, %42
store i32 0, i32* %9
br label %51
51: ; preds = %67, %50
%52 = load i32, i32* %9
%53 = load i32, i32* %4
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %70
55: ; preds = %51
%56 = load i8**, i8*** %5
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
%60 = load i8*, i8** %59
%61 = call i32 @sscanf(i8* %60, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %8)
%62 = load i32, i32* %8
%63 = load i32, i32* %8
%64 = load i32, i32* %6
%65 = call i32 @read_reg(i32 %63, i32 %64)
%66 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %62, i32 %65)
br label %67
67: ; preds = %55
%68 = load i32, i32* %9
%69 = add nsw i32 %68, 1
store i32 %69, i32* %9
br label %51
70: ; preds = %51
%71 = load i32, i32* %6
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %70
%74 = call i32 (...) @intel_register_access_fini()
br label %77
75: ; preds = %70
%76 = call i64 @iopl(i32 0)
br label %77
77: ; preds = %75, %73
%78 = load i32, i32* %7
store i32 %78, i32* %3
br label %79
79: ; preds = %77, %36
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
@.str = [2 x i8] c"m\00"
@optind = global i64 0
@.str.1 = [5 x i8] c"0x%x\00"
@.str.2 = [13 x i8] c"0x%X : 0x%X\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i8*, i8** %1
br label %6
6: ; preds = %9, %2
%7 = phi i1 [ true, %2 ], [ false, %9 ]
%8 = phi i32 [ 0, %2 ], [ 1, %9 ]
br label %9
9: ; preds = %6, %9
%10 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
switch i32 %10, label %9 [
i32 -1, label %11
i32 109, label %6
]
11: ; preds = %9
%12 = load i64, i64* @optind
%13 = trunc i64 %12 to i32
%14 = sub i32 %0, %13
%15 = getelementptr inbounds i8*, i8** %1, i64 %12
%16 = icmp slt i32 %14, 1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = tail call i32 @usage(i8* %5)
br label %45
19: ; preds = %11
br i1 %7, label %23, label %20
20: ; preds = %19
%21 = tail call i32 (...) @intel_get_pci_device()
%22 = tail call i32 @intel_register_access_init(i32 %21, i32 0)
br label %28
23: ; preds = %19
%24 = tail call i64 @iopl(i32 3)
%25 = icmp eq i64 %24, 0
%26 = zext i1 %25 to i32
%27 = tail call i32 @assert(i32 %26)
br label %28
28: ; preds = %20, %23
%29 = zext i32 %14 to i64
br label %30
30: ; preds = %28, %30
%31 = phi i64 [ 0, %28 ], [ %38, %30 ]
%32 = getelementptr inbounds i8*, i8** %15, i64 %31
%33 = load i8*, i8** %32
%34 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%35 = load i32, i32* %3
%36 = call i32 @read_reg(i32 %35, i32 %8)
%37 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 %36)
%38 = add nuw nsw i64 %31, 1
%39 = icmp eq i64 %38, %29
br i1 %39, label %40, label %30
40: ; preds = %30
br i1 %7, label %43, label %41
41: ; preds = %40
%42 = call i32 (...) @intel_register_access_fini()
br label %45
43: ; preds = %40
%44 = call i64 @iopl(i32 0)
br label %45
45: ; preds = %41, %43, %17
%46 = phi i32 [ 1, %17 ], [ 0, %43 ], [ 0, %41 ]
ret i32 %46
}