// File: tomfunkhouser[P]gaps/pkgs/png/pngwutil.c
// Function: png_write_pCAL
void /* PRIVATE */
png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
   png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
{
   PNG_pCAL;
   png_size_t purpose_len, units_len, total_len;
   png_uint_32p params_len;
   png_byte buf[10];
   png_charp new_purpose;
   int i;

   png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);

   if (type >= PNG_EQUATION_LAST)
      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");

   purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
   png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
   units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
   png_debug1(3, "pCAL units length = %d", (int)units_len);
   total_len = purpose_len + units_len + 10;

   params_len = (png_uint_32p)png_malloc(png_ptr,
      (png_alloc_size_t)(nparams * png_sizeof(png_uint_32)));

   /* Find the length of each parameter, making sure we don't count the
      null terminator for the last parameter. */
   for (i = 0; i < nparams; i++)
   {
      params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
      png_debug2(3, "pCAL parameter %d length = %lu", i,
        (unsigned long) params_len[i]);
      total_len += (png_size_t)params_len[i];
   }

   png_debug1(3, "pCAL total length = %d", (int)total_len);
   png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
   png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
     (png_size_t)purpose_len);
   png_save_int_32(buf, X0);
   png_save_int_32(buf + 4, X1);
   buf[8] = (png_byte)type;
   buf[9] = (png_byte)nparams;
   png_write_chunk_data(png_ptr, buf, (png_size_t)10);
   png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);

   png_free(png_ptr, new_purpose);

   for (i = 0; i < nparams; i++)
   {
      png_write_chunk_data(png_ptr, (png_bytep)params[i],
         (png_size_t)params_len[i]);
   }

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

// Assembly:
subq $0x68, %rsp
movq 0x78(%rsp), %rax
movq 0x70(%rsp), %rax
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movl %edx, 0x54(%rsp)
movl %ecx, 0x50(%rsp)
movl %r8d, 0x4c(%rsp)
movl %r9d, 0x48(%rsp)
movl 0x11f86(%rip), %eax     # 0x13fac6
movl %eax, 0x43(%rsp)
movb 0x11f80(%rip), %al      # 0x13faca
movb %al, 0x47(%rsp)
cmpl $0x4, 0x4c(%rsp)
jl 0x12db66
movq 0x60(%rsp), %rdi
leaq 0x11fee(%rip), %rsi     # 0x13fb4f
callq 0x113940
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x12bf40
addq $0x1, %rax
movq %rax, 0x38(%rsp)
movq 0x70(%rsp), %rdi
callq 0xf200
movl 0x48(%rsp), %esi
movl $0x1, %ecx
xorl %edx, %edx
cmpl $0x0, %esi
cmovel %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rax
addq 0x30(%rsp), %rax
addq $0xa, %rax
movq %rax, 0x28(%rsp)
movq 0x60(%rsp), %rdi
movslq 0x48(%rsp), %rsi
shlq $0x2, %rsi
callq 0x1135b0
movq %rax, 0x20(%rsp)
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
cmpl 0x48(%rsp), %eax
jge 0x12dc49
movq 0x78(%rsp), %rax
movslq 0x4(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xf200
movl 0x4(%rsp), %esi
movl 0x48(%rsp), %edi
subl $0x1, %edi
movl $0x1, %ecx
xorl %edx, %edx
cmpl %edi, %esi
cmovel %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movl %eax, %edx
movq 0x20(%rsp), %rax
movslq 0x4(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movq 0x20(%rsp), %rax
movslq 0x4(%rsp), %rcx
movl (%rax,%rcx,4), %eax
addq 0x28(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x12dbdc
movq 0x60(%rsp), %rdi
leaq 0x43(%rsp), %rsi
movq 0x28(%rsp), %rax
movl %eax, %edx
callq 0x12b0a0
movq 0x60(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x12b140
leaq 0x16(%rsp), %rdi
movl 0x54(%rsp), %esi
callq 0x12af10
leaq 0x16(%rsp), %rdi
addq $0x4, %rdi
movl 0x50(%rsp), %esi
callq 0x12af10
movl 0x4c(%rsp), %eax
movb %al, 0x1e(%rsp)
movl 0x48(%rsp), %eax
movb %al, 0x1f(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x16(%rsp), %rsi
movl $0xa, %edx
callq 0x12b140
movq 0x60(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x12b140
movq 0x60(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x1136d0
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
cmpl 0x48(%rsp), %eax
jge 0x12dd20
movq 0x60(%rsp), %rdi
movq 0x78(%rsp), %rax
movslq 0x4(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x20(%rsp), %rax
movslq 0x4(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, %edx
callq 0x12b140
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x12dce2
movq 0x60(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x1136d0
movq 0x60(%rsp), %rdi
callq 0x12b1a0
addq $0x68, %rsp
retq
nop