// File: /workspace/github_cpp_makefile_star2_O1/AnthonyCalandra[P]chip8-emu/src/main.cc
// Function: main
int main(int argc, char* argv[]) {
  if (argc != 2) {
    std::cout << "Usage: emu ROMPath" << std::endl;
    return 1;
  }

  if (SDL_Init(SDL_INIT_VIDEO) != 0){
    std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
    return 1;
  }

  std::ifstream input;
  input.open(argv[1], std::ios::binary);
  if (input.fail()) {
    std::cout << "Failed to open file." << std::endl;
    SDL_Quit();
    return 1;
  }

  auto inputIt = std::istreambuf_iterator<char>(input);
  auto eos = std::istreambuf_iterator<char>();
  std::vector<char> romBuffer(inputIt, eos);
  Registers registers;
  MemoryStore memory;
  Stack stack;
  memory.loadRom(std::move(romBuffer));
  memory.loadFont(std::move(chip8Fontset));
  input.close();

  const char* windowTitle = "CHIP-8 Emulator";
  SDL_Window* window = SDL_CreateWindow(windowTitle, 100, 100, screenWidth, screenHeight, SDL_WINDOW_SHOWN);
  if (window == nullptr){
    std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
    SDL_Quit();
    return 1;
  }

  SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  if (renderer == nullptr) {
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 1;
  }

  std::thread chipThread(executeChipCpu, std::move(registers), std::move(memory), std::move(stack));
  gameLoop(window, renderer);

  chipThread.join();
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x12c0, %rsp           # imm = 0x12C0
cmpl $0x2, %edi
jne 0x4cbb
movq %rsi, %rbx
movl $0x20, %edi
callq 0x32b0
testl %eax, %eax
je 0x4d10
movq 0xa349(%rip), %rbx      # 0xefc8
leaq 0x5528(%rip), %rsi      # 0xa1ae
movl $0x10, %edx
movq %rbx, %rdi
callq 0x33a0
callq 0x3250
testq %rax, %rax
je 0x4d9a
movq %rax, %r14
movq %rax, %rdi
callq 0x3140
movq 0xa315(%rip), %rdi      # 0xefc8
movq %r14, %rsi
movq %rax, %rdx
jmp 0x4cd1
movq 0xa306(%rip), %rbx      # 0xefc8
leaq 0x54d2(%rip), %rsi      # 0xa19b
movl $0x12, %edx
movq %rbx, %rdi
callq 0x33a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3310
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movl $0x1, %ebx
movl %ebx, %eax
addq $0x12c0, %rsp           # imm = 0x12C0
popq %rbx
popq %r14
popq %rbp
retq
leaq -0x2c8(%rbp), %r14
movq %r14, %rdi
callq 0x3390
movq 0x8(%rbx), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0x31d0
movq -0x2c8(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x2a8(%rbp,%rax)
je 0x4db6
movq 0xa27c(%rip), %rbx      # 0xefc8
leaq 0x546c(%rip), %rsi      # 0xa1bf
movl $0x14, %edx
movq %rbx, %rdi
callq 0x33a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3310
movsbl %al, %esi
movq 0xa24a(%rip), %rdi      # 0xefc8
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movl $0x1, %ebx
callq 0x3090
jmp 0x4fa6
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x3490
jmp 0x4cd6
movq -0x1e0(%rbp,%rax), %rsi
leaq -0x30(%rbp), %rdi
leaq -0x12c8(%rbp), %r9
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x501a
leaq -0xc0(%rbp), %rdi
callq 0x505c
leaq -0x12c8(%rbp), %rbx
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3180
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
callq 0x6986
leaq 0xa47d(%rip), %rsi      # 0xf298
leaq -0x12c8(%rbp), %rdi
callq 0x69b8
leaq -0x2c8(%rbp), %rdi
callq 0x30c0
leaq 0x539a(%rip), %rdi      # 0xa1d4
movl $0x64, %esi
movl $0x64, %edx
movl $0x280, %ecx            # imm = 0x280
movl $0x140, %r8d            # imm = 0x140
movl $0x4, %r9d
callq 0x3070
movq %rax, %rbx
testq %rax, %rax
je 0x4ed5
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x6, %edx
callq 0x3050
movq %rax, %r14
testq %rax, %rax
je 0x4f19
leaq 0x1e4a(%rip), %rsi      # 0x6cd1
leaq -0x18(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
leaq -0x12c8(%rbp), %rcx
leaq -0x50(%rbp), %r8
callq 0x55b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3f38
leaq -0x18(%rbp), %rdi
callq 0x34a0
movq %rbx, %rdi
callq 0x3030
callq 0x3090
cmpq $0x0, -0x18(%rbp)
jne 0x4fc9
xorl %ebx, %ebx
jmp 0x4f79
movq 0xa0ec(%rip), %rbx      # 0xefc8
leaq 0x5301(%rip), %rsi      # 0xa1e4
movl $0x18, %edx
movq %rbx, %rdi
callq 0x33a0
callq 0x3250
movq %rax, %r14
testq %rax, %rax
je 0x4f2d
movq %r14, %rdi
callq 0x3140
movq 0xa0bc(%rip), %rdi      # 0xefc8
movq %r14, %rsi
movq %rax, %rdx
callq 0x33a0
jmp 0x4f44
movq %rbx, %rdi
callq 0x3030
movl $0x1, %ebx
callq 0x3090
jmp 0x4f79
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x3490
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3310
movsbl %al, %esi
movq 0xa066(%rip), %rdi      # 0xefc8
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movl $0x1, %ebx
callq 0x3090
leaq -0x88(%rbp), %rdi
callq 0x635e
leaq -0xc0(%rbp), %rdi
callq 0x632a
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4fa6
movq -0x20(%rbp), %rsi
subq %rdi, %rsi
callq 0x3340
leaq -0x2c8(%rbp), %rdi
callq 0x30e0
jmp 0x4d02
jmp 0x4fd0
movq %rax, %rbx
jmp 0x4feb
jmp 0x5002
jmp 0x4fd0
cmpq $0x0, -0x18(%rbp)
je 0x4fd0
callq 0x30f0
jmp 0x4fd0
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x635e
leaq -0xc0(%rbp), %rdi
callq 0x632a
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x5005
movq -0x20(%rbp), %rsi
subq %rdi, %rsi
callq 0x3340
jmp 0x5005
movq %rax, %rbx
leaq -0x2c8(%rbp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x34c0
nop