// File: Lupo-CalPoly[P]cpe315-thumbsim/main.cpp
// Function: main
int main(int argc, char ** argv) {
  int c;
  string filename;
  while ((c = getopt(argc, argv, "pdiwsc:f:")) != -1) {
    switch(c) {
    case 'p':
      opts.program = true;
      break;
    case 'd':
      opts.dump = true;
      break;
    case 'i':
      opts.instrs = true;
      break;
    case 'w':
      opts.writes = true;
      break;
    case 's':
      opts.stats = true;
      break;
    case 'c':
      caches = Caches(atoi(optarg));
      break;
    case 'f':
      filename = optarg;
      break;
    }
  }

  if (filename == "") {
    cerr << "ERROR: no source file specified; "
         << "run this program with -f filename" << endl;
    exit(1);
  }

  parse(filename.c_str());

  if (opts.program) {
    cout << "Instructions:" << endl;
    imem.dump(INSTRUCTIONS);
  }

  cout << "Starting at PC " << hex << pc << endl;
  rf.write(15, pc);
  while(imem.inRange(rf[15])) {
    execute();
  }

  if (opts.dump) {
    cout << "DATA:" << endl;
    dmem.dump(DATA);
    cout << "RF:" << endl;
    rf.dump(DATA);
  }

  if (opts.stats) {
    stats.print();
    caches.printStats();
  }

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x22d0
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0xb8cc(%rip), %rdx      # 0xe05c
callq 0x2050
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x286a
movl -0x14(%rbp), %eax
addl $-0x63, %eax
movl %eax, %ecx
movq %rcx, -0x78(%rbp)
subl $0x14, %eax
ja 0x2865
movq -0x78(%rbp), %rax
leaq 0xb843(%rip), %rcx      # 0xe004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, 0x12ab3(%rip)     # 0x15284
jmp 0x2865
movb $0x1, 0x12aa8(%rip)     # 0x15285
jmp 0x2865
movb $0x1, 0x12a9d(%rip)     # 0x15286
jmp 0x2865
movb $0x1, 0x12a95(%rip)     # 0x15287
jmp 0x2865
movb $0x1, 0x12a8d(%rip)     # 0x15288
jmp 0x2865
movq 0x127b4(%rip), %rax     # 0x14fb8
movq (%rax), %rdi
callq 0x2300
movl %eax, %esi
leaq -0x58(%rbp), %rdi
callq 0x2db0
jmp 0x2819
leaq 0x12aa0(%rip), %rdi     # 0x152c0
leaq -0x58(%rbp), %rsi
callq 0x2e70
leaq -0x58(%rbp), %rdi
callq 0x2eb0
jmp 0x2865
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2110
jmp 0x2b2b
movq 0x12763(%rip), %rax     # 0x14fb8
movq (%rax), %rsi
leaq -0x38(%rbp), %rdi
callq 0x22c0
jmp 0x2863
jmp 0x2865
jmp 0x2782
leaq 0xba5e(%rip), %rsi      # 0xe2cf
leaq -0x38(%rbp), %rdi
callq 0x2ed0
movb %al, -0x79(%rbp)
jmp 0x287f
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x2888
jmp 0x28df
movq 0x12761(%rip), %rdi     # 0x14ff0
leaq 0xb7d0(%rip), %rsi      # 0xe066
callq 0x21d0
movq %rax, -0x88(%rbp)
jmp 0x28a4
movq -0x88(%rbp), %rdi
leaq 0xb7d6(%rip), %rsi      # 0xe088
callq 0x21d0
movq %rax, -0x90(%rbp)
jmp 0x28c0
movq -0x90(%rbp), %rdi
movq 0x126e2(%rip), %rsi     # 0x14fb0
callq 0x2200
jmp 0x28d5
movl $0x1, %edi
callq 0x2260
leaq -0x38(%rbp), %rdi
callq 0x20c0
movq %rax, %rdi
callq 0xb6c0
jmp 0x28f2
testb $0x1, 0x1298b(%rip)     # 0x15284
je 0x293e
movq 0x126c6(%rip), %rdi     # 0x14fc8
leaq 0xb7a1(%rip), %rsi      # 0xe0aa
callq 0x21d0
movq %rax, -0x98(%rbp)
jmp 0x2917
movq -0x98(%rbp), %rdi
movq 0x1268b(%rip), %rsi     # 0x14fb0
callq 0x2200
jmp 0x292c
leaq 0x128d5(%rip), %rdi     # 0x15208
xorl %esi, %esi
callq 0x85e0
jmp 0x293c
jmp 0x293e
movq 0x12683(%rip), %rdi     # 0x14fc8
leaq 0xb76c(%rip), %rsi      # 0xe0b8
callq 0x21d0
movq %rax, -0xa0(%rbp)
jmp 0x295a
movq -0xa0(%rbp), %rdi
leaq 0x598(%rip), %rsi       # 0x2f00
callq 0x2090
movq %rax, -0xa8(%rbp)
jmp 0x2976
leaq 0x12903(%rip), %rdi     # 0x15280
callq 0x2f30
movl %eax, -0xac(%rbp)
jmp 0x298a
movl -0xac(%rbp), %esi
movq -0xa8(%rbp), %rdi
callq 0x22b0
movq %rax, -0xb8(%rbp)
jmp 0x29a5
movq -0xb8(%rbp), %rdi
movq 0x125fd(%rip), %rsi     # 0x14fb0
callq 0x2200
jmp 0x29ba
leaq 0x128bf(%rip), %rdi     # 0x15280
callq 0x2f50
movl %eax, -0xbc(%rbp)
jmp 0x29ce
movl -0xbc(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %edx
leaq 0x12877(%rip), %rdi     # 0x15258
movl $0xf, %esi
callq 0x82f0
jmp 0x29ed
jmp 0x29ef
leaq 0x12862(%rip), %rdi     # 0x15258
movl $0xf, %esi
callq 0x8430
movl %eax, -0xc0(%rbp)
jmp 0x2a08
movl -0xc0(%rbp), %eax
movl %eax, -0x6c(%rbp)
leaq -0x6c(%rbp), %rdi
callq 0x2fd0
movl %eax, -0xc4(%rbp)
jmp 0x2a22
movl -0xc4(%rbp), %esi
leaq 0x127d9(%rip), %rdi     # 0x15208
callq 0x2f70
movb %al, -0xc5(%rbp)
jmp 0x2a3c
movb -0xc5(%rbp), %al
testb $0x1, %al
jne 0x2a48
jmp 0x2a51
callq 0xc120
jmp 0x2a4f
jmp 0x29ef
testb $0x1, 0x1282d(%rip)     # 0x15285
je 0x2ae8
movq 0x12563(%rip), %rdi     # 0x14fc8
leaq 0xb65c(%rip), %rsi      # 0xe0c8
callq 0x21d0
movq %rax, -0xd0(%rbp)
jmp 0x2a7a
movq -0xd0(%rbp), %rdi
movq 0x12528(%rip), %rsi     # 0x14fb0
callq 0x2200
jmp 0x2a8f
leaq 0x1279a(%rip), %rdi     # 0x15230
movl $0x1, %esi
callq 0x8470
jmp 0x2aa2
movq 0x1251f(%rip), %rdi     # 0x14fc8
leaq 0xb61e(%rip), %rsi      # 0xe0ce
callq 0x21d0
movq %rax, -0xd8(%rbp)
jmp 0x2abe
movq -0xd8(%rbp), %rdi
movq 0x124e4(%rip), %rsi     # 0x14fb0
callq 0x2200
jmp 0x2ad3
leaq 0x1277e(%rip), %rdi     # 0x15258
movl $0x1, %esi
callq 0x8640
jmp 0x2ae6
jmp 0x2ae8
testb $0x1, 0x12799(%rip)     # 0x15288
je 0x2b0f
leaq 0x1279c(%rip), %rdi     # 0x15294
callq 0x86c0
jmp 0x2aff
leaq 0x127ba(%rip), %rdi     # 0x152c0
callq 0x2ff0
jmp 0x2b0d
jmp 0x2b0f
movl $0x0, -0x4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2110
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x2370
nopw %cs:(%rax,%rax)
nop