// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
movl 0x45c(%rdi), %eax
testl %eax, %eax
je 0x8c89b
cmpl $0x1, %eax
je 0x8c97d
decl %eax
movl %eax, 0x45c(%rbx)
cmpl $0x1, %eax
jne 0x8c89b
leaq 0x1726c(%rip), %rsi     # 0xa3b02
jmp 0x8c975
movl 0x11c(%rbx), %eax
testb $0x1, %al
jne 0x8c8b6
leaq 0x17277(%rip), %rsi     # 0xa3b23
movq %rbx, %rdi
callq 0x849c0
jmp 0x8c8be
testb $0x4, %al
jne 0x8c96e
movq 0x488(%rbx), %rsi
movq %rbx, %rdi
callq 0x84923
leal 0x1(%r15), %esi
movq %rbx, %rdi
callq 0x848bb
movq %rax, 0x488(%rbx)
movl %r15d, %r12d
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x8b6d7
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8b703
movq 0x488(%rbx), %rsi
testl %eax, %eax
je 0x8c925
movq %rbx, %rdi
callq 0x84923
andq $0x0, 0x488(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, (%rsi,%r12)
movq 0x488(%rbx), %rsi
decl %r15d
movq %rsi, %r13
decl %r15d
cmpb $0x0, (%r13)
leaq 0x1(%r13), %r13
jne 0x8c937
leaq (%rsi,%r12), %rax
addq $-0x2, %rax
cmpq %rax, %r13
jbe 0x8c996
movq %rbx, %rdi
callq 0x84923
andq $0x0, 0x488(%rbx)
leaq 0x171eb(%rip), %rsi     # 0xa3b54
jmp 0x8caee
leaq 0x171c7(%rip), %rsi     # 0xa3b3c
movq %rbx, %rdi
callq 0x84a53
movq %rbx, %rdi
movl %r15d, %esi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8b703
movb (%r13), %bpl
movb %bpl, 0x10(%rsp)
xorl %eax, %eax
cmpb $0x8, %bpl
setne %al
leal 0x6(,%rax,4), %ecx
movl %r15d, %eax
cltd
idivl %ecx
testl %edx, %edx
je 0x8c9d5
movq %rbx, %rdi
callq 0x84923
andq $0x0, 0x488(%rbx)
leaq 0x17199(%rip), %rsi     # 0xa3b69
jmp 0x8caee
movl %eax, 0x20(%rsp)
cmpl $0x9999999a, %eax       # imm = 0x9999999A
jb 0x8c9ec
leaq 0x1719c(%rip), %rsi     # 0xa3b83
jmp 0x8caee
movl %eax, %r15d
imulq $0xa, %r15, %rsi
movq %rbx, %rdi
callq 0x84960
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x8cae7
incq %r13
imulq $0xa, %r15, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x8ca9f
cmpb $0x8, %bpl
jne 0x8ca49
movzbl (%r13), %esi
movw %si, (%rax,%rdx)
movzbl 0x1(%r13), %esi
movw %si, 0x2(%rax,%rdx)
movzbl 0x2(%r13), %esi
movw %si, 0x4(%rax,%rdx)
movzbl 0x3(%r13), %esi
addq $0x4, %r13
jmp 0x8ca7f
movzwl (%r13), %esi
rolw $0x8, %si
movw %si, (%rax,%rdx)
movzwl 0x2(%r13), %esi
rolw $0x8, %si
movw %si, 0x2(%rax,%rdx)
movzwl 0x4(%r13), %esi
rolw $0x8, %si
movw %si, 0x4(%rax,%rdx)
movzwl 0x6(%r13), %esi
rolw $0x8, %si
addq $0x8, %r13
movw %si, 0x6(%rax,%rdx)
movzwl (%r13), %esi
rolw $0x8, %si
movw %si, 0x8(%rax,%rdx)
addq $0x2, %r13
addq $0xa, %rdx
jmp 0x8ca12
movq 0x488(%rbx), %rax
leaq 0x8(%rsp), %r15
movq %rax, (%r15)
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x7bad5
movq 0x488(%rbx), %rsi
movq %rbx, %rdi
callq 0x84923
andq $0x0, 0x488(%rbx)
movq 0x10(%r15), %rsi
movq %rbx, %rdi
callq 0x84923
jmp 0x8c916
leaq 0x170a9(%rip), %rsi     # 0xa3b97
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x84a53