// File: tomfunkhouser[P]gaps/pkgs/png/pngwutil.c
// Function: png_write_iTXt
void /* PRIVATE */
png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
    png_charp lang, png_charp lang_key, png_charp text)
{
   PNG_iTXt;
   png_size_t lang_len, key_len, lang_key_len, text_len;
   png_charp new_lang;
   png_charp new_key = NULL;
   png_byte cbuf[2];
   compression_state comp;

   png_debug(1, "in png_write_iTXt");

   comp.num_output_ptr = 0;
   comp.max_output_ptr = 0;
   comp.output_ptr = NULL;
   comp.input = NULL;

   if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
      return;

   if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
   {
      png_warning(png_ptr, "Empty language field in iTXt chunk");
      new_lang = NULL;
      lang_len = 0;
   }

   if (lang_key == NULL)
      lang_key_len = 0;
   else
      lang_key_len = png_strlen(lang_key);

   if (text == NULL)
      text_len = 0;
   else
      text_len = png_strlen(text);

   /* Compute the compressed data; do it now for the length */
   text_len = png_text_compress(png_ptr, text, text_len, compression-2,
      &comp);


   /* Make sure we include the compression flag, the compression byte,
    * and the NULs after the key, lang, and lang_key parts */

   png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
          (png_uint_32)(
        5 /* comp byte, comp flag, terminators for key, lang and lang_key */
        + key_len
        + lang_len
        + lang_key_len
        + text_len));

   /* We leave it to the application to meet PNG-1.0 requirements on the
    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.
    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
    */
   png_write_chunk_data(png_ptr, (png_bytep)new_key,
     (png_size_t)(key_len + 1));

   /* Set the compression flag */
   if (compression == PNG_ITXT_COMPRESSION_NONE || \
       compression == PNG_TEXT_COMPRESSION_NONE)
       cbuf[0] = 0;
   else /* compression == PNG_ITXT_COMPRESSION_zTXt */
       cbuf[0] = 1;
   /* Set the compression method */
   cbuf[1] = 0;
   png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);

   cbuf[0] = 0;
   png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
     (png_size_t)(lang_len + 1));
   png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
     (png_size_t)(lang_key_len + 1));
   png_write_compressed_data_out(png_ptr, &comp);

   png_write_chunk_end(png_ptr);
   png_free(png_ptr, new_key);
   png_free(png_ptr, new_lang);
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movl %esi, 0xac(%rsp)
movq %rdx, 0xa0(%rsp)
movq %rcx, 0x98(%rsp)
movq %r8, 0x90(%rsp)
movq %r9, 0x88(%rsp)
movl 0x126ad(%rip), %eax     # 0x13fe89
movl %eax, 0x83(%rsp)
movb 0x126a4(%rip), %al      # 0x13fe8d
movb %al, 0x87(%rsp)
movq $0x0, 0x50(%rsp)
movl $0x0, 0x34(%rsp)
movl $0x0, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x28(%rsp)
movq 0xb0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x12bf40
movq %rax, 0x70(%rsp)
cmpq $0x0, %rax
jne 0x12d845
jmp 0x12da6a
movq 0xb0(%rsp), %rdi
movq 0x98(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x12bf40
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
jne 0x12d890
movq 0xb0(%rsp), %rdi
leaq 0x132cc(%rip), %rsi     # 0x140b45
callq 0x113940
movq $0x0, 0x58(%rsp)
movq $0x0, 0x78(%rsp)
cmpq $0x0, 0x90(%rsp)
jne 0x12d8a6
movq $0x0, 0x68(%rsp)
jmp 0x12d8b8
movq 0x90(%rsp), %rdi
callq 0xf200
movq %rax, 0x68(%rsp)
cmpq $0x0, 0x88(%rsp)
jne 0x12d8ce
movq $0x0, 0x60(%rsp)
jmp 0x12d8e0
movq 0x88(%rsp), %rdi
callq 0xf200
movq %rax, 0x60(%rsp)
movq 0xb0(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0x60(%rsp), %rdx
movl 0xac(%rsp), %ecx
subl $0x2, %ecx
leaq 0x28(%rsp), %r8
callq 0x12c2c0
cltq
movq %rax, 0x60(%rsp)
movq 0xb0(%rsp), %rdi
leaq 0x83(%rsp), %rsi
movq 0x70(%rsp), %rax
addq $0x5, %rax
addq 0x78(%rsp), %rax
addq 0x68(%rsp), %rax
addq 0x60(%rsp), %rax
movl %eax, %edx
callq 0x12b0a0
movq 0xb0(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq $0x1, %rdx
callq 0x12b140
cmpl $0x1, 0xac(%rsp)
je 0x12d96e
cmpl $-0x1, 0xac(%rsp)
jne 0x12d975
movb $0x0, 0x4e(%rsp)
jmp 0x12d97a
movb $0x1, 0x4e(%rsp)
movb $0x0, 0x4f(%rsp)
movq 0xb0(%rsp), %rdi
leaq 0x4e(%rsp), %rsi
movl $0x2, %edx
callq 0x12b140
movb $0x0, 0x4e(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x58(%rsp)
je 0x12d9bc
movq 0x58(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0x12d9c6
leaq 0x4e(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x78(%rsp), %rdx
addq $0x1, %rdx
callq 0x12b140
movq 0xb0(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x90(%rsp)
je 0x12da05
movq 0x90(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x12da0f
leaq 0x4e(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x68(%rsp), %rdx
addq $0x1, %rdx
callq 0x12b140
movq 0xb0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x12c820
movq 0xb0(%rsp), %rdi
callq 0x12b1a0
movq 0xb0(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x1136d0
movq 0xb0(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x1136d0
addq $0xb8, %rsp
retq
nopw %cs:(%rax,%rax)