// File: 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:
subq $0xe8, %rsp
movq %rsi, 0xd8(%rsp)
movq %rdx, 0xd0(%rsp)
movq %rdi, 0xc8(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0xa7(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x33d0
movq 0x48(%rsp), %rcx
leaq 0x7b36(%rip), %rsi      # 0xe350
leaq 0xa8(%rsp), %rdi
movl $0x17, %edx
callq 0x32b0
jmp 0x682e
leaq 0xa7(%rsp), %rdi
callq 0x3280
leaq 0x7b99(%rip), %rsi      # 0xe3db
leaq 0xa8(%rsp), %rdi
callq 0x3230
jmp 0x6851
leaq 0xa8(%rsp), %rdi
movl $0xc, %esi
callq 0x3170
jmp 0x6865
leaq 0xa8(%rsp), %rdi
callq 0x31c0
movq %rax, %rcx
movq 0x40(%rsp), %rax
movl %ecx, 0x18(%rax)
leaq 0x6f(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x33d0
movq 0x38(%rsp), %rcx
leaq 0x7ad0(%rip), %rsi      # 0xe368
leaq 0x70(%rsp), %rdi
movl $0xc, %edx
callq 0x32b0
jmp 0x68a9
leaq 0xa8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x32f0
jmp 0x68bd
leaq 0x70(%rsp), %rdi
callq 0x3160
leaq 0x6f(%rsp), %rdi
callq 0x3280
movq 0x40(%rsp), %rdi
movq 0xd8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
callq 0x5d40
movb %al, 0x37(%rsp)
jmp 0x690d
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x69b2
jmp 0x691b
movb $0x0, 0xe7(%rsp)
movl $0x1, 0x54(%rsp)
jmp 0x6b42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xa7(%rsp), %rdi
callq 0x3280
jmp 0x6b6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
jmp 0x6b60
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
jmp 0x69a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x3160
leaq 0x6f(%rsp), %rdi
callq 0x3280
jmp 0x6b60
movq 0x40(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x5900
jmp 0x69c6
leaq 0xa8(%rsp), %rdi
callq 0x31c0
movq %rax, 0x20(%rsp)
movl $0x1e, %edi
callq 0x3180
movq %rax, %rcx
movq 0x20(%rsp), %rax
xorl %edx, %edx
divq %rcx
addq $0x1, %rax
movq %rax, 0x28(%rsp)
movl $0x1e, %edi
callq 0x3180
movq 0x28(%rsp), %rcx
movq %rax, %rdx
movq 0x40(%rsp), %rax
imulq %rdx, %rcx
movl %ecx, 0x8(%rax)
movslq 0x8(%rax), %rsi
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x22, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq %r9, %rdi
callq 0x33e0
movq %rax, %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x10(%rax)
movq $-0x1, %rcx
cmpq %rcx, 0x10(%rax)
jne 0x6a96
movq 0xd589(%rip), %rax      # 0x13fe0
movq (%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x3040
movl (%rax), %edi
callq 0x30c0
movq 0x18(%rsp), %rdi
movq %rax, %rdx
leaq 0x7963(%rip), %rsi      # 0xe3dd
movb $0x0, %al
callq 0x3360
movb $0x0, 0xe7(%rsp)
movl $0x1, 0x54(%rsp)
jmp 0x6b42
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x3300
movq %rax, 0x10(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x31c0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
callq 0x33a0
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rdi
movslq 0x8(%rax), %rsi
movl $0x5, %edx
callq 0x3390
cmpl $0x0, %eax
je 0x6b32
movq 0xd4e8(%rip), %rax      # 0x13fe0
movq (%rax), %rax
movq %rax, (%rsp)
callq 0x3040
movl (%rax), %edi
callq 0x30c0
movq (%rsp), %rdi
movq %rax, %rdx
leaq 0x78e8(%rip), %rsi      # 0xe401
movb $0x0, %al
callq 0x3360
movb $0x0, 0xe7(%rsp)
movl $0x1, 0x54(%rsp)
jmp 0x6b42
movb $0x1, 0xe7(%rsp)
movl $0x1, 0x54(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x3160
movb 0xe7(%rsp), %al
andb $0x1, %al
addq $0xe8, %rsp
retq
leaq 0xa8(%rsp), %rdi
callq 0x3160
movq 0x98(%rsp), %rdi
callq 0x33c0
nopw (%rax,%rax)