// File: phillip-h[P]fractal-gen/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*)lodepng_malloc(*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)
      {
        unsigned char* padded = (unsigned char*)lodepng_malloc(h * ((w * bpp + 7) / 8));
        if(!padded) error = 83; /*alloc fail*/
        if(!error)
        {
          addPaddingBits(padded, in, ((w * bpp + 7) / 8) * 8, w * bpp, h);
          error = filter(*out, padded, w, h, &info_png->color, settings);
        }
        lodepng_free(padded);
      }
      else
      {
        /*we can immediately 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 passw[7], passh[7];
    size_t filter_passstart[8], padded_passstart[8], passstart[8];
    unsigned char* adam7;

    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*)lodepng_malloc(*outsize);
    if(!(*out)) error = 83; /*alloc fail*/

    adam7 = (unsigned char*)lodepng_malloc(passstart[7]);
    if(!adam7 && passstart[7]) error = 83; /*alloc fail*/

    if(!error)
    {
      unsigned i;

      Adam7_interlace(adam7, in, w, h, bpp);
      for(i = 0; i != 7; ++i)
      {
        if(bpp < 8)
        {
          unsigned char* padded = (unsigned char*)lodepng_malloc(padded_passstart[i + 1] - padded_passstart[i]);
          if(!padded) ERROR_BREAK(83); /*alloc fail*/
          addPaddingBits(padded, &adam7[passstart[i]],
                         ((passw[i] * bpp + 7) / 8) * 8, passw[i] * bpp, passh[i]);
          error = filter(&(*out)[filter_passstart[i]], padded,
                         passw[i], passh[i], &info_png->color, settings);
          lodepng_free(padded);
        }
        else
        {
          error = filter(&(*out)[filter_passstart[i]], &adam7[padded_passstart[i]],
                         passw[i], passh[i], &info_png->color, settings);
        }

        if(error) break;
      }
    }

    lodepng_free(adam7);
  }

  return error;
}

