// File: lvandeve[P]lodepng/lodepng_util.cpp
// Function: lodepng::getFilterTypesInterlaced(std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>>&, std::vector<unsigned char, std::allocator<unsigned char>> const&)
unsigned getFilterTypesInterlaced(std::vector<std::vector<unsigned char> >& filterTypes,
                                  const std::vector<unsigned char>& png) {
  //Get color type and interlace type
  lodepng::State state;
  unsigned w, h;
  unsigned error;
  error = lodepng_inspect(&w, &h, &state, png.empty() ? NULL : &png[0], png.size());

  if(error) return 1;

  //Read literal data from all IDAT chunks
  const unsigned char *chunk, *begin, *end;
  end = &png.back() + 1;
  begin = chunk = &png.front() + 8;

  std::vector<unsigned char> zdata;

  while(chunk < end && end - chunk >= 8) {
    char type[5];
    lodepng_chunk_type(type, chunk);
    if(std::string(type).size() != 4) return 1; //Probably not a PNG file

    if(std::string(type) == "IDAT") {
      const unsigned char* cdata = lodepng_chunk_data_const(chunk);
      unsigned clength = lodepng_chunk_length(chunk);
      if(chunk + clength + 12 > end || clength > png.size() || chunk + clength + 12 < begin) {
        return 1; // corrupt chunk length
      }
      zdata.insert(zdata.end(), cdata, cdata + clength);
    }

    chunk = lodepng_chunk_next_const(chunk, end);
  }

  //Decompress all IDAT data
  std::vector<unsigned char> data;
  error = lodepng::decompress(data, zdata.empty() ? NULL : &zdata[0], zdata.size());

  if(error) return 1;

  if(state.info_png.interlace_method == 0) {
    filterTypes.resize(1);

    //A line is 1 filter byte + all pixels
    size_t linebytes = 1 + lodepng_get_raw_size(w, 1, &state.info_png.color);

    for(size_t i = 0; i < data.size(); i += linebytes) {
      filterTypes[0].push_back(data[i]);
    }
  } else {
    //Interlaced
    filterTypes.resize(7);
    static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/
    static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/
    static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/
    static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/
    size_t pos = 0;
    for(size_t j = 0; j < 7; j++) {
      unsigned w2 = (w - ADAM7_IX[j] + ADAM7_DX[j] - 1) / ADAM7_DX[j];
      unsigned h2 = (h - ADAM7_IY[j] + ADAM7_DY[j] - 1) / ADAM7_DY[j];
      if(ADAM7_IX[j] >= w || ADAM7_IY[j] >= h) continue;
      size_t linebytes = 1 + lodepng_get_raw_size(w2, 1, &state.info_png.color);
      for(size_t i = 0; i < h2; i++) {
        filterTypes[j].push_back(data[pos]);
        pos += linebytes;
      }
    }
  }
  return 0; /* OK */
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x500, %rsp            # imm = 0x500
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x298(%rbp), %rdi
callq 0x11ad0
movq -0x18(%rbp), %rdi
callq 0x1a3a0
movb %al, -0x3d9(%rbp)
jmp 0x1d130
movb -0x3d9(%rbp), %al
testb $0x1, %al
jne 0x1d13c
jmp 0x1d147
xorl %eax, %eax
movq %rax, -0x3e8(%rbp)
jmp 0x1d16d
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a3e0
movq %rax, -0x3f0(%rbp)
jmp 0x1d15d
movq -0x3f0(%rbp), %rax
movq %rax, -0x3e8(%rbp)
jmp 0x1d16d
movq -0x3e8(%rbp), %rax
movq %rax, -0x400(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1a400
movq %rax, -0x3f8(%rbp)
jmp 0x1d18d
movq -0x3f8(%rbp), %r8
movq -0x400(%rbp), %rcx
leaq -0x29c(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
leaq -0x298(%rbp), %rdx
callq 0xa1d0
movl %eax, -0x404(%rbp)
jmp 0x1d1bd
movl -0x404(%rbp), %eax
movl %eax, -0x2a4(%rbp)
cmpl $0x0, -0x2a4(%rbp)
je 0x1d1ff
movl $0x1, -0x4(%rbp)
movl $0x1, -0x2b8(%rbp)
jmp 0x1da60
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2b0(%rbp)
movl %eax, -0x2b4(%rbp)
jmp 0x1da78
movq -0x18(%rbp), %rdi
callq 0x23160
movq %rax, -0x410(%rbp)
jmp 0x1d211
movq -0x410(%rbp), %rax
incq %rax
movq %rax, -0x2d0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x231a0
movq %rax, -0x418(%rbp)
jmp 0x1d234
movq -0x418(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x2c0(%rbp)
movq %rax, -0x2c8(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0x1a4a0
jmp 0x1d25b
jmp 0x1d25d
movq -0x2c0(%rbp), %rcx
xorl %eax, %eax
cmpq -0x2d0(%rbp), %rcx
movb %al, -0x419(%rbp)
jae 0x1d293
movq -0x2d0(%rbp), %rax
movq -0x2c0(%rbp), %rcx
subq %rcx, %rax
cmpq $0x8, %rax
setge %al
movb %al, -0x419(%rbp)
movb -0x419(%rbp), %al
testb $0x1, %al
jne 0x1d2a2
jmp 0x1d5a4
movq -0x2c0(%rbp), %rsi
leaq -0x2ed(%rbp), %rdi
callq 0x4de0
jmp 0x1d2b7
leaq -0x311(%rbp), %rdi
movq %rdi, -0x428(%rbp)
callq 0x3440
movq -0x428(%rbp), %rdx
leaq -0x310(%rbp), %rdi
leaq -0x2ed(%rbp), %rsi
callq 0x3340
jmp 0x1d2e6
leaq -0x310(%rbp), %rdi
callq 0x3190
movq %rax, -0x430(%rbp)
jmp 0x1d2fb
movq -0x430(%rbp), %rax
subq $0x4, %rax
setne %al
movb %al, -0x431(%rbp)
leaq -0x310(%rbp), %rdi
callq 0x34a8
jmp 0x1d31d
leaq -0x311(%rbp), %rdi
callq 0x3290
movb -0x431(%rbp), %al
testb $0x1, %al
jne 0x1d335
jmp 0x1d3a9
movl $0x1, -0x4(%rbp)
movl $0x1, -0x2b8(%rbp)
jmp 0x1da40
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2b0(%rbp)
movl %eax, -0x2b4(%rbp)
jmp 0x1da50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2b0(%rbp)
movl %eax, -0x2b4(%rbp)
jmp 0x1d398
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2b0(%rbp)
movl %eax, -0x2b4(%rbp)
leaq -0x310(%rbp), %rdi
callq 0x34a8
jmp 0x1d396
jmp 0x1d398
leaq -0x311(%rbp), %rdi
callq 0x3290
jmp 0x1da50
leaq -0x339(%rbp), %rdi
movq %rdi, -0x440(%rbp)
callq 0x3440
movq -0x440(%rbp), %rdx
leaq -0x338(%rbp), %rdi
leaq -0x2ed(%rbp), %rsi
callq 0x3340
jmp 0x1d3d8
leaq 0x955cb(%rip), %rsi     # 0xb29aa
leaq -0x338(%rbp), %rdi
callq 0x232b0
movb %al, -0x441(%rbp)
jmp 0x1d3f3
leaq -0x338(%rbp), %rdi
callq 0x34a8
jmp 0x1d401
leaq -0x339(%rbp), %rdi
callq 0x3290
movb -0x441(%rbp), %al
testb $0x1, %al
jne 0x1d41c
jmp 0x1d575
movq -0x2c0(%rbp), %rdi
callq 0x4f90
movq %rax, -0x450(%rbp)
jmp 0x1d431
movq -0x450(%rbp), %rax
movq %rax, -0x348(%rbp)
movq -0x2c0(%rbp), %rdi
callq 0x4d80
movl %eax, -0x454(%rbp)
jmp 0x1d453
movl -0x454(%rbp), %eax
movl %eax, -0x34c(%rbp)
movq -0x2c0(%rbp), %rax
movl -0x34c(%rbp), %ecx
addq %rcx, %rax
addq $0xc, %rax
cmpq -0x2d0(%rbp), %rax
ja 0x1d4cb
movl -0x34c(%rbp), %eax
movq %rax, -0x468(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1a400
movq %rax, -0x460(%rbp)
jmp 0x1d49b
movq -0x468(%rbp), %rax
movq -0x460(%rbp), %rcx
cmpq %rcx, %rax
ja 0x1d4cb
movq -0x2c0(%rbp), %rax
movl -0x34c(%rbp), %ecx
addq %rcx, %rax
addq $0xc, %rax
cmpq -0x2c8(%rbp), %rax
jae 0x1d528
movl $0x1, -0x4(%rbp)
movl $0x1, -0x2b8(%rbp)
jmp 0x1da40
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2b0(%rbp)
movl %eax, -0x2b4(%rbp)
jmp 0x1d517
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2b0(%rbp)
movl %eax, -0x2b4(%rbp)
leaq -0x338(%rbp), %rdi
callq 0x34a8
jmp 0x1d515
jmp 0x1d517
leaq -0x339(%rbp), %rdi
callq 0x3290
jmp 0x1da50
leaq -0x2e8(%rbp), %rdi
callq 0x1a460
movq %rax, -0x470(%rbp)
jmp 0x1d53d
movq -0x470(%rbp), %rax
movq %rax, -0x358(%rbp)
movq -0x348(%rbp), %rdx
movl -0x34c(%rbp), %eax
movq %rdx, %rcx
addq %rax, %rcx
movq -0x358(%rbp), %rsi
leaq -0x2e8(%rbp), %rdi
callq 0x23750
jmp 0x1d573
jmp 0x1d575
movq -0x2c0(%rbp), %rdi
movq -0x2d0(%rbp), %rsi
callq 0x51c0
movq %rax, -0x478(%rbp)
jmp 0x1d591
movq -0x478(%rbp), %rax
movq %rax, -0x2c0(%rbp)
jmp 0x1d25d
leaq -0x370(%rbp), %rdi
callq 0x1a4a0
jmp 0x1d5b2
leaq -0x2e8(%rbp), %rdi
callq 0x1a3a0
movb %al, -0x479(%rbp)
jmp 0x1d5c6
movb -0x479(%rbp), %al
testb $0x1, %al
jne 0x1d5d2
jmp 0x1d5dd
xorl %eax, %eax
movq %rax, -0x488(%rbp)
jmp 0x1d606
xorl %eax, %eax
movl %eax, %esi
leaq -0x2e8(%rbp), %rdi
callq 0x1a5f0
movq %rax, -0x490(%rbp)
jmp 0x1d5f6
movq -0x490(%rbp), %rax
movq %rax, -0x488(%rbp)
jmp 0x1d606
movq -0x488(%rbp), %rax
movq %rax, -0x4a0(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0x1a400
movq %rax, -0x498(%rbp)
jmp 0x1d629
movq -0x498(%rbp), %rdx
movq -0x4a0(%rbp), %rsi
leaq 0x91c1a(%rip), %rcx     # 0xaf258
leaq -0x370(%rbp), %rdi
callq 0x11740
movl %eax, -0x4a4(%rbp)
jmp 0x1d652
movl -0x4a4(%rbp), %eax
movl %eax, -0x2a4(%rbp)
cmpl $0x0, -0x2a4(%rbp)
je 0x1d694
movl $0x1, -0x4(%rbp)
movl $0x1, -0x2b8(%rbp)
jmp 0x1da20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2b0(%rbp)
movl %eax, -0x2b4(%rbp)
jmp 0x1da30
cmpl $0x0, -0x1c0(%rbp)
jne 0x1d7e6
movq -0x10(%rbp), %rax
movq %rax, -0x4b0(%rbp)
leaq -0x388(%rbp), %rdi
callq 0x1a4a0
jmp 0x1d6ba
movq -0x4b0(%rbp), %rdi
movl $0x1, %esi
leaq -0x388(%rbp), %rdx
callq 0x23790
jmp 0x1d6d4
leaq -0x388(%rbp), %rdi
callq 0x1a4c0
jmp 0x1d6e2
movl -0x29c(%rbp), %edi
leaq -0x1b8(%rbp), %rdx
movl $0x1, %esi
callq 0x5c80
movq %rax, -0x4b8(%rbp)
jmp 0x1d702
movq -0x4b8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x390(%rbp)
movq $0x0, -0x398(%rbp)
movq -0x398(%rbp), %rax
movq %rax, -0x4c8(%rbp)
leaq -0x370(%rbp), %rdi
callq 0x1a400
movq %rax, -0x4c0(%rbp)
jmp 0x1d742
movq -0x4c8(%rbp), %rax
movq -0x4c0(%rbp), %rcx
cmpq %rcx, %rax
jae 0x1d7e1
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x23840
movq %rax, -0x4d0(%rbp)
jmp 0x1d76f
movq -0x398(%rbp), %rsi
leaq -0x370(%rbp), %rdi
callq 0x1a5f0
movq %rax, -0x4d8(%rbp)
jmp 0x1d78b
movq -0x4d8(%rbp), %rsi
movq -0x4d0(%rbp), %rdi
callq 0x23860
jmp 0x1d7a0
jmp 0x1d7a2
movq -0x390(%rbp), %rax
addq -0x398(%rbp), %rax
movq %rax, -0x398(%rbp)
jmp 0x1d71f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2b0(%rbp)
movl %eax, -0x2b4(%rbp)
leaq -0x388(%rbp), %rdi
callq 0x1a4c0
jmp 0x1d7dc
jmp 0x1da30
jmp 0x1da0f
movq -0x10(%rbp), %rax
movq %rax, -0x4e0(%rbp)
leaq -0x3b0(%rbp), %rdi
callq 0x1a4a0
jmp 0x1d7ff
movq -0x4e0(%rbp), %rdi
movl $0x7, %esi
leaq -0x3b0(%rbp), %rdx
callq 0x23790
jmp 0x1d819
leaq -0x3b0(%rbp), %rdi
callq 0x1a4c0
jmp 0x1d827
movq $0x0, -0x3b8(%rbp)
movq $0x0, -0x3c0(%rbp)
cmpq $0x7, -0x3c0(%rbp)
jae 0x1da0d
movl -0x29c(%rbp), %eax
movq -0x3c0(%rbp), %rdx
leaq 0x952d1(%rip), %rcx     # 0xb2b30
subl (%rcx,%rdx,4), %eax
movq -0x3c0(%rbp), %rdx
leaq 0x95300(%rip), %rcx     # 0xb2b70
addl (%rcx,%rdx,4), %eax
subl $0x1, %eax
movq -0x3c0(%rbp), %rsi
leaq 0x952ec(%rip), %rcx     # 0xb2b70
xorl %edx, %edx
divl (%rcx,%rsi,4)
movl %eax, -0x3c4(%rbp)
movl -0x2a0(%rbp), %eax
movq -0x3c0(%rbp), %rdx
leaq 0x952ad(%rip), %rcx     # 0xb2b50
subl (%rcx,%rdx,4), %eax
movq -0x3c0(%rbp), %rdx
leaq 0x952dc(%rip), %rcx     # 0xb2b90
addl (%rcx,%rdx,4), %eax
subl $0x1, %eax
movq -0x3c0(%rbp), %rsi
leaq 0x952c8(%rip), %rcx     # 0xb2b90
xorl %edx, %edx
divl (%rcx,%rsi,4)
movl %eax, -0x3c8(%rbp)
movq -0x3c0(%rbp), %rcx
leaq 0x9524f(%rip), %rax     # 0xb2b30
movl (%rax,%rcx,4), %eax
cmpl -0x29c(%rbp), %eax
jae 0x1d905
movq -0x3c0(%rbp), %rcx
leaq 0x95256(%rip), %rax     # 0xb2b50
movl (%rax,%rcx,4), %eax
cmpl -0x2a0(%rbp), %eax
jb 0x1d92f
jmp 0x1d9f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2b0(%rbp)
movl %eax, -0x2b4(%rbp)
leaq -0x3b0(%rbp), %rdi
callq 0x1a4c0
jmp 0x1d92a
jmp 0x1da30
movl -0x3c4(%rbp), %edi
leaq -0x1b8(%rbp), %rdx
movl $0x1, %esi
callq 0x5c80
movq %rax, -0x4e8(%rbp)
jmp 0x1d94f
movq -0x4e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x3d0(%rbp)
movq $0x0, -0x3d8(%rbp)
movq -0x3d8(%rbp), %rax
movl -0x3c8(%rbp), %ecx
cmpq %rcx, %rax
jae 0x1d9f4
movq -0x10(%rbp), %rdi
movq -0x3c0(%rbp), %rsi
callq 0x23840
movq %rax, -0x4f0(%rbp)
jmp 0x1d997
movq -0x3b8(%rbp), %rsi
leaq -0x370(%rbp), %rdi
callq 0x1a5f0
movq %rax, -0x4f8(%rbp)
jmp 0x1d9b3
movq -0x4f8(%rbp), %rsi
movq -0x4f0(%rbp), %rdi
callq 0x23860
jmp 0x1d9c8
movq -0x3d0(%rbp), %rax
addq -0x3b8(%rbp), %rax
movq %rax, -0x3b8(%rbp)
movq -0x3d8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x3d8(%rbp)
jmp 0x1d96c
jmp 0x1d9f6
movq -0x3c0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x3c0(%rbp)
jmp 0x1d83d
jmp 0x1da0f
movl $0x0, -0x4(%rbp)
movl $0x1, -0x2b8(%rbp)
leaq -0x370(%rbp), %rdi
callq 0x1a4c0
jmp 0x1da2e
jmp 0x1da40
leaq -0x370(%rbp), %rdi
callq 0x1a4c0
jmp 0x1da3e
jmp 0x1da50
leaq -0x2e8(%rbp), %rdi
callq 0x1a4c0
jmp 0x1da4e
jmp 0x1da60
leaq -0x2e8(%rbp), %rdi
callq 0x1a4c0
jmp 0x1da5e
jmp 0x1da78
leaq -0x298(%rbp), %rdi
callq 0x11b20
movl -0x4(%rbp), %eax
addq $0x500, %rsp            # imm = 0x500
popq %rbp
retq
leaq -0x298(%rbp), %rdi
callq 0x11b20
jmp 0x1da86
jmp 0x1da88
movq -0x2b0(%rbp), %rdi
callq 0x3430
movq %rax, %rdi
callq 0x1a490
nopl (%rax)