// File: /workspace/github_c_makefile_star2_O0/sadistech[P]nesromtool2/./src/subcommands/prg.c
// Function: subcommand_prg_replace
void subcommand_prg_replace(prg_opts *opts) {
  nrt_prgbank *prg_bank = NRT_PRG_ALLOC;

  if (opts->format == prg_raw_format) {
    // it's a raw file, so let's read that in.
    FILE *ifile = fopen(opts->sourcefile_path, "r");
    if (!ifile) {
      perror("Failed to open source file");
      free(prg_bank);
      free(opts);
      exit(EXIT_FAILURE);
    }

    // read in the data.
    if (fread(prg_bank, NRT_PRG_BANK_SIZE, 1, ifile) != 1) {
      perror("Failed to read prg bank from file");
      fclose(ifile);
      free(prg_bank);
      free(opts);
      exit(EXIT_FAILURE);
    }

    fclose(ifile);
  } else {
    fprintf(stderr, "Unsupported format: %d\n", opts->format);
  }

  static char tempfile_path[] = "/tmp/nrt-rom.XXXXXX";

  // ok, let's read in the rom file, and replace the chr
  FILE *romfile = fopen(opts->romfile_path, "r");

  if (!romfile) {
    perror("Failed to open rom file for reading");
    exit(EXIT_FAILURE);
  }

  nrt_rom *rom = nrt_read_rom_from_file(romfile);
  fclose(romfile);

  nrt_prg_listitem *bank_listitem = nrt_prg_listitem_at(rom->prgs, opts->bank_index);
  free(bank_listitem->prg);
  bank_listitem->prg = prg_bank;

  int fd = mkstemp(tempfile_path);
  FILE *tempfile = fdopen(fd, "w");

  nrt_rom_write_to_file(rom, tempfile);

  fclose(tempfile);
  nrt_rom_free(rom);

  rename(tempfile_path, opts->outfile_path);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x4000, %edi           # imm = 0x4000
callq 0x2220
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x1, 0x400(%rax)
jne 0x57ce
movq -0x8(%rbp), %rdi
addq $0x408, %rdi            # imm = 0x408
leaq 0x3007(%rip), %rsi      # 0x8744
callq 0x22a0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x5775
leaq 0x3fc6(%rip), %rdi      # 0x971a
callq 0x22b0
movq -0x10(%rbp), %rdi
callq 0x2040
movq -0x8(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl $0x4000, %esi           # imm = 0x4000
movl $0x1, %edx
callq 0x20a0
cmpq $0x1, %rax
je 0x57c3
leaq 0x3f9c(%rip), %rdi      # 0x9735
callq 0x22b0
movq -0x18(%rbp), %rdi
callq 0x20e0
movq -0x10(%rbp), %rdi
callq 0x2040
movq -0x8(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movq -0x18(%rbp), %rdi
callq 0x20e0
jmp 0x57f0
movq 0x6823(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x400(%rax), %edx
leaq 0x3f6e(%rip), %rsi      # 0x9757
movb $0x0, %al
callq 0x21d0
movq -0x8(%rbp), %rdi
leaq 0x2f49(%rip), %rsi      # 0x8744
callq 0x22a0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x5821
leaq 0x3f5d(%rip), %rdi      # 0x976f
callq 0x22b0
movl $0x1, %edi
callq 0x2310
movq -0x20(%rbp), %rdi
callq 0x39b0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x20e0
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x404(%rax), %esi
callq 0x3410
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rdi
callq 0x2040
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
leaq 0x6ea0(%rip), %rdi      # 0xc710
callq 0x2250
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %edi
leaq 0x3d92(%rip), %rsi      # 0x9614
callq 0x2270
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3b20
movq -0x40(%rbp), %rdi
callq 0x20e0
movq -0x28(%rbp), %rdi
callq 0x3970
movq -0x8(%rbp), %rsi
addq $0x808, %rsi            # imm = 0x808
leaq 0x6e54(%rip), %rdi      # 0xc710
callq 0x22c0
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)