// Assembly:
subq $0x168, %rsp            # imm = 0x168
movq 0x170(%rsp), %rax
movq %rdi, 0x160(%rsp)
movq %rsi, 0x158(%rsp)
movq %rdx, 0x150(%rsp)
movl %ecx, 0x14c(%rsp)
movl %r8d, 0x148(%rsp)
movq %r9, 0x140(%rsp)
movq 0x140(%rsp), %rdi
addq $0x10, %rdi
callq 0x8cd0
movl %eax, 0x13c(%rsp)
movl $0x0, 0x138(%rsp)
movq 0x140(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jne 0xf2e2
movl 0x148(%rsp), %eax
movl 0x148(%rsp), %ecx
movl 0x14c(%rsp), %edx
imull 0x13c(%rsp), %edx
addl $0x7, %edx
shrl $0x3, %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %ecx
movq 0x158(%rsp), %rax
movq %rcx, (%rax)
movq 0x158(%rsp), %rax
movq (%rax), %rdi
callq 0x6cd0
movq %rax, %rcx
movq 0x160(%rsp), %rax
movq %rcx, (%rax)
movq 0x160(%rsp), %rax
cmpq $0x0, (%rax)
jne 0xf15f
movq 0x158(%rsp), %rax
cmpq $0x0, (%rax)
je 0xf15f
movl $0x53, 0x138(%rsp)
cmpl $0x0, 0x138(%rsp)
jne 0xf2dd
cmpl $0x8, 0x13c(%rsp)
jae 0xf29a
movl 0x14c(%rsp), %eax
imull 0x13c(%rsp), %eax
movl 0x14c(%rsp), %ecx
imull 0x13c(%rsp), %ecx
addl $0x7, %ecx
shrl $0x3, %ecx
shll $0x3, %ecx
cmpl %ecx, %eax
je 0xf29a
movl 0x148(%rsp), %eax
movl 0x14c(%rsp), %ecx
imull 0x13c(%rsp), %ecx
addl $0x7, %ecx
shrl $0x3, %ecx
imull %ecx, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x6cd0
movq %rax, 0x130(%rsp)
cmpq $0x0, 0x130(%rsp)
jne 0xf1f0
movl $0x53, 0x138(%rsp)
cmpl $0x0, 0x138(%rsp)
jne 0xf28b
movq 0x130(%rsp), %rdi
movq 0x150(%rsp), %rsi
movl 0x14c(%rsp), %eax
imull 0x13c(%rsp), %eax
addl $0x7, %eax
shrl $0x3, %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %edx
movl 0x14c(%rsp), %eax
imull 0x13c(%rsp), %eax
movl %eax, %eax
movl %eax, %ecx
movl 0x148(%rsp), %r8d
callq 0x183a0
movq 0x160(%rsp), %rax
movq (%rax), %rdi
movq 0x130(%rsp), %rsi
movl 0x14c(%rsp), %edx
movl 0x148(%rsp), %ecx
movq 0x140(%rsp), %r8
addq $0x10, %r8
movq 0x170(%rsp), %r9
callq 0x18490
movl %eax, 0x138(%rsp)
movq 0x130(%rsp), %rdi
callq 0x78b0
jmp 0xf2db
movq 0x160(%rsp), %rax
movq (%rax), %rdi
movq 0x150(%rsp), %rsi
movl 0x14c(%rsp), %edx
movl 0x148(%rsp), %ecx
movq 0x140(%rsp), %r8
addq $0x10, %r8
movq 0x170(%rsp), %r9
callq 0x18490
movl %eax, 0x138(%rsp)
jmp 0xf2dd
jmp 0xf56a
leaq 0x110(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x30(%rsp), %r8
movl 0x14c(%rsp), %r9d
movl 0x148(%rsp), %r10d
movl 0x13c(%rsp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x176c0
movq 0xe8(%rsp), %rcx
movq 0x158(%rsp), %rax
movq %rcx, (%rax)
movq 0x158(%rsp), %rax
movq (%rax), %rdi
callq 0x6cd0
movq %rax, %rcx
movq 0x160(%rsp), %rax
movq %rcx, (%rax)
movq 0x160(%rsp), %rax
cmpq $0x0, (%rax)
jne 0xf372
movl $0x53, 0x138(%rsp)
movq 0x68(%rsp), %rdi
callq 0x6cd0
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0xf39c
cmpq $0x0, 0x68(%rsp)
je 0xf39c
movl $0x53, 0x138(%rsp)
cmpl $0x0, 0x138(%rsp)
jne 0xf560
movq 0x28(%rsp), %rdi
movq 0x150(%rsp), %rsi
movl 0x14c(%rsp), %edx
movl 0x148(%rsp), %ecx
movl 0x13c(%rsp), %r8d
callq 0x192a0
movl $0x0, 0x24(%rsp)
cmpl $0x7, 0x24(%rsp)
je 0xf55e
cmpl $0x8, 0x13c(%rsp)
jae 0xf4e5
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movq 0x70(%rsp,%rax,8), %rdi
movl 0x24(%rsp), %eax
subq 0x70(%rsp,%rax,8), %rdi
callq 0x6cd0
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0xf42c
movl $0x53, 0x138(%rsp)
jmp 0xf55e
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl 0x24(%rsp), %eax
addq 0x30(%rsp,%rax,8), %rsi
movl 0x24(%rsp), %eax
movl 0x110(%rsp,%rax,4), %eax
imull 0x13c(%rsp), %eax
addl $0x7, %eax
shrl $0x3, %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %edx
movl 0x24(%rsp), %eax
movl 0x110(%rsp,%rax,4), %eax
imull 0x13c(%rsp), %eax
movl %eax, %eax
movl %eax, %ecx
movl 0x24(%rsp), %eax
movl 0xf0(%rsp,%rax,4), %r8d
callq 0x183a0
movq 0x160(%rsp), %rax
movq (%rax), %rdi
movl 0x24(%rsp), %eax
addq 0xb0(%rsp,%rax,8), %rdi
movq 0x18(%rsp), %rsi
movl 0x24(%rsp), %eax
movl 0x110(%rsp,%rax,4), %edx
movl 0x24(%rsp), %eax
movl 0xf0(%rsp,%rax,4), %ecx
movq 0x140(%rsp), %r8
addq $0x10, %r8
movq 0x170(%rsp), %r9
callq 0x18490
movl %eax, 0x138(%rsp)
movq 0x18(%rsp), %rdi
callq 0x78b0
jmp 0xf540
movq 0x160(%rsp), %rax
movq (%rax), %rdi
movl 0x24(%rsp), %eax
addq 0xb0(%rsp,%rax,8), %rdi
movq 0x28(%rsp), %rsi
movl 0x24(%rsp), %eax
addq 0x70(%rsp,%rax,8), %rsi
movl 0x24(%rsp), %eax
movl 0x110(%rsp,%rax,4), %edx
movl 0x24(%rsp), %eax
movl 0xf0(%rsp,%rax,4), %ecx
movq 0x140(%rsp), %r8
addq $0x10, %r8
movq 0x170(%rsp), %r9
callq 0x18490
movl %eax, 0x138(%rsp)
cmpl $0x0, 0x138(%rsp)
je 0xf54c
jmp 0xf55e
jmp 0xf54e
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0xf3da
jmp 0xf560
movq 0x28(%rsp), %rdi
callq 0x78b0
movl 0x138(%rsp), %eax
addq $0x168, %rsp            # imm = 0x168
retq
nopl (%rax)