// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/png/pngrtran.c
// Function: png_do_quantize
void /* PRIVATE */
png_do_quantize(png_row_infop row_info, png_bytep row,
    png_bytep palette_lookup, png_bytep quantize_lookup)
{
   png_bytep sp, dp;
   png_uint_32 i;
   png_uint_32 row_width=row_info->width;

   png_debug(1, "in png_do_quantize");

   {
      if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
         palette_lookup && row_info->bit_depth == 8)
      {
         int r, g, b, p;
         sp = row;
         dp = row;
         for (i = 0; i < row_width; i++)
         {
            r = *sp++;
            g = *sp++;
            b = *sp++;

            /* This looks real messy, but the compiler will reduce
             * it down to a reasonable formula.  For example, with
             * 5 bits per color, we get:
             * p = (((r >> 3) & 0x1f) << 10) |
             *    (((g >> 3) & 0x1f) << 5) |
             *    ((b >> 3) & 0x1f);
             */
            p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
               ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
               (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
               (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
               ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
               (PNG_QUANTIZE_BLUE_BITS)) |
               ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
               ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));

            *dp++ = palette_lookup[p];
         }
         row_info->color_type = PNG_COLOR_TYPE_PALETTE;
         row_info->channels = 1;
         row_info->pixel_depth = row_info->bit_depth;
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
         palette_lookup != NULL && row_info->bit_depth == 8)
      {
         int r, g, b, p;
         sp = row;
         dp = row;
         for (i = 0; i < row_width; i++)
         {
            r = *sp++;
            g = *sp++;
            b = *sp++;
            sp++;

            p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
               ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
               (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
               (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
               ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
               (PNG_QUANTIZE_BLUE_BITS)) |
               ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
               ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));

            *dp++ = palette_lookup[p];
         }
         row_info->color_type = PNG_COLOR_TYPE_PALETTE;
         row_info->channels = 1;
         row_info->pixel_depth = row_info->bit_depth;
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
         quantize_lookup && row_info->bit_depth == 8)
      {
         sp = row;
         for (i = 0; i < row_width; i++, sp++)
         {
            *sp = quantize_lookup[*sp];
         }
      }
   }
}

// Assembly:
movl (%rdi), %eax
movb 0x10(%rdi), %r8b
cmpb $0x2, %r8b
setne %r9b
testq %rdx, %rdx
sete %r10b
orb %r9b, %r10b
jne 0x90eea
cmpb $0x8, 0x11(%rdi)
jne 0x90fae
testq %rax, %rax
je 0x90faf
xorl %ecx, %ecx
movq %rsi, %r8
movzbl (%r8), %r9d
movzbl 0x1(%r8), %r10d
movzbl 0x2(%r8), %r11d
shll $0x7, %r9d
andl $0x7c00, %r9d           # imm = 0x7C00
andl $-0x8, %r10d
leal (%r9,%r10,4), %r9d
shrl $0x3, %r11d
orl %r9d, %r11d
movb (%rdx,%r11), %r9b
movb %r9b, (%rsi,%rcx)
incq %rcx
addq $0x3, %r8
cmpl %ecx, %eax
jne 0x90ead
jmp 0x90f4d
testq %rdx, %rdx
sete %r9b
cmpb $0x6, %r8b
setne %r10b
orb %r9b, %r10b
jne 0x90f70
cmpb $0x8, 0x11(%rdi)
jne 0x90fae
testq %rax, %rax
je 0x90faf
xorl %ecx, %ecx
movzbl (%rsi,%rcx,4), %r8d
movzbl 0x1(%rsi,%rcx,4), %r9d
movzbl 0x2(%rsi,%rcx,4), %r10d
shll $0x7, %r8d
andl $0x7c00, %r8d           # imm = 0x7C00
andl $-0x8, %r9d
leal (%r8,%r9,4), %r8d
shrl $0x3, %r10d
orl %r8d, %r10d
movb (%rdx,%r10), %r8b
movb %r8b, (%rsi,%rcx)
incq %rcx
cmpl %ecx, %eax
jne 0x90f13
movzbl 0x11(%rdi), %ecx
movb $0x3, 0x10(%rdi)
movb $0x1, 0x12(%rdi)
movb %cl, 0x13(%rdi)
cmpq $0x8, %rcx
jae 0x90fbb
imulq %rax, %rcx
addq $0x7, %rcx
shrq $0x3, %rcx
jmp 0x90fc5
cmpb $0x3, %r8b
setne %dl
testq %rcx, %rcx
sete %r8b
orb %dl, %r8b
jne 0x90fae
cmpb $0x8, 0x11(%rdi)
sete %dl
testl %eax, %eax
setne %dil
andb %dl, %dil
cmpb $0x1, %dil
jne 0x90fae
xorl %edx, %edx
movzbl (%rsi,%rdx), %edi
movb (%rcx,%rdi), %dil
movb %dil, (%rsi,%rdx)
incq %rdx
cmpl %edx, %eax
jne 0x90f9b
retq
movb $0x3, 0x10(%rdi)
movw $0x801, 0x12(%rdi)      # imm = 0x801
movb $0x8, %cl
shrb $0x3, %cl
movzbl %cl, %ecx
imulq %rax, %rcx
movq %rcx, 0x8(%rdi)
retq