// File: /workspace/github_cpp_makefile_star2_O3/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 0x4368
movq %r13, 0x58(%rsp)
cmpq %rbx, %r13
je 0x4a8e
addq $0x18, %r14
movl $0x200, %ebp            # imm = 0x200
movq %rsp, %r15
leaq 0x58(%rsp), %r12
movzbl (%r13), %eax
cmpl $0x5d, %eax
je 0x499d
cmpl $0x5b, %eax
jne 0x4a78
movq 0x30(%rsp), %rax
movq 0x40(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x4a38
movq %r13, (%rax)
movq 0x30(%rsp), %rax
addq $0x8, %rax
jmp 0x4a73
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 0x4a78
cmpq %rcx, %rsi
jne 0x49f4
movq -0x8(%rax), %rsi
addq %rbp, %rsi
addq $-0x8, %rsi
movq %r14, %rdi
callq 0x4f00
incq %r13
movq %r13, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq 0x18(%rax), %rdi
movq $0x0, 0x18(%rax)
testq %rdi, %rdi
je 0x4a23
callq 0x4f66
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rdi
cmpq %rdi, %rax
je 0x4a45
addq $-0x8, %rax
jmp 0x4a73
movq %r15, %rdi
movq %r12, %rsi
callq 0x44d0
jmp 0x4a78
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 0x4962
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 0x4af7
cmpq %r15, %r12
sete %bl
movq %rsp, %rdi
callq 0x431e
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x44e2(%rip), %rsi      # 0x8fe0
cmpq %rdx, %rcx
jne 0x4b0c
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 0x3db4
movq 0x58(%rsp), %rdx
leaq 0x17a7(%rip), %rsi      # 0x62dc
movq %r14, %rdi
xorl %eax, %eax
callq 0x2230
movq 0x58(%rsp), %rdi
cmpq %r13, %rdi
je 0x4ad8
callq 0x2180
jmp 0x4ad8
jmp 0x4b54
jmp 0x4b54
movq %rax, %rbx
movq %rsp, %rdi
callq 0x431e
movq %rbx, %rdi
callq 0x22a0
nop