// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x21a0
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x2, 0x400(%rax)
jne 0x6360
movq -0x8(%rbp), %rdi
addq $0x40c, %rdi            # imm = 0x40C
leaq 0x2502(%rip), %rsi      # 0x8744
callq 0x22a0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x6271
leaq 0x358a(%rip), %rdi      # 0x97e3
callq 0x22b0
movq -0x8(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movl $0x200, %edi            # imm = 0x200
movl $0x10, %esi
callq 0x21a0
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x2c60
testb $0x1, %al
jne 0x62d4
movq 0x5d5c(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
addq $0x40c, %rdx            # imm = 0x40C
leaq 0x354e(%rip), %rsi      # 0x97ff
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x20e0
movq -0x8(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movl $0x1, %edi
movl $0x10, %esi
callq 0x21a0
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x200, -0x2c(%rbp)     # imm = 0x200
jge 0x633f
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
movslq -0x2c(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
callq 0x28f0
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x62ee
movq -0x28(%rbp), %rdi
callq 0x2040
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2040
movq -0x20(%rbp), %rdi
callq 0x2040
jmp 0x6387
movq -0x8(%rbp), %rax
cmpl $0x1, 0x400(%rax)
jne 0x6385
leaq 0x34ab(%rip), %rdi      # 0x981f
movb $0x0, %al
callq 0x2120
movl $0x1, %edi
callq 0x2310
jmp 0x6387
movq -0x8(%rbp), %rdi
addq $0x80c, %rdi            # imm = 0x80C
leaq 0x23ab(%rip), %rsi      # 0x8744
callq 0x22a0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x63bf
leaq 0x33bf(%rip), %rdi      # 0x976f
callq 0x22b0
movl $0x1, %edi
callq 0x2310
movq -0x38(%rbp), %rdi
callq 0x39b0
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x20e0
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x408(%rax), %esi
callq 0x37a0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rdi
callq 0x2040
movq -0x10(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
leaq 0x6422(%rip), %rdi      # 0xc830
callq 0x2250
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %edi
leaq 0x31f4(%rip), %rsi      # 0x9614
callq 0x2270
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x3b20
movq -0x58(%rbp), %rdi
callq 0x20e0
movq -0x40(%rbp), %rdi
callq 0x3970
movq -0x8(%rbp), %rsi
addq $0x80c, %rsi            # imm = 0x80C
leaq 0x63d6(%rip), %rdi      # 0xc830
callq 0x22c0
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop