// File: brianquinlan[P]brainfuck-jit/bf_compile_and_go.cpp
// Function: BrainfuckCompileAndGo::emit_offset_table(std::map<signed char, unsigned char, std::less<signed char>, std::allocator<std::pair<signed char const, unsigned char>>>*, signed char*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*)
void BrainfuckCompileAndGo::emit_offset_table(
    map<int8_t, uint8_t>* offset_to_change, int8_t* offset, string *code) {
  for (auto it = offset_to_change->begin();
       it != offset_to_change->end();
       ++it) {
    int8_t change_offset = it->first;
    uint8_t change_value = it->second;

    if (change_value == 0) {
      continue;
    }

    *code += "\x80\x43";                                  // addb [rbx+XX],YY
    *code += string(reinterpret_cast<char *>(&change_offset), 1);  // XX
    *code += string(reinterpret_cast<char *>(&change_value), 1);   // YY
  }

  if (*offset != 0) {
    *code += "\x48\x83\xc3";                                // add rbx ...
    *code += string(reinterpret_cast<char *>(offset), 1);   // ... offset
    *offset = 0;
  }
  offset_to_change->clear();
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq %rdx, 0xd0(%rsp)
movq %rcx, 0xc8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x6cc0
movq %rax, 0xc0(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x6d10
movq %rax, 0xb8(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x6cf0
testb $0x1, %al
jne 0x643f
jmp 0x65dd
leaq 0xc0(%rsp), %rdi
callq 0x6d40
movb (%rax), %al
movb %al, 0xb7(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x6d40
movb 0x1(%rax), %al
movb %al, 0xb6(%rsp)
movzbl 0xb6(%rsp), %eax
cmpl $0x0, %eax
jne 0x647e
jmp 0x654b
movq 0xc8(%rsp), %rdi
leaq 0x7f13(%rip), %rsi      # 0xe3a0
callq 0x3230
leaq 0x8f(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x33d0
movq 0x20(%rsp), %rcx
leaq 0x90(%rsp), %rdi
leaq 0xb7(%rsp), %rsi
movl $0x1, %edx
callq 0x32b0
jmp 0x64c5
movq 0xc8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x32f0
jmp 0x64dc
leaq 0x90(%rsp), %rdi
callq 0x3160
leaq 0x8f(%rsp), %rdi
callq 0x3280
leaq 0x57(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x33d0
movq 0x18(%rsp), %rcx
leaq 0x58(%rsp), %rdi
leaq 0xb6(%rsp), %rsi
movl $0x1, %edx
callq 0x32b0
jmp 0x6523
movq 0xc8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x32f0
jmp 0x6537
leaq 0x58(%rsp), %rdi
callq 0x3160
leaq 0x57(%rsp), %rdi
callq 0x3280
leaq 0xc0(%rsp), %rdi
callq 0x6d60
jmp 0x640c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x658e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x3160
leaq 0x8f(%rsp), %rdi
callq 0x3280
jmp 0x66c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x65ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x3160
leaq 0x57(%rsp), %rdi
callq 0x3280
jmp 0x66c0
movq 0xd0(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x66ab
movq 0xc8(%rsp), %rdi
leaq 0x7da3(%rip), %rsi      # 0xe3a3
callq 0x3230
movq 0xd0(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x2f(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x33d0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rcx
leaq 0x30(%rsp), %rdi
movl $0x1, %edx
callq 0x32b0
jmp 0x663c
movq 0xc8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x32f0
jmp 0x6650
leaq 0x30(%rsp), %rdi
callq 0x3160
leaq 0x2f(%rsp), %rdi
callq 0x3280
movq 0xd0(%rsp), %rax
movb $0x0, (%rax)
jmp 0x66ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x669f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x3160
leaq 0x2f(%rsp), %rdi
callq 0x3280
jmp 0x66c0
movq 0xd8(%rsp), %rdi
callq 0x6d90
addq $0xe8, %rsp
retq
movq 0x80(%rsp), %rdi
callq 0x33c0
nopl (%rax)