// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rcx, %r14
movb $0x0, 0x7(%rsp)
leaq 0x30(%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
je 0x3538
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r12
leaq 0x2e94(%rip), %rbp      # 0x6280
leaq 0x28(%rsp), %r15
movzbl (%r13), %eax
cmpl $0x3b, %eax
jg 0x3425
addl $-0x2b, %eax
cmpl $0x3, %eax
ja 0x352c
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
movq %r15, %rdi
leaq 0x7(%rsp), %rsi
callq 0x3adc
movb $0x1, %cl
jmp 0x34ab
cmpl $0x5b, %eax
je 0x3454
cmpl $0x3e, %eax
je 0x34e9
cmpl $0x3c, %eax
jne 0x352c
movb 0x7(%rsp), %al
leal -0x1(%rax), %ecx
movb %cl, 0x7(%rsp)
cmpb $-0x7e, %al
jl 0x34f8
jmp 0x352c
movq %r15, %rsi
leaq 0x7(%rsp), %rdx
movq %r14, %rcx
callq 0x3716
movq %r15, %rbp
leaq 0x1(%r13), %r15
cmpq %rbx, %r15
je 0x354e
movl $0x1, %eax
movzbl (%r15), %ecx
cmpl $0x5d, %ecx
je 0x348b
cmpl $0x5b, %ecx
jne 0x348f
incl %eax
jmp 0x348f
decl %eax
je 0x350a
incq %r15
cmpq %rbx, %r15
jne 0x3479
jmp 0x354e
movq %r15, %rdi
leaq 0x7(%rsp), %rsi
callq 0x3adc
movb $-0x1, %cl
addb %cl, (%rax)
jmp 0x352c
movq %r15, %rsi
leaq 0x7(%rsp), %rdx
movq %r14, %rcx
callq 0x3716
movq %r12, %rdi
movq %r14, %rsi
callq 0x368a
jmp 0x352c
movq %r15, %rsi
leaq 0x7(%rsp), %rdx
movq %r14, %rcx
callq 0x3716
movq %r12, %rdi
movq %r14, %rsi
callq 0x35ce
jmp 0x352c
movb 0x7(%rsp), %al
leal 0x1(%rax), %ecx
movb %cl, 0x7(%rsp)
cmpb $0x7e, %al
jl 0x352c
movq %r15, %rsi
leaq 0x7(%rsp), %rdx
movq %r14, %rcx
callq 0x3716
jmp 0x352c
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x31fa
movq %r15, %r13
testb %al, %al
movq %rbp, %r15
leaq 0x2d56(%rip), %rbp      # 0x6280
je 0x3596
incq %r13
cmpq %rbx, %r13
jne 0x33f1
movb $0x1, %bl
leaq 0x28(%rsp), %rsi
leaq 0x7(%rsp), %rdx
movq %r14, %rcx
callq 0x3716
jmp 0x3598
movq 0x5a8b(%rip), %rax      # 0x8fe0
movq (%rax), %r14
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3db4
movq 0x8(%rsp), %rdx
leaq 0x2d5f(%rip), %rsi      # 0x62dc
movq %r14, %rdi
xorl %eax, %eax
callq 0x2230
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x3596
callq 0x2180
xorl %ebx, %ebx
leaq 0x28(%rsp), %rdi
callq 0x3b42
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x35b9
jmp 0x35b9
jmp 0x35b9
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x3b42
movq %rbx, %rdi
callq 0x22a0