// File: /workspace/github_cpp_makefile_star2_O1/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 0x43c2
movq 0x8(%rbp), %rax
movq %rax, (%rsp)
cmpq %rax, 0x10(%rbp)
je 0x4eed
leaq 0x18(%rbp), %r14
leaq 0x17e4(%rip), %r13      # 0x6410
movzbl (%rax), %ecx
cmpl $0x3b, %ecx
jg 0x4c51
addl $-0x2b, %ecx
cmpl $0x3, %ecx
ja 0x4cd6
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
incb (%rbx)
jmp 0x4dc3
cmpl $0x5a, %ecx
jg 0x4c65
cmpl $0x3c, %ecx
je 0x4cd3
cmpl $0x3e, %ecx
jne 0x4cd6
incq %rbx
jmp 0x4cd6
cmpl $0x5b, %ecx
je 0x4ce2
cmpl $0x5d, %ecx
jne 0x4cd6
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 0x4cd6
cmpq %rdi, %rcx
je 0x4e08
movq -0x8(%rcx), %rax
addq $-0x8, %rcx
movq %rax, (%rsp)
jmp 0x4e50
decq %rbx
incq %rax
movq %rax, (%rsp)
jmp 0x4dc7
movq %r14, %rdi
movq %rsp, %rsi
callq 0x4f48
movq %rax, %r15
cmpq $0x0, 0x10(%rax)
jne 0x4d6a
cmpq $0x15, 0x8(%r15)
jb 0x4d6a
movl $0x20, %edi
callq 0x21a0
movq %rax, %r12
movq %rax, %rdi
callq 0x38e6
movq %r12, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x529e
movq (%r15), %r12
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq (%rdi), %rax
movq %r12, %rdx
callq *(%rax)
testb %al, %al
je 0x4e7c
movq 0x8(%rsp), %rax
movq %rax, 0x10(%r15)
movq %r15, %rdi
addq $0x18, %rdi
leaq 0x10(%rsp), %rsi
callq 0x501e
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4d6a
callq 0x4fae
movq 0x10(%r15), %rdi
testq %rdi, %rdi
je 0x4dda
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 0x4f48
movq (%rax), %rax
jmp 0x4cd9
decb (%rbx)
jmp 0x4dc3
movsbl (%rbx), %esi
movq 0x30(%rsp), %rdi
callq *0x28(%rsp)
jmp 0x4dc3
movq 0x20(%rsp), %rdi
callq *0x18(%rsp)
movb %al, (%rbx)
incq (%rsp)
movq (%rsp), %rax
cmpq 0x10(%rbp), %rax
jne 0x4c2c
jmp 0x4eed
incq 0x8(%r15)
cmpb $0x0, (%rbx)
je 0x4e5a
movq 0x70(%rsp), %rax
movq 0x80(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x4e6a
movq (%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x70(%rsp)
jmp 0x4dc3
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 %rcx, 0x70(%rsp)
jmp 0x4dc7
movq %r14, %rdi
movq %rsp, %rsi
callq 0x4f48
jmp 0x4d9e
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
callq 0x452a
jmp 0x4dc3
movq 0x415d(%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 0x3e22
movq 0x98(%rsp), %rdx
movq 0x38(%rsp), %rdi
leaq 0x156d(%rip), %rsi      # 0x6430
xorl %eax, %eax
callq 0x2230
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x4d5b
callq 0x2180
jmp 0x4d5b
leaq 0x40(%rsp), %rdi
callq 0x437e
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4f0e
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4f36
callq 0x4fae
jmp 0x4f36
movq %rax, %rbx
movq %r12, %rdi
callq 0x2180
jmp 0x4f36
jmp 0x4f33
jmp 0x4f33
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x437e
movq %rbx, %rdi
callq 0x22a0