// File: /workspace/github_cpp_makefile_star2_O1/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 0x11482
cmpq $0x8, %rax
jl 0x11482
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4e89
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 0x1517a
movq 0x18(%rsp), %r13
cmpq $0x4, %r13
jne 0x1145a
movq %r12, %rdi
leaq 0x633a9(%rip), %rsi     # 0x74758
callq 0x20b0
testl %eax, %eax
je 0x113ff
movq %r12, %rdi
leaq 0x647e0(%rip), %rsi     # 0x75ba2
callq 0x20b0
testl %eax, %eax
je 0x1141e
movq %r12, %rdi
leaq 0x65ba7(%rip), %rsi     # 0x76f7c
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 0x1144c
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 0x1144c
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 0x4fe2
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x1146e
callq 0x2200
cmpq $0x4, %r13
je 0x11350
movl $0x1, %eax
jmp 0x11616
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x8(%rdi), %rsi
movq 0x50(%rsp), %rbp
movq (%rbp), %rdx
movq 0x8(%rsp), %rax
leaq (%rdx,%rax), %rcx
callq 0x159a6
movq (%r15), %rax
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
cmpq %rax, 0x8(%r15)
je 0x11505
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 0x15bba
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 0x114d1
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq (%rbp), %rcx
movq 0x8(%rsp), %rdx
addq %rcx, %rdx
addq 0x30(%rsp), %rcx
callq 0x159a6
movq 0x18(%r15), %rax
cmpq %rax, 0x20(%r15)
je 0x11571
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 0x15bba
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 0x1153c
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq (%rbp), %rcx
movq 0x30(%rsp), %rdx
addq %rcx, %rdx
addq 0x38(%rsp), %rcx
callq 0x159a6
movq 0x30(%r15), %rax
cmpq %rax, 0x38(%r15)
je 0x115dd
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 0x15bba
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 0x115a8
leaq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq 0x38(%rsp), %rdx
addq (%rbp), %rdx
movq 0x8(%rbp), %rcx
callq 0x159a6
leaq 0x10(%rsp), %rsi
movq %rbp, %rdi
callq 0x15008
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x11614
callq 0x2200
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x11633
jmp 0x11633
jmp 0x11633
jmp 0x11633
jmp 0x11633
jmp 0x11633
jmp 0x11633
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
jne 0x11659
jmp 0x1165e
movq %rax, %rbx
jmp 0x1165e
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x1165e
callq 0x2200
movq %rbx, %rdi
callq 0x23b0