// File: tomfunkhouser[P]gaps/pkgs/png/pngrutil.c
// Function: png_handle_sPLT
void /* PRIVATE */
png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
/* Note: this does not properly handle chunks that are > 64K under DOS */
{
   png_bytep entry_start;
   png_sPLT_t new_palette;
#ifdef PNG_POINTER_INDEXING_SUPPORTED
   png_sPLT_entryp pp;
#endif
   int data_length, entry_size, i;
   png_uint_32 skip = 0;
   png_size_t slength;

   png_debug(1, "in png_handle_sPLT");

#ifdef PNG_USER_LIMITS_SUPPORTED

   if (png_ptr->user_chunk_cache_max != 0)
   {
      if (png_ptr->user_chunk_cache_max == 1)
      {
         png_crc_finish(png_ptr, length);
         return;
      }
      if (--png_ptr->user_chunk_cache_max == 1)
      {
         png_warning(png_ptr, "No space in chunk cache for sPLT");
         png_crc_finish(png_ptr, length);
         return;
      }
   }
#endif

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before sPLT");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid sPLT after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }

#ifdef PNG_MAX_MALLOC_64K
   if (length > (png_uint_32)65535L)
   {
      png_warning(png_ptr, "sPLT chunk too large to fit in memory");
      skip = length - (png_uint_32)65535L;
      length = (png_uint_32)65535L;
   }
#endif

   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
   slength = (png_size_t)length;
   png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);

   if (png_crc_finish(png_ptr, skip))
   {
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
      return;
   }

   png_ptr->chunkdata[slength] = 0x00;

   for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start;
       entry_start++)
      /* Empty loop to find end of name */ ;
   ++entry_start;

   /* A sample depth should follow the separator, and we should be on it  */
   if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
   {
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
      png_warning(png_ptr, "malformed sPLT chunk");
      return;
   }

   new_palette.depth = *entry_start++;
   entry_size = (new_palette.depth == 8 ? 6 : 10);
   data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata));

   /* Integrity-check the data length */
   if (data_length % entry_size)
   {
      png_free(png_ptr, png_ptr->chunkdata);
      png_ptr->chunkdata = NULL;
      png_warning(png_ptr, "sPLT chunk has bad length");
      return;
   }

   new_palette.nentries = (png_int_32) ( data_length / entry_size);
   if ((png_uint_32) new_palette.nentries >
       (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry)))
   {
       png_warning(png_ptr, "sPLT chunk too long");
       return;
   }
   new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
       png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
   if (new_palette.entries == NULL)
   {
       png_warning(png_ptr, "sPLT chunk requires too much memory");
       return;
   }

#ifdef PNG_POINTER_INDEXING_SUPPORTED
   for (i = 0; i < new_palette.nentries; i++)
   {
      pp = new_palette.entries + i;

      if (new_palette.depth == 8)
      {
          pp->red = *entry_start++;
          pp->green = *entry_start++;
          pp->blue = *entry_start++;
          pp->alpha = *entry_start++;
      }
      else
      {
          pp->red   = png_get_uint_16(entry_start); entry_start += 2;
          pp->green = png_get_uint_16(entry_start); entry_start += 2;
          pp->blue  = png_get_uint_16(entry_start); entry_start += 2;
          pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
      }
      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
   }
#else
   pp = new_palette.entries;
   for (i = 0; i < new_palette.nentries; i++)
   {

      if (new_palette.depth == 8)
      {
          pp[i].red   = *entry_start++;
          pp[i].green = *entry_start++;
          pp[i].blue  = *entry_start++;
          pp[i].alpha = *entry_start++;
      }
      else
      {
          pp[i].red   = png_get_uint_16(entry_start); entry_start += 2;
          pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
          pp[i].blue  = png_get_uint_16(entry_start); entry_start += 2;
          pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
      }
      pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
   }
