// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0x21c0
movq %rax, %r14
movl 0x400(%rbx), %eax
cmpl $0x2, %eax
je 0x4d37
cmpl $0x1, %eax
jne 0x4dd4
leaq 0x2af2(%rip), %rdi      # 0x781f
callq 0x20b0
jmp 0x4ec2
leaq 0x40c(%rbx), %r12
leaq 0x19ff(%rip), %rsi      # 0x6744
movq %r12, %rdi
callq 0x22c0
testq %rax, %rax
je 0x4e89
movq %rax, %r13
pushq $0x10
popq %rsi
movl $0x200, %edi            # imm = 0x200
callq 0x21c0
movq %rax, %r15
movq %r13, %rdi
movq %rax, %rsi
callq 0x29c1
testb %al, %al
je 0x4e97
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0x21c0
movq %rax, %r12
xorl %r13d, %r13d
movq %r14, %rbp
cmpq $0x8000, %r13           # imm = 0x8000
je 0x4dbb
movq 0x8(%r15), %rdi
addq %r13, %rdi
movq %r12, %rsi
callq 0x2734
movups (%r12), %xmm0
movups %xmm0, (%rbp)
addq $0x40, %r13
addq $0x10, %rbp
jmp 0x4d90
movq %r12, %rdi
callq 0x2040
movq 0x8(%r15), %rdi
callq 0x2040
movq %r15, %rdi
callq 0x2040
leaq 0x80c(%rbx), %r15
leaq 0x1962(%rip), %rsi      # 0x6744
movq %r15, %rdi
callq 0x22c0
testq %rax, %rax
je 0x4e7b
movq %rax, %r13
movq %rax, %rdi
callq 0x305d
movq %rax, %r12
movq %r13, %rdi
callq 0x2100
movq 0x10(%r12), %rdi
movl 0x408(%rbx), %esi
callq 0x2f8a
movq %rax, %rbx
movq (%rax), %rdi
callq 0x2040
movq %r14, (%rbx)
leaq 0x6a12(%rip), %rbx      # 0xb840
movq %rbx, %rdi
callq 0x2270
leaq 0x27d7(%rip), %rsi      # 0x7614
movl %eax, %edi
callq 0x2290
movq %rax, %r14
movq %r12, %rdi
movq %rax, %rsi
callq 0x3156
movq %r14, %rdi
callq 0x2100
movq %r12, %rdi
callq 0x303e
movq %rbx, %rdi
movq %r15, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22e0
leaq 0x28ed(%rip), %rdi      # 0x776f
callq 0x22d0
jmp 0x4ec2
leaq 0x2953(%rip), %rdi      # 0x77e3
callq 0x22d0
jmp 0x4eba
movq 0x615a(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x2957(%rip), %rsi      # 0x77ff
movq %r12, %rdx
xorl %eax, %eax
callq 0x21f0
movq %r13, %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x2040
pushq $0x1
popq %rdi
callq 0x2330
nop