// File: /workspace/github_cpp_makefile_star2_O2/lvandeve[P]lodepng/lodepng_util.cpp
// Function: lodepng::insertChunks(std::vector<unsigned char, std::allocator<unsigned char>>&, std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>> const*)
unsigned insertChunks(std::vector<unsigned char>& png,
                      const std::vector<std::vector<unsigned char> > chunks[3]) {
  const unsigned char *chunk, *begin, *end;
  end = &png.back() + 1;
  begin = chunk = &png.front() + 8;

  long l0 = 0; //location 0: IHDR-l0-PLTE (or IHDR-l0-l1-IDAT)
  long l1 = 0; //location 1: PLTE-l1-IDAT (or IHDR-l0-l1-IDAT)
  long l2 = 0; //location 2: IDAT-l2-IEND

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

    if(name == "PLTE") {
      if(l0 == 0) l0 = chunk - begin + 8;
    } else if(name == "IDAT") {
      if(l0 == 0) l0 = chunk - begin + 8;
      if(l1 == 0) l1 = chunk - begin + 8;
    } else if(name == "IEND") {
      if(l2 == 0) l2 = chunk - begin + 8;
    }

    chunk = lodepng_chunk_next_const(chunk, end);
  }

  std::vector<unsigned char> result;
  result.insert(result.end(), png.begin(), png.begin() + l0);
  for(size_t i = 0; i < chunks[0].size(); i++) result.insert(result.end(), chunks[0][i].begin(), chunks[0][i].end());
  result.insert(result.end(), png.begin() + l0, png.begin() + l1);
  for(size_t i = 0; i < chunks[1].size(); i++) result.insert(result.end(), chunks[1][i].begin(), chunks[1][i].end());
  result.insert(result.end(), png.begin() + l1, png.begin() + l2);
  for(size_t i = 0; i < chunks[2].size(); i++) result.insert(result.end(), chunks[2][i].begin(), chunks[2][i].end());
  result.insert(result.end(), png.begin() + l2, png.end());

  png = result;
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r15
movq (%rdi), %r12
movq %rdi, 0x48(%rsp)
movq 0x8(%rdi), %rbp
addq $0x8, %r12
movq $0x0, 0x10(%rsp)
leaq 0x53(%rsp), %rbx
leaq 0x20(%rsp), %r14
movq $0x0, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movq %r12, 0x40(%rsp)
movq %rbp, %rax
subq %r12, %rax
jbe 0xef0a
cmpq $0x8, %rax
jl 0xef0a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4574
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x7(%rsp), %rdx
callq 0x20c0
movq 0x28(%rsp), %r13
cmpq $0x4, %r13
jne 0xeef0
movq %r14, %rdi
leaq 0x4f933(%rip), %rsi     # 0x5e778
callq 0x121b0
testb %al, %al
je 0xee6d
movq %r12, %rax
subq 0x40(%rsp), %rax
addq $0x8, %rax
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsp)
jmp 0xeee2
movq %r14, %rdi
leaq 0x50d4b(%rip), %rsi     # 0x5fbc2
callq 0x121b0
testb %al, %al
je 0xeeb0
movq %r12, %rax
subq 0x40(%rsp), %rax
addq $0x8, %rax
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsp)
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
cmoveq %rax, %rcx
movq %rcx, 0x18(%rsp)
jmp 0xeee2
movq %r14, %rdi
leaq 0x52142(%rip), %rsi     # 0x60ffc
callq 0x121b0
movq %r12, %rcx
subq 0x40(%rsp), %rcx
addq $0x8, %rcx
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
cmovneq %rdx, %rcx
testb %al, %al
cmovneq %rcx, %rdx
movq %rdx, 0x10(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x4705
movq %rax, %r12
movq %r14, %rdi
callq 0x2270
cmpq $0x4, %r13
je 0xedfb
pushq $0x1
popq %rax
jmp 0xf05c
leaq 0x20(%rsp), %rdi
andq $0x0, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movq 0x48(%rsp), %rbp
movq (%rbp), %rdx
movq 0x8(%rsp), %rax
leaq (%rdx,%rax), %rcx
xorl %esi, %esi
callq 0x129ea
pushq $0x8
popq %r12
xorl %r13d, %r13d
pushq $0x18
popq %r14
leaq 0x20(%rsp), %rbx
movq (%r15), %rcx
movq 0x8(%r15), %rax
subq %rcx, %rax
cqto
idivq %r14
movq 0x28(%rsp), %rsi
cmpq %rax, %r13
jae 0xef76
movq -0x8(%rcx,%r12), %rdx
movq (%rcx,%r12), %rcx
movq %rbx, %rdi
callq 0x12b76
incq %r13
addq $0x18, %r12
jmp 0xef43
movq (%rbp), %rcx
movq 0x8(%rsp), %rdx
addq %rcx, %rdx
addq 0x18(%rsp), %rcx
leaq 0x20(%rsp), %rdi
callq 0x129ea
pushq $0x8
popq %r12
xorl %r13d, %r13d
leaq 0x20(%rsp), %rbx
movq 0x18(%r15), %rcx
movq 0x20(%r15), %rax
subq %rcx, %rax
cqto
idivq %r14
movq 0x28(%rsp), %rsi
cmpq %rax, %r13
jae 0xefd1
movq -0x8(%rcx,%r12), %rdx
movq (%rcx,%r12), %rcx
movq %rbx, %rdi
callq 0x12b76
incq %r13
addq $0x18, %r12
jmp 0xef9d
movq (%rbp), %rcx
movq 0x18(%rsp), %rdx
addq %rcx, %rdx
addq 0x10(%rsp), %rcx
leaq 0x20(%rsp), %rdi
callq 0x129ea
pushq $0x8
popq %r12
xorl %r13d, %r13d
leaq 0x20(%rsp), %rbx
movq 0x30(%r15), %rcx
movq 0x38(%r15), %rax
subq %rcx, %rax
cqto
idivq %r14
movq 0x28(%rsp), %rsi
cmpq %rax, %r13
jae 0xf02c
movq -0x8(%rcx,%r12), %rdx
movq (%rcx,%r12), %rcx
movq %rbx, %rdi
callq 0x12b76
incq %r13
addq $0x18, %r12
jmp 0xeff8
movq 0x10(%rsp), %rdx
addq (%rbp), %rdx
movq 0x8(%rbp), %rcx
leaq 0x20(%rsp), %rdi
callq 0x129ea
leaq 0x20(%rsp), %rsi
movq %rbp, %rdi
callq 0x12224
leaq 0x20(%rsp), %rdi
callq 0xea7c
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf079
jmp 0xf079
jmp 0xf079
jmp 0xf079
jmp 0xf079
jmp 0xf079
jmp 0xf079
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0xea7c
jmp 0xf09a
movq %rax, %rbx
jmp 0xf09a
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2270
movq %rbx, %rdi
callq 0x2380
movq %rax, %rdi
callq 0xe6cc