// File: /workspace/github_cpp_makefile_star2_O2/brianquinlan[P]brainfuck-jit/bf_compile_and_go.cpp
// Function: BrainfuckCompileAndGo::generate_sequence_code(__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>>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*)
bool BrainfuckCompileAndGo::generate_sequence_code(string::const_iterator start,
                                                   string::const_iterator end,
                                                   string* code) {
  int8_t offset = 0;
  // Maps offset relative to the datapointer into the amount to change it.
  map<int8_t, uint8_t> offset_to_change;

  for (string::const_iterator it = start; it != end; ++it) {
    switch (*it) {
      case '<':
        --offset;
        if (offset == 127 || offset == -128) {
          emit_offset_table(&offset_to_change, &offset, code);
        }
        break;
      case '>':
        ++offset;
        if (offset == 127 || offset == -128) {
          emit_offset_table(&offset_to_change, &offset, code);
        }
        break;
      case '-':
        offset_to_change[offset] -= 1;
        break;
      case '+':
        offset_to_change[offset] += 1;
        break;
      case ',':
        emit_offset_table(&offset_to_change, &offset, code);
        generate_read_code(code);
        break;
      case '.':
        emit_offset_table(&offset_to_change, &offset, code);
        generate_write_code(code);
        break;
      case '[':
        emit_offset_table(&offset_to_change, &offset, code);
        string::const_iterator loop_end;
        if (!find_loop_end(it, end, &loop_end)) {
          fprintf(
              stderr,
              "Unable to find loop end in block starting with: %s\n",
              string(it, end).c_str());
          return false;
        }
        if (!generate_loop_code(it, loop_end, code)) {
          return false;
        }
        it = loop_end;
        break;
    }
  }
  // The offset table must be emitted because this function is called
  // recursively to handle loops.
  emit_offset_table(&offset_to_change, &offset, code);
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, 0x10(%rsp)
leaq 0xf(%rsp), %r13
leaq 0x20(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movb $0x0, (%r13)
leaq 0x21eb(%rip), %rbp      # 0x5280
cmpq %rbx, %r12
je 0x3216
movzbl (%r12), %eax
leal -0x2b(%rax), %ecx
cmpl $0x3, %ecx
jbe 0x30cb
cmpl $0x5b, %eax
je 0x3129
cmpl $0x3e, %eax
je 0x30e6
cmpl $0x3c, %eax
jne 0x311b
movb 0xf(%rsp), %al
leal -0x1(%rax), %ecx
movb %cl, 0xf(%rsp)
cmpb $-0x7e, %al
jl 0x30f5
jmp 0x311b
movslq (%rbp,%rcx,4), %rax
addq %rbp, %rax
jmpq *%rax
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x36f6
movb $0x1, %cl
jmp 0x3119
movb 0xf(%rsp), %al
leal 0x1(%rax), %ecx
movb %cl, 0xf(%rsp)
cmpb $0x7e, %al
jl 0x311b
leaq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x3396
movq %r12, %r14
jmp 0x311e
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x36f6
movb $-0x1, %cl
addb %cl, (%rax)
movq %r12, %r14
incq %r14
movq %r14, %r12
jmp 0x3095
leaq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x3396
movq %r12, %r14
pushq $0x1
popq %rax
incq %r14
cmpq %rbx, %r14
je 0x31d0
movzbl (%r14), %ecx
incq %r14
cmpl $0x5d, %ecx
je 0x3160
cmpl $0x5b, %ecx
jne 0x3142
incl %eax
jmp 0x3164
decl %eax
je 0x31b0
decq %r14
jmp 0x313f
leaq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x3396
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x331a
movq %r12, %r14
jmp 0x311e
leaq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x3396
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x3264
movq %r12, %r14
jmp 0x311e
decq %r14
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2eee
testb %al, %al
jne 0x311e
jmp 0x3228
movq 0x5e09(%rip), %rax      # 0x8fe0
movq (%rax), %r14
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3a2a
leaq 0x48(%rsp), %r15
movq (%r15), %rdx
leaq 0x20f7(%rip), %rsi      # 0x52f9
movq %r14, %rdi
xorl %eax, %eax
callq 0x2250
movq %r15, %rdi
callq 0x20f0
jmp 0x3228
leaq 0x18(%rsp), %rsi
leaq 0xf(%rsp), %rdx
movq %r15, %rcx
callq 0x3396
cmpq %rbx, %r12
sete %bl
leaq 0x18(%rsp), %rdi
callq 0x3744
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x324f
jmp 0x324f
jmp 0x324f
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x3744
movq %rbx, %rdi
callq 0x22b0