// File: /workspace/github_c_makefile_star2_O2/sadistech[P]nesromtool2/./src/tilec_main.c
// Function: tilec
void tilec(tilec_opts *opts) {
  char *infile_path = NULL;
  FILE *ifile = NULL;
  FILE *varfile = fopen(opts->varfile, "w");

  int i;

  nrt_chrbank *chr = NRT_CHR_ALLOC;
  int bank_cursor = 0;

  // the tile that we're currently working with
  nrt_tile *tile = NRT_TILE_ALLOC;

  nrt_tile_bitmap *bitmaps = (nrt_tile_bitmap*)calloc(NRT_TILE_TABLE_SIZE, sizeof(nrt_tile_bitmap));

  printf("Will write %s\n", opts->outfile);

  while ((infile_path = *(opts->argv++))) {
    printf("Processing: %s\n", infile_path);
    char *varname = filename_no_ext(basename(infile_path));

    // convert file into tiles

    // first, open file for reading
    if (!(ifile = fopen(infile_path, "r"))) {
      perror(infile_path);
      exit(EXIT_FAILURE);
    }

    // start converting
    nrt_tile_bitmap_lockup *lockup = (nrt_tile_bitmap_lockup*)calloc(1, sizeof(nrt_tile_bitmap_lockup));

    // read the bitmap
    if (! nrt_png_to_tiles(ifile, lockup)) {
      fprintf(stderr, "Something went wrong with reading the png.\n");
      fclose(ifile);
      exit(EXIT_FAILURE);
    }

    printf("Read lockup: %d tiles and width: %d\n", lockup->count, lockup->width);

    // now let's move them to the bank
    for (i = 0; i < lockup->count; i++) {
      nrt_bitmap_to_tile(&lockup->bitmaps[i], tile);

      if (is_in_bank(chr, bank_cursor, tile)) {
        fprintf(varfile, "%s_%i = $%02x\n", varname, i, last_offset);
        fprintf(stderr, "Tile %d exists, skipping...\n", i);
        continue;
      }

      fprintf(stderr, "Tile %d does not exist. adding!\n", i);

      fprintf(varfile, "%s_%i = $%02x\n", varname, i, bank_cursor);
      // it was not in the bank, let's append.
      memcpy(&(chr->tile[bank_cursor]), tile, NRT_TILE_SIZE);
      bank_cursor++;

      if (bank_cursor > NRT_TILE_TABLE_SIZE) {
        fprintf(stderr, "TOO MANY SPRITES (%d)\n", bank_cursor);
        exit(EXIT_FAILURE);
      }
    }

    free(lockup->bitmaps);
    free(lockup);
  }

  // convert back into bitmaps
  for (i = 0; i < NRT_TILE_TABLE_SIZE; i++) {
    nrt_tile_to_bitmap(&chr->tile[i], &bitmaps[i]);
  }

  free(chr);

  // now let's write that file.

  FILE *ofile = fopen(opts->outfile, "w");

  if (!ofile) {
    perror(opts->outfile);
    exit(EXIT_FAILURE);
  }

  // write to file
  nrt_tiles_to_png(bitmaps, NRT_TILE_TABLE_SIZE, 16, ofile);

  fclose(ofile);

  printf("Done.\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq 0x20f8(%rip), %rsi      # 0x7614
callq 0x22a0
movq %rax, 0x8(%rsp)
pushq $0x1
popq %r14
movl $0x2000, %esi           # imm = 0x2000
movq %r14, %rdi
callq 0x21b0
movq %rax, 0x10(%rsp)
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x21b0
movq %rax, %r15
pushq $0x40
popq %rsi
movl $0x100, %edi            # imm = 0x100
callq 0x21b0
movq %rax, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
leaq 0x1000(%rbx), %rsi
leaq 0x2348(%rip), %rdi      # 0x78b7
movq %rsi, 0x20(%rsp)
xorl %eax, %eax
callq 0x2140
xorl %ebp, %ebp
movq 0x10(%rsp), %rbx
movq 0x30(%rsp), %rdx
movq 0x2008(%rdx), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x2008(%rdx)
movq (%rax), %r13
testq %r13, %r13
je 0x5706
leaq 0x231a(%rip), %rdi      # 0x78c6
movq %r13, %rsi
xorl %eax, %eax
callq 0x2140
movq %r13, %rdi
callq 0x21f0
movq %rax, %rdi
callq 0x3222
movq %rax, 0x18(%rsp)
movq %r13, %rdi
leaq 0x116f(%rip), %rsi      # 0x6744
callq 0x22a0
testq %rax, %rax
je 0x5792
movq %rax, %r12
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0x21b0
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
callq 0x29a1
testb %al, %al
je 0x579c
movl (%r13), %esi
movl 0x4(%r13), %edx
leaq 0x22ec(%rip), %rdi      # 0x7902
xorl %eax, %eax
callq 0x2140
xorl %r14d, %r14d
xorl %r12d, %r12d
movslq (%r13), %rax
movq 0x8(%r13), %rdi
cmpq %rax, %r12
jge 0x56f4
addq %r14, %rdi
movq %r15, %rsi
callq 0x2714
movq %rbx, %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x57e7
testb %al, %al
je 0x568f
movl 0x639d(%rip), %r8d      # 0xb9f4
movq 0x8(%rsp), %rdi
leaq 0x22c4(%rip), %rsi      # 0x7927
movq 0x18(%rsp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x21e0
movq 0x597f(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x22b3(%rip), %rsi      # 0x7936
movl %r12d, %edx
xorl %eax, %eax
callq 0x21e0
jmp 0x56e8
movq 0x5962(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x22b3(%rip), %rsi      # 0x7953
movl %r12d, %edx
xorl %eax, %eax
callq 0x21e0
movq 0x8(%rsp), %rdi
leaq 0x2271(%rip), %rsi      # 0x7927
movq 0x18(%rsp), %rdx
movl %r12d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x21e0
movslq %ebp, %rax
shlq $0x4, %rax
movups (%r15), %xmm0
movups %xmm0, (%rbx,%rax)
leal 0x1(%rbp), %edx
cmpl $0x100, %ebp            # imm = 0x100
jge 0x57c2
movl %edx, %ebp
incq %r12
addq $0x40, %r14
jmp 0x5623
callq 0x2040
movq %r13, %rdi
callq 0x2040
jmp 0x5582
xorl %r14d, %r14d
movq 0x28(%rsp), %r15
movq %r15, %rbx
movq 0x10(%rsp), %r12
cmpq $0x1000, %r14           # imm = 0x1000
je 0x5735
leaq (%r12,%r14), %rdi
movq %rbx, %rsi
callq 0x26a0
addq $0x10, %r14
addq $0x40, %rbx
jmp 0x5716
movq %r12, %rdi
callq 0x2040
leaq 0x1ed0(%rip), %rsi      # 0x7614
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x22a0
testq %rax, %rax
je 0x57e2
movq %rax, %rbx
pushq $0x10
popq %rdx
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rax, %rcx
callq 0x2765
movq %rbx, %rdi
callq 0x2100
leaq 0x2212(%rip), %rdi      # 0x7991
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20b0
movq %r13, %rdi
callq 0x22b0
jmp 0x57da
movq 0x5855(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x2129(%rip), %rdi      # 0x78d6
pushq $0x2b
popq %rsi
pushq $0x1
popq %rdx
callq 0x2320
movq %r12, %rdi
callq 0x2100
jmp 0x57da
movq 0x582f(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x21a1(%rip), %rsi      # 0x7974
xorl %eax, %eax
callq 0x21e0
pushq $0x1
popq %rdi
callq 0x2310
movq %r14, %rdi
jmp 0x5795