// File: 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:
movq %rdi, -0x8(%rsp)
movq %rsi, -0x10(%rsp)
movq %rdx, -0x18(%rsp)
movq %rcx, -0x20(%rsp)
movl %r8d, -0x24(%rsp)
movq -0x8(%rsp), %rax
movl (%rax), %eax
movl %eax, -0x48(%rsp)
movq -0x8(%rsp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x3, %eax
jne 0x10b4d8
movq -0x8(%rsp), %rax
movzbl 0x11(%rax), %eax
cmpl $0x8, %eax
jge 0x10b23e
movq -0x8(%rsp), %rax
movzbl 0x11(%rax), %eax
movl %eax, -0x4c(%rsp)
subl $0x1, %eax
je 0x10afdd
jmp 0x10afbc
movl -0x4c(%rsp), %eax
subl $0x2, %eax
je 0x10b0a4
jmp 0x10afcb
movl -0x4c(%rsp), %eax
subl $0x4, %eax
je 0x10b167
jmp 0x10b21d
movq -0x10(%rsp), %rax
movl -0x48(%rsp), %ecx
subl $0x1, %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x38(%rsp)
movq -0x10(%rsp), %rax
movl -0x48(%rsp), %ecx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x40(%rsp)
movl -0x48(%rsp), %ecx
addl $0x7, %ecx
andl $0x7, %ecx
movl $0x7, %eax
subl %ecx, %eax
movl %eax, -0x28(%rsp)
movl $0x0, -0x44(%rsp)
movl -0x44(%rsp), %eax
cmpl -0x48(%rsp), %eax
jae 0x10b09f
movq -0x38(%rsp), %rax
movzbl (%rax), %eax
movl -0x28(%rsp), %ecx
sarl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x10b052
movq -0x40(%rsp), %rax
movb $0x1, (%rax)
jmp 0x10b05a
movq -0x40(%rsp), %rax
movb $0x0, (%rax)
cmpl $0x7, -0x28(%rsp)
jne 0x10b079
movl $0x0, -0x28(%rsp)
movq -0x38(%rsp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rsp)
jmp 0x10b084
movl -0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rsp)
movq -0x40(%rsp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rsp)
movl -0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rsp)
jmp 0x10b028
jmp 0x10b21d
movq -0x10(%rsp), %rax
movl -0x48(%rsp), %ecx
subl $0x1, %ecx
shrl $0x2, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x38(%rsp)
movq -0x10(%rsp), %rax
movl -0x48(%rsp), %ecx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x40(%rsp)
movl -0x48(%rsp), %ecx
addl $0x3, %ecx
andl $0x3, %ecx
movl $0x3, %eax
subl %ecx, %eax
shll %eax
movl %eax, -0x28(%rsp)
movl $0x0, -0x44(%rsp)
movl -0x44(%rsp), %eax
cmpl -0x48(%rsp), %eax
jae 0x10b162
movq -0x38(%rsp), %rax
movzbl (%rax), %eax
movl -0x28(%rsp), %ecx
sarl %cl, %eax
andl $0x3, %eax
movl %eax, -0x2c(%rsp)
movl -0x2c(%rsp), %eax
movb %al, %cl
movq -0x40(%rsp), %rax
movb %cl, (%rax)
cmpl $0x6, -0x28(%rsp)
jne 0x10b13c
movl $0x0, -0x28(%rsp)
movq -0x38(%rsp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rsp)
jmp 0x10b147
movl -0x28(%rsp), %eax
addl $0x2, %eax
movl %eax, -0x28(%rsp)
movq -0x40(%rsp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rsp)
movl -0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rsp)
jmp 0x10b0f1
jmp 0x10b21d
movq -0x10(%rsp), %rax
movl -0x48(%rsp), %ecx
subl $0x1, %ecx
shrl %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x38(%rsp)
movq -0x10(%rsp), %rax
movl -0x48(%rsp), %ecx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x40(%rsp)
movl -0x48(%rsp), %eax
andl $0x1, %eax
shll $0x2, %eax
movl %eax, -0x28(%rsp)
movl $0x0, -0x44(%rsp)
movl -0x44(%rsp), %eax
cmpl -0x48(%rsp), %eax
jae 0x10b21b
movq -0x38(%rsp), %rax
movzbl (%rax), %eax
movl -0x28(%rsp), %ecx
sarl %cl, %eax
andl $0xf, %eax
movl %eax, -0x2c(%rsp)
movl -0x2c(%rsp), %eax
movb %al, %cl
movq -0x40(%rsp), %rax
movb %cl, (%rax)
cmpl $0x4, -0x28(%rsp)
jne 0x10b1f5
movl $0x0, -0x28(%rsp)
movq -0x38(%rsp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rsp)
jmp 0x10b200
movl -0x28(%rsp), %eax
addl $0x4, %eax
movl %eax, -0x28(%rsp)
movq -0x40(%rsp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rsp)
movl -0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rsp)
jmp 0x10b1aa
jmp 0x10b21d
movq -0x8(%rsp), %rax
movb $0x8, 0x11(%rax)
movq -0x8(%rsp), %rax
movb $0x8, 0x13(%rax)
movl -0x48(%rsp), %eax
movl %eax, %ecx
movq -0x8(%rsp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rsp), %rax
movb 0x11(%rax), %al
subb $0x8, %al
jne 0x10b4d6
jmp 0x10b250
cmpq $0x0, -0x20(%rsp)
je 0x10b3c0
movq -0x10(%rsp), %rax
movl -0x48(%rsp), %ecx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x38(%rsp)
movq -0x10(%rsp), %rax
movl -0x48(%rsp), %ecx
shll $0x2, %ecx
movl %ecx, %ecx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x40(%rsp)
movl $0x0, -0x44(%rsp)
movl -0x44(%rsp), %eax
cmpl -0x48(%rsp), %eax
jae 0x10b383
movq -0x38(%rsp), %rax
movzbl (%rax), %eax
cmpl -0x24(%rsp), %eax
jl 0x10b2c5
movq -0x40(%rsp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x40(%rsp)
movb $-0x1, (%rax)
jmp 0x10b2e8
movq -0x20(%rsp), %rax
movq -0x38(%rsp), %rcx
movzbl (%rcx), %ecx
movb (%rax,%rcx), %cl
movq -0x40(%rsp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x40(%rsp)
movb %cl, (%rax)
movq -0x18(%rsp), %rax
movq -0x38(%rsp), %rcx
movzbl (%rcx), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movb 0x2(%rax), %cl
movq -0x40(%rsp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x40(%rsp)
movb %cl, (%rax)
movq -0x18(%rsp), %rax
movq -0x38(%rsp), %rcx
movzbl (%rcx), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movb 0x1(%rax), %cl
movq -0x40(%rsp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x40(%rsp)
movb %cl, (%rax)
movq -0x18(%rsp), %rax
movq -0x38(%rsp), %rcx
movzbl (%rcx), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movb (%rax), %cl
movq -0x40(%rsp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x40(%rsp)
movb %cl, (%rax)
movq -0x38(%rsp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rsp)
movl -0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rsp)
jmp 0x10b293
movq -0x8(%rsp), %rax
movb $0x8, 0x11(%rax)
movq -0x8(%rsp), %rax
movb $0x20, 0x13(%rax)
movl -0x48(%rsp), %eax
shll $0x2, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x8(%rsp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rsp), %rax
movb $0x6, 0x10(%rax)
movq -0x8(%rsp), %rax
movb $0x4, 0x12(%rax)
jmp 0x10b4d4
movq -0x10(%rsp), %rax
movl -0x48(%rsp), %ecx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x38(%rsp)
movq -0x10(%rsp), %rax
imull $0x3, -0x48(%rsp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x40(%rsp)
movl $0x0, -0x44(%rsp)
movl -0x44(%rsp), %eax
cmpl -0x48(%rsp), %eax
jae 0x10b49e
movq -0x18(%rsp), %rax
movq -0x38(%rsp), %rcx
movzbl (%rcx), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movb 0x2(%rax), %cl
movq -0x40(%rsp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x40(%rsp)
movb %cl, (%rax)
movq -0x18(%rsp), %rax
movq -0x38(%rsp), %rcx
movzbl (%rcx), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movb 0x1(%rax), %cl
movq -0x40(%rsp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x40(%rsp)
movb %cl, (%rax)
movq -0x18(%rsp), %rax
movq -0x38(%rsp), %rcx
movzbl (%rcx), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movb (%rax), %cl
movq -0x40(%rsp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, -0x40(%rsp)
movb %cl, (%rax)
movq -0x38(%rsp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rsp)
movl -0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rsp)
jmp 0x10b3f5
movq -0x8(%rsp), %rax
movb $0x8, 0x11(%rax)
movq -0x8(%rsp), %rax
movb $0x18, 0x13(%rax)
imull $0x3, -0x48(%rsp), %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x8(%rsp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rsp), %rax
movb $0x2, 0x10(%rax)
movq -0x8(%rsp), %rax
movb $0x3, 0x12(%rax)
jmp 0x10b4d6
jmp 0x10b4d8
retq
nopl (%rax)