// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rdi, 0x10(%rsp)
movb $0x0, 0xf(%rsp)
leaq 0x40(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
cmpq %rdx, %rsi
sete %r13b
je 0x35a5
movq %rdx, %r14
movq %rsi, %rbp
leaq 0x2e3a(%rip), %r12      # 0x6280
leaq 0x38(%rsp), %r15
movzbl (%rbp), %eax
cmpl $0x3b, %eax
jg 0x347d
addl $-0x2b, %eax
cmpl $0x3, %eax
ja 0x3595
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq %r15, %rdi
leaq 0xf(%rsp), %rsi
callq 0x3b4a
movb $0x1, %cl
jmp 0x3534
cmpl $0x5b, %eax
je 0x34ac
cmpl $0x3e, %eax
je 0x3576
cmpl $0x3c, %eax
jne 0x3595
movb 0xf(%rsp), %al
leal -0x1(%rax), %ecx
movb %cl, 0xf(%rsp)
cmpb $-0x7e, %al
jl 0x3585
jmp 0x3595
movq %r15, %rsi
leaq 0xf(%rsp), %rdx
movq %rbx, %rcx
callq 0x3784
movq %r15, %r12
leaq 0x1(%rbp), %r15
cmpq %r14, %r15
setne %al
je 0x34f1
movl $0x1, %ecx
movzbl (%r15), %edx
cmpl $0x5d, %edx
je 0x34e2
cmpl $0x5b, %edx
jne 0x34e6
incl %ecx
jmp 0x34e6
decl %ecx
je 0x34f4
incq %r15
cmpq %r14, %r15
setne %al
jne 0x34d0
xorl %r15d, %r15d
testb $0x1, %al
je 0x35d7
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x324e
movq %r15, %rbp
testb %al, %al
movq %r12, %r15
leaq 0x2d62(%rip), %r12      # 0x6280
jne 0x3595
jmp 0x35b7
movq %r15, %rdi
leaq 0xf(%rsp), %rsi
callq 0x3b4a
movb $-0x1, %cl
addb %cl, (%rax)
jmp 0x3595
movq %r15, %rsi
leaq 0xf(%rsp), %rdx
movq %rbx, %rcx
callq 0x3784
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x36f8
jmp 0x3595
movq %r15, %rsi
leaq 0xf(%rsp), %rdx
movq %rbx, %rcx
callq 0x3784
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x363c
jmp 0x3595
movb 0xf(%rsp), %al
leal 0x1(%rax), %ecx
movb %cl, 0xf(%rsp)
cmpb $0x7e, %al
jl 0x3595
movq %r15, %rsi
leaq 0xf(%rsp), %rdx
movq %rbx, %rcx
callq 0x3784
incq %rbp
cmpq %r14, %rbp
sete %r13b
jne 0x344b
leaq 0x38(%rsp), %rsi
leaq 0xf(%rsp), %rdx
movq %rbx, %rcx
callq 0x3784
leaq 0x38(%rsp), %rdi
callq 0x3bae
andb $0x1, %r13b
movl %r13d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5a02(%rip), %rax      # 0x8fe0
movq (%rax), %rbx
leaq 0x28(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x3e22
movq 0x18(%rsp), %rdx
leaq 0x2cd6(%rip), %rsi      # 0x62dc
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2230
movq 0x18(%rsp), %rdi
cmpq %r15, %rdi
je 0x35b7
callq 0x2180
jmp 0x35b7
jmp 0x3627
jmp 0x3627
jmp 0x3627
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3bae
movq %rbx, %rdi
callq 0x22a0