// File: 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:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
movq %rcx, 0xb8(%rsp)
movq %r8, 0xb0(%rsp)
movq %r9, 0xa8(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x8c60
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x30(%rsp), %rsi
addq $0x10, %rsi
leaq 0x48(%rsp), %rdi
callq 0x6db0
testb $0x1, %al
jne 0x8a28
jmp 0x8c17
leaq 0x48(%rsp), %rdi
callq 0x6df0
movsbl (%rax), %eax
addl $-0x2b, %eax
movl %eax, %ecx
movq %rcx, 0x28(%rsp)
subl $0x32, %eax
ja 0x8c08
movq 0x28(%rsp), %rax
leaq 0x5a14(%rip), %rcx      # 0xe468
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0xa0(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0xa0(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x6fa0
jmp 0x8c12
movq 0xa0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa0(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x6fa0
jmp 0x8c12
movq 0xa0(%rsp), %rax
movzbl (%rax), %ecx
subl $0x1, %ecx
movb %cl, (%rax)
leaq 0x48(%rsp), %rdi
callq 0x6fa0
jmp 0x8c12
movq 0xa0(%rsp), %rax
movzbl (%rax), %ecx
addl $0x1, %ecx
movb %cl, (%rax)
leaq 0x48(%rsp), %rdi
callq 0x6fa0
jmp 0x8c12
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rdi
callq *%rax
movb %al, 0x27(%rsp)
jmp 0x8af9
movb 0x27(%rsp), %cl
movq 0xa0(%rsp), %rax
movb %cl, (%rax)
leaq 0x48(%rsp), %rdi
callq 0x6fa0
jmp 0x8c12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x8de0
jmp 0x8c39
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %rdi
movq 0xa0(%rsp), %rcx
movsbl (%rcx), %esi
callq *%rax
jmp 0x8b52
leaq 0x48(%rsp), %rdi
callq 0x6fa0
jmp 0x8c12
movq 0xa0(%rsp), %rax
cmpb $0x0, (%rax)
je 0x8b8b
leaq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x8c90
jmp 0x8b7f
leaq 0x48(%rsp), %rdi
callq 0x6fa0
jmp 0x8bb2
movq 0x30(%rsp), %rdi
addq $0x18, %rdi
leaq 0x48(%rsp), %rsi
callq 0x8ce0
movq %rax, 0x18(%rsp)
jmp 0x8ba5
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x48(%rsp)
jmp 0x8c12
leaq 0x50(%rsp), %rdi
callq 0x8cc0
movq %rax, 0x10(%rsp)
jmp 0x8bc5
movq 0x10(%rsp), %rax
cmpq $0x0, %rax
je 0x8bfc
leaq 0x50(%rsp), %rdi
callq 0x8cd0
movq %rax, 0x8(%rsp)
jmp 0x8be1
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x8dd0
jmp 0x8bfa
jmp 0x8c06
leaq 0x48(%rsp), %rdi
callq 0x6fa0
jmp 0x8c12
leaq 0x48(%rsp), %rdi
callq 0x6fa0
jmp 0x8a0c
movq 0xa0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x50(%rsp), %rdi
callq 0x8de0
movq (%rsp), %rax
addq $0xd8, %rsp
retq
movq 0x40(%rsp), %rdi
callq 0x33c0
nopw %cs:(%rax,%rax)
nopl (%rax)