// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x40, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1da9(%rip), %rsi      # 0x52a0
leaq 0x1db9(%rip), %rdx      # 0x52b7
movq %rsp, %r12
movq %r12, %rdi
callq 0x2afe
leaq 0x1e1d(%rip), %rsi      # 0x532d
movq %r12, %rdi
callq 0x22f0
movq %rsp, %rdi
pushq $0xc
popq %rsi
callq 0x2180
movq %rsp, %rdi
movl 0x8(%rdi), %eax
movl %eax, 0x18(%rbx)
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
movabsq $0x415e415d5bd88948, %rcx # imm = 0x415E415D5BD88948
movq %rcx, (%rax)
movl $0xc35c415d, 0x8(%rax)  # imm = 0xC35C415D
movq $0xc, -0x8(%rax)
movb $0x0, 0xc(%rax)
leaq 0x20(%rsp), %rsi
callq 0x2300
leaq 0x20(%rsp), %rdi
callq 0x20f0
movq %rsp, %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x304e
testb %al, %al
je 0x360c
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x2ee2
movq 0x8(%rsp), %r14
pushq $0x1e
popq %rbp
movl %ebp, %edi
callq 0x2120
movq %rax, %rcx
movq %r14, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r14
incq %r14
movl %ebp, %edi
callq 0x2120
imulq %r14, %rax
movl %eax, 0x8(%rbx)
movslq %eax, %rsi
pushq $0x3
popq %rdx
pushq $0x22
popq %rcx
pushq $-0x1
popq %r8
xorl %edi, %edi
xorl %r9d, %r9d
callq 0x22c0
movq %rax, 0x10(%rbx)
cmpq $-0x1, %rax
je 0x3625
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rax, %rdi
callq 0x2290
movq 0x10(%rbx), %rdi
movslq 0x8(%rbx), %rsi
pushq $0x5
popq %rdx
callq 0x2280
movb $0x1, %bl
testl %eax, %eax
je 0x360e
leaq 0x1d56(%rip), %r14      # 0x5360
jmp 0x362c
xorl %ebx, %ebx
movq %rsp, %rdi
callq 0x20f0
movl %ebx, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d10(%rip), %r14      # 0x533c
movq 0x59ad(%rip), %rax      # 0x8fe0
movq (%rax), %r15
callq 0x2040
movl (%rax), %edi
callq 0x20a0
xorl %ebx, %ebx
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2250
jmp 0x360e
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x20f0
jmp 0x3668
movq %rax, %rbx
movq %rsp, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x22b0