// File: /workspace/github_cpp_makefile_star2_O2/alan0201tw[P]RayTracer/include/stb_image_write.h
// Function: stbi_write_png_to_mem
STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
{
   int force_filter = stbi_write_force_png_filter;
   int ctype[5] = { -1, 0, 4, 2, 6 };
   unsigned char sig[8] = { 137,80,78,71,13,10,26,10 };
   unsigned char *out,*o, *filt, *zlib;
   signed char *line_buffer;
   int j,zlen;

   if (stride_bytes == 0)
      stride_bytes = x * n;

   if (force_filter >= 5) {
      force_filter = -1;
   }

   filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0;
   line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; }
   for (j=0; j < y; ++j) {
      int filter_type;
      if (force_filter > -1) {
         filter_type = force_filter;
         stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, force_filter, line_buffer);
      } else { // Estimate the best filter by running through all of them:
         int best_filter = 0, best_filter_val = 0x7fffffff, est, i;
         for (filter_type = 0; filter_type < 5; filter_type++) {
            stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, filter_type, line_buffer);

            // Estimate the entropy of the line using this filter; the less, the better.
            est = 0;
            for (i = 0; i < x*n; ++i) {
               est += abs((signed char) line_buffer[i]);
            }
            if (est < best_filter_val) {
               best_filter_val = est;
               best_filter = filter_type;
            }
         }
         if (filter_type != best_filter) {  // If the last iteration already got us the best filter, don't redo it
            stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, best_filter, line_buffer);
            filter_type = best_filter;
         }
      }
      // when we get here, filter_type contains the filter type, and line_buffer contains the data
      filt[j*(x*n+1)] = (unsigned char) filter_type;
      STBIW_MEMMOVE(filt+j*(x*n+1)+1, line_buffer, x*n);
   }
   STBIW_FREE(line_buffer);
   zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, stbi_write_png_compression_level);
   STBIW_FREE(filt);
   if (!zlib) return 0;

   // each tag requires 12 bytes of overhead
   out = (unsigned char *) STBIW_MALLOC(8 + 12+13 + 12+zlen + 12);
   if (!out) return 0;
   *out_len = 8 + 12+13 + 12+zlen + 12;

   o=out;
   STBIW_MEMMOVE(o,sig,8); o+= 8;
   stbiw__wp32(o, 13); // header length
   stbiw__wptag(o, "IHDR");
   stbiw__wp32(o, x);
   stbiw__wp32(o, y);
   *o++ = 8;
   *o++ = STBIW_UCHAR(ctype[n]);
   *o++ = 0;
   *o++ = 0;
   *o++ = 0;
   stbiw__wpcrc(&o,13);

   stbiw__wp32(o, zlen);
   stbiw__wptag(o, "IDAT");
   STBIW_MEMMOVE(o, zlib, zlen);
   o += zlen;
   STBIW_FREE(zlib);
   stbiw__wpcrc(&o, zlen);

   stbiw__wp32(o,0);
   stbiw__wptag(o, "IEND");
   stbiw__wpcrc(&o,0);

   STBIW_ASSERT(o == out + *out_len);

   return out;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %r15
