// File: /workspace/github_cpp_makefile_star2_O3/brianquinlan[P]brainfuck-jit/bf_interpreter.cpp
// Function: BrainfuckInterpreter::run(char (*)(void*), void*, bool (*)(void*, char), void*, void*)
void* BrainfuckInterpreter::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 '[':
        if (*byte_memory) {
          return_stack.push(it);
           ++it;
        } else {
          it = loop_start_to_after_end_[it];
        }
        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 $0x78, %rsp
movq %r9, %rbx
movq %r8, 0x18(%rsp)
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbp
xorps %xmm0, %xmm0
leaq 0x20(%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, 0x8(%rsp)
cmpq %rax, 0x10(%rbp)
je 0x4287
leaq 0x18(%rbp), %rcx
movq %rcx, 0x10(%rsp)
leaq 0x2293(%rip), %r14      # 0x639c
movzbl (%rax), %ecx
cmpl $0x3b, %ecx
jg 0x412d
addl $-0x2b, %ecx
cmpl $0x3, %ecx
ja 0x4202
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
incb (%rbx)
jmp 0x41fd
cmpl $0x5a, %ecx
jg 0x414c
cmpl $0x3c, %ecx
je 0x41bc
cmpl $0x3e, %ecx
jne 0x4202
incq %rbx
jmp 0x4202
cmpl $0x5b, %ecx
je 0x41c1
cmpl $0x5d, %ecx
jne 0x4202
movq 0x68(%rsp), %rdx
movq %rdx, %rsi
subq 0x48(%rsp), %rsi
shrq $0x3, %rsi
cmpq $0x1, %rdx
adcq $-0x1, %rsi
shlq $0x6, %rsi
movq 0x40(%rsp), %r8
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdi
movq %rcx, %r9
subq %rdi, %r9
sarq $0x3, %r9
addq %rsi, %r9
subq 0x30(%rsp), %r8
sarq $0x3, %r8
addq %r9, %r8
je 0x4202
cmpq %rdi, %rcx
je 0x423b
movq -0x8(%rcx), %rax
addq $-0x8, %rcx
movq %rax, 0x8(%rsp)
jmp 0x4280
decq %rbx
jmp 0x4202
cmpb $0x0, (%rbx)
je 0x4216
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x422a
movq %rax, (%rcx)
addq $0x8, 0x50(%rsp)
jmp 0x41fd
decb (%rbx)
jmp 0x41fd
movsbl (%rbx), %esi
movq 0x18(%rsp), %rdi
callq *%r15
jmp 0x41fd
movq %r12, %rdi
callq *%r13
movb %al, (%rbx)
movq 0x8(%rsp), %rax
incq %rax
movq %rax, 0x8(%rsp)
cmpq 0x10(%rbp), %rax
jne 0x4109
jmp 0x4287
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x42b8
movq (%rax), %rax
jmp 0x4205
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x44d0
jmp 0x41fd
movq -0x8(%rdx), %rax
movq 0x1f8(%rax), %rax
movq %rax, 0x8(%rsp)
callq 0x2180
movq 0x68(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x68(%rsp)
movq -0x8(%rax), %rcx
movq %rcx, 0x58(%rsp)
leaq 0x200(%rcx), %rax
movq %rax, 0x60(%rsp)
movl $0x1f8, %eax            # imm = 0x1F8
addq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x50(%rsp)
jmp 0x420a
leaq 0x20(%rsp), %rdi
callq 0x431e
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x431e
movq %rbx, %rdi
callq 0x22a0