// File: /workspace/github_c_makefile_star2_O1/sadistech[P]nesromtool2/./src/chr2png_main.c
// Function: chr2png
int chr2png(options *opts, int argc, char **argv) {
  char *filename = argv[0];
  char output_path[PATH_MAX];

  validate_outdir(opts->outdir);

  while (filename) {
    validate_chrfile(filename);
    char *base_filename = filename_no_ext(basename(filename));

    // first let's figure out the to and from stuff.
    if (opts->outdir[0] == '\0') {
      // if the outdir is not specified, it's gonna be outpu to the same place as the original file
      // so to get the output path for each file,
      // we need to get the filename sans extension
      // and the dirname
      // and then concat dirname, basename, new extension
      sprintf(output_path, "%s/%s.png", dirname(filename), base_filename);
    } else {
      // the output path is set to somethign
      sprintf(output_path, "%s/%s.png", opts->outdir, base_filename);
    }

    printf("Will convert %s -> %s\n", filename, output_path);

    // read in chr from filesystem
    // convert to bitmap
    // write to tempfile
    // move tempfile in place.

    FILE *chrfile = fopen(filename, "r");

    if (!chrfile) {
      perror(filename);
      exit(EXIT_FAILURE);
    }

    nrt_chrbank *chr = NRT_CHR_ALLOC;
    /* nrt_chrbank *chr = (nrt_chrbank*)malloc(sizeof(nrt_chrbank)); */

    // find out how many tiles are in this chr
    // only read that much
    int tile_count = chr_tile_count(filename);

    if (fread(chr, NRT_TILE_SIZE, tile_count, chrfile) != tile_count) {
      free(chr);
      fclose(chrfile);
      fprintf(stderr, "Failed to read CHR bank due to an unknown error.\n");
      exit(EXIT_FAILURE);
    }

    fclose(chrfile);

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

    free(chr);

    FILE *outfile = fopen(output_path, "w");

    if (!outfile) {
      free(bitmaps);
      perror(output_path);
      exit(EXIT_FAILURE);
    }

    nrt_tiles_to_png(bitmaps, tile_count, opts->width, outfile);

    fclose(outfile);
    free(bitmaps);

    // get next and continue loop.
    filename = *(++argv);
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1028, %rsp           # imm = 0x1028
movq %rdx, %rbx
movq (%rdx), %r12
movq %rdi, 0x10(%rsp)
leaq 0x4(%rdi), %r15
movq %r15, %rdi
callq 0x64ab
testq %r12, %r12
je 0x623b
leaq 0x20(%rsp), %r13
movq %r15, 0x8(%rsp)
movq %r12, %rdi
callq 0x64fd
movq %r12, %rdi
callq 0x2210
movq %rax, %rdi
callq 0x3b04
movq %rax, %r14
cmpb $0x0, (%r15)
je 0x6109
movq %r13, %rdi
leaq 0x2895(%rip), %rsi      # 0x8999
movq %r15, %rdx
jmp 0x611e
movq %r12, %rdi
callq 0x2170
movq %r13, %rdi
leaq 0x287e(%rip), %rsi      # 0x8999
movq %rax, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2320
leaq 0x2874(%rip), %rdi      # 0x89a3
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2140
movq %r12, %rdi
leaq 0x15fe(%rip), %rsi      # 0x7744
callq 0x22c0
testq %rax, %rax
je 0x624f
movq %rax, %r14
movq %rbx, 0x18(%rsp)
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x21c0
movq %rax, %r15
movq %r12, %rdi
callq 0x658e
movl %eax, %r12d
movslq %eax, %r13
movl $0x10, %esi
movq %r15, %rdi
movq %r13, %rdx
movq %r14, %rcx
callq 0x20c0
cmpq %r13, %rax
jne 0x6264
movq %r14, %rdi
callq 0x2100
movl $0x40, %esi
movq %r13, %rdi
callq 0x21c0
movq %rax, %rbx
testl %r12d, %r12d
jle 0x61d3
movq %r15, %rbp
movq %rbx, %r14
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2e08
addq $0x40, %r14
addq $0x10, %rbp
decq %r13
jne 0x61bb
movq %r15, %rdi
callq 0x2040
leaq 0x20(%rsp), %r13
movq %r13, %rdi
leaq 0x242a(%rip), %rsi      # 0x8614
callq 0x22c0
testq %rax, %rax
je 0x6257
movq %rax, %r14
movq 0x10(%rsp), %rax
movl 0x1004(%rax), %edx
movq %rbx, %rdi
movl %r12d, %esi
movq %r14, %rcx
callq 0x2ec8
movq %r14, %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x2040
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %r12
addq $0x8, %rbx
testq %r12, %r12
movq 0x8(%rsp), %r15
jne 0x60d9
xorl %eax, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x2948
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x292f
movq %r15, %rdi
movq %r14, %rsi
callq 0x28fa