// File: /workspace/github_cpp_makefile_star2_O2/brianquinlan[P]brainfuck-jit/bf_jit.cpp
// Function: BrainfuckJIT::run(char (*)(void*), void*, bool (*)(void*, char), void*, void*)
void* BrainfuckJIT::run(BrainfuckReader reader,
                        void* reader_arg,
                        BrainfuckWriter writer,
                        void* writer_arg,
                        void* memory) {
  uint8_t* byte_memory = reinterpret_cast<uint8_t *>(memory);
  // When processing a "[", push the position of that command onto a stack so
  // that we can quickly return to the start of the block when then "]" is
  // interpreted.
  stack<string::const_iterator> return_stack;

  for (string::const_iterator it = start_; it != end_;) {
    switch (*it) {
      case '<':
        --byte_memory;
         ++it;
        break;
      case '>':
        ++byte_memory;
         ++it;
        break;
      case '-':
        *byte_memory -= 1;
         ++it;
        break;
      case '+':
        *byte_memory += 1;
         ++it;
        break;
      case ',':
        *byte_memory = reader(reader_arg);
         ++it;
        break;
      case '.':
        writer(writer_arg, *byte_memory);
         ++it;
        break;
      case '[':
        {
          Loop &loop = loop_start_to_loop_[it];

          if (loop.compiled == nullptr &&
              loop.condition_evaluation_count > kLoopCompilationThreshold) {
            shared_ptr<BrainfuckCompileAndGo> compiled(
              new BrainfuckCompileAndGo());
            string::const_iterator compilation_end(loop.after_end);

            if (!compiled->init(it, compilation_end)) {
              fprintf(stderr,
                      "Unable to compile code: %s\n",
                      string(it, compilation_end).c_str());
            } else {
              loop.compiled = compiled;
            }
          }

          if (loop.compiled) {
            byte_memory = reinterpret_cast<uint8_t *>(
                loop.compiled->run(reader,
                                   reader_arg,
                                   writer,
                                   writer_arg,
                                   byte_memory));
            it = loop_start_to_loop_[it].after_end;
          } else {
            ++loop.condition_evaluation_count;
            if (*byte_memory) {
              return_stack.push(it);
              ++it;
            } else {
              it = loop_start_to_loop_[it].after_end;
            }
          }
        }
        break;
      case ']':
        if (return_stack.size() != 0) {
          it = return_stack.top();
          return_stack.pop();
        } else {
          ++it;
        }
        break;
      default:
        ++it;
        break;
    }
  }
  return byte_memory;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, %rbx
movq %r8, %rbp
movq %rcx, %r15
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
leaq 0x68(%rsp), %rdi
callq 0x3d98
movq 0x8(%r14), %rax
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x18(%r14), %rcx
movq %rcx, 0x10(%rsp)
movq %r15, 0x40(%rsp)
cmpq 0x10(%r14), %rax
je 0x4952
movzbl (%rax), %ecx
leal -0x2b(%rcx), %edx
cmpl $0x3, %edx
jbe 0x4768
cmpl $0x5d, %ecx
je 0x477f
cmpl $0x3e, %ecx
je 0x48cf
cmpl $0x5b, %ecx
je 0x47dc
cmpl $0x3c, %ecx
jne 0x48c2
decq %rbx
jmp 0x48c2
leaq 0xca1(%rip), %rcx       # 0x5410
movslq (%rcx,%rdx,4), %rax
addq %rcx, %rax
jmpq *%rax
incb (%rbx)
jmp 0x48bd
leaq 0x68(%rsp), %rdi
callq 0x40c2
testq %rax, %rax
je 0x48bd
movq 0x98(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jne 0x47b8
movq 0xb0(%rsp), %rax
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movq -0x8(%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x44ca
movq 0x8(%rsp), %rax
jmp 0x472b
decb (%rbx)
jmp 0x48bd
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x49a8
movq %rax, %r12
movq 0x10(%rax), %rdi
testq %rdi, %rdi
jne 0x486a
cmpq $0x15, 0x8(%r12)
jb 0x486a
pushq $0x20
popq %rdi
callq 0x21b0
movq %rax, %r13
movq %rax, %rdi
callq 0x34c2
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x4dc2
movq %rbp, %r15
movq (%r12), %rbp
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq (%rdi), %rax
movq %rbp, %rdx
callq *(%rax)
movq %r12, %r13
addq $0x10, %r13
testb %al, %al
je 0x4900
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4abe
movq %r15, %rbp
leaq 0x30(%rsp), %rdi
callq 0x4a42
movq (%r13), %rdi
movq 0x40(%rsp), %r15
testq %rdi, %rdi
je 0x48d4
movq (%rdi), %rax
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %rbx, %r9
callq *0x8(%rax)
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x49a8
movq (%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0x472b
movsbl (%rbx), %esi
movq %rbp, %rdi
callq *%r15
jmp 0x48bd
movq 0x20(%rsp), %rdi
callq *0x18(%rsp)
movb %al, (%rbx)
movq 0x8(%rsp), %rax
incq %rax
movq %rax, 0x8(%rsp)
jmp 0x472b
incq %rbx
jmp 0x48c2
incq 0x8(%r12)
cmpb $0x0, (%rbx)
je 0x48ef
leaq 0x68(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4026
jmp 0x48bd
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x49a8
jmp 0x489a
movq 0x46d9(%rip), %rax      # 0x8fe0
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x8(%rsp), %rsi
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x48(%rsp), %rdi
movq %rbp, %rdx
callq 0x3a2a
movq 0x48(%rsp), %rdx
movq 0x38(%rsp), %rdi
leaq 0xaf4(%rip), %rsi       # 0x5430
xorl %eax, %eax
callq 0x2250
leaq 0x48(%rsp), %rdi
callq 0x20f0
jmp 0x4854
leaq 0x68(%rsp), %rdi
callq 0x3e04
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4973
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x4a42
jmp 0x4996
movq %rax, %rbx
movq %r13, %rdi
callq 0x2190
jmp 0x4996
jmp 0x4993
jmp 0x4993
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3e04
movq %rbx, %rdi
callq 0x22b0