// File: /workspace/github_c_makefile_star2_O1/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 0x283d(%rip), %rsi      # 0x8614
callq 0x22a0
movq %rax, 0x8(%rsp)
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x21b0
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movl $0x10, %esi
callq 0x21b0
movq %rax, %rbp
movl $0x100, %edi            # imm = 0x100
movl $0x40, %esi
callq 0x21b0
movq %rax, 0x28(%rsp)
leaq 0x1000(%rbx), %rsi
leaq 0x2a8e(%rip), %rdi      # 0x88b7
movq %rsi, 0x20(%rsp)
xorl %eax, %eax
callq 0x2140
movq 0x2008(%rbx), %rax
leaq 0x8(%rax), %rcx
movq %rbx, 0x30(%rsp)
movq %rcx, 0x2008(%rbx)
movq (%rax), %r12
testq %r12, %r12
je 0x5fed
xorl %ebx, %ebx
movq 0x10(%rsp), %r14
leaq 0x2a60(%rip), %rdi      # 0x88c6
movq %r12, %rsi
xorl %eax, %eax
callq 0x2140
movq %r12, %rdi
callq 0x21f0
movq %rax, %rdi
callq 0x3904
movq %rax, 0x18(%rsp)
movq %r12, %rdi
leaq 0x18b5(%rip), %rsi      # 0x7744
callq 0x22a0
testq %rax, %rax
je 0x6075
movq %rax, %r15
movl $0x1, %edi
movl $0x10, %esi
callq 0x21b0
movq %rax, %r13
movq %r15, %rdi
movq %rax, %rsi
callq 0x2f36
testb %al, %al
je 0x607d
movl (%r13), %esi
movl 0x4(%r13), %edx
leaq 0x2a2e(%rip), %rdi      # 0x8902
xorl %eax, %eax
callq 0x2140
cmpl $0x0, (%r13)
jle 0x5fb9
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x8(%r13), %rdi
addq %r15, %rdi
movq %rbp, %rsi
callq 0x2c7b
movq %r14, %rdi
movl %ebx, %esi
movq %rbp, %rdx
callq 0x6092
testb %al, %al
je 0x5f4b
movl 0x6ae1(%rip), %r8d      # 0xc9f4
movq 0x8(%rsp), %rdi
leaq 0x2a08(%rip), %rsi      # 0x8927
movq 0x18(%rsp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x21e0
movq 0x60c3(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x29f7(%rip), %rsi      # 0x8936
movl %r12d, %edx
xorl %eax, %eax
callq 0x21e0
jmp 0x5fa5
movq 0x60a6(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x29f7(%rip), %rsi      # 0x8953
movl %r12d, %edx
xorl %eax, %eax
callq 0x21e0
movq 0x8(%rsp), %rdi
leaq 0x29b5(%rip), %rsi      # 0x8927
movq 0x18(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x21e0
movslq %ebx, %rax
shlq $0x4, %rax
movups (%rbp), %xmm0
movups %xmm0, (%r14,%rax)
leal 0x1(%rbx), %edi
cmpl $0x100, %ebx            # imm = 0x100
jge 0x6085
movl %edi, %ebx
incq %r12
movslq (%r13), %rax
addq $0x40, %r15
cmpq %rax, %r12
jl 0x5eec
movq 0x8(%r13), %rdi
callq 0x2040
movq %r13, %rdi
callq 0x2040
movq 0x30(%rsp), %rdx
movq 0x2008(%rdx), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x2008(%rdx)
movq (%rax), %r12
testq %r12, %r12
jne 0x5e5f
xorl %r14d, %r14d
movq 0x28(%rsp), %r15
movq %r15, %rbx
movq 0x10(%rsp), %r12
leaq (%r12,%r14), %rdi
movq %rbx, %rsi
callq 0x2c08
addq $0x40, %rbx
addq $0x10, %r14
cmpq $0x1000, %r14           # imm = 0x1000
jne 0x5ffd
movq %r12, %rdi
callq 0x2040
leaq 0x25eb(%rip), %rsi      # 0x8614
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x22a0
testq %rax, %rax
je 0x608a
movq %rax, %rbx
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movl $0x10, %edx
movq %rax, %rcx
callq 0x2cc8
movq %rbx, %rdi
callq 0x2100
leaq 0x292f(%rip), %rdi      # 0x8991
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20b0
movq %r12, %rdi
callq 0x28ef
movq %r15, %rdi
callq 0x289c
callq 0x28cc
movq %r14, %rdi
callq 0x28fd