// File: brianquinlan[P]brainfuck-jit/bf_interpreter.cpp
// Function: BrainfuckInterpreter::init(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>)
bool BrainfuckInterpreter::init(string::const_iterator start,
                                string::const_iterator end) {
  start_ = start;
  end_ = end;

  // Build the mapping from the position of the start of a block (i.e. "]") to
  // the character *after* the end of the block.
  stack<string::const_iterator> block_starts;
  for (string::const_iterator it = start; it != end; ++it) {
    if (*it == '[') {
      block_starts.push(it);
    } else if (*it == ']') {
      if (block_starts.size() != 0) {
        const string::const_iterator &loop_start = block_starts.top();
        loop_start_to_after_end_[loop_start] = it+1;
        block_starts.pop();
      }
    }
  }

  if (block_starts.size() != 0) {
    fprintf(
        stderr,
        "Unable to find loop end in block starting with: %s\n",
        string(block_starts.top(), end).c_str());
    return false;
  }
  return true;
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq %rsi, 0x108(%rsp)
movq %rdx, 0x100(%rsp)
movq %rdi, 0xf8(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x108(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x100(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0xa8(%rsp), %rdi
callq 0x8c60
movq 0x108(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x6db0
testb $0x1, %al
jne 0x874f
jmp 0x8862
leaq 0xa0(%rsp), %rdi
callq 0x6df0
movsbl (%rax), %eax
cmpl $0x5b, %eax
jne 0x8799
leaq 0xa8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x8c90
jmp 0x877b
jmp 0x884e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
jmp 0x8980
leaq 0xa0(%rsp), %rdi
callq 0x6df0
movsbl (%rax), %eax
cmpl $0x5d, %eax
jne 0x884c
leaq 0xa8(%rsp), %rdi
callq 0x8cc0
movq %rax, 0x38(%rsp)
jmp 0x87c6
movq 0x38(%rsp), %rax
cmpq $0x0, %rax
je 0x884a
leaq 0xa8(%rsp), %rdi
callq 0x8cd0
movq %rax, 0x30(%rsp)
jmp 0x87e5
movq 0x30(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0xa0(%rsp), %rdi
movl $0x1, %esi
callq 0x6c80
movq 0x40(%rsp), %rdi
movq %rax, 0x80(%rsp)
addq $0x18, %rdi
movq 0x88(%rsp), %rsi
callq 0x8ce0
movq %rax, 0x28(%rsp)
jmp 0x8829
movq 0x28(%rsp), %rax
movq 0x80(%rsp), %rcx
movq %rcx, (%rax)
leaq 0xa8(%rsp), %rdi
callq 0x8dd0
jmp 0x8848
jmp 0x884a
jmp 0x884c
jmp 0x884e
jmp 0x8850
leaq 0xa0(%rsp), %rdi
callq 0x6fa0
jmp 0x8731
leaq 0xa8(%rsp), %rdi
callq 0x8cc0
movq %rax, 0x20(%rsp)
jmp 0x8876
movq 0x20(%rsp), %rax
cmpq $0x0, %rax
je 0x8952
movq 0xb754(%rip), %rax      # 0x13fe0
movq (%rax), %rax
movq %rax, 0x10(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x8cd0
movq %rax, 0x18(%rsp)
jmp 0x88a8
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x4f(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x33d0
movq 0x8(%rsp), %rcx
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rdx
leaq 0x60(%rsp), %rdi
callq 0x6f10
jmp 0x88ec
leaq 0x60(%rsp), %rdi
callq 0x3100
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0x5aa2(%rip), %rsi      # 0xe3a7
movb $0x0, %al
callq 0x3360
leaq 0x60(%rsp), %rdi
callq 0x3160
leaq 0x4f(%rsp), %rdi
callq 0x3280
movb $0x0, 0x117(%rsp)
movl $0x1, 0x48(%rsp)
jmp 0x8962
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0x4f(%rsp), %rdi
callq 0x3280
jmp 0x8980
movb $0x1, 0x117(%rsp)
movl $0x1, 0x48(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x8de0
movb 0x117(%rsp), %al
andb $0x1, %al
addq $0x118, %rsp            # imm = 0x118
retq
leaq 0xa8(%rsp), %rdi
callq 0x8de0
movq 0x98(%rsp), %rdi
callq 0x33c0
nopw (%rax,%rax)