// File: /workspace/github_cpp_makefile_star2_O1/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 0x43c2
movq 0x8(%rbp), %rax
movq %rax, 0x8(%rsp)
cmpq %rax, 0x10(%rbp)
je 0x42e7
leaq 0x18(%rbp), %rcx
movq %rcx, 0x10(%rsp)
leaq 0x2229(%rip), %r14      # 0x639c
movzbl (%rax), %ecx
cmpl $0x3b, %ecx
jg 0x4197
addl $-0x2b, %ecx
cmpl $0x3, %ecx
ja 0x421a
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
incb (%rbx)
jmp 0x4265
cmpl $0x5a, %ecx
jg 0x41ab
cmpl $0x3c, %ecx
je 0x4217
cmpl $0x3e, %ecx
jne 0x421a
incq %rbx
jmp 0x421a
cmpl $0x5b, %ecx
je 0x4224
cmpl $0x5d, %ecx
jne 0x421a
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 0x421a
cmpq %rdi, %rcx
je 0x42a0
movq -0x8(%rcx), %rax
addq $-0x8, %rcx
movq %rax, 0x8(%rsp)
jmp 0x42e0
decq %rbx
incq %rax
movq %rax, 0x8(%rsp)
jmp 0x426a
cmpb $0x0, (%rbx)
je 0x427b
movq 0x50(%rsp), %rax
movq 0x60(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x428f
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x50(%rsp)
jmp 0x4265
decb (%rbx)
jmp 0x4265
movsbl (%rbx), %esi
movq 0x18(%rsp), %rdi
callq *%r15
jmp 0x4265
movq %r12, %rdi
callq *%r13
movb %al, (%rbx)
incq 0x8(%rsp)
movq 0x8(%rsp), %rax
cmpq 0x10(%rbp), %rax
jne 0x4173
jmp 0x42e7
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4318
movq (%rax), %rax
jmp 0x421d
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x452a
jmp 0x4265
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 %rcx, 0x50(%rsp)
jmp 0x426a
leaq 0x20(%rsp), %rdi
callq 0x437e
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 0x437e
movq %rbx, %rdi
callq 0x22a0