// File: /workspace/github_cpp_makefile_star2_O1/brianquinlan[P]brainfuck-jit/bf_compile_and_go.cpp
// Function: BrainfuckCompileAndGo::init(__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>>>)
bool BrainfuckCompileAndGo::init(string::const_iterator start,
                                 string::const_iterator end) {
  string code(START, sizeof(START) - 1);
  code += "\xeb";  // relative jump;
  code += sizeof(EXIT) - 1;
  exit_offset_ = code.size();
  code += string(EXIT, sizeof(EXIT) - 1);

  if (!generate_sequence_code(start, end, &code)) {
    return false;
  }
  add_jmp_to_exit(&code);

  executable_size_ = (code.size() /
                      sysconf(_SC_PAGESIZE) + 1) * sysconf(_SC_PAGESIZE);

  executable_ = mmap(
      NULL,
      executable_size_,
      PROT_READ | PROT_WRITE,
      MAP_PRIVATE | MAP_ANON, -1, 0);
  if (executable_ == MAP_FAILED) {
    fprintf(stderr, "Error making memory executable: %s\n", strerror(errno));
    return false;
  }

  memmove(executable_, code.data(), code.size());
  if (mprotect(executable_, executable_size_, PROT_EXEC | PROT_READ) != 0) {
    fprintf(stderr, "mprotect failed: %s\n", strerror(errno));
    return false;
  }

  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x18(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x28(%rsp), %r12
movq $0x17, (%r12)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x22c0
movq %rax, (%r13)
movq (%r12), %rcx
movq %rcx, 0x10(%r13)
movaps 0x2a24(%rip), %xmm0     # 0x6370
movups %xmm0, (%rax)
movabsq $-0x3c76b33276b72977, %rdx # imm = 0xC3894CCD8948D689
movq %rdx, 0xf(%rax)
movq %rcx, 0x8(%r13)
movq (%r13), %rax
movb $0x0, (%rax,%rcx)
leaq 0x29a0(%rip), %rsi      # 0x6310
movq %r13, %rdi
callq 0x22e0
leaq 0x8(%rsp), %rdi
movl $0xc, %esi
callq 0x2170
leaq 0x8(%rsp), %rdi
movl 0x8(%rdi), %eax
movl %eax, 0x18(%rbx)
leaq 0x38(%rsp), %r12
movq %r12, -0x10(%r12)
movabsq $0x415e415d5bd88948, %rax # imm = 0x415E415D5BD88948
movq %rax, (%r12)
movl $0xc35c415d, 0x8(%r12)  # imm = 0xC35C415D
movq $0xc, -0x8(%r12)
movb $0x0, 0xc(%r12)
movl $0xc, %edx
movq %r12, %rsi
callq 0x20a0
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x39de
callq 0x2180
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x33f4
testb %al, %al
je 0x3a9d
movl 0x18(%rbx), %esi
leaq 0x8(%rsp), %rdx
callq 0x31c8
movq 0x10(%rsp), %r14
movl $0x1e, %edi
callq 0x2110
movq %rax, %rcx
movq %r14, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r14
incq %r14
movl $0x1e, %edi
callq 0x2110
imulq %r14, %rax
movl %eax, 0x8(%rbx)
movslq %eax, %rsi
xorl %edi, %edi
movl $0x3, %edx
movl $0x22, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
callq 0x22b0
movq %rax, 0x10(%rbx)
cmpq $-0x1, %rax
je 0x3a91
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rax, %rdi
callq 0x2270
movq 0x10(%rbx), %rdi
movslq 0x8(%rbx), %rsi
movl $0x5, %edx
callq 0x2260
movb $0x1, %bl
testl %eax, %eax
je 0x3a9f
leaq 0x28b4(%rip), %rdi      # 0x6343
jmp 0x3a98
leaq 0x2887(%rip), %rdi      # 0x631f
callq 0x2495
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x3aae
callq 0x2180
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x3ad6
callq 0x2180
jmp 0x3ad6
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x3ae5
callq 0x2180
movq %rbx, %rdi
callq 0x22a0
nop