// File: 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:
subq $0x148, %rsp            # imm = 0x148
movq %rsi, 0x138(%rsp)
movq %rdx, 0x130(%rsp)
movq %rdi, 0x128(%rsp)
movq %rcx, 0x120(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x120(%rsp), %rdi
callq 0x31c0
movl %eax, 0x11c(%rsp)
leaq 0xf7(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x33d0
movq 0x50(%rsp), %rcx
leaq 0xe8a2(%rip), %rsi      # 0x14238
leaq 0xf8(%rsp), %rdi
movl $0x3, %edx
callq 0x32b0
jmp 0x59aa
movq 0x120(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x32f0
jmp 0x59c1
leaq 0xf8(%rsp), %rdi
callq 0x3160
leaq 0xf7(%rsp), %rdi
callq 0x3280
movq 0x120(%rsp), %rdi
callq 0x31c0
movl %eax, 0xe0(%rsp)
leaq 0xbf(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x33d0
movq 0x40(%rsp), %rdx
leaq 0x8989(%rip), %rsi      # 0xe396
leaq 0xc0(%rsp), %rdi
callq 0x3320
jmp 0x5a1c
movq 0x120(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x32f0
jmp 0x5a33
leaq 0xc0(%rsp), %rdi
callq 0x3160
leaq 0xbf(%rsp), %rdi
callq 0x3280
leaq 0x138(%rsp), %rdi
movl $0x1, %esi
callq 0x6c80
movq 0x48(%rsp), %rdi
movq %rax, 0xb0(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x120(%rsp), %rcx
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
callq 0x5d40
xorb $-0x1, %al
testb $0x1, %al
jne 0x5aa4
jmp 0x5b43
movb $0x0, 0x147(%rsp)
jmp 0x5d14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0x5ae8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x3160
leaq 0xf7(%rsp), %rdi
callq 0x3280
jmp 0x5d25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0x5b31
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x3160
leaq 0xbf(%rsp), %rdi
callq 0x3280
jmp 0x5d25
movq 0x48(%rsp), %rdi
movl 0x11c(%rsp), %esi
movq 0x120(%rsp), %rdx
callq 0x5820
leaq 0x87(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x33d0
movq 0x38(%rsp), %rdx
leaq 0x8823(%rip), %rsi      # 0xe39d
leaq 0x88(%rsp), %rdi
callq 0x3320
jmp 0x5b89
leaq 0x87(%rsp), %rdi
callq 0x3280
movq 0x120(%rsp), %rdi
callq 0x31c0
movq %rax, 0x28(%rsp)
movl 0xe0(%rsp), %eax
movl %eax, 0x24(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x31c0
movl 0x24(%rsp), %ecx
movq %rax, %rdx
movq 0x28(%rsp), %rax
addl %edx, %ecx
subl %ecx, %eax
addl $-0x4, %eax
movl %eax, 0x80(%rsp)
leaq 0x5f(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x33d0
movq 0x30(%rsp), %rcx
leaq 0x60(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl $0x4, %edx
callq 0x32b0
jmp 0x5c07
leaq 0x88(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x32f0
jmp 0x5c1b
leaq 0x60(%rsp), %rdi
callq 0x3160
leaq 0x5f(%rsp), %rdi
callq 0x3280
movq 0x120(%rsp), %rax
movq %rax, 0x8(%rsp)
movslq 0xe0(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x88(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x31c0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rcx
movq %rax, %rdx
callq 0x30e0
jmp 0x5c74
movb $0x1, 0x147(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x3160
jmp 0x5d14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0x87(%rsp), %rdi
callq 0x3280
jmp 0x5d25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
jmp 0x5ce5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x3160
leaq 0x5f(%rsp), %rdi
callq 0x3280
jmp 0x5d05
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x3160
jmp 0x5d25
movb 0x147(%rsp), %al
andb $0x1, %al
addq $0x148, %rsp            # imm = 0x148
retq
movq 0xe8(%rsp), %rdi
callq 0x33c0
nopw %cs:(%rax,%rax)