// File: lvandeve[P]lodepng/lodepng.cpp
// Function: addChunk_iTXt(ucvector*, unsigned int, char const*, char const*, char const*, char const*, LodePNGCompressSettings*)
static unsigned addChunk_iTXt(ucvector* out, unsigned compress, const char* keyword, const char* langtag,
                              const char* transkey, const char* textstring, LodePNGCompressSettings* zlibsettings) {
  unsigned error = 0;
  unsigned char* chunk = 0;
  unsigned char* compressed = 0;
  size_t compressedsize = 0;
  size_t textsize = lodepng_strlen(textstring);
  size_t keysize = lodepng_strlen(keyword), langsize = lodepng_strlen(langtag), transsize = lodepng_strlen(transkey);

  if(keysize < 1 || keysize > 79) return 89; /*error: invalid keyword size*/

  if(compress) {
    error = zlib_compress(&compressed, &compressedsize,
                          (const unsigned char*)textstring, textsize, zlibsettings);
  }
  if(!error) {
    size_t size = keysize + 3 + langsize + 1 + transsize + 1 + (compress ? compressedsize : textsize);
    error = lodepng_chunk_init(&chunk, out, size, "iTXt");
  }
  if(!error) {
    size_t pos = 8;
    lodepng_memcpy(chunk + pos, keyword, keysize);
    pos += keysize;
    chunk[pos++] = 0; /*null termination char*/
    chunk[pos++] = (compress ? 1 : 0); /*compression flag*/
    chunk[pos++] = 0; /*compression method: 0*/
    lodepng_memcpy(chunk + pos, langtag, langsize);
    pos += langsize;
    chunk[pos++] = 0; /*null termination char*/
    lodepng_memcpy(chunk + pos, transkey, transsize);
    pos += transsize;
    chunk[pos++] = 0; /*null termination char*/
    if(compress) {
      lodepng_memcpy(chunk + pos, compressed, compressedsize);
    } else {
      lodepng_memcpy(chunk + pos, textstring, textsize);
    }
    lodepng_chunk_generate_crc(chunk);
  }

  lodepng_free(compressed);
  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x38(%rbp), %rdi
callq 0x4ed0
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4ed0
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4ed0
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
callq 0x4ed0
movq %rax, -0x78(%rbp)
cmpq $0x1, -0x68(%rbp)
jb 0x10a17
cmpq $0x4f, -0x68(%rbp)
jbe 0x10a23
movl $0x59, -0x4(%rbp)
jmp 0x10c3d
cmpl $0x0, -0x14(%rbp)
je 0x10a45
movq -0x38(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq 0x10(%rbp), %r8
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x119d0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x10ab8
movq -0x68(%rbp), %rax
addq $0x3, %rax
addq -0x70(%rbp), %rax
addq $0x1, %rax
addq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x10a7d
movq -0x58(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x10a88
movq -0x60(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
movq -0x98(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rsi
movq -0x80(%rbp), %rdx
leaq -0x48(%rbp), %rdi
leaq 0xa0ac9(%rip), %rcx     # 0xb1579
callq 0x167a0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x10c2e
movq $0x8, -0x88(%rbp)
movq -0x48(%rbp), %rdi
addq -0x88(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x5820
movq -0x68(%rbp), %rax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x48(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x88(%rbp)
movb $0x0, (%rax,%rcx)
movl -0x14(%rbp), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, %dl
movq -0x48(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x88(%rbp)
movb %dl, (%rax,%rcx)
movq -0x48(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x88(%rbp)
movb $0x0, (%rax,%rcx)
movq -0x48(%rbp), %rdi
addq -0x88(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0x5820
movq -0x70(%rbp), %rax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x48(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x88(%rbp)
movb $0x0, (%rax,%rcx)
movq -0x48(%rbp), %rdi
addq -0x88(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x78(%rbp), %rdx
callq 0x5820
movq -0x78(%rbp), %rax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x48(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x88(%rbp)
movb $0x0, (%rax,%rcx)
cmpl $0x0, -0x14(%rbp)
je 0x10c0d
movq -0x48(%rbp), %rdi
addq -0x88(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x5820
jmp 0x10c25
movq -0x48(%rbp), %rdi
addq -0x88(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x5820
movq -0x48(%rbp), %rdi
callq 0x5020
movq -0x50(%rbp), %rdi
callq 0x4230
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)