// File: /workspace/github_c_makefile_star2_O3/sadistech[P]nesromtool2/./src/nrt/tile.c
// Function: nrt_tiles_to_png
bool nrt_tiles_to_png(nrt_tile_bitmap *tiles, int count, int width, FILE *outfile) {
  int height = count / width;

  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  png_infop info_ptr = png_create_info_struct(png_ptr);

  if (!info_ptr) {
    png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
    return false;
  }

  if (setjmp(png_jmpbuf(png_ptr))) {
    png_destroy_write_struct(&png_ptr, &info_ptr);
    return false;
  }

  // set up the palette.
  static const png_color palette[] = {
    {0x00, 0x00, 0x00},
    {0xFF, 0x00, 0x00},
    {0xFF, 0xFF, 0x00},
    {0x00, 0x00, 0xFF}
  };

  // 16w x 32h tiles
  png_byte *row = (png_byte*)malloc(width * (NRT_TILE_WIDTH_PX / NRT_TILE_BITMAP_PALETTE_SIZE) * sizeof(png_byte));

  png_init_io(png_ptr, outfile);
  //png_set_write_status_fn(png_ptr, write_row_callback);

  png_set_IHDR(png_ptr, info_ptr, width * NRT_TILE_WIDTH_PX, height * NRT_TILE_WIDTH_PX, NRT_TILE_BITMAP_BITDEPTH, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  png_set_PLTE(png_ptr, info_ptr, palette, NRT_TILE_BITMAP_PALETTE_SIZE);

  png_write_info(png_ptr, info_ptr);

  int tile_row = 0;
  int tile_idx = 0;
  int idx = 0;
  int i;
  int j;

  // iterate over each row of tiles
  for (tile_row = 0; tile_row < height; tile_row++) {
    // iterate over each pixel in the row
    for (i = 0; i < NRT_TILE_WIDTH_PX; i++) {
      bzero(row, width * 2 * sizeof(png_byte));
      //iterate over each row of pixels in the tile
      for(j = 0; j < NRT_TILE_HEIGHT_PX; j++) {
        // iterate over each tile in the row
        for (tile_idx = 0; tile_idx < width; tile_idx++) {
          idx = 0;
          if (j >3) {
            idx = 1;
          }

          idx += (tile_idx * 2);
          row[idx] = row[idx] << 2;
          row[idx] += tiles[tile_row * width + tile_idx].pixels[i * NRT_TILE_HEIGHT_PX + j];
        }
      }
      png_write_row(png_ptr, row);
    }
  }

  png_write_end(png_ptr, NULL);

  if (info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
	if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
  free(row);

  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r12
movl %edx, %ebx
movl %esi, %eax
movq %rdi, 0x38(%rsp)
cltd
idivl %ebx
movl %eax, %r14d
leaq 0x4181(%rip), %rdi      # 0x7080
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x22f0
movq %rax, %r13
movq %rax, (%rsp)
movq %rax, %rdi
callq 0x2220
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x2f5e
movq %rax, %r15
movq 0x90af(%rip), %rsi      # 0xbfe0
movl $0xc8, %edx
movq %r13, %rdi
callq 0x2120
movq %rax, %rdi
callq 0x21b0
testl %eax, %eax
je 0x2f71
movq %rsp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2230
xorl %eax, %eax
jmp 0x3129
xorl %ebx, %ebx
movl %ebx, %esi
movq %rsp, %rdi
callq 0x2230
movl %ebx, %eax
jmp 0x3129
movl %ebx, %eax
addl %eax, %eax
movslq %eax, %rdi
movq %rdi, 0x28(%rsp)
callq 0x2240
movq %rax, %rbp
movq %rax, 0x8(%rsp)
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x22a0
movq (%rsp), %rdi
movl %ebx, %eax
leal (,%rax,8), %edx
movl %r14d, %eax
leal (,%rax,8), %ecx
subq $0x8, %rsp
xorl %r12d, %r12d
movl %r12d, %eax
movq %r15, %rsi
movl $0x2, %r8d
movl $0x3, %r9d
pushq %rax
pushq %rax
pushq %rax
callq 0x2370
addq $0x20, %rsp
movq (%rsp), %rdi
leaq 0x40da(%rip), %rdx      # 0x70b7
movq %r15, %rsi
movl $0x4, %ecx
callq 0x20f0
movq (%rsp), %rdi
movq %r15, 0x30(%rsp)
movq %r15, %rsi
callq 0x2030
testl %r14d, %r14d
jle 0x30e5
movl %r14d, %r14d
movl %r14d, %eax
movq %rax, 0x20(%rsp)
movl %ebx, %eax
movl %eax, %r14d
shlq $0x5, %r14
movl %r12d, %ecx
movl %r12d, %eax
movq %rcx, 0x18(%rsp)
movl %eax, 0x14(%rsp)
movl %eax, %eax
movl %eax, %r13d
shlq $0x6, %r13
addq 0x38(%rsp), %r13
movl %r12d, %r15d
movq 0x8(%rsp), %r12
movq %r12, %rdi
xorl %r12d, %r12d
movl %r12d, %esi
movq 0x28(%rsp), %rdx
callq 0x2180
movl %r12d, %ecx
movq %r13, %rax
testl %ebx, %ebx
jle 0x3089
cmpq $0x4, %rcx
movq %rbp, %rdx
sbbq $-0x1, %rdx
movl %r12d, %esi
movb (%rdx), %dil
shlb $0x2, %dil
addb (%rax,%rsi,2), %dil
movb %dil, (%rdx)
movq %rsi, %rdi
addq $0x20, %rdi
addq $0x2, %rdx
movq %rdi, %rsi
cmpq %rdi, %r14
jne 0x3068
movq %rcx, %rdx
incq %rdx
incq %rax
movq %rdx, %rcx
cmpq $0x8, %rdx
jne 0x3056
movq (%rsp), %rdi
movq 0x8(%rsp), %r12
movq %r12, %rsi
callq 0x20d0
movq %r15, %rax
incq %rax
addq $0x8, %r13
movq %rax, %r15
cmpq $0x8, %rax
jne 0x303d
movq 0x18(%rsp), %rdx
incq %rdx
movl 0x14(%rsp), %eax
addl %ebx, %eax
movq %rdx, %rcx
cmpq 0x20(%rsp), %rdx
movl $0x0, %r12d
jne 0x301e
movq (%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x21a0
movq (%rsp), %rdi
movq 0x30(%rsp), %rsi
movl $0xffff, %edx           # imm = 0xFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x2150
cmpq $0x0, (%rsp)
je 0x311d
xorl %eax, %eax
movl %eax, %esi
movq %rsp, %rdi
callq 0x2230
movq 0x8(%rsp), %rdi
callq 0x2040
movb $0x1, %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq