// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: readChunk_iTXt(LodePNGInfo*, LodePNGDecompressSettings const*, unsigned char const*, unsigned long)
static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings,
                               const unsigned char* data, size_t chunkLength)
{
  unsigned error = 0;
  unsigned i;

  unsigned length, begin, compressed;
  char *key = 0, *langtag = 0, *transkey = 0;
  ucvector decoded;
  ucvector_init(&decoded);

  while(!error) /*not really a while loop, only used to break on error*/
  {
    /*Quick check if the chunk length isn't too small. Even without check
    it'd still fail with other error checks below if it's too short. This just gives a different error code.*/
    if(chunkLength < 5) CERROR_BREAK(error, 30); /*iTXt chunk too short*/

    /*read the key*/
    for(length = 0; length < chunkLength && data[length] != 0; ++length) ;
    if(length + 3 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination char, corrupt?*/
    if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/

    key = (char*)lodepng_malloc(length + 1);
    if(!key) CERROR_BREAK(error, 83); /*alloc fail*/

    key[length] = 0;
    for(i = 0; i != length; ++i) key[i] = (char)data[i];

    /*read the compression method*/
    compressed = data[length + 1];
    if(data[length + 2] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/

    /*even though it's not allowed by the standard, no error is thrown if
    there's no null termination char, if the text is empty for the next 3 texts*/

    /*read the langtag*/
    begin = length + 3;
    length = 0;
    for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length;

    langtag = (char*)lodepng_malloc(length + 1);
    if(!langtag) CERROR_BREAK(error, 83); /*alloc fail*/

    langtag[length] = 0;
    for(i = 0; i != length; ++i) langtag[i] = (char)data[begin + i];

    /*read the transkey*/
    begin += length + 1;
    length = 0;
    for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length;

    transkey = (char*)lodepng_malloc(length + 1);
    if(!transkey) CERROR_BREAK(error, 83); /*alloc fail*/

    transkey[length] = 0;
    for(i = 0; i != length; ++i) transkey[i] = (char)data[begin + i];

    /*read the actual text*/
    begin += length + 1;

    length = chunkLength < begin ? 0 : chunkLength - begin;

    if(compressed)
    {
      /*will fail if zlib error, e.g. if length is too small*/
      error = zlib_decompress(&decoded.data, &decoded.size,
                              (unsigned char*)(&data[begin]),
                              length, zlibsettings);
      if(error) break;
      if(decoded.allocsize < decoded.size) decoded.allocsize = decoded.size;
      ucvector_push_back(&decoded, 0);
    }
    else
    {
      if(!ucvector_resize(&decoded, length + 1)) CERROR_BREAK(error, 83 /*alloc fail*/);

      decoded.data[length] = 0;
      for(i = 0; i != length; ++i) decoded.data[i] = data[begin + i];
    }

    error = lodepng_add_itext(info, key, langtag, transkey, (char*)decoded.data);

    break;
  }

  lodepng_free(key);
  lodepng_free(langtag);
  lodepng_free(transkey);
  ucvector_cleanup(&decoded);

  return error;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
leaq 0x10(%rsp), %rdi
callq 0xf580
cmpl $0x0, 0x54(%rsp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x16b08
jmp 0x16f36
cmpq $0x5, 0x58(%rsp)
jae 0x16b1d
movl $0x1e, 0x54(%rsp)
jmp 0x16f36
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq 0x58(%rsp), %rcx
movb %al, 0xf(%rsp)
jae 0x16b4f
movq 0x60(%rsp), %rax
movl 0x4c(%rsp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setne %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x16b59
jmp 0x16b68
jmp 0x16b5b
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x16b25
movl 0x4c(%rsp), %eax
addl $0x3, %eax
movl %eax, %eax
cmpq 0x58(%rsp), %rax
jb 0x16b85
movl $0x4b, 0x54(%rsp)
jmp 0x16f36
cmpl $0x1, 0x4c(%rsp)
jb 0x16b93
cmpl $0x4f, 0x4c(%rsp)
jbe 0x16ba0
movl $0x59, 0x54(%rsp)
jmp 0x16f36
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x6cd0
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x16bca
movl $0x53, 0x54(%rsp)
jmp 0x16f36
movq 0x38(%rsp), %rax
movl 0x4c(%rsp), %ecx
movb $0x0, (%rax,%rcx)
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x4c(%rsp), %eax
je 0x16c0e
movq 0x60(%rsp), %rax
movl 0x50(%rsp), %ecx
movb (%rax,%rcx), %dl
movq 0x38(%rsp), %rax
movl 0x50(%rsp), %ecx
movb %dl, (%rax,%rcx)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x16bdf
movq 0x60(%rsp), %rax
movl 0x4c(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl %eax, 0x44(%rsp)
movq 0x60(%rsp), %rax
movl 0x4c(%rsp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x16c48
movl $0x48, 0x54(%rsp)
jmp 0x16f36
movl 0x4c(%rsp), %eax
addl $0x3, %eax
movl %eax, 0x48(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0x50(%rsp)
movl 0x50(%rsp), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq 0x58(%rsp), %rcx
movb %al, 0xe(%rsp)
jae 0x16c8d
movq 0x60(%rsp), %rax
movl 0x50(%rsp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setne %al
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
testb $0x1, %al
jne 0x16c97
jmp 0x16caf
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x16c63
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x6cd0
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x16cd9
movl $0x53, 0x54(%rsp)
jmp 0x16f36
movq 0x30(%rsp), %rax
movl 0x4c(%rsp), %ecx
movb $0x0, (%rax,%rcx)
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x4c(%rsp), %eax
je 0x16d23
movq 0x60(%rsp), %rax
movl 0x48(%rsp), %ecx
addl 0x50(%rsp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq 0x30(%rsp), %rax
movl 0x50(%rsp), %ecx
movb %dl, (%rax,%rcx)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x16cee
movl 0x4c(%rsp), %eax
addl $0x1, %eax
addl 0x48(%rsp), %eax
movl %eax, 0x48(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0x50(%rsp)
movl 0x50(%rsp), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq 0x58(%rsp), %rcx
movb %al, 0xd(%rsp)
jae 0x16d6c
movq 0x60(%rsp), %rax
movl 0x50(%rsp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setne %al
movb %al, 0xd(%rsp)
movb 0xd(%rsp), %al
testb $0x1, %al
jne 0x16d76
jmp 0x16d8e
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x16d42
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x6cd0
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x16db8
movl $0x53, 0x54(%rsp)
jmp 0x16f36
movq 0x28(%rsp), %rax
movl 0x4c(%rsp), %ecx
movb $0x0, (%rax,%rcx)
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x4c(%rsp), %eax
je 0x16e02
movq 0x60(%rsp), %rax
movl 0x48(%rsp), %ecx
addl 0x50(%rsp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq 0x28(%rsp), %rax
movl 0x50(%rsp), %ecx
movb %dl, (%rax,%rcx)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x16dcd
movl 0x4c(%rsp), %eax
addl $0x1, %eax
addl 0x48(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0x58(%rsp), %rax
movl 0x48(%rsp), %ecx
cmpq %rcx, %rax
jae 0x16e27
xorl %eax, %eax
movq %rax, (%rsp)
jmp 0x16e37
movq 0x58(%rsp), %rax
movl 0x48(%rsp), %ecx
subq %rcx, %rax
movq %rax, (%rsp)
movq (%rsp), %rax
movl %eax, 0x4c(%rsp)
cmpl $0x0, 0x44(%rsp)
je 0x16ea4
leaq 0x10(%rsp), %rdi
leaq 0x10(%rsp), %rsi
addq $0x8, %rsi
movq 0x60(%rsp), %rdx
movl 0x48(%rsp), %eax
addq %rax, %rdx
movl 0x4c(%rsp), %eax
movl %eax, %ecx
movq 0x68(%rsp), %r8
callq 0x10f10
movl %eax, 0x54(%rsp)
cmpl $0x0, 0x54(%rsp)
je 0x16e80
jmp 0x16f36
movq 0x20(%rsp), %rax
cmpq 0x18(%rsp), %rax
jae 0x16e96
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x81d0
jmp 0x16f14
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x10(%rsp), %rdi
callq 0x12310
cmpl $0x0, %eax
jne 0x16ec8
movl $0x53, 0x54(%rsp)
jmp 0x16f36
movq 0x10(%rsp), %rax
movl 0x4c(%rsp), %ecx
movb $0x0, (%rax,%rcx)
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x4c(%rsp), %eax
je 0x16f12
movq 0x60(%rsp), %rax
movl 0x48(%rsp), %ecx
addl 0x50(%rsp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq 0x10(%rsp), %rax
movl 0x50(%rsp), %ecx
movb %dl, (%rax,%rcx)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x16edd
jmp 0x16f14
movq 0x70(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %r8
callq 0x9340
movl %eax, 0x54(%rsp)
movq 0x38(%rsp), %rdi
callq 0x78b0
movq 0x30(%rsp), %rdi
callq 0x78b0
movq 0x28(%rsp), %rdi
callq 0x78b0
leaq 0x10(%rsp), %rdi
callq 0x17170
movl 0x54(%rsp), %eax
addq $0x78, %rsp
retq
nopw (%rax,%rax)