// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/png/pngwtran.c
// Function: png_do_shift
void /* PRIVATE */
png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
{
   png_debug(1, "in png_do_shift");

   if (
      row_info->color_type != PNG_COLOR_TYPE_PALETTE)
   {
      int shift_start[4], shift_dec[4];
      int channels = 0;

      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
      {
         shift_start[channels] = row_info->bit_depth - bit_depth->red;
         shift_dec[channels] = bit_depth->red;
         channels++;
         shift_start[channels] = row_info->bit_depth - bit_depth->green;
         shift_dec[channels] = bit_depth->green;
         channels++;
         shift_start[channels] = row_info->bit_depth - bit_depth->blue;
         shift_dec[channels] = bit_depth->blue;
         channels++;
      }
      else
      {
         shift_start[channels] = row_info->bit_depth - bit_depth->gray;
         shift_dec[channels] = bit_depth->gray;
         channels++;
      }
      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
      {
         shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
         shift_dec[channels] = bit_depth->alpha;
         channels++;
      }

      /* With low row depths, could only be grayscale, so one channel */
      if (row_info->bit_depth < 8)
      {
         png_bytep bp = row;
         png_uint_32 i;
         png_byte mask;
         png_uint_32 row_bytes = row_info->rowbytes;

         if (bit_depth->gray == 1 && row_info->bit_depth == 2)
            mask = 0x55;
         else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
            mask = 0x11;
         else
            mask = 0xff;

         for (i = 0; i < row_bytes; i++, bp++)
         {
            png_uint_16 v;
            int j;

            v = *bp;
            *bp = 0;
            for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
            {
               if (j > 0)
                  *bp |= (png_byte)((v << j) & 0xff);
               else
                  *bp |= (png_byte)((v >> (-j)) & mask);
            }
         }
      }
      else if (row_info->bit_depth == 8)
      {
         png_bytep bp = row;
         png_uint_32 i;
         png_uint_32 istop = channels * row_info->width;

         for (i = 0; i < istop; i++, bp++)
         {

            png_uint_16 v;
            int j;
            int c = (int)(i%channels);

            v = *bp;
            *bp = 0;
            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
            {
               if (j > 0)
                  *bp |= (png_byte)((v << j) & 0xff);
               else
                  *bp |= (png_byte)((v >> (-j)) & 0xff);
            }
         }
      }
      else
      {
         png_bytep bp;
         png_uint_32 i;
         png_uint_32 istop = channels * row_info->width;

         for (bp = row, i = 0; i < istop; i++)
         {
            int c = (int)(i%channels);
            png_uint_16 value, v;
            int j;

            v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
            value = 0;
            for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
            {
               if (j > 0)
                  value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
               else
                  value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
            }
            *bp++ = (png_byte)(value >> 8);
            *bp++ = (png_byte)(value & 0xff);
         }
      }
   }
}

// Assembly:
movb 0x10(%rdi), %cl
cmpb $0x3, %cl
je 0x91cec
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
movzbl 0x11(%rdi), %eax
testb $0x2, %cl
jne 0x91b1a
movb 0x3(%rdx), %r9b
movl $0x1, %r8d
jmp 0x91b4d
movb (%rdx), %r9b
movzbl 0x1(%rdx), %r8d
movl %eax, %r10d
subl %r8d, %r10d
movl %r10d, -0x14(%rsp)
movl %r8d, -0x24(%rsp)
movzbl 0x2(%rdx), %r8d
movl %eax, %r10d
subl %r8d, %r10d
movl %r10d, -0x10(%rsp)
movl %r8d, -0x20(%rsp)
movl $0x3, %r8d
movzbl %r9b, %r9d
movl %eax, %r10d
subl %r9d, %r10d
movl %r10d, -0x18(%rsp)
movl %r9d, -0x28(%rsp)
testb $0x4, %cl
je 0x91b84
movzbl 0x4(%rdx), %ecx
movl %eax, %r11d
subl %ecx, %r11d
leal (,%r8,4), %ebx
movl %r11d, -0x18(%rsp,%rbx)
movl %ecx, -0x28(%rsp,%rbx)
incl %r8d
cmpb $0x7, %al
ja 0x91c13
movl 0x8(%rdi), %edi
movb 0x3(%rdx), %cl
movl %ecx, %edx
xorb $0x1, %dl
movl %eax, %r8d
xorb $0x2, %r8b
movl %eax, %r11d
xorb $0x4, %r11b
xorb $0x3, %cl
orb %r11b, %cl
movl $0x11, %ecx
movl $0xff, %r11d
cmovel %ecx, %r11d
orb %dl, %r8b
movl $0x55, %edx
cmovnel %r11d, %edx
testl %edi, %edi
je 0x91ce6
xorl %r8d, %r8d
movzbl (%rsi), %r11d
movb $0x0, (%rsi)
testb %al, %al
je 0x91c03
xorl %ebx, %ebx
movl %r10d, %ecx
testl %r10d, %r10d
jle 0x91bf5
movl %r11d, %ebp
shll %cl, %ebp
orb %bpl, %bl
subl %r9d, %ecx
testl %ecx, %ecx
jg 0x91be6
negb %cl
shrl %cl, %r11d
andl %edx, %r11d
orb %bl, %r11b
movb %r11b, (%rsi)
incl %r8d
incq %rsi
cmpl %edi, %r8d
jne 0x91bd1
jmp 0x91ce6
movl (%rdi), %r9d
imull %r8d, %r9d
cmpl $0x8, %eax
jne 0x91c74
testl %r9d, %r9d
je 0x91ce6
xorl %edi, %edi
movl %edi, %eax
xorl %edx, %edx
divl %r8d
movzbl (%rsi), %eax
movb $0x0, (%rsi)
shll $0x2, %edx
movl -0x18(%rsp,%rdx), %ecx
movl -0x28(%rsp,%rdx), %r10d
movl %r10d, %edx
negl %edx
cmpl %edx, %ecx
jle 0x91c68
xorl %edx, %edx
testl %ecx, %ecx
jle 0x91c60
movl %eax, %r11d
shll %cl, %r11d
orb %r11b, %dl
subl %r10d, %ecx
jmp 0x91c4e
negb %cl
shrl %cl, %eax
orb %dl, %al
movb %al, (%rsi)
incl %edi
incq %rsi
cmpl %r9d, %edi
jne 0x91c2a
jmp 0x91ce6
testl %r9d, %r9d
je 0x91ce6
xorl %edi, %edi
xorl %r10d, %r10d
movl %edi, %eax
xorl %edx, %edx
divl %r8d
shll $0x2, %edx
movl -0x18(%rsp,%rdx), %eax
movl -0x28(%rsp,%rdx), %r11d
movl %r11d, %ecx
negl %ecx
cmpl %ecx, %eax
jle 0x91cd2
movzwl (%rsi), %ecx
rolw $0x8, %cx
movzwl %cx, %ebx
movl %eax, %edx
negl %edx
xorl %r10d, %r10d
movl %eax, %ebp
movl %ebx, %r14d
movl %eax, %ecx
shll %cl, %r14d
movl %ebx, %r15d
movl %edx, %ecx
shrl %cl, %r15d
testl %eax, %eax
cmovgl %r14d, %r15d
orl %r15d, %r10d
subl %r11d, %ebp
addl %r11d, %edx
testl %eax, %eax
movl %ebp, %eax
jg 0x91cad
rolw $0x8, %r10w
movw %r10w, (%rsi)
addq $0x2, %rsi
incl %edi
cmpl %r9d, %edi
jne 0x91c7b
popq %rbx
popq %r14
popq %r15
popq %rbp
retq