// File: drummyfish[P]flowchart/src/lodepng.cpp
// Function: preProcessScanlines(unsigned char**, unsigned long*, unsigned char const*, unsigned int, unsigned int, LodePNGInfo const*, LodePNGEncoderSettings const*)
static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const unsigned char* in,
                                    unsigned w, unsigned h,
                                    const LodePNGInfo* info_png, const LodePNGEncoderSettings* settings)
{
  /*
  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_get_bpp(&info_png->color);
  unsigned error = 0;

  if(info_png->interlace_method == 0)
  {
    *outsize = h + (h * ((w * bpp + 7) / 8)); /*image size plus an extra byte per scanline + possible padding bits*/
    *out = (unsigned char*)mymalloc(*outsize);
    if(!(*out) && (*outsize)) error = 83; /*alloc fail*/

    if(!error)
    {
      /*non multiple of 8 bits per scanline, padding bits needed per scanline*/
      if(bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8)
      {
        ucvector padded;
        ucvector_init(&padded);
        if(!ucvector_resize(&padded, h * ((w * bpp + 7) / 8))) error = 83; /*alloc fail*/
        if(!error)
        {
          addPaddingBits(padded.data, in, ((w * bpp + 7) / 8) * 8, w * bpp, h);
          error = filter(*out, padded.data, w, h, &info_png->color, settings);
        }
        ucvector_cleanup(&padded);
      }
      else
      {
        /*we can immediatly filter into the out buffer, no other steps needed*/
        error = filter(*out, in, w, h, &info_png->color, settings);
      }
    }
  }
  else /*interlace_method is 1 (Adam7)*/
  {
    unsigned char* adam7 = (unsigned char*)mymalloc((h * w * bpp + 7) / 8);
    if(!adam7 && ((h * w * bpp + 7) / 8)) error = 83; /*alloc fail*/

    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*)mymalloc(*outsize);
      if(!(*out) && (*outsize)) ERROR_BREAK(83 /*alloc fail*/);

      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 = 83; /*alloc fail*/
          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], &info_png->color, settings);
          }

          ucvector_cleanup(&padded);
        }
        else
        {
          error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]],
                         passw[i], passh[i], &info_png->color, settings);
        }
      }

      break;
    }

    myfree(adam7);
  }

  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
