// File: /workspace/github_c_makefile_star2_O0/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
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 0x7501
movq -0x20(%rbp), %rdi
callq 0x7650
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 0x720f
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 0x27fb(%rip), %rsi      # 0x9a01
movb $0x0, %al
callq 0x2300
jmp 0x7233
leaq -0x1020(%rbp), %rdi
movq -0x8(%rbp), %rdx
addq $0x4, %rdx
movq -0x1028(%rbp), %rcx
leaq 0x27d5(%rip), %rsi      # 0x9a01
movb $0x0, %al
callq 0x2300
movq -0x20(%rbp), %rsi
leaq -0x1020(%rbp), %rdx
leaq 0x2773(%rip), %rdi      # 0x99b8
movb $0x0, %al
callq 0x2120
movq -0x20(%rbp), %rdi
leaq 0x14ed(%rip), %rsi      # 0x8744
callq 0x22a0
movq %rax, -0x1030(%rbp)
cmpq $0x0, -0x1030(%rbp)
jne 0x7280
movq -0x20(%rbp), %rdi
callq 0x22b0
movl $0x1, %edi
callq 0x2310
movq -0x8(%rbp), %rax
movslq 0x1008(%rax), %rdi
movl $0x10, %esi
callq 0x21a0
movq %rax, -0x1038(%rbp)
movq -0x1030(%rbp), %rdi
movq -0x1038(%rbp), %rsi
callq 0x2c60
testb $0x1, %al
jne 0x72e5
movq 0x4d3e(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x2743(%rip), %rsi      # 0x9a0b
movb $0x0, %al
callq 0x21d0
movq -0x1030(%rbp), %rdi
callq 0x20e0
movl $0x1, %edi
callq 0x2310
movq -0x1030(%rbp), %rdi
callq 0x20e0
movq -0x1038(%rbp), %rax
movl (%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x1008(%rcx), %eax
jle 0x7357
movq 0x4ceb(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x1038(%rbp), %rax
movl (%rax), %edx
movq -0x8(%rbp), %rax
movl 0x1008(%rax), %ecx
leaq 0x26fe(%rip), %rsi      # 0x9a28
movb $0x0, %al
callq 0x21d0
movq -0x1038(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2040
movq -0x1038(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x21a0
movq %rax, -0x1040(%rbp)
movl $0x1, %edi
movl $0x10, %esi
callq 0x21a0
movq %rax, -0x1048(%rbp)
movl $0x0, -0x104c(%rbp)
movl -0x104c(%rbp), %eax
movq -0x1038(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x73fe
movq -0x1038(%rbp), %rax
movq 0x8(%rax), %rdi
movslq -0x104c(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rdi
movq -0x1048(%rbp), %rsi
callq 0x28f0
movq -0x1040(%rbp), %rax
movslq -0x104c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x1048(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movl -0x104c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104c(%rbp)
jmp 0x738d
movq -0x1048(%rbp), %rdi
callq 0x2040
movq -0x1038(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2040
movq -0x1038(%rbp), %rdi
callq 0x2040
leaq -0x1020(%rbp), %rdi
leaq 0x21e0(%rip), %rsi      # 0x9614
callq 0x22a0
movq %rax, -0x1058(%rbp)
cmpq $0x0, -0x1058(%rbp)
jne 0x746c
movq -0x1040(%rbp), %rdi
callq 0x2040
leaq -0x1020(%rbp), %rdi
callq 0x22b0
movl $0x1, %edi
callq 0x2310
movq -0x1040(%rbp), %rdi
movq -0x8(%rbp), %rax
movslq 0x1008(%rax), %rdx
movq -0x1058(%rbp), %rcx
movl $0x10, %esi
callq 0x2320
movq -0x8(%rbp), %rcx
movslq 0x1008(%rcx), %rcx
cmpq %rcx, %rax
je 0x74cd
movq -0x1040(%rbp), %rdi
callq 0x2040
movq -0x1058(%rbp), %rdi
callq 0x20e0
leaq -0x1020(%rbp), %rdi
callq 0x22b0
movl $0x1, %edi
callq 0x2310
movq -0x1058(%rbp), %rdi
callq 0x20e0
movq -0x1040(%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 0x719e
xorl %eax, %eax
addq $0x1060, %rsp           # imm = 0x1060
popq %rbp
retq
nopl (%rax)