// File: /workspace/github_c_makefile_star2_O0/sadistech[P]nesromtool2/./src/subcommands/prg.c
// Function: subcommand_prg_extract
void subcommand_prg_extract(prg_opts *opts) {
  // now we're ready to write this stuff out.
  FILE *ifile = fopen(opts->romfile_path, "r");
  if (!ifile) {
    fprintf(stderr, "Failed to open file: %s\n", opts->romfile_path);
    exit(EXIT_FAILURE);
  }

  nrt_header *header = NRT_HEADER_ALLOC;
  nrt_header_extract(ifile, header);

  if (! nrt_validate_header(header)) {
    fprintf(stderr, "Rom file does not look valid: %s\n", opts->romfile_path);
    fclose(ifile);
    free(header);
    free(opts);
    exit(EXIT_FAILURE);
  }

  free(header);

  nrt_prgbank *prg = NRT_PRG_ALLOC;
  if (!nrt_extract_prg(ifile, opts->bank_index, prg)) {
    fprintf(stderr, "Failed to read PRG bank %d from %s\n", opts->bank_index, opts->romfile_path);
    fclose(ifile);
    free(prg);
    free(opts);
    exit(EXIT_FAILURE);
  }

  fclose(ifile);

  // ok, now let's write out the PRG
  FILE *ofile = fopen(opts->outfile_path, "w");

  if (!ofile) {
    perror("Failed to open file for writing");
    exit(EXIT_FAILURE);
  }

  if (opts->format == prg_raw_format) {
    // just write out the data to the file.

    fwrite(prg, NRT_PRG_BANK_SIZE, 1, ofile);

    fclose(ofile);
    free(prg);
  }

  free(opts);
  exit(EXIT_SUCCESS);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x31ed(%rip), %rsi      # 0x8744
callq 0x22a0
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x558d
movq 0x6a8a(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0x411f(%rip), %rsi      # 0x969b
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2310
movl $0x10, %edi
callq 0x2220
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2690
movq -0x18(%rbp), %rdi
callq 0x26d0
testb $0x1, %al
jne 0x55f6
movq 0x6a3c(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0x40ea(%rip), %rsi      # 0x96b4
movb $0x0, %al
callq 0x21d0
movq -0x10(%rbp), %rdi
callq 0x20e0
movq -0x18(%rbp), %rdi
callq 0x2040
movq -0x8(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x4000, %edi           # imm = 0x4000
callq 0x2220
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x404(%rax), %esi
movq -0x20(%rbp), %rdx
callq 0x27e0
cmpl $0x0, %eax
jne 0x5674
movq 0x69c8(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x404(%rax), %edx
movq -0x8(%rbp), %rcx
leaq 0x408e(%rip), %rsi      # 0x96d6
movb $0x0, %al
callq 0x21d0
movq -0x10(%rbp), %rdi
callq 0x20e0
movq -0x20(%rbp), %rdi
callq 0x2040
movq -0x8(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movq -0x10(%rbp), %rdi
callq 0x20e0
movq -0x8(%rbp), %rdi
addq $0x808, %rdi            # imm = 0x808
leaq 0x3f85(%rip), %rsi      # 0x9614
callq 0x22a0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x56b5
leaq 0x4054(%rip), %rdi      # 0x96fa
callq 0x22b0
movl $0x1, %edi
callq 0x2310
movq -0x8(%rbp), %rax
cmpl $0x1, 0x400(%rax)
jne 0x56eb
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x4000, %esi           # imm = 0x4000
movl $0x1, %edx
callq 0x2320
movq -0x28(%rbp), %rdi
callq 0x20e0
movq -0x20(%rbp), %rdi
callq 0x2040
movq -0x8(%rbp), %rdi
callq 0x2040
xorl %edi, %edi
callq 0x2310
nopl (%rax,%rax)