// File: /workspace/github_cpp_makefile_star2_O3/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), %r14
movq %rdi, 0x50(%rsp)
movq 0x8(%rdi), %rbp
addq $0x8, %r14
movq $0x0, 0x38(%rsp)
leaq 0x4b(%rsp), %rbx
leaq 0x10(%rsp), %r12
movq $0x0, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
movq %r14, 0x40(%rsp)
movq %rbp, %rax
subq %r14, %rax
jbe 0x11100
cmpq $0x8, %rax
jl 0x11100
movq %rbx, %rdi
movq %r14, %rsi
callq 0x544e
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
callq 0x2100
leaq (%rsp,%rax), %rdx
addq $0x4b, %rdx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x14f6e
movq 0x18(%rsp), %r13
cmpq $0x4, %r13
jne 0x110d8
movq %r12, %rdi
leaq 0x6384f(%rip), %rsi     # 0x7487c
callq 0x20b0
testl %eax, %eax
je 0x1107d
movq %r12, %rdi
leaq 0x64c86(%rip), %rsi     # 0x75cc6
callq 0x20b0
testl %eax, %eax
je 0x1109c
movq %r12, %rdi
leaq 0x66149(%rip), %rsi     # 0x7719c
callq 0x20b0
movq %r14, %rcx
subq 0x40(%rsp), %rcx
addq $0x8, %rcx
movq 0x38(%rsp), %rdx
testq %rdx, %rdx
cmovneq %rdx, %rcx
testl %eax, %eax
cmoveq %rcx, %rdx
movq %rdx, 0x38(%rsp)
jmp 0x110ca
movq %r14, %rax
subq 0x40(%rsp), %rax
addq $0x8, %rax
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsp)
jmp 0x110ca
movq %r14, %rax
subq 0x40(%rsp), %rax
addq $0x8, %rax
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
cmoveq %rax, %rcx
movq %rcx, 0x8(%rsp)
movq 0x30(%rsp), %rcx
testq %rcx, %rcx
cmoveq %rax, %rcx
movq %rcx, 0x30(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x55b2
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x110ec
callq 0x2210
cmpq $0x4, %r13
je 0x10fce
movl $0x1, %eax
jmp 0x11292
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x50(%rsp), %rbp
movq (%rbp), %rdx
movq 0x8(%rsp), %rax
leaq (%rdx,%rax), %rcx
xorl %esi, %esi
callq 0x157d2
movq (%r15), %rax
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
cmpq %rax, 0x8(%r15)
je 0x11181
movl $0x8, %r12d
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rsi
movq -0x8(%rax,%r12), %rdx
movq (%rax,%r12), %rcx
movq %rbx, %rdi
callq 0x159b4
incq %r13
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r14, %rcx
addq $0x18, %r12
cmpq %rcx, %r13
jb 0x1114d
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq (%rbp), %rcx
movq 0x8(%rsp), %rdx
addq %rcx, %rdx
addq 0x30(%rsp), %rcx
callq 0x157d2
movq 0x18(%r15), %rax
cmpq %rax, 0x20(%r15)
je 0x111ed
movl $0x8, %r12d
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rsi
movq -0x8(%rax,%r12), %rdx
movq (%rax,%r12), %rcx
movq %rbx, %rdi
callq 0x159b4
incq %r13
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r14, %rcx
addq $0x18, %r12
cmpq %rcx, %r13
jb 0x111b8
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq (%rbp), %rcx
movq 0x30(%rsp), %rdx
addq %rcx, %rdx
addq 0x38(%rsp), %rcx
callq 0x157d2
movq 0x30(%r15), %rax
cmpq %rax, 0x38(%r15)
je 0x11259
movl $0x8, %r12d
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rsi
movq -0x8(%rax,%r12), %rdx
movq (%rax,%r12), %rcx
movq %rbx, %rdi
callq 0x159b4
incq %r13
movq 0x30(%r15), %rax
movq 0x38(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r14, %rcx
addq $0x18, %r12
cmpq %rcx, %r13
jb 0x11224
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq 0x38(%rsp), %rdx
addq (%rbp), %rdx
movq 0x8(%rbp), %rcx
callq 0x157d2
leaq 0x10(%rsp), %rsi
movq %rbp, %rdi
callq 0x14de6
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x11290
callq 0x2210
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x112af
jmp 0x112af
jmp 0x112af
jmp 0x112af
jmp 0x112af
jmp 0x112af
jmp 0x112af
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
jne 0x112d5
jmp 0x112da
movq %rax, %rbx
jmp 0x112da
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x112da
callq 0x2210
movq %rbx, %rdi
callq 0x23c0