// File: brianquinlan[P]brainfuck-jit/bf_main.cpp
// Function: run_brainfuck_program(BrainfuckRunner*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
int run_brainfuck_program(BrainfuckRunner* runner,
                          const string& source_file_path) {
  FILE *bf_source_file = fopen(source_file_path.c_str(), "rb");
  if (bf_source_file == NULL) {
    fprintf(stderr, "Could not open file \"%s\": %s\n",
            source_file_path.c_str(), strerror(errno));
    return 1;
  }

  if (fseek(bf_source_file, 0, SEEK_END)) {
    fprintf(stderr, "Could not seek in \"%s\": %s\n",
            source_file_path.c_str(), strerror(ferror(bf_source_file)));
    return 1;
  }

  long source_size = ftell(bf_source_file);
  if (source_size == -1) {
    fprintf(stderr, "Could not tell in \"%s\": %s\n",
            source_file_path.c_str(), strerror(errno));
    return 1;
  }
  rewind(bf_source_file);

  char* source_buffer = reinterpret_cast<char *>(malloc(source_size));
  size_t amount_read = fread(source_buffer, 1, source_size, bf_source_file);
  fclose(bf_source_file);
  if (amount_read != static_cast<size_t>(source_size)) {
    fprintf(stderr, "Error reading file \"%s\": %s\n",
            source_file_path.c_str(), strerror(errno));
    return 1;
  }

  void* memory = calloc(kBrainfuckMemorySize, 1);
  if (memory == NULL) {
    fprintf(stderr,
            "Unable to allocate memory %ld bytes for Brainfuck memory\n",
            static_cast<unsigned long>(kBrainfuckMemorySize));
  }

  const string source(source_buffer, source_size);

  if (!runner->init(source.begin(), source.end())) {
    return 1;
  }

  runner->run(bf_read, NULL, bf_write, NULL, memory);
  return 0;
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0xf8(%rsp)
movq %rsi, 0xf0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x3100
movq %rax, %rdi
leaq 0xaa96(%rip), %rsi      # 0xe004
callq 0x32d0
movq %rax, 0xe8(%rsp)
cmpq $0x0, 0xe8(%rsp)
jne 0x35de
movq 0x10a53(%rip), %rax     # 0x13fe0
movq (%rax), %rax
movq %rax, 0x68(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x3100
movq %rax, 0x70(%rsp)
callq 0x3040
movl (%rax), %edi
callq 0x30c0
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rdx
movq %rax, %rcx
leaq 0xaa40(%rip), %rsi      # 0xe007
movb $0x0, %al
callq 0x3360
movl $0x1, 0x104(%rsp)
jmp 0x3944
movq 0xe8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x32a0
cmpl $0x0, %eax
je 0x3659
movq 0x109e0(%rip), %rax     # 0x13fe0
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x3100
movq %rax, 0x60(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x3400
movl %eax, %edi
callq 0x30c0
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rdx
movq %rax, %rcx
leaq 0xa9e3(%rip), %rsi      # 0xe025
movb $0x0, %al
callq 0x3360
movl $0x1, 0x104(%rsp)
jmp 0x3944
movq 0xe8(%rsp), %rdi
callq 0x3030
movq %rax, 0xe0(%rsp)
cmpq $-0x1, 0xe0(%rsp)
jne 0x36d1
movq 0x10960(%rip), %rax     # 0x13fe0
movq (%rax), %rax
movq %rax, 0x48(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x3100
movq %rax, 0x50(%rsp)
callq 0x3040
movl (%rax), %edi
callq 0x30c0
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rdx
movq %rax, %rcx
leaq 0xa987(%rip), %rsi      # 0xe041
movb $0x0, %al
callq 0x3360
movl $0x1, 0x104(%rsp)
jmp 0x3944
movq 0xe8(%rsp), %rdi
callq 0x3060
movq 0xe0(%rsp), %rdi
callq 0x3310
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
movq 0xe0(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movl $0x1, %esi
callq 0x3380
movq %rax, 0xd0(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x3240
movq 0xd0(%rsp), %rax
cmpq 0xe0(%rsp), %rax
je 0x3794
movq 0x1089d(%rip), %rax     # 0x13fe0
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x3100
movq %rax, 0x40(%rsp)
callq 0x3040
movl (%rax), %edi
callq 0x30c0
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rdx
movq %rax, %rcx
leaq 0xa8e0(%rip), %rsi      # 0xe05d
movb $0x0, %al
callq 0x3360
movl $0x1, 0x104(%rsp)
jmp 0x3944
movl $0x100000, %edi         # imm = 0x100000
movl $0x1, %esi
callq 0x3190
movq %rax, 0xc8(%rsp)
cmpq $0x0, 0xc8(%rsp)
jne 0x37d3
movq 0x10823(%rip), %rax     # 0x13fe0
movq (%rax), %rdi
leaq 0xa8b3(%rip), %rsi      # 0xe07a
movl $0x100000, %edx         # imm = 0x100000
movb $0x0, %al
callq 0x3360
movq 0xd8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0xa7(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x33d0
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
leaq 0xa8(%rsp), %rdi
callq 0x32b0
jmp 0x381d
leaq 0xa7(%rsp), %rdi
callq 0x3280
movq 0xf8(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xa8(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x3220
movq 0x8(%rsp), %rdi
movq %rax, 0x88(%rsp)
callq 0x3410
movq 0x10(%rsp), %rdi
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rsi
movq 0x80(%rsp), %rdx
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
movb %al, 0x1f(%rsp)
jmp 0x3886
movb 0x1f(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3892
jmp 0x38f3
movl $0x1, 0x104(%rsp)
movl $0x1, 0x7c(%rsp)
jmp 0x3937
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xa7(%rsp), %rdi
callq 0x3280
jmp 0x3953
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x3160
jmp 0x3953
movq 0xf8(%rsp), %rdi
movq 0xc8(%rsp), %r9
movq (%rdi), %rax
movq 0x8(%rax), %rax
leaq 0x4f(%rip), %rsi        # 0x3960
leaq 0x88(%rip), %rcx        # 0x39a0
xorl %edx, %edx
movl %edx, %r8d
movq %r8, %rdx
callq *%rax
jmp 0x3924
movl $0x0, 0x104(%rsp)
movl $0x1, 0x7c(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x3160
movl 0x104(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq
movq 0x98(%rsp), %rdi
callq 0x33c0