// File: /workspace/github_cpp_makefile_star2_O1/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 0x9e44f
movl %ebp, %eax
leaq 0x15634(%rip), %rcx     # 0xb3a60
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x10, %r14d
ja 0x9e446
movl $0x10116, %eax          # imm = 0x10116
btl %r14d, %eax
jb 0x9e47c
leaq 0x156cf(%rip), %rsi     # 0xb3b1c
jmp 0x9e456
leaq 0x1577e(%rip), %rsi     # 0xb3bd4
movq %rbx, %rdi
callq 0x92dd0
jmp 0x9e4ef
cmpl $0x8, %r14d
ja 0x9e7b1
movl $0x116, %eax            # imm = 0x116
btl %r14d, %eax
jae 0x9e7b1
movb $0x1, 0x252(%rbx)
jmp 0x9e4ef
cmpl $0x8, %r14d
je 0x9e4a0
cmpl $0x10, %r14d
je 0x9e4a0
leaq 0x156aa(%rip), %rsi     # 0xb3b42
movq %rbx, %rdi
callq 0x92dd0
movb $0x3, 0x252(%rbx)
jmp 0x9e4ef
cmpl $0x8, %r14d
je 0x9e4c4
cmpl $0x10, %r14d
je 0x9e4c4
leaq 0x156cb(%rip), %rsi     # 0xb3b87
movq %rbx, %rdi
callq 0x92dd0
movb $0x2, 0x252(%rbx)
jmp 0x9e4ef
cmpl $0x8, %r14d
je 0x9e4e8
cmpl $0x10, %r14d
je 0x9e4e8
leaq 0x156d3(%rip), %rsi     # 0xb3bb3
movq %rbx, %rdi
callq 0x92dd0
movb $0x4, 0x252(%rbx)
testl %r15d, %r15d
je 0x9e503
leaq 0x156fc(%rip), %rsi     # 0xb3bf7
movq %rbx, %rdi
callq 0x92e6e
movl 0x58(%rsp), %r15d
movl 0x50(%rsp), %r12d
testb $0x4, 0x408(%rbx)
je 0x9e53d
testb $0x10, 0x11d(%rbx)
jne 0x9e53d
movl %ebp, %eax
andl $-0x5, %eax
xorl $0x2, %eax
movl %r12d, %ecx
xorl $0x40, %ecx
orl %eax, %ecx
setne %al
testl %r12d, %r12d
je 0x9e554
testb %al, %al
jne 0x9e542
jmp 0x9e554
testl %r12d, %r12d
je 0x9e551
leaq 0x156d1(%rip), %rsi     # 0xb3c1a
movq %rbx, %rdi
callq 0x92e6e
xorl %r12d, %r12d
cmpl $0x2, %r15d
jb 0x9e56f
leaq 0x156d7(%rip), %rsi     # 0xb3c38
movq %rbx, %rdi
callq 0x92e6e
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 0x9e5c7
shrb $0x3, %al
movzbl %al, %eax
imulq %rdx, %rax
jmp 0x9e5d6
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 0x9e2b1
leaq -0x16b30(%rip), %rax    # 0x87b29
movq %rax, 0x168(%rbx)
leaq -0x16ae8(%rip), %rax    # 0x87b7f
movq %rax, 0x170(%rbx)
movq %rbx, 0x178(%rbx)
cmpb $0x0, 0x24d(%rbx)
jne 0x9e69e
movb $0x8, %al
cmpb $0x3, 0x24e(%rbx)
je 0x9e698
cmpb $0x8, 0x24f(%rbx)
setb %al
shlb $0x4, %al
addb $-0x8, %al
movb %al, 0x24d(%rbx)
movl 0x120(%rbx), %eax
testb $0x1, %al
jne 0x9e6ba
xorl %ecx, %ecx
cmpb $0x8, 0x24d(%rbx)
setne %cl
movl %ecx, 0x1b8(%rbx)
testb $0x2, %al
jne 0x9e6c8
movl $0xffffffff, 0x1a8(%rbx) # imm = 0xFFFFFFFF
testb $0x4, %al
jne 0x9e6d6
movl $0x8, 0x1b4(%rbx)
testb $0x8, %al
jne 0x9e6e4
movl $0xf, 0x1b0(%rbx)
leaq 0x128(%rbx), %rdi
testb $0x10, %al
jne 0x9e6f9
movl $0x8, 0x1ac(%rbx)
movl 0x1a8(%rbx), %esi
movl 0x1ac(%rbx), %edx
movl 0x1b0(%rbx), %ecx
movl 0x1b4(%rbx), %r8d
movl 0x1b8(%rbx), %r9d
leaq 0xe16f(%rip), %rax      # 0xac88f
pushq $0x70
pushq %rax
callq 0x93517
addq $0x10, %rsp
addl $0x6, %eax
cmpl $0x6, %eax
ja 0x9e765
leaq 0x15341(%rip), %rcx     # 0xb3a7c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x1550e(%rip), %rsi     # 0xb3c59
jmp 0x9e75d
leaq 0x15570(%rip), %rsi     # 0xb3cc4
jmp 0x9e75d
leaq 0x15532(%rip), %rsi     # 0xb3c8f
movq %rbx, %rdi
callq 0x92dd0
leaq 0x1558a(%rip), %rsi     # 0xb3cf6
movq %rbx, %rdi
callq 0x92dd0
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
leaq 0x153aa(%rip), %rsi     # 0xb3b62
jmp 0x9e456