// File: /workspace/github_cpp_makefile_star2_O3/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 0x9ad8f
cmpl $0x1, %eax
je 0x9ae77
decl %eax
movl %eax, 0x45c(%rbx)
cmpl $0x1, %eax
jne 0x9ad8f
leaq 0x180c8(%rip), %rsi     # 0xb2e52
jmp 0x9ae6f
movl 0x11c(%rbx), %eax
testb $0x1, %al
jne 0x9adaa
leaq 0x180d3(%rip), %rsi     # 0xb2e73
movq %rbx, %rdi
callq 0x920b4
jmp 0x9adb2
testb $0x4, %al
jne 0x9ae68
movq 0x488(%rbx), %rsi
movq %rbx, %rdi
callq 0x92016
leal 0x1(%r15), %esi
movq %rbx, %rdi
callq 0x91fa8
movq %rax, 0x488(%rbx)
movl %r15d, %r12d
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x99b23
movq %rbx, %rdi
xorl %esi, %esi
callq 0x99b4f
movq 0x488(%rbx), %rsi
testl %eax, %eax
je 0x9ae1c
movq %rbx, %rdi
callq 0x92016
movq $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 0x9ae2e
leaq (%rsi,%r12), %rax
addq $-0x2, %rax
cmpq %rax, %r13
jbe 0x9ae90
movq %rbx, %rdi
callq 0x92016
movq $0x0, 0x488(%rbx)
leaq 0x18041(%rip), %rsi     # 0xb2ea4
jmp 0x9aff6
leaq 0x1801d(%rip), %rsi     # 0xb2e8c
movq %rbx, %rdi
callq 0x9214d
movq %rbx, %rdi
movl %r15d, %esi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x99b4f
movb (%r13), %r12b
movb %r12b, 0x10(%rsp)
xorl %eax, %eax
cmpb $0x8, %r12b
setne %al
leal 0x6(,%rax,4), %ecx
movl %r15d, %eax
cltd
idivl %ecx
testl %edx, %edx
je 0x9aed2
movq %rbx, %rdi
callq 0x92016
movq $0x0, 0x488(%rbx)
leaq 0x17fec(%rip), %rsi     # 0xb2eb9
jmp 0x9aff6
movl %eax, %ebp
movl %eax, 0x20(%rsp)
cmpl $0x9999999a, %eax       # imm = 0x9999999A
jb 0x9aeeb
leaq 0x17fed(%rip), %rsi     # 0xb2ed3
jmp 0x9aff6
movl %ebp, %eax
addq %rax, %rax
leaq (%rax,%rax,4), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x92056
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x9afef
testl %ebp, %ebp
je 0x9afa2
incq %r13
xorl %ecx, %ecx
cmpb $0x8, %r12b
jne 0x9af48
movzbl (%r13), %edx
movw %dx, (%rax,%rcx)
movzbl 0x1(%r13), %edx
movw %dx, 0x2(%rax,%rcx)
movzbl 0x2(%r13), %edx
movw %dx, 0x4(%rax,%rcx)
movzbl 0x3(%r13), %edx
addq $0x4, %r13
jmp 0x9af7e
movzwl (%r13), %edx
rolw $0x8, %dx
movw %dx, (%rax,%rcx)
movzwl 0x2(%r13), %edx
rolw $0x8, %dx
movw %dx, 0x2(%rax,%rcx)
movzwl 0x4(%r13), %edx
rolw $0x8, %dx
movw %dx, 0x4(%rax,%rcx)
movzwl 0x6(%r13), %edx
rolw $0x8, %dx
addq $0x8, %r13
movw %dx, 0x6(%rax,%rcx)
movzwl (%r13), %edx
rolw $0x8, %dx
movw %dx, 0x8(%rax,%rcx)
addq $0x2, %r13
addq $0xa, %rcx
cmpq %rcx, %r15
jne 0x9af1a
movq 0x488(%rbx), %rax
leaq 0x8(%rsp), %r15
movq %rax, (%r15)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x1, %ecx
callq 0x88778
movq 0x488(%rbx), %rsi
movq %rbx, %rdi
callq 0x92016
movq $0x0, 0x488(%rbx)
movq 0x10(%r15), %rsi
movq %rbx, %rdi
callq 0x92016
jmp 0x9ae0d
leaq 0x17ef1(%rip), %rsi     # 0xb2ee7
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9214d