// File: /workspace/github_c_makefile_star2_O2/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, %r13
movl %edx, %ebp
movl %esi, %eax
movq %rdi, %r12
cltd
idivl %ebp
movl %eax, %r14d
leaq 0x38d6(%rip), %rdi      # 0x6080
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x22f0
movq %rax, %r15
movq %rax, (%rsp)
movq %rax, %rdi
callq 0x2220
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x2809
movq %rax, %rbx
movq 0x8804(%rip), %rsi      # 0xafe0
movl $0xc8, %edx
movq %r15, %rdi
callq 0x2120
movq %rax, %rdi
callq 0x21b0
testl %eax, %eax
je 0x281c
movq %rsp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x2230
xorl %eax, %eax
jmp 0x29b2
xorl %ebx, %ebx
movl %ebx, %esi
movq %rsp, %rdi
callq 0x2230
movl %ebx, %eax
jmp 0x29b2
movq %rbx, %r15
movl %ebp, %eax
addl %eax, %eax
movslq %eax, %rdi
movq %rdi, 0x30(%rsp)
callq 0x2240
movq %r12, 0x8(%rsp)
movq %rax, %r12
movq %rax, %rbx
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x22a0
movq (%rsp), %rdi
movl %ebp, %eax
leal (,%rax,8), %edx
movl %r14d, %eax
leal (,%rax,8), %ecx
subq $0x8, %rsp
pushq $0x2
popq %r8
pushq $0x3
popq %r9
movq %r15, %rsi
pushq $0x0
pushq $0x0
pushq $0x0
callq 0x2370
addq $0x20, %rsp
movq (%rsp), %rdi
leaq 0x3830(%rip), %rdx      # 0x60b7
pushq $0x4
popq %rcx
movq %r15, %rsi
callq 0x20f0
movq (%rsp), %rdi
movq %r15, 0x38(%rsp)
movq %r15, %rsi
callq 0x2030
xorl %r13d, %r13d
testl %ebp, %ebp
movl %r13d, %eax
cmovgl %ebp, %eax
movslq %ebp, %rcx
testl %r14d, %r14d
cmovlel %r13d, %r14d
movl %r14d, %edx
movq %rdx, 0x28(%rsp)
movl %eax, %ebp
shlq $0x6, %rcx
movq %rcx, 0x20(%rsp)
movl %r13d, %ecx
movq 0x8(%rsp), %rax
cmpq 0x28(%rsp), %rcx
je 0x2972
movq %rcx, 0x10(%rsp)
movl %r13d, %r15d
movq %rax, 0x18(%rsp)
movq %rax, %r14
cmpq $0x8, %r15
je 0x295b
movq %rbx, %rdi
movl %r13d, %esi
movq 0x30(%rsp), %rdx
callq 0x2180
movl %r13d, %ecx
movq %r14, %rax
cmpq $0x8, %rcx
je 0x2946
cmpq $0x4, %rcx
movq %r12, %rdx
sbbq $-0x1, %rdx
movl %r13d, %edi
movq %rax, %rsi
cmpq %rdi, %rbp
je 0x293e
movb (%rdx,%rdi,2), %r8b
shlb $0x2, %r8b
addb (%rsi), %r8b
movb %r8b, (%rdx,%rdi,2)
incq %rdi
addq $0x40, %rsi
jmp 0x2921
incq %rcx
incq %rax
jmp 0x290a
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x20d0
incq %r15
addq $0x8, %r14
jmp 0x28ee
movq 0x10(%rsp), %rcx
incq %rcx
movq 0x18(%rsp), %rax
addq 0x20(%rsp), %rax
jmp 0x28d3
movq (%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x21a0
movq (%rsp), %rdi
pushq $-0x1
popq %rcx
movq 0x38(%rsp), %rsi
movl $0xffff, %edx           # imm = 0xFFFF
callq 0x2150
cmpq $0x0, (%rsp)
je 0x29a8
xorl %eax, %eax
movl %eax, %esi
movq %rsp, %rdi
callq 0x2230
movq %rbx, %rdi
callq 0x2040
movb $0x1, %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq