// File: 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:
movq %rdi, -0x8(%rsp)
movq %rsi, -0x10(%rsp)
movq %rdx, -0x18(%rsp)
movq %rcx, -0x20(%rsp)
movq -0x8(%rsp), %rax
movl (%rax), %eax
movl %eax, -0x38(%rsp)
movq -0x8(%rsp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x2, %eax
jne 0x11014a
cmpq $0x0, -0x18(%rsp)
je 0x11014a
movq -0x8(%rsp), %rax
movzbl 0x11(%rax), %eax
cmpl $0x8, %eax
jne 0x11014a
movq -0x10(%rsp), %rax
movq %rax, -0x28(%rsp)
movq -0x10(%rsp), %rax
movq %rax, -0x30(%rsp)
movl $0x0, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x38(%rsp), %eax
jae 0x1100cd
movq -0x28(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rsp)
movzbl (%rax), %eax
movl %eax, -0x3c(%rsp)
movq -0x28(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rsp)
movzbl (%rax), %eax
movl %eax, -0x40(%rsp)
movq -0x28(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rsp)
movzbl (%rax), %eax
movl %eax, -0x44(%rsp)
movl -0x3c(%rsp), %eax
sarl $0x3, %eax
andl $0x1f, %eax
shll $0xa, %eax
movl -0x40(%rsp), %ecx
sarl $0x3, %ecx
andl $0x1f, %ecx
shll $0x5, %ecx
orl %ecx, %eax
movl -0x44(%rsp), %ecx
sarl $0x3, %ecx
andl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x48(%rsp)
movq -0x18(%rsp), %rax
movslq -0x48(%rsp), %rcx
movb (%rax,%rcx), %cl
movq -0x30(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rsp)
movb %cl, (%rax)
movl -0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rsp)
jmp 0x11001b
movq -0x8(%rsp), %rax
movb $0x3, 0x10(%rax)
movq -0x8(%rsp), %rax
movb $0x1, 0x12(%rax)
movq -0x8(%rsp), %rax
movb 0x11(%rax), %cl
movq -0x8(%rsp), %rax
movb %cl, 0x13(%rax)
movq -0x8(%rsp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x8, %eax
jl 0x110119
movl -0x38(%rsp), %eax
movq -0x8(%rsp), %rcx
movzbl 0x13(%rcx), %ecx
shrq $0x3, %rcx
imulq %rcx, %rax
movq %rax, -0x60(%rsp)
jmp 0x110137
movl -0x38(%rsp), %eax
movq -0x8(%rsp), %rcx
movzbl 0x13(%rcx), %ecx
imulq %rcx, %rax
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, -0x60(%rsp)
movq -0x60(%rsp), %rcx
movq -0x8(%rsp), %rax
movq %rcx, 0x8(%rax)
jmp 0x110348
movq -0x8(%rsp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x6, %eax
jne 0x1102d0
cmpq $0x0, -0x18(%rsp)
je 0x1102d0
movq -0x8(%rsp), %rax
movzbl 0x11(%rax), %eax
cmpl $0x8, %eax
jne 0x1102d0
movq -0x10(%rsp), %rax
movq %rax, -0x28(%rsp)
movq -0x10(%rsp), %rax
movq %rax, -0x30(%rsp)
movl $0x0, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x38(%rsp), %eax
jae 0x110256
movq -0x28(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rsp)
movzbl (%rax), %eax
movl %eax, -0x4c(%rsp)
movq -0x28(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rsp)
movzbl (%rax), %eax
movl %eax, -0x50(%rsp)
movq -0x28(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x28(%rsp)
movzbl (%rax), %eax
movl %eax, -0x54(%rsp)
movq -0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rsp)
movl -0x4c(%rsp), %eax
sarl $0x3, %eax
andl $0x1f, %eax
shll $0xa, %eax
movl -0x50(%rsp), %ecx
sarl $0x3, %ecx
andl $0x1f, %ecx
shll $0x5, %ecx
orl %ecx, %eax
movl -0x54(%rsp), %ecx
sarl $0x3, %ecx
andl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x58(%rsp)
movq -0x18(%rsp), %rax
movslq -0x58(%rsp), %rcx
movb (%rax,%rcx), %cl
movq -0x30(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rsp)
movb %cl, (%rax)
movl -0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rsp)
jmp 0x110196
movq -0x8(%rsp), %rax
movb $0x3, 0x10(%rax)
movq -0x8(%rsp), %rax
movb $0x1, 0x12(%rax)
movq -0x8(%rsp), %rax
movb 0x11(%rax), %cl
movq -0x8(%rsp), %rax
movb %cl, 0x13(%rax)
movq -0x8(%rsp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x8, %eax
jl 0x1102a2
movl -0x38(%rsp), %eax
movq -0x8(%rsp), %rcx
movzbl 0x13(%rcx), %ecx
shrq $0x3, %rcx
imulq %rcx, %rax
movq %rax, -0x68(%rsp)
jmp 0x1102c0
movl -0x38(%rsp), %eax
movq -0x8(%rsp), %rcx
movzbl 0x13(%rcx), %ecx
imulq %rcx, %rax
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, -0x68(%rsp)
movq -0x68(%rsp), %rcx
movq -0x8(%rsp), %rax
movq %rcx, 0x8(%rax)
jmp 0x110346
movq -0x8(%rsp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x3, %eax
jne 0x110344
cmpq $0x0, -0x20(%rsp)
je 0x110344
movq -0x8(%rsp), %rax
movzbl 0x11(%rax), %eax
cmpl $0x8, %eax
jne 0x110344
movq -0x10(%rsp), %rax
movq %rax, -0x28(%rsp)
movl $0x0, -0x34(%rsp)
movl -0x34(%rsp), %eax
cmpl -0x38(%rsp), %eax
jae 0x110342
movq -0x20(%rsp), %rax
movq -0x28(%rsp), %rcx
movzbl (%rcx), %ecx
movb (%rax,%rcx), %cl
movq -0x28(%rsp), %rax
movb %cl, (%rax)
movl -0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rsp)
movq -0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rsp)
jmp 0x110306
jmp 0x110344
jmp 0x110346
jmp 0x110348
retq
nopl (%rax)