// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r14
movq %rsi, 0x8(%rdi)
movq %rdx, 0x10(%rdi)
xorps %xmm0, %xmm0
movq %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 %r13, 0x58(%rsp)
cmpq %rbx, %r13
je 0x4ae8
addq $0x18, %r14
movl $0x200, %ebp            # imm = 0x200
movq %rsp, %r15
leaq 0x58(%rsp), %r12
movzbl (%r13), %eax
cmpl $0x5d, %eax
je 0x49f7
cmpl $0x5b, %eax
jne 0x4ad2
movq 0x30(%rsp), %rax
movq 0x40(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x4a92
movq 0x58(%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x30(%rsp)
jmp 0x4ad2
movq 0x48(%rsp), %rax
movq %rax, %rdx
subq 0x28(%rsp), %rdx
shrq $0x3, %rdx
cmpq $0x1, %rax
adcq $-0x1, %rdx
shlq $0x6, %rdx
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rcx
movq %rsi, %r8
subq %rcx, %r8
sarq $0x3, %r8
addq %rdx, %r8
subq 0x10(%rsp), %rdi
sarq $0x3, %rdi
addq %r8, %rdi
je 0x4ad2
cmpq %rcx, %rsi
jne 0x4a4e
movq -0x8(%rax), %rsi
addq %rbp, %rsi
addq $-0x8, %rsi
movq %r14, %rdi
callq 0x4f48
incq %r13
movq %r13, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x18(%rax), %rdi
movq $0x0, 0x18(%rax)
testq %rdi, %rdi
je 0x4a7d
callq 0x4fae
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rdi
cmpq %rdi, %rax
je 0x4a9f
addq $-0x8, %rax
jmp 0x4acd
movq %r15, %rdi
movq %r12, %rsi
callq 0x452a
jmp 0x4ad2
callq 0x2180
movq 0x48(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x48(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0x38(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x40(%rsp)
addq $0x1f8, %rax            # imm = 0x1F8
movq %rax, 0x30(%rsp)
movq 0x58(%rsp), %r13
incq %r13
movq %r13, 0x58(%rsp)
cmpq %rbx, %r13
jne 0x49ba
movq 0x48(%rsp), %rax
movq %rax, %rsi
subq 0x28(%rsp), %rsi
shrq $0x3, %rsi
cmpq $0x1, %rax
adcq $-0x1, %rsi
shlq $0x6, %rsi
movq 0x20(%rsp), %r15
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rdx
movq %rcx, %r12
subq %rdx, %r12
sarq $0x3, %r12
subq 0x10(%rsp), %r15
addq %rsi, %r12
sarq $0x3, %r15
negq %r15
cmpq %r15, %r12
jne 0x4b51
cmpq %r15, %r12
sete %bl
movq %rsp, %rdi
callq 0x437e
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4488(%rip), %rsi      # 0x8fe0
cmpq %rdx, %rcx
jne 0x4b66
movl $0x200, %ecx            # imm = 0x200
addq -0x8(%rax), %rcx
movq (%rsi), %r14
movq -0x8(%rcx), %rsi
leaq 0x68(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x58(%rsp), %rdi
movq %rbx, %rdx
callq 0x3e22
movq 0x58(%rsp), %rdx
leaq 0x174d(%rip), %rsi      # 0x62dc
movq %r14, %rdi
xorl %eax, %eax
callq 0x2230
movq 0x58(%rsp), %rdi
cmpq %r13, %rdi
je 0x4b32
callq 0x2180
jmp 0x4b32
jmp 0x4bae
jmp 0x4bae
movq %rax, %rbx
movq %rsp, %rdi
callq 0x437e
movq %rbx, %rdi
callq 0x22a0
nop