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

  validate_outdir(opts->outdir);

  while (filename) {
    validate_pngfile(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.chr", dirname(filename), base_filename);
    } else {
      // the output path is set to somethign
      sprintf(output_path, "%s/%s.chr", opts->outdir, base_filename);
    }

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

    // read in png to bitmap
    // convert to chr
    // write out to file

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

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

    nrt_tile_bitmap_lockup *lockup = (nrt_tile_bitmap_lockup*)calloc(opts->size, sizeof(nrt_tile_bitmap_lockup));

    if (!nrt_png_to_tiles(pngfile, lockup)) {
      fprintf(stderr, "Failed to read PNG file: %s\n", filename);
      fclose(pngfile);
      exit(EXIT_FAILURE);
    }

    fclose(pngfile);

    if (lockup->count > opts->size) {
      fprintf(stderr, "Incoming image is larger than the expected size (%d > %d tiles)\n", lockup->count, opts->size);
      free(lockup->bitmaps);
      free(lockup);
      exit(EXIT_FAILURE);
    }

    nrt_chrbank *chr_bank = NRT_CHR_ALLOC;
    nrt_tile *raw_tile = NRT_TILE_ALLOC;

    // convert the bitmap into CHR stuff
    int i;
    for (i = 0; i < lockup->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);

    // write this out.
    FILE *outfile = fopen(output_path, "w");

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

    if (fwrite(chr_bank, NRT_TILE_SIZE, opts->size, outfile) != opts->size) {
      free(chr_bank);
      fclose(outfile);
      perror(output_path);
      exit(EXIT_FAILURE);
    }

    fclose(outfile);
    free(chr_bank);

    // 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 %rdi, %r14
movq (%rdx), %r12
leaq 0x4(%rdi), %r13
movq %r13, %rdi
callq 0x6446
testq %r12, %r12
je 0x63f7
leaq 0x20(%rsp), %rbp
movq %r13, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq %r12, %rdi
callq 0x64dc
movq %r12, %rdi
callq 0x2210
movq %rax, %rdi
callq 0x3acc
movq %rax, %r15
cmpb $0x0, (%r13)
je 0x6272
movq %rbp, %rdi
leaq 0x277f(%rip), %rsi      # 0x89ec
movq %r13, %rdx
jmp 0x6287
movq %r12, %rdi
callq 0x2170
movq %rbp, %rdi
leaq 0x2768(%rip), %rsi      # 0x89ec
movq %rax, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2320
leaq 0x270b(%rip), %rdi      # 0x89a3
movq %r12, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2140
movq %r12, %rdi
leaq 0x1495(%rip), %rsi      # 0x7744
callq 0x22c0
testq %rax, %rax
je 0x640b
movq %rax, %r13
movslq 0x1008(%r14), %rdi
movl $0x10, %esi
callq 0x21c0
movq %rax, %rbp
movq %r13, %rdi
movq %rax, %rsi
callq 0x3138
testb %al, %al
je 0x643b
movq %rbx, 0x18(%rsp)
movq %r13, %rdi
callq 0x2100
movl (%rbp), %r15d
movl 0x1008(%r14), %esi
cmpl %esi, %r15d
jg 0x6413
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x21c0
movq %rax, %r14
movl $0x1, %edi
movl $0x10, %esi
callq 0x21c0
movq %rax, %r13
testl %r15d, %r15d
jle 0x6366
xorl %r15d, %r15d
movq %r14, %r12
xorl %ebx, %ebx
movq 0x8(%rbp), %rdi
addq %r15, %rdi
movq %r13, %rsi
callq 0x2e8b
movups (%r13), %xmm0
movups %xmm0, (%r12)
incq %rbx
movslq (%rbp), %rax
addq $0x10, %r12
addq $0x40, %r15
cmpq %rax, %rbx
jl 0x6339
movq %r13, %rdi
callq 0x2040
movq 0x8(%rbp), %rdi
callq 0x2040
movq %rbp, %rdi
callq 0x2040
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
leaq 0x2286(%rip), %rsi      # 0x8614
callq 0x22c0
testq %rax, %rax
je 0x641e
movq %rax, %r13
movq 0x8(%rsp), %rbx
movslq 0x1008(%rbx), %rdx
movl $0x10, %esi
movq %r14, %rdi
movq %rax, %rcx
callq 0x2340
movslq 0x1008(%rbx), %rcx
cmpq %rcx, %rax
jne 0x642b
movq %r13, %rdi
callq 0x2100
movq %r14, %rdi
callq 0x2040
movq 0x18(%rsp), %rbx
movq 0x8(%rbx), %r12
addq $0x8, %rbx
testq %r12, %r12
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r13
jne 0x6241
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 0x29fe
movl %r15d, %edi
movq %rbp, %rdx
callq 0x29c5
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x29ac
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2985
movq %r12, %rdi
movq %r13, %rsi
callq 0x2956