// File: /workspace/github_cpp_makefile_star2_O2/mhe[P]raytracerframework_cpp/lodepng.cpp
// Function: preProcessScanlines(unsigned char**, unsigned long*, unsigned char const*, LodePNG_InfoPng const*)
static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const unsigned char* in, const LodePNG_InfoPng* infoPng) /*return value is error*/
{
  /*
  This function converts the pure 2D image with the PNG's colortype, into filtered-padded-interlaced data. Steps:
  *) if no Adam7: 1) add padding bits (= posible extra bits per scanline if bpp < 8) 2) filter
  *) if adam7: 1) Adam7_interlace 2) 7x add padding bits 3) 7x filter
  */
  unsigned bpp = LodePNG_InfoColor_getBpp(&infoPng->color);
  unsigned w = infoPng->width;
  unsigned h = infoPng->height;
  unsigned error = 0;
  
  if(infoPng->interlaceMethod == 0)
  {
    *outsize = h + (h * ((w * bpp + 7) / 8)); /*image size plus an extra byte per scanline + possible padding bits*/
    *out = (unsigned char*)malloc(*outsize);
    if(!(*out) && (*outsize)) error = 9950;

    if(!error)
    {
      if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) /*non multiple of 8 bits per scanline, padding bits needed per scanline*/
      {
        ucvector padded;
        ucvector_init(&padded);
        if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 9951;
        if(!error)
        {
          addPaddingBits(padded.data, in, ((w * bpp + 7) / 8) * 8, w * bpp, h);
          error = filter(*out, padded.data, w, h, &infoPng->color);
        }
        ucvector_cleanup(&padded);
      }
      else error = filter(*out, in, w, h, &infoPng->color); /*we can immediatly filter into the out buffer, no other steps needed*/
    }
  }
  else /*interlaceMethod is 1 (Adam7)*/
  {
    unsigned char* adam7 = (unsigned char*)malloc((h * w * bpp + 7) / 8);
    if(!adam7 && ((h * w * bpp + 7) / 8)) error = 9952; /*malloc failed*/
    
    while(!error) /*not a real while loop, used to break out to cleanup to avoid a goto*/
    {
      unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8];
      unsigned i;
      
      Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
      
      *outsize = filter_passstart[7]; /*image size plus an extra byte per scanline + possible padding bits*/
      *out = (unsigned char*)malloc(*outsize);
      if(!(*out) && (*outsize)) { error = 9953; break; }
      
      Adam7_interlace(adam7, in, w, h, bpp);
      
      for(i = 0; i < 7; i++)
      {
        if(bpp < 8)
        {
          ucvector padded;
          ucvector_init(&padded);
          if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 9954;
          if(!error)
          {
            addPaddingBits(&padded.data[padded_passstart[i]], &adam7[passstart[i]], ((passw[i] * bpp + 7) / 8) * 8, passw[i] * bpp, passh[i]);
            error = filter(&(*out)[filter_passstart[i]], &padded.data[padded_passstart[i]], passw[i], passh[i], &infoPng->color);
          }
          
          ucvector_cleanup(&padded);
        }
        else
        {
          error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]], passw[i], passh[i], &infoPng->color);
        }
      }
      
      break;
    }

    free(adam7);
  }
  
  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %rcx, %r13
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r15
leaq 0x18(%rcx), %rdi
movq %rdi, -0x60(%rbp)
callq 0x11cfc
movl (%r13), %ecx
movl 0x4(%r13), %edx
movl %ecx, -0x2c(%rbp)
movq %rax, -0x38(%rbp)
imull %eax, %ecx
cmpl $0x0, 0x10(%r13)
movq %rcx, -0x48(%rbp)
movq %rdx, -0x40(%rbp)
je 0x151da
movl %ecx, %r14d
imull %edx, %r14d
addl $0x7, %r14d
movl %r14d, %edi
shrl $0x3, %edi
callq 0x43f0
movq %rax, %r13
testq %rax, %rax
setne %al
cmpl $0x8, %r14d
setb %cl
xorl %edx, %edx
orb %al, %cl
movl $0x26e0, %eax           # imm = 0x26E0
cmovnel %edx, %eax
movl %eax, -0x4c(%rbp)
je 0x15479
leaq -0x110(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
leaq -0x190(%rbp), %rdx
leaq -0x1d0(%rbp), %rcx
leaq -0x250(%rbp), %r8
movl -0x2c(%rbp), %r9d
pushq -0x38(%rbp)
pushq -0x40(%rbp)
callq 0x177a6
popq %rax
popq %rcx
movq %r15, %r14
leaq -0x190(%rbp), %rax
movq 0x38(%rax), %r15
movq %r15, (%r12)
movq %r15, %rdi
callq 0x43f0
movq %r14, -0x58(%rbp)
movq %rax, (%r14)
testq %rax, %rax
sete %al
testq %r15, %r15
setne %cl
testb %al, %cl
jne 0x15479
leaq -0x150(%rbp), %rdi
leaq -0x130(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
leaq -0x290(%rbp), %rcx
leaq -0x210(%rbp), %r8
movl -0x2c(%rbp), %r9d
movq -0x38(%rbp), %r14
pushq %r14
movq -0x40(%rbp), %rax
pushq %rax
callq 0x177a6
popq %rax
popq %rcx
cmpl $0x7, %r14d
jbe 0x15255
movl %r14d, %eax
shrl $0x3, %eax
xorl %ecx, %ecx
leaq 0xd9f7(%rip), %rdi      # 0x22b20
leaq 0xd9d0(%rip), %r8       # 0x22b00
cmpq $0x7, %rcx
je 0x15364
movl -0x130(%rbp,%rcx,4), %edx
movl %edx, -0x68(%rbp)
xorl %r10d, %r10d
cmpl -0x68(%rbp), %r10d
je 0x151d2
movl -0x150(%rbp,%rcx,4), %r11d
movl %r11d, %r14d
imull %r10d, %r14d
xorl %r15d, %r15d
cmpq %r11, %r15
je 0x151ca
movl %r14d, %r12d
imulq %rax, %r12
addq -0x210(%rbp,%rcx,8), %r12
addq %r13, %r12
movl (%r8,%rcx,4), %edx
imull %r15d, %edx
addl (%rdi,%rcx,4), %edx
leaq 0xd9b4(%rip), %rsi      # 0x22b40
movl (%rsi,%rcx,4), %r9d
imull %r10d, %r9d
leaq 0xd9c5(%rip), %rsi      # 0x22b60
addl (%rsi,%rcx,4), %r9d
imull -0x2c(%rbp), %r9d
addl %edx, %r9d
imulq %rax, %r9
addq %rbx, %r9
xorl %edx, %edx
cmpq %rdx, %rax
je 0x151c2
movb (%r9,%rdx), %sil
movb %sil, (%r12,%rdx)
incq %rdx
jmp 0x151b0
incq %r15
incl %r14d
jmp 0x15163
incl %r10d
jmp 0x15147
incq %rcx
jmp 0x15130
leal 0x7(%rcx), %r14d
movq %r12, %rax
movq %r15, %r12
movl %r14d, %r15d
shrl $0x3, %r15d
imull %edx, %r15d
leal (%r15,%rdx), %r13d
movq %r13, (%rax)
movq %r13, %rdi
callq 0x43f0
movq %r12, -0x58(%rbp)
movq %rax, (%r12)
testq %rax, %rax
sete %cl
testl %r13d, %r13d
setne %dl
testb %cl, %dl
jne 0x154f3
cmpl $0x7, -0x38(%rbp)
ja 0x1522e
andl $-0x8, %r14d
cmpl %r14d, -0x48(%rbp)
jne 0x15492
movq %rax, %rdi
movq %rbx, %rsi
movl -0x2c(%rbp), %edx
movq -0x40(%rbp), %rcx
movq -0x60(%rbp), %r8
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x179eb
xorl %esi, %esi
leaq -0x90(%rbp), %r15
cmpq $0x7, %rsi
je 0x15364
movl -0x150(%rbp,%rsi,4), %eax
movq %rax, -0x78(%rbp)
movl -0x130(%rbp,%rsi,4), %eax
movl %eax, -0x6c(%rbp)
xorl %ecx, %ecx
movq %rsi, -0x80(%rbp)
cmpl -0x6c(%rbp), %ecx
je 0x1535c
movl %ecx, %eax
imull -0x78(%rbp), %eax
movq %rax, -0x88(%rbp)
xorl %edx, %edx
movl %ecx, -0x70(%rbp)
cmpq -0x78(%rbp), %rdx
je 0x15355
leaq 0xd891(%rip), %rax      # 0x22b40
movl (%rax,%rsi,4), %eax
imull %ecx, %eax
leaq 0xd8a4(%rip), %rcx      # 0x22b60
addl (%rcx,%rsi,4), %eax
imull -0x2c(%rbp), %eax
leaq 0xd836(%rip), %rcx      # 0x22b00
movl (%rcx,%rsi,4), %r14d
imull %edx, %r14d
leaq 0xd847(%rip), %rcx      # 0x22b20
addl (%rcx,%rsi,4), %r14d
movq -0x88(%rbp), %rcx
movq %rdx, -0x68(%rbp)
addl %edx, %ecx
addl %eax, %r14d
movq -0x38(%rbp), %rdx
imull %edx, %r14d
movq -0x210(%rbp,%rsi,8), %rax
imull %edx, %ecx
leaq (%rcx,%rax,8), %rax
movq %rax, -0x90(%rbp)
movl %edx, %r12d
subl $0x1, %r12d
jb 0x15342
movq %r14, %rax
shrq $0x3, %rax
movl %r14d, %ecx
notb %cl
movzbl (%rbx,%rax), %eax
movzbl %cl, %ecx
andl $0x7, %ecx
xorl %edx, %edx
btl %ecx, %eax
setb %dl
incq %r14
movq %r15, %rdi
movq %r13, %rsi
callq 0x178f6
jmp 0x1530e
movq -0x68(%rbp), %rdx
incq %rdx
movq -0x80(%rbp), %rsi
movl -0x70(%rbp), %ecx
jmp 0x1529e
incl %ecx
jmp 0x15283
incq %rsi
jmp 0x1525e
movq -0x48(%rbp), %rax
addl $0x7, %eax
shrl $0x3, %eax
imull -0x40(%rbp), %eax
movq %rax, -0x48(%rbp)
xorl %r15d, %r15d
leaq -0xd0(%rbp), %r14
cmpq $0x1c, %r15
je 0x15479
cmpl $0x7, -0x38(%rbp)
ja 0x15438
xorps %xmm0, %xmm0
movaps %xmm0, -0xd0(%rbp)
andq $0x0, -0xc0(%rbp)
movq %r14, %rdi
movq -0x48(%rbp), %rsi
callq 0x10bdb
testl %eax, %eax
movl -0x4c(%rbp), %ebx
movl $0x26e2, %eax           # imm = 0x26E2
cmovel %eax, %ebx
testl %ebx, %ebx
jne 0x1542e
movq -0xd0(%rbp), %r14
addq -0x1d0(%rbp,%r15,2), %r14
movq -0x250(%rbp,%r15,2), %rsi
addq %r13, %rsi
movl -0x110(%rbp,%r15), %r12d
movl %r12d, %ecx
imull -0x38(%rbp), %ecx
leal 0x7(%rcx), %edx
andl $-0x8, %edx
movl -0xf0(%rbp,%r15), %ebx
movq %r14, %rdi
movl %ebx, %r8d
callq 0x17928
movq -0x58(%rbp), %rax
movq (%rax), %rdi
addq -0x190(%rbp,%r15,2), %rdi
movq %r14, %rsi
leaq -0xd0(%rbp), %r14
movl %r12d, %edx
movl %ebx, %ecx
movq -0x60(%rbp), %r8
callq 0x179eb
movl %eax, %ebx
movq %r14, %rdi
callq 0x11873
jmp 0x1546d
movq -0x58(%rbp), %rax
movq (%rax), %rdi
addq -0x190(%rbp,%r15,2), %rdi
movq -0x1d0(%rbp,%r15,2), %rsi
addq %r13, %rsi
movl -0x110(%rbp,%r15), %edx
movl -0xf0(%rbp,%r15), %ecx
movq -0x60(%rbp), %r8
callq 0x179eb
movl %eax, %ebx
movl %ebx, -0x4c(%rbp)
addq $0x4, %r15
jmp 0x15380
movq %r13, %rdi
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4350
xorps %xmm0, %xmm0
leaq -0xd0(%rbp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movl %r15d, %esi
callq 0x10bdb
testl %eax, %eax
je 0x154e7
movq -0xd0(%rbp), %r15
movl %r14d, %edx
movl -0x48(%rbp), %ecx
movq %r15, %rdi
movq %rbx, %rsi
movq -0x40(%rbp), %rbx
movl %ebx, %r8d
callq 0x17928
movq -0x58(%rbp), %rax
movq (%rax), %rdi
movq %r15, %rsi
movl -0x2c(%rbp), %edx
movl %ebx, %ecx
movq -0x60(%rbp), %r8
callq 0x179eb
leaq -0xd0(%rbp), %rdi
callq 0x11873
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq