// File: /workspace/github_c_makefile_star2_O1/sadistech[P]nesromtool2/./src/subcommands/chr.c
// Function: subcommand_chr_replace
void subcommand_chr_replace(chr_opts *opts) {
  // now we want to read in the source file, and get the data we really want
  // then we'll read in the rom, replace the bank and write it to a tempfile
  // then move the tempfile to the final location

  nrt_chrbank *chr_bank = NRT_CHR_ALLOC;

  if (opts->format == png_format) {
    FILE *pngfile = fopen(opts->sourcefile_path, "r");
    if (!pngfile) {
      perror("Failed to open up bank file");
      free(opts);
      exit(EXIT_FAILURE);
    }

    nrt_tile_bitmap_lockup *lockup= (nrt_tile_bitmap_lockup*)calloc(NRT_CHR_TILE_COUNT, sizeof(nrt_tile_bitmap_lockup));

    if (! nrt_png_to_tiles(pngfile, lockup)) {
      fprintf(stderr, "Failed to read in PNG file: %s\n", opts->sourcefile_path);
      fclose(pngfile);
      free(opts);
      exit(EXIT_FAILURE);
    }

    // convert from bitmap to tiles in bank.
    nrt_tile *raw_tile = NRT_TILE_ALLOC;
    int i;
    for(i = 0; i < NRT_CHR_TILE_COUNT; i++) {
      nrt_bitmap_to_tile(&lockup->bitmaps[i], raw_tile);
      memcpy(&chr_bank->tile[i], raw_tile, NRT_TILE_SIZE);
    }

    free(raw_tile);
    free(lockup->bitmaps);
    free(lockup);
  } else if (opts->format == raw_format) {
    printf("Not implemented\n");
    exit(EXIT_FAILURE);
  }

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

  // ok, let's read in the rom file, and replace the chr
  FILE *romfile = fopen(opts->outfile_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_chr_listitem *bank_listitem = nrt_chr_listitem_at(rom->chrs, opts->bank_index);
  free(bank_listitem->chr);
  bank_listitem->chr = chr_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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x21c0
movq %rax, %r14
movl 0x400(%rbx), %eax
cmpl $0x2, %eax
je 0x588e
cmpl $0x1, %eax
jne 0x592f
leaq 0x2fa0(%rip), %rdi      # 0x881f
callq 0x20b0
movl $0x1, %edi
callq 0x2330
leaq 0x40c(%rbx), %r12
leaq 0x1ea8(%rip), %rsi      # 0x7744
movq %r12, %rdi
callq 0x22c0
testq %rax, %rax
je 0x59db
movq %rax, %r13
movl $0x200, %edi            # imm = 0x200
movl $0x10, %esi
callq 0x21c0
movq %rax, %r15
movq %r13, %rdi
movq %rax, %rsi
callq 0x3136
testb %al, %al
je 0x59e3
movl $0x1, %edi
movl $0x10, %esi
callq 0x21c0
movq %rax, %r12
xorl %r13d, %r13d
movq %r14, %rbp
movq 0x8(%r15), %rdi
addq %r13, %rdi
movq %r12, %rsi
callq 0x2e7b
movups (%r12), %xmm0
movups %xmm0, (%rbp)
addq $0x10, %rbp
addq $0x40, %r13
cmpq $0x8000, %r13           # imm = 0x8000
jne 0x58ed
movq %r12, %rdi
callq 0x2040
movq 0x8(%r15), %rdi
callq 0x2040
movq %r15, %rdi
callq 0x2040
leaq 0x80c(%rbx), %r15
leaq 0x1e07(%rip), %rsi      # 0x7744
movq %r15, %rdi
callq 0x22c0
testq %rax, %rax
je 0x59d6
movq %rax, %r13
movq %rax, %rdi
callq 0x38df
movq %rax, %r12
movq %r13, %rdi
callq 0x2100
movq 0x10(%r12), %rdi
movl 0x408(%rbx), %esi
callq 0x37d7
movq %rax, %rbx
movq (%rax), %rdi
callq 0x2040
movq %r14, (%rbx)
leaq 0x6eb7(%rip), %rbx      # 0xc840
movq %rbx, %rdi
callq 0x2270
leaq 0x2c7c(%rip), %rsi      # 0x8614
movl %eax, %edi
callq 0x2290
movq %rax, %r14
movq %r12, %rdi
movq %rax, %rsi
callq 0x3a0a
movq %r14, %rdi
callq 0x2100
movq %r12, %rdi
callq 0x38c0
movq %rbx, %rdi
movq %r15, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22e0
callq 0x278a
movq %rbx, %rdi
callq 0x276a
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x272d
nopl (%rax)