// File: tomfunkhouser[P]gaps/pkgs/png/pngwutil.c
// Function: png_write_iCCP
void /* PRIVATE */
png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
   png_charp profile, int profile_len)
{
   PNG_iCCP;
   png_size_t name_len;
   png_charp new_name;
   compression_state comp;
   int embedded_profile_len = 0;

   png_debug(1, "in png_write_iCCP");

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

   if ((name_len = png_check_keyword(png_ptr, name,
      &new_name)) == 0)
      return;

   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
      png_warning(png_ptr, "Unknown compression type in iCCP chunk");

   if (profile == NULL)
      profile_len = 0;

   if (profile_len > 3)
      embedded_profile_len =
          ((*( (png_bytep)profile    ))<<24) |
          ((*( (png_bytep)profile + 1))<<16) |
          ((*( (png_bytep)profile + 2))<< 8) |
          ((*( (png_bytep)profile + 3))    );

   if (embedded_profile_len < 0)
   {
      png_warning(png_ptr,
        "Embedded profile length in iCCP chunk is negative");
      png_free(png_ptr, new_name);
      return;
   }

   if (profile_len < embedded_profile_len)
   {
      png_warning(png_ptr,
        "Embedded profile length too large in iCCP chunk");
      png_free(png_ptr, new_name);
      return;
   }

   if (profile_len > embedded_profile_len)
   {
      png_warning(png_ptr,
        "Truncating profile to actual length in iCCP chunk");
      profile_len = embedded_profile_len;
   }

   if (profile_len)
      profile_len = png_text_compress(png_ptr, profile,
        (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);

   /* Make sure we include the NULL after the name and the compression type */
   png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
          (png_uint_32)(name_len + profile_len + 2));
   new_name[name_len + 1] = 0x00;
   png_write_chunk_data(png_ptr, (png_bytep)new_name,
     (png_size_t)(name_len + 2));

   if (profile_len)
      png_write_compressed_data_out(png_ptr, &comp);

   png_write_chunk_end(png_ptr);
   png_free(png_ptr, new_name);
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movl %edx, 0x54(%rsp)
movq %rcx, 0x48(%rsp)
movl %r8d, 0x44(%rsp)
movl 0x138fe(%rip), %eax     # 0x13f650
movl %eax, 0x3f(%rsp)
movb 0x138f8(%rip), %al      # 0x13f654
movb %al, 0x43(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x12bf40
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
jne 0x12bdb6
jmp 0x12bf37
cmpl $0x0, 0x54(%rsp)
je 0x12bdce
movq 0x60(%rsp), %rdi
leaq 0x149f3(%rip), %rsi     # 0x1407bc
callq 0x113940
cmpq $0x0, 0x48(%rsp)
jne 0x12bdde
movl $0x0, 0x44(%rsp)
cmpl $0x3, 0x44(%rsp)
jle 0x12be1b
movq 0x48(%rsp), %rax
movzbl (%rax), %eax
shll $0x18, %eax
movq 0x48(%rsp), %rcx
movzbl 0x1(%rcx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movq 0x48(%rsp), %rcx
movzbl 0x2(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq 0x48(%rsp), %rcx
movzbl 0x3(%rcx), %ecx
orl %ecx, %eax
movl %eax, 0x4(%rsp)
cmpl $0x0, 0x4(%rsp)
jge 0x12be47
movq 0x60(%rsp), %rdi
leaq 0x149b5(%rip), %rsi     # 0x1407e3
callq 0x113940
movq 0x60(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x1136d0
jmp 0x12bf37
movl 0x44(%rsp), %eax
cmpl 0x4(%rsp), %eax
jge 0x12be76
movq 0x60(%rsp), %rdi
leaq 0x149b8(%rip), %rsi     # 0x140815
callq 0x113940
movq 0x60(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x1136d0
jmp 0x12bf37
movl 0x44(%rsp), %eax
cmpl 0x4(%rsp), %eax
jle 0x12be99
movq 0x60(%rsp), %rdi
leaq 0x149b9(%rip), %rsi     # 0x140845
callq 0x113940
movl 0x4(%rsp), %eax
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x44(%rsp)
je 0x12bebf
movq 0x60(%rsp), %rdi
movq 0x48(%rsp), %rsi
movslq 0x44(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x8(%rsp), %r8
callq 0x12c2c0
movl %eax, 0x44(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x3f(%rsp), %rsi
movq 0x30(%rsp), %rax
movslq 0x44(%rsp), %rcx
addq %rcx, %rax
addq $0x2, %rax
movl %eax, %edx
callq 0x12b0a0
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
movb $0x0, 0x1(%rax,%rcx)
movq 0x60(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq $0x2, %rdx
callq 0x12b140
cmpl $0x0, 0x44(%rsp)
je 0x12bf1e
movq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x12c820
movq 0x60(%rsp), %rdi
callq 0x12b1a0
movq 0x60(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x1136d0
addq $0x68, %rsp
retq
nopl (%rax)