// File: /workspace/github_c_makefile_star2_O1/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, %rbp
movl %edx, %ebx
movl %esi, %eax
movq %rdi, 0x40(%rsp)
cltd
idivl %ebx
movl %eax, %r14d
leaq 0x4191(%rip), %rdi      # 0x7080
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x22f0
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x2220
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x2f4d
movq 0x8(%rsp), %rdi
movq 0x90bf(%rip), %rsi      # 0xbfe0
movl $0xc8, %edx
callq 0x2120
movq %rax, %rdi
callq 0x21b0
testl %eax, %eax
je 0x2f62
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2230
xorl %eax, %eax
jmp 0x3127
xorl %ebx, %ebx
movl %ebx, %esi
leaq 0x8(%rsp), %rdi
callq 0x2230
movl %ebx, %eax
jmp 0x3127
movl %ebx, %eax
addl %eax, %eax
movslq %eax, %rdi
movq %rdi, 0x38(%rsp)
callq 0x2240
movq %rax, %r13
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x22a0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %ebx, %eax
leal (,%rax,8), %edx
movl %r14d, %eax
leal (,%rax,8), %ecx
subq $0x8, %rsp
xorl %ebp, %ebp
movl %ebp, %eax
movl $0x2, %r8d
movl $0x3, %r9d
pushq %rax
pushq %rax
pushq %rax
callq 0x2370
addq $0x20, %rsp
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x40e1(%rip), %rdx      # 0x70b7
movl $0x4, %ecx
callq 0x20f0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x2030
testl %r14d, %r14d
jle 0x30d6
movl %r14d, %r14d
movl %r14d, %eax
movq %rax, 0x30(%rsp)
movl %ebx, %eax
movl %eax, %r14d
shlq $0x5, %r14
movl %ebp, %ecx
movl %ebp, %eax
movq %rcx, 0x28(%rsp)
movl %eax, 0x24(%rsp)
movl %eax, %eax
movl %eax, %r12d
shlq $0x6, %r12
addq 0x40(%rsp), %r12
movl %ebp, %r15d
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
xorl %ebp, %ebp
movl %ebp, %esi
movq 0x38(%rsp), %rdx
callq 0x2180
movl %ebp, %ecx
movq %r12, %rax
testl %ebx, %ebx
jle 0x307a
cmpq $0x4, %rcx
movq %r13, %rdx
sbbq $-0x1, %rdx
movl %ebp, %esi
movb (%rdx), %dil
shlb $0x2, %dil
movb %dil, (%rdx)
addb (%rax,%rsi,2), %dil
movb %dil, (%rdx)
movq %rsi, %rdi
addq $0x20, %rdi
addq $0x2, %rdx
movq %rdi, %rsi
cmpq %rdi, %r14
jne 0x3056
movq %rcx, %rdx
incq %rdx
incq %rax
movq %rdx, %rcx
cmpq $0x8, %rdx
jne 0x3045
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rbp
movq %rbp, %rsi
callq 0x20d0
movq %r15, %rax
incq %rax
addq $0x8, %r12
movq %rax, %r15
cmpq $0x8, %rax
jne 0x302f
movq 0x28(%rsp), %rdx
incq %rdx
movl 0x24(%rsp), %eax
addl %ebx, %eax
movq %rdx, %rcx
cmpq 0x30(%rsp), %rdx
movl $0x0, %ebp
jne 0x3010
movq 0x8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x21a0
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x3102
movq 0x8(%rsp), %rdi
movl $0xffff, %edx           # imm = 0xFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x2150
cmpq $0x0, 0x8(%rsp)
movq 0x18(%rsp), %rbx
je 0x311d
xorl %eax, %eax
movl %eax, %esi
leaq 0x8(%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