// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/png/pngrtran.c
// Function: png_do_expand_palette
void /* PRIVATE */
png_do_expand_palette(png_row_infop row_info, png_bytep row,
   png_colorp palette, png_bytep trans_alpha, int num_trans)
{
   int shift, value;
   png_bytep sp, dp;
   png_uint_32 i;
   png_uint_32 row_width=row_info->width;

   png_debug(1, "in png_do_expand_palette");

   if (
       row_info->color_type == PNG_COLOR_TYPE_PALETTE)
   {
      if (row_info->bit_depth < 8)
      {
         switch (row_info->bit_depth)
         {
            case 1:
            {
               sp = row + (png_size_t)((row_width - 1) >> 3);
               dp = row + (png_size_t)row_width - 1;
               shift = 7 - (int)((row_width + 7) & 0x07);
               for (i = 0; i < row_width; i++)
               {
                  if ((*sp >> shift) & 0x01)
                     *dp = 1;
                  else
                     *dp = 0;
                  if (shift == 7)
                  {
                     shift = 0;
                     sp--;
                  }
                  else
                     shift++;

                  dp--;
               }
               break;
            }

            case 2:
            {
               sp = row + (png_size_t)((row_width - 1) >> 2);
               dp = row + (png_size_t)row_width - 1;
               shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
               for (i = 0; i < row_width; i++)
               {
                  value = (*sp >> shift) & 0x03;
                  *dp = (png_byte)value;
                  if (shift == 6)
                  {
                     shift = 0;
                     sp--;
                  }
                  else
                     shift += 2;

                  dp--;
               }
               break;
            }

            case 4:
            {
               sp = row + (png_size_t)((row_width - 1) >> 1);
               dp = row + (png_size_t)row_width - 1;
               shift = (int)((row_width & 0x01) << 2);
               for (i = 0; i < row_width; i++)
               {
                  value = (*sp >> shift) & 0x0f;
                  *dp = (png_byte)value;
                  if (shift == 4)
                  {
                     shift = 0;
                     sp--;
                  }
                  else
                     shift += 4;

                  dp--;
               }
               break;
            }
         }
         row_info->bit_depth = 8;
         row_info->pixel_depth = 8;
         row_info->rowbytes = row_width;
      }
      switch (row_info->bit_depth)
      {
         case 8:
         {
            if (trans_alpha != NULL)
            {
               sp = row + (png_size_t)row_width - 1;
               dp = row + (png_size_t)(row_width << 2) - 1;

               for (i = 0; i < row_width; i++)
               {
                  if ((int)(*sp) >= num_trans)
                     *dp-- = 0xff;
                  else
                     *dp-- = trans_alpha[*sp];
                  *dp-- = palette[*sp].blue;
                  *dp-- = palette[*sp].green;
                  *dp-- = palette[*sp].red;
                  sp--;
               }
               row_info->bit_depth = 8;
               row_info->pixel_depth = 32;
               row_info->rowbytes = row_width * 4;
               row_info->color_type = 6;
               row_info->channels = 4;
            }
            else
            {
               sp = row + (png_size_t)row_width - 1;
               dp = row + (png_size_t)(row_width * 3) - 1;

               for (i = 0; i < row_width; i++)
               {
                  *dp-- = palette[*sp].blue;
                  *dp-- = palette[*sp].green;
                  *dp-- = palette[*sp].red;
                  sp--;
               }

               row_info->bit_depth = 8;
               row_info->pixel_depth = 24;
               row_info->rowbytes = row_width * 3;
               row_info->color_type = 2;
               row_info->channels = 3;
            }
            break;
         }
      }
   }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
cmpb $0x3, 0x10(%rdi)
jne 0x8f0d1
movq %rcx, %rax
movl (%rdi), %r9d
movzbl 0x11(%rdi), %ecx
cmpl $0x7, %ecx
ja 0x8ef14
cmpl $0x4, %ecx
je 0x8ef7b
cmpl $0x2, %ecx
je 0x8ef22
movq %r9, %r10
cmpl $0x1, %ecx
jne 0x8efce
xorl %r10d, %r10d
testl %r9d, %r9d
je 0x8efce
movl %r9d, %r14d
negl %r14d
andl $0x7, %r14d
leal -0x1(%r9), %ecx
shrl $0x3, %ecx
addq %rsi, %rcx
leaq (%r9,%rsi), %r11
decq %r11
movl %r9d, %ebx
movzbl (%rcx), %ebp
btl %r14d, %ebp
setb (%r11)
xorl %r15d, %r15d
cmpl $0x7, %r14d
leal 0x1(%r14), %ebp
sete %r15b
cmovel %r10d, %ebp
subq %r15, %rcx
decq %r11
movl %ebp, %r14d
decl %ebx
jne 0x8eee4
jmp 0x8efcb
cmpl $0x8, %ecx
je 0x8efda
jmp 0x8f0d1
xorl %r10d, %r10d
testl %r9d, %r9d
je 0x8efce
leal 0x6(,%r9,2), %ecx
notl %ecx
andl $0x6, %ecx
leal -0x1(%r9), %r11d
shrl $0x2, %r11d
addq %rsi, %r11
leaq (%r9,%rsi), %rbx
decq %rbx
movl %r9d, %ebp
movzbl (%r11), %r14d
shrl %cl, %r14d
andb $0x3, %r14b
movb %r14b, (%rbx)
xorl %r14d, %r14d
cmpl $0x6, %ecx
leal 0x2(%rcx), %ecx
sete %r14b
cmovel %r10d, %ecx
subq %r14, %r11
decq %rbx
decl %ebp
jne 0x8ef50
jmp 0x8efcb
xorl %r10d, %r10d
testl %r9d, %r9d
je 0x8efce
leal (,%r9,4), %ecx
andl $0x4, %ecx
leal -0x1(%r9), %r11d
shrl %r11d
addq %rsi, %r11
leaq (%r9,%rsi), %rbx
decq %rbx
movl %r9d, %ebp
movzbl (%r11), %r14d
shrl %cl, %r14d
andb $0xf, %r14b
movb %r14b, (%rbx)
xorl %r14d, %r14d
cmpl $0x4, %ecx
leal 0x4(%rcx), %ecx
sete %r14b
cmovel %r10d, %ecx
subq %r14, %r11
decq %rbx
decl %ebp
jne 0x8efa2
movq %r9, %r10
movb $0x8, %cl
movb %cl, 0x11(%rdi)
movb %cl, 0x13(%rdi)
movq %r10, 0x8(%rdi)
testq %rax, %rax
je 0x8f063
leal (,%r9,4), %ecx
movb $0x4, %r10b
movb $0x6, %r11b
movb $0x20, %bl
testl %r9d, %r9d
je 0x8f0be
leaq (%rsi,%rcx), %r14
decq %r14
addq %r9, %rsi
decq %rsi
xorl %r15d, %r15d
movzbl (%rsi,%r15), %r12d
movb $-0x1, %bpl
cmpl %r8d, %r12d
jge 0x8f01d
movb (%rax,%r12), %bpl
movb %bpl, (%r14,%r15,4)
movzbl (%rsi,%r15), %r12d
leaq (%r12,%r12,2), %r12
movb 0x2(%rdx,%r12), %bpl
movb %bpl, -0x1(%r14,%r15,4)
movzbl (%rsi,%r15), %r12d
leaq (%r12,%r12,2), %r12
movb 0x1(%rdx,%r12), %bpl
movb %bpl, -0x2(%r14,%r15,4)
movzbl (%rsi,%r15), %r12d
leaq (%r12,%r12,2), %r12
movb (%rdx,%r12), %bpl
movb %bpl, -0x3(%r14,%r15,4)
decq %r15
decl %r9d
jne 0x8f00c
jmp 0x8f0be
leal (%r9,%r9,2), %ecx
movb $0x3, %r10b
movb $0x2, %r11b
movb $0x18, %bl
testl %r9d, %r9d
je 0x8f0be
leaq (%rsi,%rcx), %rax
decq %rax
addq %r9, %rsi
decq %rsi
movzbl (%rsi), %r8d
leaq (%r8,%r8,2), %r8
movb 0x2(%rdx,%r8), %r8b
movb %r8b, (%rax)
movzbl (%rsi), %r8d
leaq (%r8,%r8,2), %r8
movb 0x1(%rdx,%r8), %r8b
movb %r8b, -0x1(%rax)
movzbl (%rsi), %r8d
leaq (%r8,%r8,2), %r8
movb (%rdx,%r8), %r8b
movb %r8b, -0x2(%rax)
decq %rsi
addq $-0x3, %rax
decl %r9d
jne 0x8f081
movb $0x8, 0x11(%rdi)
movb %bl, 0x13(%rdi)
movq %rcx, 0x8(%rdi)
movb %r11b, 0x10(%rdi)
movb %r10b, 0x12(%rdi)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq