// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/png/pngrtran.c
// Function: png_do_gamma
void /* PRIVATE */
png_do_gamma(png_row_infop row_info, png_bytep row,
   png_bytep gamma_table, png_uint_16pp gamma_16_table,
   int gamma_shift)
{
   png_bytep sp;
   png_uint_32 i;
   png_uint_32 row_width=row_info->width;

   png_debug(1, "in png_do_gamma");

   if (
       ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
        (row_info->bit_depth == 16 && gamma_16_table != NULL)))
   {
      switch (row_info->color_type)
      {
         case PNG_COLOR_TYPE_RGB:
         {
            if (row_info->bit_depth == 8)
            {
               sp = row;
               for (i = 0; i < row_width; i++)
               {
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
               }
            }
            else /* if (row_info->bit_depth == 16) */
            {
               sp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_uint_16 v;

                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
               }
            }
            break;
         }

         case PNG_COLOR_TYPE_RGB_ALPHA:
         {
            if (row_info->bit_depth == 8)
            {
               sp = row;
               for (i = 0; i < row_width; i++)
               {
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
                  sp++;
               }
            }
            else /* if (row_info->bit_depth == 16) */
            {
               sp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 4;
               }
            }
            break;
         }

         case PNG_COLOR_TYPE_GRAY_ALPHA:
         {
            if (row_info->bit_depth == 8)
            {
               sp = row;
               for (i = 0; i < row_width; i++)
               {
                  *sp = gamma_table[*sp];
                  sp += 2;
               }
            }
            else /* if (row_info->bit_depth == 16) */
            {
               sp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 4;
               }
            }
            break;
         }

         case PNG_COLOR_TYPE_GRAY:
         {
            if (row_info->bit_depth == 2)
            {
               sp = row;
               for (i = 0; i < row_width; i += 4)
               {
                  int a = *sp & 0xc0;
                  int b = *sp & 0x30;
                  int c = *sp & 0x0c;
                  int d = *sp & 0x03;

                  *sp = (png_byte)(
                      ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|
                      ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
                      ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
                      ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
                  sp++;
               }
            }

            if (row_info->bit_depth == 4)
            {
               sp = row;
               for (i = 0; i < row_width; i += 2)
               {
                  int msb = *sp & 0xf0;
                  int lsb = *sp & 0x0f;

                  *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
                      | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
                  sp++;
               }
            }

            else if (row_info->bit_depth == 8)
            {
               sp = row;
               for (i = 0; i < row_width; i++)
               {
                  *sp = gamma_table[*sp];
                  sp++;
               }
            }

            else if (row_info->bit_depth == 16)
            {
               sp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
               }
            }
            break;
         }
      }
   }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, %rax
movl (%rdi), %r9d
movb 0x11(%rdi), %cl
cmpb $0x9, %cl
setb %r10b
testq %rdx, %rdx
setne %r11b
testb %r10b, %r11b
jne 0x83561
cmpb $0x10, %cl
setne %r10b
testq %rax, %rax
sete %r11b
orb %r10b, %r11b
jne 0x83874
movzbl 0x10(%rdi), %r10d
cmpq $0x6, %r10
ja 0x83874
leaq 0x1a559(%rip), %r11     # 0x9dad0
movslq (%r11,%r10,4), %r10
addq %r11, %r10
jmpq *%r10
cmpb $0x2, %cl
jne 0x836fe
xorl %ecx, %ecx
movq %rsi, %r10
cmpl %r9d, %ecx
jae 0x836fb
movzbl (%r10), %r15d
movl %r15d, %r12d
andl $-0x40, %r12d
movl %r15d, %r14d
andl $0x30, %r14d
movl %r15d, %ebx
andl $0xc, %ebx
movl %r15d, %r11d
andl $0x3, %r11d
movl %r12d, %ebp
shrl $0x2, %ebp
movl %r12d, %r13d
shrl $0x4, %r13d
shrl $0x6, %r15d
orl %r13d, %r15d
orl %ebp, %r15d
orq %r12, %r15
movb (%rdx,%r15), %bpl
andb $-0x40, %bpl
movl %r14d, %r15d
shrl $0x2, %r15d
leal (%r15,%r14,4), %r15d
movl %r14d, %r12d
shrl $0x4, %r12d
orl %r15d, %r12d
orq %r14, %r12
movb (%rdx,%r12), %r14b
shrb $0x2, %r14b
andb $0x30, %r14b
orb %bpl, %r14b
imull $0x14, %ebx, %ebp
movl %ebx, %r15d
shrl $0x2, %r15d
orl %ebp, %r15d
orq %rbx, %r15
movb (%rdx,%r15), %bl
shrb $0x4, %bl
andb $0xc, %bl
orb %r14b, %bl
imulq $0x55, %r11, %r11
movb (%rdx,%r11), %r11b
shrb $0x6, %r11b
orb %bl, %r11b
movb %r11b, (%r10)
incq %r10
addl $0x4, %ecx
jmp 0x8358f
cmpb $0x8, %cl
jne 0x836cd
xorl %eax, %eax
cmpl %eax, %r9d
je 0x83874
movzbl (%rsi,%rax,2), %ecx
movb (%rdx,%rcx), %cl
movb %cl, (%rsi,%rax,2)
incq %rax
jmp 0x83645
cmpb $0x8, %cl
jne 0x8374a
subl $0x1, %r9d
jb 0x83874
movzbl (%rsi), %eax
movb (%rdx,%rax), %al
movb %al, (%rsi)
movzbl 0x1(%rsi), %eax
movb (%rdx,%rax), %al
movb %al, 0x1(%rsi)
movzbl 0x2(%rsi), %eax
movb (%rdx,%rax), %al
movb %al, 0x2(%rsi)
addq $0x3, %rsi
jmp 0x83666
cmpb $0x8, %cl
jne 0x837b4
xorl %eax, %eax
cmpl %eax, %r9d
je 0x83874
movzbl (%rsi,%rax,4), %ecx
movb (%rdx,%rcx), %cl
movb %cl, (%rsi,%rax,4)
movzbl 0x1(%rsi,%rax,4), %ecx
movb (%rdx,%rcx), %cl
movb %cl, 0x1(%rsi,%rax,4)
movzbl 0x2(%rsi,%rax,4), %ecx
movb (%rdx,%rcx), %cl
movb %cl, 0x2(%rsi,%rax,4)
incq %rax
jmp 0x8369d
xorl %edx, %edx
cmpl %edx, %r9d
je 0x83874
movzbl 0x1(%rsi,%rdx,4), %edi
movl %r8d, %ecx
shrl %cl, %edi
movq (%rax,%rdi,8), %rcx
movzbl (%rsi,%rdx,4), %edi
movzwl (%rcx,%rdi,2), %ecx
rolw $0x8, %cx
movw %cx, (%rsi,%rdx,4)
incq %rdx
jmp 0x836cf
movb 0x11(%rdi), %cl
cmpb $0x4, %cl
je 0x83838
movzbl %cl, %ecx
cmpl $0x8, %ecx
je 0x83822
cmpl $0x10, %ecx
jne 0x83874
xorl %edx, %edx
cmpl %edx, %r9d
je 0x83874
movzbl 0x1(%rsi,%rdx,2), %edi
movl %r8d, %ecx
shrl %cl, %edi
movq (%rax,%rdi,8), %rcx
movzbl (%rsi,%rdx,2), %edi
movzwl (%rcx,%rdi,2), %ecx
rolw $0x8, %cx
movw %cx, (%rsi,%rdx,2)
incq %rdx
jmp 0x8371e
addq $0x5, %rsi
subl $0x1, %r9d
jb 0x83874
movzbl -0x4(%rsi), %edx
movl %r8d, %ecx
shrl %cl, %edx
movq (%rax,%rdx,8), %rcx
movzbl -0x5(%rsi), %edx
movzwl (%rcx,%rdx,2), %ecx
rolw $0x8, %cx
movw %cx, -0x5(%rsi)
movzbl -0x2(%rsi), %edx
movl %r8d, %ecx
shrl %cl, %edx
movq (%rax,%rdx,8), %rcx
movzbl -0x3(%rsi), %edx
movzwl (%rcx,%rdx,2), %ecx
rolw $0x8, %cx
movw %cx, -0x3(%rsi)
movzbl (%rsi), %edx
movl %r8d, %ecx
shrl %cl, %edx
movq (%rax,%rdx,8), %rcx
movzbl -0x1(%rsi), %edx
movzwl (%rcx,%rdx,2), %ecx
rolw $0x8, %cx
movw %cx, -0x1(%rsi)
addq $0x6, %rsi
jmp 0x8374e
xorl %edx, %edx
cmpl %edx, %r9d
je 0x83874
movzbl 0x1(%rsi,%rdx,8), %edi
movl %r8d, %ecx
shrl %cl, %edi
movq (%rax,%rdi,8), %rcx
movzbl (%rsi,%rdx,8), %edi
movzwl (%rcx,%rdi,2), %ecx
rolw $0x8, %cx
movw %cx, (%rsi,%rdx,8)
movzbl 0x3(%rsi,%rdx,8), %edi
movl %r8d, %ecx
shrl %cl, %edi
movq (%rax,%rdi,8), %rcx
movzbl 0x2(%rsi,%rdx,8), %edi
movzwl (%rcx,%rdi,2), %ecx
rolw $0x8, %cx
movw %cx, 0x2(%rsi,%rdx,8)
movzbl 0x5(%rsi,%rdx,8), %edi
movl %r8d, %ecx
shrl %cl, %edi
movq (%rax,%rdi,8), %rcx
movzbl 0x4(%rsi,%rdx,8), %edi
movzwl (%rcx,%rdi,2), %ecx
rolw $0x8, %cx
movw %cx, 0x4(%rsi,%rdx,8)
incq %rdx
jmp 0x837b6
xorl %eax, %eax
cmpl %eax, %r9d
je 0x83874
movzbl (%rsi,%rax), %ecx
movb (%rdx,%rcx), %cl
movb %cl, (%rsi,%rax)
incq %rax
jmp 0x83824
xorl %eax, %eax
cmpl %r9d, %eax
jae 0x83874
movzbl (%rsi), %ecx
movl %ecx, %edi
andl $-0x10, %edi
movl %ecx, %r8d
andl $0xf, %r8d
shrl $0x4, %ecx
orq %rdi, %rcx
movb (%rdx,%rcx), %cl
andb $-0x10, %cl
imulq $0x11, %r8, %rdi
movb (%rdx,%rdi), %dil
shrb $0x4, %dil
orb %cl, %dil
movb %dil, (%rsi)
incq %rsi
addl $0x2, %eax
jmp 0x8383a
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq