// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1060, %rsp           # imm = 0x1060
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4, %rdi
callq 0x7510
cmpq $0x0, -0x20(%rbp)
je 0x715f
movq -0x20(%rbp), %rdi
callq 0x75c0
movq -0x20(%rbp), %rdi
callq 0x21f0
movq %rax, %rdi
callq 0x3ca0
movq %rax, -0x1028(%rbp)
movq -0x8(%rbp), %rax
movsbl 0x4(%rax), %eax
cmpl $0x0, %eax
jne 0x6f3f
leaq -0x1020(%rbp), %rax
movq %rax, -0x1060(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2150
movq -0x1060(%rbp), %rdi
movq %rax, %rdx
movq -0x1028(%rbp), %rcx
leaq 0x2a78(%rip), %rsi      # 0x99ae
movb $0x0, %al
callq 0x2300
jmp 0x6f63
leaq -0x1020(%rbp), %rdi
movq -0x8(%rbp), %rdx
addq $0x4, %rdx
movq -0x1028(%rbp), %rcx
leaq 0x2a52(%rip), %rsi      # 0x99ae
movb $0x0, %al
callq 0x2300
movq -0x20(%rbp), %rsi
leaq -0x1020(%rbp), %rdx
leaq 0x2a43(%rip), %rdi      # 0x99b8
movb $0x0, %al
callq 0x2120
movq -0x20(%rbp), %rdi
leaq 0x17bd(%rip), %rsi      # 0x8744
callq 0x22a0
movq %rax, -0x1030(%rbp)
cmpq $0x0, -0x1030(%rbp)
jne 0x6fb0
movq -0x20(%rbp), %rdi
callq 0x22b0
movl $0x1, %edi
callq 0x2310
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x21a0
movq %rax, -0x1038(%rbp)
movq -0x20(%rbp), %rdi
callq 0x76f0
movl %eax, -0x103c(%rbp)
movq -0x1038(%rbp), %rdi
movslq -0x103c(%rbp), %rdx
movq -0x1030(%rbp), %rcx
movl $0x10, %esi
callq 0x20a0
movslq -0x103c(%rbp), %rcx
cmpq %rcx, %rax
je 0x703a
movq -0x1038(%rbp), %rdi
callq 0x2040
movq -0x1030(%rbp), %rdi
callq 0x20e0
movq 0x4fd9(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x29a6(%rip), %rsi      # 0x99cf
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2310
movq -0x1030(%rbp), %rdi
callq 0x20e0
movslq -0x103c(%rbp), %rdi
movl $0x40, %esi
callq 0x21a0
movq %rax, -0x1048(%rbp)
movl $0x0, -0x104c(%rbp)
movl -0x104c(%rbp), %eax
cmpl -0x103c(%rbp), %eax
jge 0x70b6
movq -0x1038(%rbp), %rdi
movslq -0x104c(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x1048(%rbp), %rsi
movslq -0x104c(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
callq 0x2830
movl -0x104c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104c(%rbp)
jmp 0x7068
movq -0x1038(%rbp), %rdi
callq 0x2040
leaq -0x1020(%rbp), %rdi
leaq 0x2544(%rip), %rsi      # 0x9614
callq 0x22a0
movq %rax, -0x1058(%rbp)
cmpq $0x0, -0x1058(%rbp)
jne 0x7108
movq -0x1048(%rbp), %rdi
callq 0x2040
leaq -0x1020(%rbp), %rdi
callq 0x22b0
movl $0x1, %edi
callq 0x2310
movq -0x1048(%rbp), %rdi
movl -0x103c(%rbp), %esi
movq -0x8(%rbp), %rax
movl 0x1004(%rax), %edx
movq -0x1058(%rbp), %rcx
callq 0x29b0
movq -0x1058(%rbp), %rdi
callq 0x20e0
movq -0x1048(%rbp), %rdi
callq 0x2040
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x18(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x6ece
xorl %eax, %eax
addq $0x1060, %rsp           # imm = 0x1060
popq %rbp
retq
nopw (%rax,%rax)