// File: brianquinlan[P]brainfuck-jit/bf_jit.cpp
// Function: BrainfuckJIT::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 BrainfuckJIT::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
  // a Loop struct.
  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_loop_[loop_start] = Loop(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 $0x138, %rsp            # imm = 0x138
movq %rsi, 0x128(%rsp)
movq %rdx, 0x120(%rsp)
movq %rdi, 0x118(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x128(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x120(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0xc8(%rsp), %rdi
callq 0x8c60
movq 0x128(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x6db0
testb $0x1, %al
jne 0xb57f
jmp 0xb6e4
leaq 0xc0(%rsp), %rdi
callq 0x6df0
movsbl (%rax), %eax
cmpl $0x5b, %eax
jne 0xb5c9
leaq 0xc8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x8c90
jmp 0xb5ab
jmp 0xb6d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0xb800
leaq 0xc0(%rsp), %rdi
callq 0x6df0
movsbl (%rax), %eax
cmpl $0x5d, %eax
jne 0xb6ce
leaq 0xc8(%rsp), %rdi
callq 0x8cc0
movq %rax, 0x38(%rsp)
jmp 0xb5f6
movq 0x38(%rsp), %rax
cmpq $0x0, %rax
je 0xb6cc
leaq 0xc8(%rsp), %rdi
callq 0x8cd0
movq %rax, 0x30(%rsp)
jmp 0xb619
movq 0x30(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0xc0(%rsp), %rdi
movl $0x1, %esi
callq 0x6c80
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rsi
leaq 0x88(%rsp), %rdi
callq 0xbe00
jmp 0xb657
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
movq 0xa8(%rsp), %rsi
callq 0xbe40
movq %rax, 0x28(%rsp)
jmp 0xb674
movq 0x28(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movq %rsi, 0x20(%rsp)
callq 0xbf30
movq 0x20(%rsp), %rdi
callq 0xbf80
leaq 0xc8(%rsp), %rdi
callq 0x8dd0
jmp 0xb6a4
jmp 0xb6cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x88(%rsp), %rdi
callq 0xbf80
jmp 0xb800
jmp 0xb6ce
jmp 0xb6d0
jmp 0xb6d2
leaq 0xc0(%rsp), %rdi
callq 0x6fa0
jmp 0xb561
leaq 0xc8(%rsp), %rdi
callq 0x8cc0
movq %rax, 0x18(%rsp)
jmp 0xb6f8
movq 0x18(%rsp), %rax
cmpq $0x0, %rax
je 0xb7d2
movq 0x88d2(%rip), %rax      # 0x13fe0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x8cd0
movq %rax, 0x10(%rsp)
jmp 0xb72a
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x4f(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x33d0
movq (%rsp), %rcx
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rdx
leaq 0x60(%rsp), %rdi
callq 0x6f10
jmp 0xb76c
leaq 0x60(%rsp), %rdi
callq 0x3100
movq 0x8(%rsp), %rdi
movq %rax, %rdx
leaq 0x2c22(%rip), %rsi      # 0xe3a7
movb $0x0, %al
callq 0x3360
leaq 0x60(%rsp), %rdi
callq 0x3160
leaq 0x4f(%rsp), %rdi
callq 0x3280
movb $0x0, 0x137(%rsp)
movl $0x1, 0x48(%rsp)
jmp 0xb7e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x4f(%rsp), %rdi
callq 0x3280
jmp 0xb800
movb $0x1, 0x137(%rsp)
movl $0x1, 0x48(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x8de0
movb 0x137(%rsp), %al
andb $0x1, %al
addq $0x138, %rsp            # imm = 0x138
retq
leaq 0xc8(%rsp), %rdi
callq 0x8de0
movq 0xb8(%rsp), %rdi
callq 0x33c0
nopw (%rax,%rax)