// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
movl 0x45c(%rdi), %eax
testl %eax, %eax
je 0x9b71a
cmpl $0x1, %eax
je 0x9b801
decl %eax
movl %eax, 0x45c(%rbx)
cmpl $0x1, %eax
jne 0x9b71a
leaq 0x176e9(%rip), %rsi     # 0xb2dfe
jmp 0x9b7f9
movl 0x11c(%rbx), %eax
testb $0x1, %al
jne 0x9b735
leaq 0x176f4(%rip), %rsi     # 0xb2e1f
movq %rbx, %rdi
callq 0x92dd0
jmp 0x9b73d
testb $0x4, %al
jne 0x9b7f2
movq 0x488(%rbx), %rsi
movq %rbx, %rdi
callq 0x92d32
leal 0x1(%r15), %esi
movq %rbx, %rdi
callq 0x92cb5
movq %rax, 0x488(%rbx)
movl %r15d, %r12d
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x9a4db
movq %rbx, %rdi
xorl %esi, %esi
callq 0x9a507
movq 0x488(%rbx), %rsi
testl %eax, %eax
je 0x9b7a6
movq %rbx, %rdi
callq 0x92d32
movq $0x0, 0x488(%rbx)
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movb $0x0, (%rsi,%r12)
movq 0x488(%rbx), %rsi
decl %r15d
movq %rsi, %r13
decl %r15d
cmpb $0x0, (%r13)
leaq 0x1(%r13), %r13
jne 0x9b7b8
leaq (%rsi,%r12), %rax
addq $-0x2, %rax
cmpq %rax, %r13
jbe 0x9b819
movq %rbx, %rdi
callq 0x92d32
movq $0x0, 0x488(%rbx)
leaq 0x17663(%rip), %rsi     # 0xb2e50
jmp 0x9b98a
leaq 0x1763f(%rip), %rsi     # 0xb2e38
movq %rbx, %rdi
callq 0x92e6e
movq %rbx, %rdi
movl %r15d, %esi
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x9a507
movb (%r13), %al
movb %al, 0x8(%rsp)
xorl %ecx, %ecx
cmpb $0x8, %al
setne %cl
leal 0x6(,%rcx,4), %ecx
movl %r15d, %eax
cltd
idivl %ecx
testl %edx, %edx
je 0x9b858
movq %rbx, %rdi
callq 0x92d32
movq $0x0, 0x488(%rbx)
leaq 0x17612(%rip), %rsi     # 0xb2e65
jmp 0x9b98a
movl %eax, 0x18(%rsp)
cmpl $0x9999999a, %eax       # imm = 0x9999999A
jb 0x9b86f
leaq 0x17615(%rip), %rsi     # 0xb2e7f
jmp 0x9b98a
movl %eax, %eax
addq %rax, %rax
leaq (%rax,%rax,4), %rsi
movq %rbx, %rdi
callq 0x92d72
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x9b983
movslq 0x18(%rsp), %rdx
testq %rdx, %rdx
jle 0x9b938
incq %r13
movb 0x8(%rsp), %cl
addq %rdx, %rdx
leaq (%rdx,%rdx,4), %rdx
xorl %esi, %esi
cmpb $0x8, %cl
jne 0x9b8de
movzbl (%r13), %edi
movw %di, (%rax,%rsi)
movzbl 0x1(%r13), %edi
movw %di, 0x2(%rax,%rsi)
movzbl 0x2(%r13), %edi
movw %di, 0x4(%rax,%rsi)
movzbl 0x3(%r13), %edi
addq $0x4, %r13
movw %di, 0x6(%rax,%rsi)
jmp 0x9b919
movzwl (%r13), %edi
rolw $0x8, %di
movw %di, (%rax,%rsi)
movzwl 0x2(%r13), %edi
rolw $0x8, %di
movw %di, 0x2(%rax,%rsi)
movzwl 0x4(%r13), %edi
rolw $0x8, %di
movw %di, 0x4(%rax,%rsi)
movzwl 0x6(%r13), %edi
rolw $0x8, %di
movw %di, 0x6(%rax,%rsi)
addq $0x8, %r13
movzwl (%r13), %edi
rolw $0x8, %di
movw %di, 0x8(%rax,%rsi)
addq $0x2, %r13
addq $0xa, %rsi
cmpq %rsi, %rdx
jne 0x9b8ac
movq 0x488(%rbx), %rax
movq %rsp, %r15
movq %rax, (%r15)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x1, %ecx
callq 0x89578
movq 0x488(%rbx), %rsi
movq %rbx, %rdi
callq 0x92d32
movq $0x0, 0x488(%rbx)
movq 0x10(%r15), %rsi
movq %rbx, %rdi
callq 0x92d32
jmp 0x9b798
leaq 0x17509(%rip), %rsi     # 0xb2e93
movq %rbx, %rdi
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x92e6e