// File: drummyfish[P]flowchart/src/lodepng.cpp
// Function: Adam7_deinterlace(unsigned char*, unsigned char const*, unsigned int, unsigned int, unsigned int)
static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp)
{
  /*Note: this function works on image buffers WITHOUT padding bits at end of scanlines
  with non-multiple-of-8 bit amounts, only between reduced images is padding
  out must be big enough AND must be 0 everywhere if bpp < 8 in the current implementation
  (because that's likely a little bit faster)*/
  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);

  if(bpp >= 8)
  {
    for(i = 0; i < 7; i++)
    {
      unsigned x, y, b;
      size_t bytewidth = bpp / 8;
      for(y = 0; y < passh[i]; y++)
      for(x = 0; x < passw[i]; x++)
      {
        size_t pixelinstart = passstart[i] + (y * passw[i] + x) * bytewidth;
        size_t pixeloutstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth;
        for(b = 0; b < bytewidth; b++)
        {
          out[pixeloutstart + b] = in[pixelinstart + b];
        }
      }
    }
  }
  else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/
  {
    for(i = 0; i < 7; i++)
    {
      unsigned x, y, b;
      unsigned ilinebits = bpp * passw[i];
      unsigned olinebits = bpp * w;
      size_t obp, ibp; /*bit pointers (for out and in buffer)*/
      for(y = 0; y < passh[i]; y++)
      for(x = 0; x < passw[i]; x++)
      {
        ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
        obp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp;
        for(b = 0; b < bpp; b++)
        {
          unsigned char bit = readBitFromReversedStream(&ibp, in);
          /*note that this function assumes the out buffer is completely 0, use setBitOfReversedStream otherwise*/
          setBitOfReversedStream0(&obp, out, bit);
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
leaq -0xe0(%rbp), %rcx
leaq -0x120(%rbp), %r8
movl -0x14(%rbp), %r9d
movl -0x18(%rbp), %r10d
movl -0x1c(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x10e20
cmpl $0x8, -0x1c(%rbp)
jb 0x11269
movl $0x0, -0x124(%rbp)
cmpl $0x7, -0x124(%rbp)
jae 0x11264
movl -0x1c(%rbp), %eax
shrl $0x3, %eax
movl %eax, %eax
movq %rax, -0x138(%rbp)
movl $0x0, -0x12c(%rbp)
movl -0x12c(%rbp), %eax
movl -0x124(%rbp), %ecx
cmpl -0x60(%rbp,%rcx,4), %eax
jae 0x1124e
movl $0x0, -0x128(%rbp)
movl -0x128(%rbp), %eax
movl -0x124(%rbp), %ecx
cmpl -0x40(%rbp,%rcx,4), %eax
jae 0x11238
movl -0x124(%rbp), %eax
movq -0x120(%rbp,%rax,8), %rax
movl -0x12c(%rbp), %ecx
movl -0x124(%rbp), %edx
imull -0x40(%rbp,%rdx,4), %ecx
addl -0x128(%rbp), %ecx
movl %ecx, %ecx
imulq -0x138(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x140(%rbp)
movl -0x124(%rbp), %eax
movl %eax, %ecx
leaq 0xd376(%rip), %rax      # 0x1e4e0
movl (%rax,%rcx,4), %eax
movl -0x12c(%rbp), %ecx
movl -0x124(%rbp), %edx
movl %edx, %esi
leaq 0xd33e(%rip), %rdx      # 0x1e4c0
imull (%rdx,%rsi,4), %ecx
addl %ecx, %eax
imull -0x14(%rbp), %eax
movl -0x124(%rbp), %ecx
movl %ecx, %edx
leaq 0xd305(%rip), %rcx      # 0x1e4a0
addl (%rcx,%rdx,4), %eax
movl -0x128(%rbp), %ecx
movl -0x124(%rbp), %edx
movl %edx, %esi
leaq 0xd2cd(%rip), %rdx      # 0x1e480
imull (%rdx,%rsi,4), %ecx
addl %ecx, %eax
movl %eax, %eax
imulq -0x138(%rbp), %rax
movq %rax, -0x148(%rbp)
movl $0x0, -0x130(%rbp)
movl -0x130(%rbp), %eax
cmpq -0x138(%rbp), %rax
jae 0x11222
movq -0x10(%rbp), %rax
movq -0x140(%rbp), %rcx
movl -0x130(%rbp), %edx
addq %rdx, %rcx
movb (%rax,%rcx), %dl
movq -0x8(%rbp), %rax
movq -0x148(%rbp), %rcx
movl -0x130(%rbp), %esi
addq %rsi, %rcx
movb %dl, (%rax,%rcx)
movl -0x130(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x130(%rbp)
jmp 0x111d4
jmp 0x11224
movl -0x128(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x128(%rbp)
jmp 0x1110c
jmp 0x1123a
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
jmp 0x110ec
jmp 0x11250
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x110c6
jmp 0x1141f
movl $0x0, -0x124(%rbp)
cmpl $0x7, -0x124(%rbp)
jae 0x1141d
movl -0x1c(%rbp), %eax
movl -0x124(%rbp), %ecx
imull -0x40(%rbp,%rcx,4), %eax
movl %eax, -0x158(%rbp)
movl -0x1c(%rbp), %eax
imull -0x14(%rbp), %eax
movl %eax, -0x15c(%rbp)
movl $0x0, -0x150(%rbp)
movl -0x150(%rbp), %eax
movl -0x124(%rbp), %ecx
cmpl -0x60(%rbp,%rcx,4), %eax
jae 0x11407
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
movl -0x124(%rbp), %ecx
cmpl -0x40(%rbp,%rcx,4), %eax
jae 0x113f1
movl -0x124(%rbp), %eax
movq -0x120(%rbp,%rax,8), %rax
shlq $0x3, %rax
movl -0x150(%rbp), %ecx
imull -0x158(%rbp), %ecx
movl -0x14c(%rbp), %edx
imull -0x1c(%rbp), %edx
addl %edx, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x170(%rbp)
movl -0x124(%rbp), %eax
movl %eax, %ecx
leaq 0xd1b9(%rip), %rax      # 0x1e4e0
movl (%rax,%rcx,4), %eax
movl -0x150(%rbp), %ecx
movl -0x124(%rbp), %edx
movl %edx, %esi
leaq 0xd181(%rip), %rdx      # 0x1e4c0
imull (%rdx,%rsi,4), %ecx
addl %ecx, %eax
imull -0x15c(%rbp), %eax
movl -0x124(%rbp), %ecx
movl %ecx, %edx
leaq 0xd145(%rip), %rcx      # 0x1e4a0
movl (%rcx,%rdx,4), %ecx
movl -0x14c(%rbp), %edx
movl -0x124(%rbp), %esi
movl %esi, %edi
leaq 0xd10d(%rip), %rsi      # 0x1e480
imull (%rsi,%rdi,4), %edx
addl %edx, %ecx
imull -0x1c(%rbp), %ecx
addl %ecx, %eax
movl %eax, %eax
movq %rax, -0x168(%rbp)
movl $0x0, -0x154(%rbp)
movl -0x154(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x113db
movq -0x10(%rbp), %rsi
leaq -0x170(%rbp), %rdi
callq 0x10780
movb %al, -0x171(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x168(%rbp), %rdi
movzbl -0x171(%rbp), %edx
callq 0x11a00
movl -0x154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
jmp 0x11392
jmp 0x113dd
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x112cb
jmp 0x113f3
movl -0x150(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x150(%rbp)
jmp 0x112ab
jmp 0x11409
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x11273
jmp 0x1141f
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nopl (%rax,%rax)