// File: /workspace/github_cpp_makefile_star2_O3/brianquinlan[P]brainfuck-jit/bf_compile_and_go.cpp
// Function: BrainfuckCompileAndGo::generate_loop_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_loop_code(string::const_iterator start,
                                               string::const_iterator end,
                                               string* code) {
  // Converts a Brainfuck command sequence like this:
  // [<code>]
  // Into this:
  // loop_start:
  //   cmpb   [rbx],0
  //   je     loop_end
  //   <code>
  //   jmp    loop_start
  // loop_end:
  //

  int loop_start = code->size();
  *code += string(LOOP_CMP, sizeof(LOOP_CMP) - 1);

  int jump_start = code->size();
  *code += string("\xde\xad\xbe\xef\xde\xad");  // Reserve 6 bytes for je.

  if (!generate_sequence_code(start+1, end, code)) {
    return false;
  }

  add_jmp_to_offset(loop_start, code);  // Jump back to the start of the loop.

  string jump_to_end = "\x0f\x84";                              // je ...
  uint32_t relative_end_of_loop = code->size() -
      (jump_start + jump_to_end.size() + 4);
  jump_to_end += string(
      reinterpret_cast<char *>(&relative_end_of_loop), 4);      // ... loop_end

  code->replace(jump_start, jump_to_end.size(), jump_to_end);
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbp
movl 0x8(%rcx), %r15d
leaq 0x10(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movzwl 0x5f68(%rip), %eax      # 0x9190
movw %ax, (%rbx)
movb 0x5f61(%rip), %al       # 0x9192
movb %al, 0x2(%rbx)
movq $0x3, -0x8(%rbx)
movb $0x0, 0x3(%rbx)
movl $0x3, %edx
movq %rcx, %rdi
movq %rbx, %rsi
callq 0x20a0
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x325e
callq 0x2180
movq 0x8(%r14), %rax
movq %rax, 0x20(%rsp)
movq %rsp, %rdi
movq %rbx, (%rdi)
leaq 0x3048(%rip), %rsi      # 0x62bc
leaq 0x3047(%rip), %rdx      # 0x62c2
callq 0x2d4c
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rdi
callq 0x20a0
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x329f
callq 0x2180
incq %r13
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x33a0
movl %eax, %ebp
testb %al, %al
je 0x3357
movl %r15d, %esi
movq %r14, %rdx
callq 0x3174
movq %rsp, %rdi
movq %rbx, (%rdi)
leaq 0x2fee(%rip), %rsi      # 0x62c3
leaq 0x2fe9(%rip), %rdx      # 0x62c5
callq 0x2d4c
movl 0x8(%r14), %eax
movq %rsp, %rdi
movl 0x8(%rdi), %ecx
movq 0x20(%rsp), %r12
addl %r12d, %ecx
negl %ecx
addl %ecx, %eax
addl $-0x4, %eax
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
movl %eax, (%r15)
movq $0x4, -0x8(%r15)
movb $0x0, 0x4(%r15)
movl $0x4, %edx
movq %r15, %rsi
callq 0x20a0
movslq %r12d, %r12
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x3332
callq 0x2180
movq (%rsp), %rcx
movq 0x8(%rsp), %rdx
movq %r14, %rdi
movq %r12, %rsi
movq %rdx, %r8
callq 0x21e0
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x3357
callq 0x2180
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3387
movq %rax, %r14
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x338a
callq 0x2180
jmp 0x338a
jmp 0x3382
jmp 0x3387
movq %rax, %r14
jmp 0x3398
movq %rax, %r14
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x3398
callq 0x2180
movq %r14, %rdi
callq 0x22a0