// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %rbp
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
xorl %esi, %esi
callq 0x4368
movq 0x8(%rbp), %rax
movq %rax, (%rsp)
cmpq %rax, 0x10(%rbp)
je 0x4ea5
leaq 0x18(%rbp), %r14
leaq 0x183e(%rip), %r13      # 0x6410
movzbl (%rax), %ecx
cmpl $0x3b, %ecx
jg 0x4bf7
addl $-0x2b, %ecx
cmpl $0x3, %ecx
ja 0x4d78
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
incb (%rbx)
jmp 0x4d74
cmpl $0x5a, %ecx
jg 0x4c16
cmpl $0x3c, %ecx
je 0x4c8c
cmpl $0x3e, %ecx
jne 0x4d78
incq %rbx
jmp 0x4d78
cmpl $0x5b, %ecx
je 0x4c94
cmpl $0x5d, %ecx
jne 0x4d78
movq 0x88(%rsp), %rdx
movq %rdx, %rsi
subq 0x68(%rsp), %rsi
shrq $0x3, %rsi
cmpq $0x1, %rdx
adcq $-0x1, %rsi
shlq $0x6, %rsi
movq 0x60(%rsp), %r8
movq 0x70(%rsp), %rcx
movq 0x78(%rsp), %rdi
movq %rcx, %r9
subq %rdi, %r9
sarq $0x3, %r9
addq %rsi, %r9
subq 0x50(%rsp), %r8
sarq $0x3, %r8
addq %r9, %r8
je 0x4d78
cmpq %rdi, %rcx
je 0x4dbc
movq -0x8(%rcx), %rax
addq $-0x8, %rcx
movq %rax, (%rsp)
jmp 0x4e08
decq %rbx
jmp 0x4d78
movq %r14, %rdi
movq %rsp, %rsi
callq 0x4f00
movq %rax, %r15
movq 0x10(%rax), %rdi
testq %rdi, %rdi
jne 0x4d22
cmpq $0x15, 0x8(%r15)
jb 0x4d22
movl $0x20, %edi
callq 0x21a0
movq %rax, %r12
movq %rax, %rdi
callq 0x3878
movq %r12, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x525a
movq (%r15), %r12
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq (%rdi), %rax
movq %r12, %rdx
callq *(%rax)
testb %al, %al
je 0x4e34
movq 0x8(%rsp), %rax
movq %rax, 0x10(%r15)
movq %r15, %rdi
addq $0x18, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4fd6
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4d1e
callq 0x4f66
movq 0x10(%r15), %rdi
testq %rdi, %rdi
je 0x4d8e
movq (%rdi), %rax
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movq %rbx, %r9
callq *0x8(%rax)
movq %rax, %rbx
movq %r14, %rdi
movq %rsp, %rsi
callq 0x4f00
movq (%rax), %rax
jmp 0x4d7b
decb (%rbx)
jmp 0x4d74
movsbl (%rbx), %esi
movq 0x30(%rsp), %rdi
callq *0x28(%rsp)
jmp 0x4d74
movq 0x20(%rsp), %rdi
callq *0x18(%rsp)
movb %al, (%rbx)
movq (%rsp), %rax
incq %rax
movq %rax, (%rsp)
cmpq 0x10(%rbp), %rax
jne 0x4bd2
jmp 0x4ea5
incq 0x8(%r15)
cmpb $0x0, (%rbx)
je 0x4e12
movq 0x70(%rsp), %rcx
movq 0x80(%rsp), %rax
addq $-0x8, %rax
cmpq %rax, %rcx
je 0x4e22
movq (%rsp), %rax
movq %rax, (%rcx)
addq $0x8, 0x70(%rsp)
jmp 0x4d78
movq -0x8(%rdx), %rax
movq 0x1f8(%rax), %rax
movq %rax, (%rsp)
callq 0x2180
movq 0x88(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x88(%rsp)
movq -0x8(%rax), %rcx
movq %rcx, 0x78(%rsp)
leaq 0x200(%rcx), %rax
movq %rax, 0x80(%rsp)
movl $0x1f8, %eax            # imm = 0x1F8
addq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x70(%rsp)
jmp 0x4d7f
movq %r14, %rdi
movq %rsp, %rsi
callq 0x4f00
jmp 0x4d52
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
callq 0x44d0
jmp 0x4d74
movq 0x41a5(%rip), %rax      # 0x8fe0
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movq (%rsp), %rsi
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x98(%rsp), %rdi
movq %r12, %rdx
callq 0x3db4
movq 0x98(%rsp), %rdx
movq 0x38(%rsp), %rdi
leaq 0x15b5(%rip), %rsi      # 0x6430
xorl %eax, %eax
callq 0x2230
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x4d0f
callq 0x2180
jmp 0x4d0f
leaq 0x40(%rsp), %rdi
callq 0x431e
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4ec6
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4eee
callq 0x4f66
jmp 0x4eee
movq %rax, %rbx
movq %r12, %rdi
callq 0x2180
jmp 0x4eee
jmp 0x4eeb
jmp 0x4eeb
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x431e
movq %rbx, %rdi
callq 0x22a0