// File: /workspace/github_cpp_makefile_star2_O1/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 0x917b8
cmpb $0x10, %cl
setne %r10b
testq %rax, %rax
sete %r11b
orb %r10b, %r11b
jne 0x91b1e
movzbl 0x10(%rdi), %r10d
cmpq $0x6, %r10
ja 0x91b1e
leaq 0x1b5e2(%rip), %r11     # 0xacdb0
movslq (%r11,%r10,4), %r10
addq %r11, %r10
jmpq *%r10
cmpb $0x2, %cl
sete %cl
testl %r9d, %r9d
setne %r10b
andb %cl, %r10b
cmpb $0x1, %r10b
jne 0x918a6
xorl %ecx, %ecx
movq %rsi, %r10
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
leal (,%rbx,4), %r15d
leal (%r15,%r15,4), %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
cmpl %r9d, %ecx
jb 0x917f7
movzbl 0x11(%rdi), %ecx
cmpl $0x4, %ecx
je 0x91ada
cmpl $0x8, %ecx
je 0x91abf
cmpl $0x10, %ecx
jne 0x91b1e
testl %r9d, %r9d
je 0x91b1e
xorl %edx, %edx
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
cmpl %edx, %r9d
jne 0x918d0
jmp 0x91b1e
cmpb $0x8, %cl
jne 0x919a5
testl %r9d, %r9d
je 0x91b1e
xorl %eax, %eax
movzbl (%rsi,%rax,2), %ecx
movb (%rdx,%rcx), %cl
movb %cl, (%rsi,%rax,2)
incq %rax
cmpl %eax, %r9d
jne 0x9190f
jmp 0x91b1e
cmpb $0x8, %cl
jne 0x919db
testl %r9d, %r9d
je 0x91b1e
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
decl %r9d
jne 0x91938
jmp 0x91b1e
cmpb $0x8, %cl
jne 0x91a4c
testl %r9d, %r9d
je 0x91b1e
xorl %eax, %eax
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
cmpl %eax, %r9d
jne 0x91976
jmp 0x91b1e
testl %r9d, %r9d
je 0x91b1e
xorl %edx, %edx
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
cmpl %edx, %r9d
jne 0x919b0
jmp 0x91b1e
testl %r9d, %r9d
je 0x91b1e
addq $0x5, %rsi
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
decl %r9d
jne 0x919e8
jmp 0x91b1e
testl %r9d, %r9d
je 0x91b1e
xorl %edx, %edx
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
cmpl %edx, %r9d
jne 0x91a57
jmp 0x91b1e
testl %r9d, %r9d
je 0x91b1e
xorl %eax, %eax
movzbl (%rsi,%rax), %ecx
movb (%rdx,%rcx), %cl
movb %cl, (%rsi,%rax)
incq %rax
cmpl %eax, %r9d
jne 0x91ac6
jmp 0x91b1e
testl %r9d, %r9d
je 0x91b1e
xorl %eax, %eax
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
movl %r8d, %edi
shll $0x4, %edi
orq %r8, %rdi
movb (%rdx,%rdi), %dil
shrb $0x4, %dil
orb %cl, %dil
movb %dil, (%rsi)
incq %rsi
addl $0x2, %eax
cmpl %r9d, %eax
jb 0x91ae1
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq