// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbp
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x3d98
movq 0x8(%rbp), %rax
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x18(%rbp), %rcx
movq %rcx, 0x10(%rsp)
leaq 0x1721(%rip), %r15      # 0x539c
cmpq 0x10(%rbp), %rax
je 0x3d67
movzbl (%rax), %ecx
leal -0x2b(%rcx), %edx
cmpl $0x3, %edx
jbe 0x3cb4
cmpl $0x5d, %ecx
je 0x3cc1
cmpl $0x3e, %ecx
je 0x3d46
cmpl $0x5b, %ecx
je 0x3d0a
cmpl $0x3c, %ecx
jne 0x3d39
decq %rbx
jmp 0x3d39
movslq (%r15,%rdx,4), %rax
addq %r15, %rax
jmpq *%rax
incb (%rbx)
jmp 0x3d34
movq %r14, %rdi
callq 0x40c2
testq %rax, %rax
je 0x3d34
movq 0x58(%rsp), %rax
cmpq 0x60(%rsp), %rax
jne 0x3ceb
movq 0x70(%rsp), %rax
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movq -0x8(%rax), %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x44ca
movq 0x8(%rsp), %rax
jmp 0x3c7b
decb (%rbx)
jmp 0x3d34
cmpb $0x0, (%rbx)
je 0x3d4b
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4026
jmp 0x3d34
movsbl (%rbx), %esi
movq 0x20(%rsp), %rdi
callq *0x18(%rsp)
jmp 0x3d34
movq %r12, %rdi
callq *%r13
movb %al, (%rbx)
movq 0x8(%rsp), %rax
incq %rax
movq %rax, 0x8(%rsp)
jmp 0x3c7b
incq %rbx
jmp 0x3d39
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3db4
movq (%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0x3c7b
leaq 0x28(%rsp), %rdi
callq 0x3e04
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x3e04
movq %rbx, %rdi
callq 0x22b0