// File: tomfunkhouser[P]gaps/pkgs/png/pngwutil.c
// Function: png_write_IDAT
void /* PRIVATE */
png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
{
   PNG_IDAT;

   png_debug(1, "in png_write_IDAT");

   /* Optimize the CMF field in the zlib stream. */
   /* This hack of the zlib stream is compliant to the stream specification. */
   if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
       png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
   {
      unsigned int z_cmf = data[0];  /* zlib compression method and flags */
      if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
      {
         /* Avoid memory underflows and multiplication overflows.
          *
          * The conditions below are practically always satisfied;
          * however, they still must be checked.
          */
         if (length >= 2 &&
             png_ptr->height < 16384 && png_ptr->width < 16384)
         {
            png_uint_32 uncompressed_idat_size = png_ptr->height *
               ((png_ptr->width *
               png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
            unsigned int z_cinfo = z_cmf >> 4;
            unsigned int half_z_window_size = 1 << (z_cinfo + 7);
            while (uncompressed_idat_size <= half_z_window_size &&
                   half_z_window_size >= 256)
            {
               z_cinfo--;
               half_z_window_size >>= 1;
            }
            z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
            if (data[0] != (png_byte)z_cmf)
            {
               data[0] = (png_byte)z_cmf;
               data[1] &= 0xe0;
               data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
            }
         }
      }
      else
         png_error(png_ptr,
            "Invalid zlib compression method or flags in IDAT");
   }

   png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
   png_ptr->mode |= PNG_HAVE_IDAT;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movl 0xd831(%rip), %eax      # 0x1391ea
movl %eax, 0x1b(%rsp)
movb 0xd82b(%rip), %al       # 0x1391ee
movb %al, 0x1f(%rsp)
movq 0x30(%rsp), %rax
movl 0x11c(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x12bb85
movq 0x30(%rsp), %rax
movzbl 0x450(%rax), %eax
cmpl $0x0, %eax
jne 0x12bb85
movq 0x28(%rsp), %rax
movzbl (%rax), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x12bb72
movl 0x14(%rsp), %eax
andl $0xf0, %eax
cmpl $0x70, %eax
ja 0x12bb72
cmpq $0x2, 0x20(%rsp)
jb 0x12bb70
movq 0x30(%rsp), %rax
cmpl $0x4000, 0x1c0(%rax)    # imm = 0x4000
jae 0x12bb70
movq 0x30(%rsp), %rax
cmpl $0x4000, 0x1bc(%rax)    # imm = 0x4000
jae 0x12bb70
movq 0x30(%rsp), %rax
movl 0x1c0(%rax), %eax
movq 0x30(%rsp), %rcx
movl 0x1bc(%rcx), %ecx
movq 0x30(%rsp), %rdx
movzbl 0x252(%rdx), %edx
imull %edx, %ecx
movq 0x30(%rsp), %rdx
movzbl 0x24f(%rdx), %edx
imull %edx, %ecx
addl $0xf, %ecx
shrl $0x3, %ecx
imull %ecx, %eax
movl %eax, 0x10(%rsp)
movl 0x14(%rsp), %eax
shrl $0x4, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %ecx
addl $0x7, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x8(%rsp)
movl 0x10(%rsp), %ecx
xorl %eax, %eax
cmpl 0x8(%rsp), %ecx
movb %al, 0x7(%rsp)
ja 0x12bad4
cmpl $0x100, 0x8(%rsp)       # imm = 0x100
setae %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x12bade
jmp 0x12baf5
movl 0xc(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xc(%rsp)
movl 0x8(%rsp), %eax
shrl %eax
movl %eax, 0x8(%rsp)
jmp 0x12bab5
movl 0x14(%rsp), %eax
andl $0xf, %eax
movl 0xc(%rsp), %ecx
shll $0x4, %ecx
orl %ecx, %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rax
movzbl (%rax), %eax
movl 0x14(%rsp), %ecx
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x12bb6e
movl 0x14(%rsp), %eax
movb %al, %cl
movq 0x28(%rsp), %rax
movb %cl, (%rax)
movq 0x28(%rsp), %rax
movzbl 0x1(%rax), %ecx
andl $0xe0, %ecx
movb %cl, 0x1(%rax)
movl 0x14(%rsp), %eax
shll $0x8, %eax
movq 0x28(%rsp), %rcx
movzbl 0x1(%rcx), %ecx
addl %ecx, %eax
movl $0x1f, %ecx
xorl %edx, %edx
divl %ecx
movl $0x1f, %eax
subl %edx, %eax
movzbl %al, %edx
movq 0x28(%rsp), %rax
movzbl 0x1(%rax), %ecx
addl %edx, %ecx
movb %cl, 0x1(%rax)
jmp 0x12bb70
jmp 0x12bb83
movq 0x30(%rsp), %rdi
leaq 0x14be5(%rip), %rsi     # 0x140763
callq 0x113860
jmp 0x12bb85
movq 0x30(%rsp), %rdi
leaq 0x1b(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rcx
callq 0x12b040
movq 0x30(%rsp), %rax
movl 0x11c(%rax), %ecx
orl $0x4, %ecx
movl %ecx, 0x11c(%rax)
addq $0x38, %rsp
retq
nopw (%rax,%rax)