// File: /workspace/github_cpp_makefile_star2_O2/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 0x8f36e
movl %ebp, %eax
leaq 0x15415(%rip), %rcx     # 0xa4760
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x10, %r14d
ja 0x8f365
movl $0x10116, %eax          # imm = 0x10116
btl %r14d, %eax
jb 0x8f39b
leaq 0x154b0(%rip), %rsi     # 0xa481c
jmp 0x8f375
leaq 0x1555f(%rip), %rsi     # 0xa48d4
movq %rbx, %rdi
callq 0x849c0
jmp 0x8f40e
cmpl $0x8, %r14d
ja 0x8f6c7
movl $0x116, %eax            # imm = 0x116
btl %r14d, %eax
jae 0x8f6c7
movb $0x1, 0x252(%rbx)
jmp 0x8f40e
cmpl $0x8, %r14d
je 0x8f3bf
cmpl $0x10, %r14d
je 0x8f3bf
leaq 0x1548b(%rip), %rsi     # 0xa4842
movq %rbx, %rdi
callq 0x849c0
movb $0x3, 0x252(%rbx)
jmp 0x8f40e
cmpl $0x8, %r14d
je 0x8f3e3
cmpl $0x10, %r14d
je 0x8f3e3
leaq 0x154ac(%rip), %rsi     # 0xa4887
movq %rbx, %rdi
callq 0x849c0
movb $0x2, 0x252(%rbx)
jmp 0x8f40e
cmpl $0x8, %r14d
je 0x8f407
cmpl $0x10, %r14d
je 0x8f407
leaq 0x154b4(%rip), %rsi     # 0xa48b3
movq %rbx, %rdi
callq 0x849c0
movb $0x4, 0x252(%rbx)
testl %r15d, %r15d
je 0x8f422
leaq 0x154dd(%rip), %rsi     # 0xa48f7
movq %rbx, %rdi
callq 0x84a53
movl 0x58(%rsp), %r15d
movl 0x50(%rsp), %r12d
testb $0x4, 0x408(%rbx)
je 0x8f45c
testb $0x10, 0x11d(%rbx)
jne 0x8f45c
movl %ebp, %eax
andl $-0x5, %eax
xorl $0x2, %eax
movl %r12d, %ecx
xorl $0x40, %ecx
orl %eax, %ecx
setne %al
testl %r12d, %r12d
je 0x8f473
testb %al, %al
jne 0x8f461
jmp 0x8f473
testl %r12d, %r12d
je 0x8f470
leaq 0x154b2(%rip), %rsi     # 0xa491a
movq %rbx, %rdi
callq 0x84a53
xorl %r12d, %r12d
cmpl $0x2, %r15d
jb 0x8f48c
leaq 0x154b8(%rip), %rsi     # 0xa4938
movq %rbx, %rdi
callq 0x84a53
pushq $0x1
popq %r15
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 0x8f4e4
shrb $0x3, %al
movzbl %al, %eax
imulq %rdx, %rax
jmp 0x8f4f3
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
pushq $0xd
popq %rcx
movq %rbx, %rdi
callq 0x8f1fc
leaq -0x153b7(%rip), %rax    # 0x7a1bd
movq %rax, 0x168(%rbx)
leaq -0x15374(%rip), %rax    # 0x7a20e
movq %rax, 0x170(%rbx)
movq %rbx, 0x178(%rbx)
movb 0x24d(%rbx), %cl
testb %cl, %cl
jne 0x8f5bb
movb $0x8, %cl
cmpb $0x3, 0x24e(%rbx)
je 0x8f5b5
cmpb $0x8, 0x24f(%rbx)
setb %cl
shlb $0x4, %cl
addb $-0x8, %cl
movb %cl, 0x24d(%rbx)
movl 0x120(%rbx), %eax
testb $0x1, %al
jne 0x8f5d3
xorl %edx, %edx
cmpb $0x8, %cl
setne %dl
movl %edx, 0x1b8(%rbx)
testb $0x2, %al
jne 0x8f5de
orl $-0x1, 0x1a8(%rbx)
testb $0x4, %al
jne 0x8f5ec
movl $0x8, 0x1b4(%rbx)
testb $0x8, %al
jne 0x8f5fa
movl $0xf, 0x1b0(%rbx)
leaq 0x128(%rbx), %rdi
testb $0x10, %al
jne 0x8f614
movl $0x8, 0x1ac(%rbx)
pushq $0x8
popq %rdx
jmp 0x8f61a
movl 0x1ac(%rbx), %edx
movl 0x1a8(%rbx), %esi
movl 0x1b0(%rbx), %ecx
movl 0x1b4(%rbx), %r8d
movl 0x1b8(%rbx), %r9d
leaq 0xdf74(%rip), %rax      # 0x9d5af
pushq $0x70
pushq %rax
callq 0x85026
popq %rcx
popq %rdx
addl $0x6, %eax
cmpl $0x6, %eax
ja 0x8f67e
leaq 0x15128(%rip), %rcx     # 0xa477c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x152f5(%rip), %rsi     # 0xa4959
jmp 0x8f676
leaq 0x15357(%rip), %rsi     # 0xa49c4
jmp 0x8f676
leaq 0x15319(%rip), %rsi     # 0xa498f
movq %rbx, %rdi
callq 0x849c0
leaq 0x15371(%rip), %rsi     # 0xa49f6
movq %rbx, %rdi
callq 0x849c0
movq 0x198(%rbx), %rax
movq %rax, 0x140(%rbx)
movl 0x1a0(%rbx), %eax
movl %eax, 0x148(%rbx)
andl $0x0, 0x180(%rbx)
movl $0x1, 0x11c(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15194(%rip), %rsi     # 0xa4862
jmp 0x8f375