// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq $0x0, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x288d(%rip), %rsi      # 0x9614
callq 0x2280
movq %rax, -0x20(%rbp)
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x2190
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x1, %edi
movl $0x10, %esi
callq 0x2190
movq %rax, -0x40(%rbp)
movl $0x100, %edi            # imm = 0x100
movl $0x40, %esi
callq 0x2190
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rsi
addq $0x1000, %rsi           # imm = 0x1000
leaq 0x2ade(%rip), %rdi      # 0x98c0
movb $0x0, %al
callq 0x2120
movq -0x8(%rbp), %rcx
movq 0x2008(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x2008(%rcx)
movq (%rax), %rax
movq %rax, -0x10(%rbp)
cmpq $0x0, %rax
je 0x7014
movq -0x10(%rbp), %rsi
leaq 0x2ab1(%rip), %rdi      # 0x98cf
movb $0x0, %al
callq 0x2120
movq -0x10(%rbp), %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x3c80
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x18ff(%rip), %rsi      # 0x8744
callq 0x2280
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x6e67
movq -0x10(%rbp), %rdi
callq 0x2290
movl $0x1, %edi
callq 0x22f0
movl $0x1, %edi
movl $0x10, %esi
callq 0x2190
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x2c40
testb $0x1, %al
jne 0x6eb6
movq 0x5166(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x2a43(%rip), %rsi      # 0x98df
movb $0x0, %al
callq 0x21c0
movq -0x18(%rbp), %rdi
callq 0x20e0
movl $0x1, %edi
callq 0x22f0
movq -0x58(%rbp), %rax
movl (%rax), %esi
movq -0x58(%rbp), %rax
movl 0x4(%rax), %edx
leaq 0x2a41(%rip), %rdi      # 0x990b
movb $0x0, %al
callq 0x2120
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x58(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x6ff9
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
movslq -0x24(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rdi
movq -0x40(%rbp), %rsi
callq 0x28d0
movq -0x30(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x40(%rbp), %rdx
callq 0x70d0
testb $0x1, %al
jne 0x6f19
jmp 0x6f59
movq -0x20(%rbp), %rdi
movq -0x50(%rbp), %rdx
movl -0x24(%rbp), %ecx
movl 0x5ab9(%rip), %r8d      # 0xc9e4
leaq 0x29fe(%rip), %rsi      # 0x9930
movb $0x0, %al
callq 0x21c0
movq 0x50b8(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0x29f2(%rip), %rsi      # 0x993f
movb $0x0, %al
callq 0x21c0
jmp 0x6feb
movq 0x5098(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0x29ef(%rip), %rsi      # 0x995c
movb $0x0, %al
callq 0x21c0
movq -0x20(%rbp), %rdi
movq -0x50(%rbp), %rdx
movl -0x24(%rbp), %ecx
movl -0x34(%rbp), %r8d
leaq 0x29a6(%rip), %rsi      # 0x9930
movb $0x0, %al
callq 0x21c0
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x40(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
cmpl $0x100, -0x34(%rbp)     # imm = 0x100
jle 0x6fe9
movq 0x502d(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movl -0x34(%rbp), %edx
leaq 0x29a5(%rip), %rsi      # 0x997d
movb $0x0, %al
callq 0x21c0
movl $0x1, %edi
callq 0x22f0
jmp 0x6feb
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x6ed8
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2040
movq -0x58(%rbp), %rdi
callq 0x2040
jmp 0x6de9
movl $0x0, -0x24(%rbp)
cmpl $0x100, -0x24(%rbp)     # imm = 0x100
jge 0x7052
movq -0x30(%rbp), %rdi
movslq -0x24(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x48(%rbp), %rsi
movslq -0x24(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
callq 0x2810
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x701b
movq -0x30(%rbp), %rdi
callq 0x2040
movq -0x8(%rbp), %rdi
addq $0x1000, %rdi           # imm = 0x1000
leaq 0x25a7(%rip), %rsi      # 0x9614
callq 0x2280
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x7097
movq -0x8(%rbp), %rdi
addq $0x1000, %rdi           # imm = 0x1000
callq 0x2290
movl $0x1, %edi
callq 0x22f0
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %rcx
movl $0x100, %esi            # imm = 0x100
movl $0x10, %edx
callq 0x2990
movq -0x60(%rbp), %rdi
callq 0x20e0
leaq 0x28d6(%rip), %rdi      # 0x9994
movb $0x0, %al
callq 0x2120
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)