movq %rdi, 0x18(%rsp)
movl 0x25a5c(%rip), %eax     # 0x48438
movl %r8d, 0x10(%rsp)
movl %r8d, %ebp
movl %edx, 0xc(%rsp)
imull %edx, %ebp
testl %esi, %esi
cmovel %ebp, %esi
movl %esi, 0x14(%rsp)
cmpl $0x5, %eax
pushq $-0x1
popq %rdx
movl %eax, 0x28(%rsp)
cmovll %eax, %edx
movq %rdx, 0x30(%rsp)
leal 0x1(%rbp), %ebx
movl %ebx, %r13d
movl %ecx, 0x8(%rsp)
imull %ecx, %r13d
movslq %r13d, %rdi
callq 0x6510
testq %rax, %rax
je 0x22d08
movq %rax, %r12
movslq %ebp, %rdi
movq %rdi, 0x50(%rsp)
callq 0x6510
testq %rax, %rax
je 0x22d00
movq %rax, %r14
movl %r13d, 0x24(%rsp)
movq %r15, 0x38(%rsp)
xorl %r13d, %r13d
testl %ebp, %ebp
cmovlel %r13d, %ebp
movslq %ebx, %rax
movq %rax, 0x48(%rsp)
movl 0x8(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, 0x58(%rsp)
movq 0x30(%rsp), %r15
movq %r12, 0x40(%rsp)
cmpq 0x58(%rsp), %r13
je 0x22b80
testl %r15d, %r15d
js 0x22ab6
movq 0x18(%rsp), %rdi
movl 0x14(%rsp), %esi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movl %r13d, %r8d
movl 0x10(%rsp), %r9d
pushq %r14
pushq %r15
callq 0x22d19
popq %rax
popq %rcx
movl 0x28(%rsp), %eax
jmp 0x22b57
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpl $0x5, %r15d
je 0x22b1e
movq 0x18(%rsp), %rdi
movl 0x14(%rsp), %esi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movl %r13d, %r8d
movl 0x10(%rsp), %r9d
pushq %r14
pushq %r15
callq 0x22d19
popq %rax
popq %rcx
xorl %ecx, %ecx
xorl %eax, %eax
cmpq %rcx, %rbp
je 0x22b0e
movb (%r14,%rcx), %dl
movl %edx, %esi
sarb $0x7, %sil
xorb %sil, %dl
subb %sil, %dl
movzbl %dl, %edx
addl %edx, %eax
incq %rcx
jmp 0x22aef
cmpl %r12d, %eax
cmovll %r15d, %ebx
cmovll %eax, %r12d
incl %r15d
jmp 0x22ac1
pushq $0x5
popq %rax
cmpl $0x5, %ebx
movq 0x40(%rsp), %r12
movq 0x30(%rsp), %r15
je 0x22b57
movq 0x18(%rsp), %rdi
movl 0x14(%rsp), %esi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movl %r13d, %r8d
movl 0x10(%rsp), %r9d
pushq %r14
pushq %rbx
callq 0x22d19
addq $0x10, %rsp
movl %ebx, %eax
movq %r13, %rcx
imulq 0x48(%rsp), %rcx
movb %al, (%r12,%rcx)
leaq (%r12,%rcx), %rdi
incq %rdi
movq %r14, %rsi
movq 0x50(%rsp), %rdx
callq 0x6360
incq %r13
jmp 0x22a79
movq %r14, %rdi
callq 0x64d0
movl 0x258a2(%rip), %ecx     # 0x48430
leaq 0x2c(%rsp), %rdx
movq %r12, %rdi
movl 0x24(%rsp), %esi
callq 0x2212f
movq %rax, %r14
movq %r12, %rdi
callq 0x64d0
testq %r14, %r14
je 0x22d08
movslq 0x2c(%rsp), %rbx
leaq 0x39(%rbx), %r15
movq %r15, %rdi
callq 0x6510
testq %rax, %rax
je 0x22d08
movq 0x38(%rsp), %r12
movl %r15d, (%r12)
movabsq $0xa1a0a0d474e5089, %rcx # imm = 0xA1A0A0D474E5089
movq %rcx, (%rax)
movabsq $0x524448490d000000, %rcx # imm = 0x524448490D000000
movq %rcx, 0x8(%rax)
movq %rbx, %r13
movq %rax, %rbx
movl 0xc(%rsp), %ecx
movl %ecx, %eax
shrl $0x18, %eax
movb %al, 0x10(%rbx)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x11(%rbx)
movb %ch, 0x12(%rbx)
movb %cl, 0x13(%rbx)
movl 0x8(%rsp), %ecx
movl %ecx, %eax
shrl $0x18, %eax
movb %al, 0x14(%rbx)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x15(%rbx)
movb %ch, 0x16(%rbx)
movb %cl, 0x17(%rbx)
movb $0x8, 0x18(%rbx)
movslq 0x10(%rsp), %rax
leaq 0xeb75(%rip), %rcx      # 0x317b0
movb (%rcx,%rax,4), %al
movb %al, 0x19(%rbx)
andw $0x0, 0x1a(%rbx)
leaq 0x1d(%rbx), %rax
leaq 0x60(%rsp), %r15
movq %rax, (%r15)
movb $0x0, 0x1c(%rbx)
pushq $0xd
popq %rsi
movq %r15, %rdi
callq 0x22fe2
movq %r13, %rdx
movl %edx, %eax
shrl $0x18, %eax
movq (%r15), %rbp
movb %al, (%rbp)
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0x1(%rbp)
movb %dh, 0x2(%rbp)
movb %dl, 0x3(%rbp)
movl $0x54414449, 0x4(%rbp)  # imm = 0x54414449
leaq 0x8(%rbp), %rdi
movq %r14, %rsi
callq 0x6670
leaq 0x8(,%r13), %rax
addq %rbp, %rax
movq %rax, (%r15)
movq %r14, %rdi
callq 0x64d0
movq %r15, %rdi
movl %r13d, %esi
callq 0x22fe2
movq (%r15), %rax
movabsq $0x444e454900000000, %rcx # imm = 0x444E454900000000
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, (%r15)
movq %r15, %rdi
xorl %esi, %esi
callq 0x22fe2
movq %rbx, %rax
movslq (%r12), %rcx
addq %rbx, %rcx
cmpq %rcx, (%r15)
je 0x22d0a
leaq 0x10987(%rip), %rdi     # 0x3366f
leaq 0x10922(%rip), %rsi     # 0x33611
leaq 0x1098d(%rip), %rcx     # 0x33683
movl $0x483, %edx            # imm = 0x483
callq 0x6280
movq %r12, %rdi
callq 0x64d0
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq