// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl -0x14(%rbp), %eax
cltd
idivl -0x18(%rbp)
movl %eax, -0x24(%rbp)
leaq 0x56a9(%rip), %rdi      # 0x8080
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x22d0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x2200
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x2a14
leaq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2210
movb $0x0, -0x1(%rbp)
jmp 0x2c4f
movq -0x30(%rbp), %rdi
movq 0x95c1(%rip), %rsi      # 0xbfe0
movl $0xc8, %edx
callq 0x2100
movq %rax, %rdi
callq 0x2190
cmpl $0x0, %eax
je 0x2a4c
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x2210
movb $0x0, -0x1(%rbp)
jmp 0x2c4f
movl -0x18(%rbp), %eax
shll %eax
movslq %eax, %rdi
shlq $0x0, %rdi
callq 0x2220
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x2280
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x18(%rbp), %edx
shll $0x3, %edx
movl -0x24(%rbp), %ecx
shll $0x3, %ecx
movl $0x2, %r8d
movl $0x3, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x2350
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq 0x55fc(%rip), %rdx      # 0x80b7
movl $0x4, %ecx
callq 0x20d0
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2030
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x2c03
movl $0x0, -0x50(%rbp)
cmpl $0x8, -0x50(%rbp)
jge 0x2bf3
movq -0x40(%rbp), %rdi
movl -0x18(%rbp), %eax
shll %eax
movslq %eax, %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x2160
movl $0x0, -0x54(%rbp)
cmpl $0x8, -0x54(%rbp)
jge 0x2bd8
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x2bc8
movl $0x0, -0x4c(%rbp)
cmpl $0x3, -0x54(%rbp)
jle 0x2b5a
movl $0x1, -0x4c(%rbp)
movl -0x48(%rbp), %eax
shll %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
shll $0x2, %eax
movb %al, %dl
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movl -0x44(%rbp), %ecx
imull -0x18(%rbp), %ecx
addl -0x48(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movl -0x50(%rbp), %ecx
shll $0x3, %ecx
addl -0x54(%rbp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %esi
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movzbl (%rax,%rcx), %edx
addl %esi, %edx
movb %dl, (%rax,%rcx)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x2b3a
jmp 0x2bca
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2b29
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x20b0
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x2b01
jmp 0x2bf5
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2aee
movq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2180
cmpq $0x0, -0x38(%rbp)
je 0x2c2e
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0xffff, %edx           # imm = 0xFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x2130
cmpq $0x0, -0x30(%rbp)
je 0x2c42
leaq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2210
movq -0x40(%rbp), %rdi
callq 0x2040
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)