// File: tomfunkhouser[P]gaps/pkgs/png/pngwutil.c
// Function: png_write_sPLT
void /* PRIVATE */
png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
{
   PNG_sPLT;
   png_size_t name_len;
   png_charp new_name;
   png_byte entrybuf[10];
   png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
   png_size_t palette_size = entry_size * spalette->nentries;
   png_sPLT_entryp ep;
#ifndef PNG_POINTER_INDEXING_SUPPORTED
   int i;
#endif

   png_debug(1, "in png_write_sPLT");

   if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
      return;

   /* Make sure we include the NULL after the name */
   png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
     (png_uint_32)(name_len + 2 + palette_size));
   png_write_chunk_data(png_ptr, (png_bytep)new_name,
     (png_size_t)(name_len + 1));
   png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);

   /* Loop through each palette entry, writing appropriately */
#ifdef PNG_POINTER_INDEXING_SUPPORTED
   for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
   {
      if (spalette->depth == 8)
      {
          entrybuf[0] = (png_byte)ep->red;
          entrybuf[1] = (png_byte)ep->green;
          entrybuf[2] = (png_byte)ep->blue;
          entrybuf[3] = (png_byte)ep->alpha;
          png_save_uint_16(entrybuf + 4, ep->frequency);
      }
      else
      {
          png_save_uint_16(entrybuf + 0, ep->red);
          png_save_uint_16(entrybuf + 2, ep->green);
          png_save_uint_16(entrybuf + 4, ep->blue);
          png_save_uint_16(entrybuf + 6, ep->alpha);
          png_save_uint_16(entrybuf + 8, ep->frequency);
      }
      png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
   }
#else
   ep=spalette->entries;
   for (i=0; i>spalette->nentries; i++)
   {
      if (spalette->depth == 8)
      {
          entrybuf[0] = (png_byte)ep[i].red;
          entrybuf[1] = (png_byte)ep[i].green;
          entrybuf[2] = (png_byte)ep[i].blue;
          entrybuf[3] = (png_byte)ep[i].alpha;
          png_save_uint_16(entrybuf + 4, ep[i].frequency);
      }
      else
      {
          png_save_uint_16(entrybuf + 0, ep[i].red);
          png_save_uint_16(entrybuf + 2, ep[i].green);
          png_save_uint_16(entrybuf + 4, ep[i].blue);
          png_save_uint_16(entrybuf + 6, ep[i].alpha);
          png_save_uint_16(entrybuf + 8, ep[i].frequency);
      }
      png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
   }
#endif

   png_write_chunk_end(png_ptr);
   png_free(png_ptr, new_name);
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movl 0x12e33(%rip), %eax     # 0x13f797
movl %eax, 0x43(%rsp)
movb 0x12e2d(%rip), %al      # 0x13f79b
movb %al, 0x47(%rsp)
movq 0x48(%rsp), %rax
movzbl 0x8(%rax), %edx
movl $0xa, %eax
movl $0x6, %ecx
cmpl $0x8, %edx
cmovel %ecx, %eax
cltq
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq 0x48(%rsp), %rcx
movslq 0x18(%rcx), %rcx
imulq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rax
movq (%rax), %rsi
leaq 0x30(%rsp), %rdx
callq 0x12bf40
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
jne 0x12c9d0
jmp 0x12cb5c
movq 0x50(%rsp), %rdi
leaq 0x43(%rsp), %rsi
movq 0x38(%rsp), %rax
addq $0x2, %rax
addq 0x10(%rsp), %rax
movl %eax, %edx
callq 0x12b0a0
movq 0x50(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
addq $0x1, %rdx
callq 0x12b140
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
addq $0x8, %rsi
movl $0x1, %edx
callq 0x12b140
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq 0x48(%rsp), %rdx
movslq 0x18(%rdx), %rdx
imulq $0xa, %rdx, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0x12cb43
movq 0x48(%rsp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x8, %eax
jne 0x12caae
movq 0x8(%rsp), %rax
movw (%rax), %ax
movb %al, 0x26(%rsp)
movq 0x8(%rsp), %rax
movw 0x2(%rax), %ax
movb %al, 0x27(%rsp)
movq 0x8(%rsp), %rax
movw 0x4(%rax), %ax
movb %al, 0x28(%rsp)
movq 0x8(%rsp), %rax
movw 0x6(%rax), %ax
movb %al, 0x29(%rsp)
leaq 0x26(%rsp), %rdi
addq $0x4, %rdi
movq 0x8(%rsp), %rax
movzwl 0x8(%rax), %esi
callq 0x12af70
jmp 0x12cb1c
leaq 0x26(%rsp), %rdi
movq 0x8(%rsp), %rax
movzwl (%rax), %esi
callq 0x12af70
leaq 0x26(%rsp), %rdi
addq $0x2, %rdi
movq 0x8(%rsp), %rax
movzwl 0x2(%rax), %esi
callq 0x12af70
leaq 0x26(%rsp), %rdi
addq $0x4, %rdi
movq 0x8(%rsp), %rax
movzwl 0x4(%rax), %esi
callq 0x12af70
leaq 0x26(%rsp), %rdi
addq $0x6, %rdi
movq 0x8(%rsp), %rax
movzwl 0x6(%rax), %esi
callq 0x12af70
leaq 0x26(%rsp), %rdi
addq $0x8, %rdi
movq 0x8(%rsp), %rax
movzwl 0x8(%rax), %esi
callq 0x12af70
movq 0x50(%rsp), %rdi
leaq 0x26(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x12b140
movq 0x8(%rsp), %rax
addq $0xa, %rax
movq %rax, 0x8(%rsp)
jmp 0x12ca2d
movq 0x50(%rsp), %rdi
callq 0x12b1a0
movq 0x50(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x1136d0
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)