// File: /workspace/github_c_makefile_star2_O0/sadistech[P]nesromtool2/./src/subcommands/chr.c
// Function: subcommand_chr_extract
void subcommand_chr_extract(chr_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);
    free(opts);
    exit(EXIT_FAILURE);
  }

  // read in the header
  // validate header
  // validate that bank exists
  // extract the bank
  // write the output file

  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);
  }

  nrt_chrbank *chr = NRT_CHR_ALLOC;
  if (!nrt_extract_chr(ifile, header, opts->bank_index, chr)) {
    fprintf(stderr, "Failed to read CHR bank %d from %s\n", opts->bank_index, opts->romfile_path);
    fclose(ifile);
    free(header);
    free(chr);
    free(opts);
    exit(EXIT_FAILURE);
  }

  fclose(ifile);
  free(header);

  FILE *ofile = fopen(opts->outfile_path, "w");

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

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

    fwrite(chr, NRT_CHR_BANK_SIZE, 1, ofile);

    fclose(ofile);
    free(chr);
  } else if (opts->format == png_format) {
    // write out the PNG
    // first, create bitmaps, then write.

    nrt_tile_bitmap *bitmaps = (nrt_tile_bitmap*)malloc(NRT_CHR_TILE_COUNT * sizeof(nrt_tile_bitmap));
    int i;
    for (i = 0; i < NRT_CHR_TILE_COUNT; i++) {
      nrt_tile_to_bitmap(&chr->tile[i], &bitmaps[i]);
    }

    nrt_tiles_to_png(bitmaps, NRT_CHR_TILE_COUNT, opts->width, ofile);
  }

  exit(EXIT_SUCCESS);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x277d(%rip), %rsi      # 0x8744
callq 0x22a0
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x6006
movq 0x601a(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0x36af(%rip), %rsi      # 0x969b
movb $0x0, %al
callq 0x21d0
movq -0x8(%rbp), %rdi
callq 0x2040
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 0x606f
movq 0x5fc3(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0x3671(%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
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x21a0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x8(%rbp), %rax
movl 0x408(%rax), %edx
movq -0x20(%rbp), %rcx
callq 0x3040
cmpl $0x0, %eax
jne 0x60f6
movq 0x5f4f(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x408(%rax), %edx
movq -0x8(%rbp), %rcx
leaq 0x36fe(%rip), %rsi      # 0x97bf
movb $0x0, %al
callq 0x21d0
movq -0x10(%rbp), %rdi
callq 0x20e0
movq -0x18(%rbp), %rdi
callq 0x2040
movq -0x20(%rbp), %rdi
callq 0x2040
movq -0x8(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movq -0x10(%rbp), %rdi
callq 0x20e0
movq -0x18(%rbp), %rdi
callq 0x2040
movq -0x8(%rbp), %rdi
addq $0x80c, %rdi            # imm = 0x80C
leaq 0x34fa(%rip), %rsi      # 0x9614
callq 0x22a0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x6140
leaq 0x35c9(%rip), %rdi      # 0x96fa
callq 0x22b0
movl $0x1, %edi
callq 0x2310
movq -0x8(%rbp), %rax
cmpl $0x1, 0x400(%rax)
jne 0x6178
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x2000, %esi           # imm = 0x2000
movl $0x1, %edx
callq 0x2320
movq -0x28(%rbp), %rdi
callq 0x20e0
movq -0x20(%rbp), %rdi
callq 0x2040
jmp 0x61ef
movq -0x8(%rbp), %rax
cmpl $0x2, 0x400(%rax)
jne 0x61ed
movl $0x8000, %edi           # imm = 0x8000
callq 0x2220
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x200, -0x34(%rbp)     # imm = 0x200
jge 0x61d1
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x30(%rbp), %rsi
movslq -0x34(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
callq 0x2830
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x619a
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x404(%rax), %edx
movq -0x28(%rbp), %rcx
movl $0x200, %esi            # imm = 0x200
callq 0x29b0
jmp 0x61ef
xorl %edi, %edi
callq 0x2310
nopw %cs:(%rax,%rax)