// File: /workspace/github_cpp_makefile_star2_O1/logicx24[P]RayTracer/lodepng.cpp
// Function: postProcessScanlines(unsigned char*, unsigned char*, unsigned int, unsigned int, LodePNGInfo const*)
static unsigned postProcessScanlines(unsigned char* out, unsigned char* in,
                                     unsigned w, unsigned h, const LodePNGInfo* info_png)
{
  /*
  This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG's colortype.
  Steps:
  *) if no Adam7: 1) unfilter 2) remove padding bits (= posible extra bits per scanline if bpp < 8)
  *) if adam7: 1) 7x unfilter 2) 7x remove padding bits 3) Adam7_deinterlace
  NOTE: the in buffer will be overwritten with intermediate data!
  */
  unsigned bpp = lodepng_get_bpp(&info_png->color);
  if(bpp == 0) return 31; /*error: invalid colortype*/

  if(info_png->interlace_method == 0)
  {
    if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8)
    {
      CERROR_TRY_RETURN(unfilter(in, in, w, h, bpp));
      removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h);
    }
    /*we can immediatly filter into the out buffer, no other steps needed*/
    else CERROR_TRY_RETURN(unfilter(out, in, w, h, bpp));
  }
  else /*interlace_method is 1 (Adam7)*/
  {
    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);

    for(i = 0; i != 7; ++i)
    {
      CERROR_TRY_RETURN(unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp));
      /*TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline,
      move bytes instead of bits or move not at all*/
      if(bpp < 8)
      {
        /*remove padding bits in scanlines; after this there still may be padding
        bits between the different reduced images: each reduced image still starts nicely at a byte*/
        removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp,
                          ((passw[i] * bpp + 7) / 8) * 8, passh[i]);
      }
    }

    Adam7_deinterlace(out, in, w, h, bpp);
  }

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl %ecx, %r10d
movl %edx, %r9d
movq %rsi, %rbx
movq %rdi, %r14
movl 0x10(%r8), %ecx
movl 0x14(%r8), %eax
xorl %r12d, %r12d
cmpq $0x6, %rcx
ja 0x15738
leaq 0x280c(%rip), %rdx      # 0x17f40
movl (%rdx,%rcx,4), %r12d
imull %eax, %r12d
testl %r12d, %r12d
je 0x157cb
cmpl $0x0, 0x8(%r8)
je 0x157d5
leaq -0xd0(%rbp), %r15
leaq -0x70(%rbp), %rsi
leaq -0x110(%rbp), %rdx
leaq -0x1d0(%rbp), %r13
leaq -0x190(%rbp), %r8
movq %r15, %rdi
movq %r13, %rcx
movl %r9d, -0x44(%rbp)
pushq %r12
pushq %r10
movq %r10, -0x40(%rbp)
callq 0x15f5e
addq $0x10, %rsp
movq (%r13), %r13
addq %rbx, %r13
leaq -0x110(%rbp), %rax
movq (%rax), %rsi
addq %rbx, %rsi
movl (%r15), %edx
leaq -0x70(%rbp), %rax
movl (%rax), %r15d
movq %r13, %rdi
movq %rdx, -0x38(%rbp)
movl %r15d, %ecx
movl %r12d, %r8d
callq 0x15b23
testl %eax, %eax
je 0x15814
movl $0x0, -0x48(%rbp)
jmp 0x15b09
movl $0x1f, %eax
jmp 0x15b11
cmpl $0x7, %r12d
ja 0x157f3
movl %r12d, %r15d
imull %r9d, %r15d
leal 0x7(%r15), %r13d
andl $-0x8, %r13d
cmpl %r13d, %r15d
jne 0x159c9
movq %r14, %rdi
movq %rbx, %rsi
movl %r9d, %edx
movl %r10d, %ecx
movl %r12d, %r8d
callq 0x15b23
testl %eax, %eax
jne 0x15b11
jmp 0x15b0f
movl %r15d, -0x2c(%rbp)
xorl %r15d, %r15d
movl %eax, -0x4c(%rbp)
cmpl $0x7, %r12d
ja 0x1584b
movq -0x190(%rbp,%r15,2), %rdi
addq %rbx, %rdi
movq -0x38(%rbp), %rdx
imull %r12d, %edx
movl %edx, %ecx
addl $0x7, %ecx
andl $-0x8, %ecx
movq %r13, %rsi
movl -0x2c(%rbp), %r8d
callq 0x15ec1
cmpq $0x18, %r15
sete %al
movl %eax, -0x48(%rbp)
je 0x158a5
movq -0x1c8(%rbp,%r15,2), %r13
addq %rbx, %r13
movq -0x108(%rbp,%r15,2), %rsi
addq %rbx, %rsi
movl -0xcc(%rbp,%r15), %edx
movl -0x6c(%rbp,%r15), %ecx
movq %r13, %rdi
movq %rdx, -0x38(%rbp)
movl %ecx, -0x2c(%rbp)
movl %r12d, %r8d
callq 0x15b23
addq $0x4, %r15
testl %eax, %eax
movl -0x4c(%rbp), %ecx
cmovnel %eax, %ecx
movl %ecx, %eax
je 0x1581b
jmp 0x15b09
leaq -0xb0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0x250(%rbp), %rdx
leaq -0x210(%rbp), %rcx
leaq -0x150(%rbp), %r8
movl -0x44(%rbp), %r9d
pushq %r12
pushq -0x40(%rbp)
callq 0x15f5e
addq $0x10, %rsp
cmpl $0x7, %r12d
jbe 0x15a01
shrl $0x3, %r12d
xorl %eax, %eax
movq %rax, -0x40(%rbp)
movl -0x90(%rbp,%rax,4), %eax
movl %eax, -0x2c(%rbp)
testl %eax, %eax
je 0x159b3
movq -0x40(%rbp), %rax
movl -0xb0(%rbp,%rax,4), %r9d
movq $0x0, -0x38(%rbp)
xorl %r11d, %r11d
testq %r9, %r9
je 0x1599b
movq -0x40(%rbp), %rax
leaq 0x2449(%rip), %rcx      # 0x17d70
movl (%rcx,%rax,4), %r15d
imull %r11d, %r15d
leaq 0x245a(%rip), %rcx      # 0x17d90
addl (%rcx,%rax,4), %r15d
leaq 0x23ef(%rip), %rcx      # 0x17d30
movl (%rcx,%rax,4), %r13d
imull -0x44(%rbp), %r15d
leaq 0x23ff(%rip), %rcx      # 0x17d50
addl (%rcx,%rax,4), %r15d
movq -0x150(%rbp,%rax,8), %rcx
addq %rbx, %rcx
movq -0x38(%rbp), %rax
movl %eax, %edx
xorl %esi, %esi
movl %r15d, %edi
imulq %r12, %rdi
addq %r14, %rdi
movl %edx, %r8d
imulq %r12, %r8
addq %rcx, %r8
xorl %eax, %eax
movb (%r8,%rax), %r10b
movb %r10b, (%rdi,%rax)
incq %rax
cmpq %rax, %r12
jne 0x1597e
incq %rsi
addl %r13d, %r15d
incl %edx
cmpq %r9, %rsi
jne 0x15968
incl %r11d
movq -0x38(%rbp), %rax
addl %r9d, %eax
movq %rax, -0x38(%rbp)
cmpl -0x2c(%rbp), %r11d
jne 0x15917
movq -0x40(%rbp), %rax
incq %rax
cmpq $0x7, %rax
jne 0x158ea
jmp 0x15b06
movq %rbx, %rdi
movq %rbx, %rsi
movl %r9d, %edx
movl %r10d, %ecx
movl %r12d, %r8d
movq %r10, %r12
callq 0x15b23
testl %eax, %eax
jne 0x15b11
movl %r15d, %edx
movl %r13d, %ecx
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %r8d
callq 0x15ec1
jmp 0x15b0f
xorl %eax, %eax
movq %rax, -0x40(%rbp)
movl -0x90(%rbp,%rax,4), %eax
movl %eax, -0x2c(%rbp)
testl %eax, %eax
je 0x15af5
movq -0x40(%rbp), %rax
movl -0xb0(%rbp,%rax,4), %r10d
movl $0x0, -0x38(%rbp)
testq %r10, %r10
je 0x15ae4
movq -0x40(%rbp), %rax
movq -0x150(%rbp,%rax,8), %r15
shlq $0x3, %r15
movl -0x38(%rbp), %esi
movl %esi, %r13d
imull %r10d, %r13d
leaq 0x231a(%rip), %rcx      # 0x17d70
movl (%rcx,%rax,4), %edx
imull %esi, %edx
leaq 0x232d(%rip), %rcx      # 0x17d90
addl (%rcx,%rax,4), %edx
imull -0x44(%rbp), %edx
leaq 0x22bf(%rip), %rcx      # 0x17d30
movl (%rcx,%rax,4), %esi
leaq 0x22d5(%rip), %rcx      # 0x17d50
addl (%rcx,%rax,4), %edx
xorl %edi, %edi
movl %esi, %r8d
imull %edi, %r8d
addl %edx, %r8d
imull %r12d, %r8d
leal (%rdi,%r13), %r11d
imull %r12d, %r11d
addq %r15, %r11
movl %r12d, %eax
movl %r11d, %ecx
notb %cl
movq %r11, %r9
shrq $0x3, %r9
movzbl (%rbx,%r9), %r9d
movzbl %cl, %ecx
andl $0x7, %ecx
btl %ecx, %r9d
jae 0x15ad2
movl %r8d, %ecx
notb %cl
andb $0x7, %cl
movb $0x1, %r9b
shlb %cl, %r9b
movq %r8, %rcx
shrq $0x3, %rcx
orb %r9b, (%r14,%rcx)
incq %r8
incq %r11
decl %eax
jne 0x15a9c
incq %rdi
cmpq %r10, %rdi
jne 0x15a80
movl -0x38(%rbp), %eax
incl %eax
movl %eax, -0x38(%rbp)
cmpl -0x2c(%rbp), %eax
jne 0x15a2c
movq -0x40(%rbp), %rax
incq %rax
cmpq $0x7, %rax
jne 0x15a03
movl -0x4c(%rbp), %eax
cmpb $0x0, -0x48(%rbp)
je 0x15b11
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq