// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/png/pngwutil.c
// Function: png_write_IHDR
void /* PRIVATE */
png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
   int bit_depth, int color_type, int compression_type, int filter_type,
   int interlace_type)
{
   PNG_IHDR;
   int ret;

   png_byte buf[13]; /* Buffer to store the IHDR info */

   png_debug(1, "in png_write_IHDR");

   /* Check that we have valid input data from the application info */
   switch (color_type)
   {
      case PNG_COLOR_TYPE_GRAY:
         switch (bit_depth)
         {
            case 1:
            case 2:
            case 4:
            case 8:
            case 16: png_ptr->channels = 1; break;
            default: png_error(png_ptr,
                         "Invalid bit depth for grayscale image");
         }
         break;
      case PNG_COLOR_TYPE_RGB:
         if (bit_depth != 8 && bit_depth != 16)
            png_error(png_ptr, "Invalid bit depth for RGB image");
         png_ptr->channels = 3;
         break;
      case PNG_COLOR_TYPE_PALETTE:
         switch (bit_depth)
         {
            case 1:
            case 2:
            case 4:
            case 8: png_ptr->channels = 1; break;
            default: png_error(png_ptr, "Invalid bit depth for paletted image");
         }
         break;
      case PNG_COLOR_TYPE_GRAY_ALPHA:
         if (bit_depth != 8 && bit_depth != 16)
            png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
         png_ptr->channels = 2;
         break;
      case PNG_COLOR_TYPE_RGB_ALPHA:
         if (bit_depth != 8 && bit_depth != 16)
            png_error(png_ptr, "Invalid bit depth for RGBA image");
         png_ptr->channels = 4;
         break;
      default:
         png_error(png_ptr, "Invalid image color type specified");
   }

   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
   {
      png_warning(png_ptr, "Invalid compression type specified");
      compression_type = PNG_COMPRESSION_TYPE_BASE;
   }

   /* Write filter_method 64 (intrapixel differencing) only if
    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
    * 2. Libpng did not write a PNG signature (this filter_method is only
    *    used in PNG datastreams that are embedded in MNG datastreams) and
    * 3. The application called png_permit_mng_features with a mask that
    *    included PNG_FLAG_MNG_FILTER_64 and
    * 4. The filter_method is 64 and
    * 5. The color_type is RGB or RGBA
    */
   if (
#ifdef PNG_MNG_FEATURES_SUPPORTED
      !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
      ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
      (color_type == PNG_COLOR_TYPE_RGB ||
       color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
      (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
#endif
      filter_type != PNG_FILTER_TYPE_BASE)
   {
      png_warning(png_ptr, "Invalid filter type specified");
      filter_type = PNG_FILTER_TYPE_BASE;
   }

#ifdef PNG_WRITE_INTERLACING_SUPPORTED
   if (interlace_type != PNG_INTERLACE_NONE &&
      interlace_type != PNG_INTERLACE_ADAM7)
   {
      png_warning(png_ptr, "Invalid interlace type specified");
      interlace_type = PNG_INTERLACE_ADAM7;
   }
#else
   interlace_type=PNG_INTERLACE_NONE;
#endif

   /* Save the relevent information */
   png_ptr->bit_depth = (png_byte)bit_depth;
   png_ptr->color_type = (png_byte)color_type;
   png_ptr->interlaced = (png_byte)interlace_type;
#ifdef PNG_MNG_FEATURES_SUPPORTED
   png_ptr->filter_type = (png_byte)filter_type;
#endif
   png_ptr->compression_type = (png_byte)compression_type;
   png_ptr->width = width;
   png_ptr->height = height;

   png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
   /* Set the usr info, so any transformations can modify it */
   png_ptr->usr_width = png_ptr->width;
   png_ptr->usr_bit_depth = png_ptr->bit_depth;
   png_ptr->usr_channels = png_ptr->channels;

   /* Pack the header information into the buffer */
   png_save_uint_32(buf, width);
   png_save_uint_32(buf + 4, height);
   buf[8] = (png_byte)bit_depth;
   buf[9] = (png_byte)color_type;
   buf[10] = (png_byte)compression_type;
   buf[11] = (png_byte)filter_type;
   buf[12] = (png_byte)interlace_type;

   /* Write the chunk */
   png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);

   /* Initialize zlib with PNG info */
   png_ptr->zstream.zalloc = png_zalloc;
   png_ptr->zstream.zfree = png_zfree;
   png_ptr->zstream.opaque = (voidpf)png_ptr;
   if (!(png_ptr->do_filter))
   {
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
         png_ptr->bit_depth < 8)
         png_ptr->do_filter = PNG_FILTER_NONE;
      else
         png_ptr->do_filter = PNG_ALL_FILTERS;
   }
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
   {
      if (png_ptr->do_filter != PNG_FILTER_NONE)
         png_ptr->zlib_strategy = Z_FILTERED;
      else
         png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
   }
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
      png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
      png_ptr->zlib_mem_level = 8;
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
      png_ptr->zlib_window_bits = 15;
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
      png_ptr->zlib_method = 8;
   ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
         png_ptr->zlib_method, png_ptr->zlib_window_bits,
         png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
   if (ret != Z_OK)
   {
      if (ret == Z_VERSION_ERROR) png_error(png_ptr,
          "zlib failed to initialize compressor -- version error");
      if (ret == Z_STREAM_ERROR) png_error(png_ptr,
           "zlib failed to initialize compressor -- stream error");
      if (ret == Z_MEM_ERROR) png_error(png_ptr,
           "zlib failed to initialize compressor -- mem error");
      png_error(png_ptr, "zlib failed to initialize compressor");
   }
   png_ptr->zstream.next_out = png_ptr->zbuf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
   /* libpng is not interested in zstream.data_type */
   /* Set it to a predefined value, to avoid its evaluation inside zlib */
   png_ptr->zstream.data_type = Z_BINARY;

   png_ptr->mode = PNG_HAVE_IHDR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, %r15d
movl %r8d, %ebp
movl %ecx, %r14d
movl %edx, (%rsp)
movl %esi, %r13d
movq %rdi, %rbx
movb $0x0, 0x8(%rsp)
movl $0x52444849, 0x4(%rsp)  # imm = 0x52444849
cmpl $0x6, %r8d
ja 0x9da5b
movl %ebp, %eax
leaq 0x16078(%rip), %rcx     # 0xb3ab0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x10, %r14d
ja 0x9da52
movl $0x10116, %eax          # imm = 0x10116
btl %r14d, %eax
jb 0x9da88
leaq 0x16113(%rip), %rsi     # 0xb3b6c
jmp 0x9da62
leaq 0x161c2(%rip), %rsi     # 0xb3c24
movq %rbx, %rdi
callq 0x920b4
jmp 0x9dafb
cmpl $0x8, %r14d
ja 0x9dddf
movl $0x116, %eax            # imm = 0x116
btl %r14d, %eax
jae 0x9dddf
movb $0x1, 0x252(%rbx)
jmp 0x9dafb
cmpl $0x8, %r14d
je 0x9daac
cmpl $0x10, %r14d
je 0x9daac
leaq 0x160ee(%rip), %rsi     # 0xb3b92
movq %rbx, %rdi
callq 0x920b4
movb $0x3, 0x252(%rbx)
jmp 0x9dafb
cmpl $0x8, %r14d
je 0x9dad0
cmpl $0x10, %r14d
je 0x9dad0
leaq 0x1610f(%rip), %rsi     # 0xb3bd7
movq %rbx, %rdi
callq 0x920b4
movb $0x2, 0x252(%rbx)
jmp 0x9dafb
cmpl $0x8, %r14d
je 0x9daf4
cmpl $0x10, %r14d
je 0x9daf4
leaq 0x16117(%rip), %rsi     # 0xb3c03
movq %rbx, %rdi
callq 0x920b4
movb $0x4, 0x252(%rbx)
testl %r15d, %r15d
je 0x9db0f
leaq 0x16140(%rip), %rsi     # 0xb3c47
movq %rbx, %rdi
callq 0x9214d
movl 0x58(%rsp), %r15d
movl 0x50(%rsp), %r12d
testb $0x4, 0x408(%rbx)
je 0x9db49
testb $0x10, 0x11d(%rbx)
jne 0x9db49
movl %ebp, %eax
andl $-0x5, %eax
xorl $0x2, %eax
movl %r12d, %ecx
xorl $0x40, %ecx
orl %eax, %ecx
setne %al
testl %r12d, %r12d
je 0x9db60
testb %al, %al
jne 0x9db4e
jmp 0x9db60
testl %r12d, %r12d
je 0x9db5d
leaq 0x16115(%rip), %rsi     # 0xb3c6a
movq %rbx, %rdi
callq 0x9214d
xorl %r12d, %r12d
cmpl $0x2, %r15d
jb 0x9db7b
leaq 0x1611b(%rip), %rsi     # 0xb3c88
movq %rbx, %rdi
callq 0x9214d
movl $0x1, %r15d
movb %r14b, 0x24f(%rbx)
movb %bpl, 0x24e(%rbx)
movb %r15b, 0x24b(%rbx)
movb %r12b, 0x410(%rbx)
movb $0x0, 0x450(%rbx)
movl %r13d, 0x1bc(%rbx)
movl (%rsp), %eax
movl %eax, 0x1c0(%rbx)
movb 0x252(%rbx), %sil
movl %esi, %eax
mulb %r14b
movb %al, 0x251(%rbx)
movl %r13d, %edx
cmpb $0x8, %al
jb 0x9dbd3
shrb $0x3, %al
movzbl %al, %eax
imulq %rdx, %rax
jmp 0x9dbe2
movzbl %al, %eax
imulq %rdx, %rax
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, 0x1d0(%rbx)
movl %r13d, %ecx
movl %r13d, 0x1c8(%rbx)
movb %r14b, 0x250(%rbx)
movb %sil, 0x253(%rbx)
movl %r13d, %eax
shrl $0x18, %eax
leaq 0xb(%rsp), %rdx
movb %al, (%rdx)
movl %r13d, %eax
shrl $0x10, %eax
movb %al, 0x1(%rdx)
movb %ch, 0xd(%rsp)
movb %cl, 0x3(%rdx)
movl (%rsp), %ecx
movl %ecx, %eax
shrl $0x18, %eax
movb %al, 0x4(%rdx)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x5(%rdx)
movb %ch, 0x11(%rsp)
movb %cl, 0x7(%rdx)
movb %r14b, 0x8(%rdx)
movb %bpl, 0x9(%rdx)
movb $0x0, 0xa(%rdx)
movb %r12b, 0xb(%rdx)
movb %r15b, 0xc(%rdx)
leaq 0x4(%rsp), %rsi
movl $0xd, %ecx
movq %rbx, %rdi
callq 0x9d8bd
leaq -0x16f0c(%rip), %rax    # 0x86d59
movq %rax, 0x168(%rbx)
leaq -0x16ec9(%rip), %rax    # 0x86daa
movq %rax, 0x170(%rbx)
movq %rbx, 0x178(%rbx)
movb 0x24d(%rbx), %dl
testb %dl, %dl
jne 0x9dcaa
cmpb $0x3, 0x24e(%rbx)
je 0x9dca1
cmpb $0x7, 0x24f(%rbx)
ja 0x9ddc0
movb $0x8, 0x24d(%rbx)
movb $0x8, %dl
movl 0x120(%rbx), %eax
testb $0x1, %al
jne 0x9dcc2
xorl %ecx, %ecx
cmpb $0x8, %dl
setne %cl
movl %ecx, 0x1b8(%rbx)
testb $0x2, %al
jne 0x9dcd0
movl $0xffffffff, 0x1a8(%rbx) # imm = 0xFFFFFFFF
testb $0x4, %al
jne 0x9dcde
movl $0x8, 0x1b4(%rbx)
testb $0x8, %al
jne 0x9dcec
movl $0xf, 0x1b0(%rbx)
leaq 0x128(%rbx), %rdi
testb $0x10, %al
jne 0x9dd08
movl $0x8, 0x1ac(%rbx)
movl $0x8, %edx
jmp 0x9dd0e
movl 0x1ac(%rbx), %edx
movl 0x1a8(%rbx), %esi
movl 0x1b0(%rbx), %ecx
movl 0x1b4(%rbx), %r8d
movl 0x1b8(%rbx), %r9d
leaq 0xeba0(%rip), %rax      # 0xac8cf
pushq $0x70
pushq %rax
callq 0x927f3
addq $0x10, %rsp
addl $0x6, %eax
cmpl $0x6, %eax
ja 0x9dd74
leaq 0x15d82(%rip), %rcx     # 0xb3acc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x15f4f(%rip), %rsi     # 0xb3ca9
jmp 0x9dd6c
leaq 0x15fb1(%rip), %rsi     # 0xb3d14
jmp 0x9dd6c
leaq 0x15f73(%rip), %rsi     # 0xb3cdf
movq %rbx, %rdi
callq 0x920b4
leaq 0x15fcb(%rip), %rsi     # 0xb3d46
movq %rbx, %rdi
callq 0x920b4
movq 0x198(%rbx), %rax
movq %rax, 0x140(%rbx)
movl 0x1a0(%rbx), %eax
movl %eax, 0x148(%rbx)
movl $0x0, 0x180(%rbx)
movl $0x1, 0x11c(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $-0x8, 0x24d(%rbx)
movl 0x120(%rbx), %eax
movl $0x1, %ecx
testb $0x1, %al
je 0x9dcbc
jmp 0x9dcc2
leaq 0x15dcc(%rip), %rsi     # 0xb3bb2
jmp 0x9da62