// File: /workspace/github_c_makefile_star2_O2/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), %rbp
movq %rdi, 0x10(%rsp)
leaq 0x4(%rdi), %r15
movq %r15, %rdi
callq 0x5ae3
leaq 0x20(%rsp), %r12
movq %r15, 0x8(%rsp)
testq %rbp, %rbp
je 0x57ff
movq %rbp, %rdi
callq 0x5b5b
movq %rbp, %rdi
callq 0x2210
movq %rax, %rdi
callq 0x3242
movq %rax, %r14
cmpb $0x0, (%r15)
je 0x56cc
movq %r12, %rdi
leaq 0x22d2(%rip), %rsi      # 0x7999
movq %r15, %rdx
jmp 0x56e1
movq %rbp, %rdi
callq 0x2170
movq %r12, %rdi
leaq 0x22bb(%rip), %rsi      # 0x7999
movq %rax, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2320
leaq 0x22b1(%rip), %rdi      # 0x79a3
movq %rbp, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x2140
movq %rbp, %rdi
leaq 0x103b(%rip), %rsi      # 0x6744
callq 0x22c0
testq %rax, %rax
je 0x5813
movq %rax, %r14
movq %rbx, 0x18(%rsp)
movl $0x2000, %esi           # imm = 0x2000
pushq $0x1
popq %rdi
callq 0x21c0
movq %rax, %r12
movq %rbp, %rdi
callq 0x5c3e
movl %eax, %ebp
movslq %eax, %r13
movq %r12, %rdi
pushq $0x10
popq %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x20c0
cmpq %r13, %rax
jne 0x582c
movq %r14, %rdi
callq 0x2100
movq %r13, %rdi
pushq $0x40
popq %rsi
callq 0x21c0
testl %ebp, %ebp
movl $0x0, %r15d
cmovgl %ebp, %r15d
movq %rax, %r13
shlq $0x4, %r15
movq %rax, %r14
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x579c
leaq (%r12,%rbx), %rdi
movq %r14, %rsi
callq 0x26c0
addq $0x10, %rbx
addq $0x40, %r14
jmp 0x5781
movq %r12, %rdi
callq 0x2040
leaq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0x1e61(%rip), %rsi      # 0x7614
callq 0x22c0
testq %rax, %rax
movq 0x18(%rsp), %rbx
je 0x5818
movq %rax, %r14
movq 0x10(%rsp), %rax
movl 0x1004(%rax), %edx
movq %r13, %rdi
movl %ebp, %esi
movq %r14, %rcx
callq 0x2785
movq %r14, %rdi
callq 0x2100
movq %r13, %rdi
callq 0x2040
movq 0x8(%rbx), %rbp
addq $0x8, %rbx
movq 0x8(%rsp), %r15
jmp 0x5693
xorl %eax, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %rdi
jmp 0x5825
movq %r13, %rdi
callq 0x2040
leaq 0x20(%rsp), %rdi
callq 0x22d0
jmp 0x5858
movq %r12, %rdi
callq 0x2040
movq %r14, %rdi
callq 0x2100
movq 0x57b5(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x216d(%rip), %rdi      # 0x79ba
pushq $0x31
popq %rsi
pushq $0x1
popq %rdx
callq 0x2340
pushq $0x1
popq %rdi
callq 0x2330