#endif

   /* Discard all chunk data except the name and stash that */
   new_palette.name = png_ptr->chunkdata;

   png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);

   png_free(png_ptr, png_ptr->chunkdata);
   png_ptr->chunkdata = NULL;
   png_free(png_ptr, new_palette.entries);
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movl %edx, 0x54(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x60(%rsp), %rax
cmpl $0x0, 0x45c(%rax)
je 0x124c78
movq 0x60(%rsp), %rax
cmpl $0x1, 0x45c(%rax)
jne 0x124c39
movq 0x60(%rsp), %rdi
movl 0x54(%rsp), %esi
callq 0x122b90
jmp 0x125137
movq 0x60(%rsp), %rcx
movl 0x45c(%rcx), %eax
addl $-0x1, %eax
movl %eax, 0x45c(%rcx)
cmpl $0x1, %eax
jne 0x124c76
movq 0x60(%rsp), %rdi
leaq 0x1ab04(%rip), %rsi     # 0x13f762
callq 0x113940
movq 0x60(%rsp), %rdi
movl 0x54(%rsp), %esi
callq 0x122b90
jmp 0x125137
jmp 0x124c78
movq 0x60(%rsp), %rax
movl 0x11c(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x124c9e
movq 0x60(%rsp), %rdi
leaq 0x1aaec(%rip), %rsi     # 0x13f783
callq 0x113860
jmp 0x124cd7
movq 0x60(%rsp), %rax
movl 0x11c(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x124cd5
movq 0x60(%rsp), %rdi
leaq 0x1aadf(%rip), %rsi     # 0x13f79c
callq 0x113940
movq 0x60(%rsp), %rdi
movl 0x54(%rsp), %esi
callq 0x122b90
jmp 0x125137
jmp 0x124cd7
movq 0x60(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x488(%rax), %rsi
callq 0x1136d0
movq 0x60(%rsp), %rdi
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x1135b0
movq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x488(%rax)
movl 0x54(%rsp), %eax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x488(%rax), %rsi
movq 0x8(%rsp), %rdx
callq 0x122b40
movq 0x60(%rsp), %rdi
movl 0x10(%rsp), %esi
callq 0x122b90
cmpl $0x0, %eax
je 0x124d73
movq 0x60(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x488(%rax), %rsi
callq 0x1136d0
movq 0x60(%rsp), %rax
movq $0x0, 0x488(%rax)
jmp 0x125137
movq 0x60(%rsp), %rax
movq 0x488(%rax), %rax
movq 0x8(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x60(%rsp), %rax
movq 0x488(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
cmpb $0x0, (%rax)
je 0x124db5
jmp 0x124da5
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
jmp 0x124d99
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movq 0x60(%rsp), %rcx
movq 0x488(%rcx), %rcx
addq 0x8(%rsp), %rcx
addq $-0x2, %rcx
cmpq %rcx, %rax
jbe 0x124e1e
movq 0x60(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x488(%rax), %rsi
callq 0x1136d0
movq 0x60(%rsp), %rax
movq $0x0, 0x488(%rax)
movq 0x60(%rsp), %rdi
leaq 0x1a9a0(%rip), %rsi     # 0x13f7b4
callq 0x113940
jmp 0x125137
movq 0x48(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rsp)
movb (%rax), %al
movb %al, 0x30(%rsp)
movzbl 0x30(%rsp), %edx
movl $0xa, %eax
movl $0x6, %ecx
cmpl $0x8, %edx
cmovel %ecx, %eax
movl %eax, 0x18(%rsp)
movq 0x8(%rsp), %rax
movq 0x48(%rsp), %rcx
movq 0x60(%rsp), %rdx
movq 0x488(%rdx), %rdx
subq %rdx, %rcx
subq %rcx, %rax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cltd
idivl 0x18(%rsp)
cmpl $0x0, %edx
je 0x124eb8
movq 0x60(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x488(%rax), %rsi
callq 0x1136d0
movq 0x60(%rsp), %rax
movq $0x0, 0x488(%rax)
movq 0x60(%rsp), %rdi
leaq 0x1a91b(%rip), %rsi     # 0x13f7c9
callq 0x113940
jmp 0x125137
movl 0x1c(%rsp), %eax
cltd
idivl 0x18(%rsp)
movl %eax, 0x40(%rsp)
cmpl $0x99999999, 0x40(%rsp) # imm = 0x99999999
jbe 0x124ee5
movq 0x60(%rsp), %rdi
leaq 0x1a908(%rip), %rsi     # 0x13f7e3
callq 0x113940
jmp 0x125137
movq 0x60(%rsp), %rdi
movslq 0x40(%rsp), %rax
imulq $0xa, %rax, %rsi
callq 0x113760
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x124f1b
movq 0x60(%rsp), %rdi
leaq 0x1a8e6(%rip), %rsi     # 0x13f7f7
callq 0x113940
jmp 0x125137
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x40(%rsp), %eax
jge 0x1250d8
movq 0x38(%rsp), %rax
movslq 0x14(%rsp), %rcx
imulq $0xa, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movzbl 0x30(%rsp), %eax
cmpl $0x8, %eax
jne 0x124fd9
movq 0x48(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rsp)
movzbl (%rax), %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, (%rax)
movq 0x48(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rsp)
movzbl (%rax), %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x2(%rax)
movq 0x48(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rsp)
movzbl (%rax), %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x4(%rax)
movq 0x48(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rsp)
movzbl (%rax), %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x6(%rax)
jmp 0x125098
movq 0x48(%rsp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq 0x48(%rsp), %rcx
movzbl 0x1(%rcx), %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, (%rax)
movq 0x48(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq 0x48(%rsp), %rcx
movzbl 0x1(%rcx), %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x2(%rax)
movq 0x48(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq 0x48(%rsp), %rcx
movzbl 0x1(%rcx), %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x4(%rax)
movq 0x48(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq 0x48(%rsp), %rcx
movzbl 0x1(%rcx), %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x6(%rax)
movq 0x48(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq 0x48(%rsp), %rcx
movzbl 0x1(%rcx), %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x8(%rax)
movq 0x48(%rsp), %rax
addq $0x2, %rax
movq %rax, 0x48(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x124f23
movq 0x60(%rsp), %rax
movq 0x488(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
leaq 0x28(%rsp), %rdx
movl $0x1, %ecx
callq 0xfe6a0
movq 0x60(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x488(%rax), %rsi
callq 0x1136d0
movq 0x60(%rsp), %rax
movq $0x0, 0x488(%rax)
movq 0x60(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x1136d0
addq $0x68, %rsp
retq
nopl (%rax)