callq 0x5320
movl %eax, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0xa6d9
movl -0x20(%rbp), %eax
movl -0x20(%rbp), %ecx
movl -0x1c(%rbp), %edx
imull -0x2c(%rbp), %edx
addl $0x7, %edx
shrl $0x3, %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3840
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xa5df
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0xa5df
movl $0x53, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0xa6d4
cmpl $0x8, -0x2c(%rbp)
jae 0xa6ad
movl -0x1c(%rbp), %eax
imull -0x2c(%rbp), %eax
movl -0x1c(%rbp), %ecx
imull -0x2c(%rbp), %ecx
addl $0x7, %ecx
shrl $0x3, %ecx
shll $0x3, %ecx
cmpl %ecx, %eax
je 0xa6ad
leaq -0x48(%rbp), %rdi
callq 0xa980
movl -0x20(%rbp), %eax
movl -0x1c(%rbp), %ecx
imull -0x2c(%rbp), %ecx
addl $0x7, %ecx
shrl $0x3, %ecx
imull %ecx, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0xd730
cmpl $0x0, %eax
jne 0xa647
movl $0x53, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0xa6a2
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %eax
imull -0x2c(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %edx
movl -0x1c(%rbp), %eax
imull -0x2c(%rbp), %eax
movl %eax, %eax
movl %eax, %ecx
movl -0x20(%rbp), %r8d
callq 0x12300
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movq -0x28(%rbp), %r8
addq $0x10, %r8
movq 0x10(%rbp), %r9
callq 0x123e0
movl %eax, -0x30(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x10950
jmp 0xa6d2
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movq -0x28(%rbp), %r8
addq $0x10, %r8
movq 0x10(%rbp), %r9
callq 0x123e0
movl %eax, -0x30(%rbp)
jmp 0xa6d4
jmp 0xa965
movl -0x20(%rbp), %eax
imull -0x1c(%rbp), %eax
imull -0x2c(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x3840
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xa71b
movl -0x20(%rbp), %eax
imull -0x1c(%rbp), %eax
imull -0x2c(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
cmpl $0x0, %eax
je 0xa71b
movl $0x53, -0x30(%rbp)
jmp 0xa71d
cmpl $0x0, -0x30(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xa72f
jmp 0xa95c
leaq -0x70(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
leaq -0x110(%rbp), %rcx
leaq -0x150(%rbp), %r8
movl -0x1c(%rbp), %r9d
movl -0x20(%rbp), %r10d
movl -0x2c(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x10e20
movq -0x98(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3840
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xa7ab
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0xa7ab
movl $0x53, -0x30(%rbp)
jmp 0xa95c
movq -0x50(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movl -0x2c(%rbp), %r8d
callq 0x12c80
movl $0x0, -0x154(%rbp)
cmpl $0x7, -0x154(%rbp)
jae 0xa95a
cmpl $0x8, -0x2c(%rbp)
jae 0xa8f2
leaq -0x170(%rbp), %rdi
callq 0xa980
movl -0x20(%rbp), %eax
movl -0x1c(%rbp), %ecx
imull -0x2c(%rbp), %ecx
addl $0x7, %ecx
shrl $0x3, %ecx
imull %ecx, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x170(%rbp), %rdi
callq 0xd730
cmpl $0x0, %eax
jne 0xa81e
movl $0x53, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0xa8e4
movq -0x170(%rbp), %rdi
movl -0x154(%rbp), %eax
addq -0x110(%rbp,%rax,8), %rdi
movq -0x50(%rbp), %rsi
movl -0x154(%rbp), %eax
addq -0x150(%rbp,%rax,8), %rsi
movl -0x154(%rbp), %eax
movl -0x70(%rbp,%rax,4), %eax
imull -0x2c(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %edx
movl -0x154(%rbp), %eax
movl -0x70(%rbp,%rax,4), %eax
imull -0x2c(%rbp), %eax
movl %eax, %eax
movl %eax, %ecx
movl -0x154(%rbp), %eax
movl -0x90(%rbp,%rax,4), %r8d
callq 0x12300
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl -0x154(%rbp), %eax
addq -0xd0(%rbp,%rax,8), %rdi
movq -0x170(%rbp), %rsi
movl -0x154(%rbp), %eax
addq -0x110(%rbp,%rax,8), %rsi
movl -0x154(%rbp), %eax
movl -0x70(%rbp,%rax,4), %edx
movl -0x154(%rbp), %eax
movl -0x90(%rbp,%rax,4), %ecx
movq -0x28(%rbp), %r8
addq $0x10, %r8
movq 0x10(%rbp), %r9
callq 0x123e0
movl %eax, -0x30(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x10950
jmp 0xa944
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl -0x154(%rbp), %eax
addq -0xd0(%rbp,%rax,8), %rdi
movq -0x50(%rbp), %rsi
movl -0x154(%rbp), %eax
addq -0x110(%rbp,%rax,8), %rsi
movl -0x154(%rbp), %eax
movl -0x70(%rbp,%rax,4), %edx
movl -0x154(%rbp), %eax
movl -0x90(%rbp,%rax,4), %ecx
movq -0x28(%rbp), %r8
addq $0x10, %r8
movq 0x10(%rbp), %r9
callq 0x123e0
movl %eax, -0x30(%rbp)
jmp 0xa946
movl -0x154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
jmp 0xa7cc
jmp 0xa95c
movq -0x50(%rbp), %rdi
callq 0x4050
movl -0x30(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopw %cs:(%rax,%rax)