// File: /workspace/github_c_makefile_star2_O1/ggerganov[P]font-rasterizer/./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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %r14
movq %rdi, -0x68(%rbp)
movl 0xfa0c(%rip), %eax      # 0x12128
movl %r8d, -0x34(%rbp)
movl %r8d, %r13d
movl %edx, -0x30(%rbp)
imull %edx, %r13d
testl %esi, %esi
cmovel %r13d, %esi
movl %esi, -0x3c(%rbp)
cmpl $0x5, %eax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmovll %eax, %edx
movl %edx, -0x38(%rbp)
leal 0x1(%r13), %ebx
movl %ebx, %r15d
movl %ecx, -0x2c(%rbp)
imull %ecx, %r15d
movslq %r15d, %rdi
callq 0x1170
testq %rax, %rax
je 0x2a5b
movq %rax, %r12
movl %r15d, -0x4c(%rbp)
movslq %r13d, %rdi
movq %rdi, -0x88(%rbp)
callq 0x1170
testq %rax, %rax
je 0x2a53
movq %rax, %r15
movq %r13, -0x60(%rbp)
movq %r14, -0x58(%rbp)
cmpl $0x0, -0x2c(%rbp)
movl -0x38(%rbp), %r14d
jle 0x2897
movslq %ebx, %rax
movq %rax, -0x80(%rbp)
movl -0x2c(%rbp), %eax
movq %rax, -0x78(%rbp)
movl -0x60(%rbp), %ebx
movq $0x0, -0x48(%rbp)
movq %r12, -0x70(%rbp)
movl %r14d, %r13d
testl %r14d, %r14d
jns 0x283b
xorl %r13d, %r13d
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %r14d, %r14d
movq -0x68(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x30(%rbp), %edx
movl -0x2c(%rbp), %ecx
movq -0x48(%rbp), %r8
movl -0x34(%rbp), %r9d
pushq %r15
pushq %r14
callq 0x2a6c
addq $0x10, %rsp
movl $0x0, %eax
cmpl $0x0, -0x60(%rbp)
jle 0x2817
xorl %ecx, %ecx
xorl %eax, %eax
movb (%r15,%rcx), %dl
movl %edx, %esi
sarb $0x7, %sil
xorb %sil, %dl
subb %sil, %dl
movzbl %dl, %edx
addl %edx, %eax
incq %rcx
cmpq %rcx, %rbx
jne 0x27fa
cmpl %r12d, %eax
cmovll %r14d, %r13d
cmovll %eax, %r12d
incl %r14d
cmpl $0x5, %r14d
jne 0x27c9
movb $0x5, %al
cmpl $0x5, %r13d
movq -0x70(%rbp), %r12
movl -0x38(%rbp), %r14d
je 0x2860
movq -0x68(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x30(%rbp), %edx
movl -0x2c(%rbp), %ecx
movq -0x48(%rbp), %r8
movl -0x34(%rbp), %r9d
pushq %r15
pushq %r13
callq 0x2a6c
addq $0x10, %rsp
movl %r13d, %eax
movq -0x48(%rbp), %r13
movq %r13, %rcx
imulq -0x80(%rbp), %rcx
movb %al, (%r12,%rcx)
leaq (%r12,%rcx), %rdi
incq %rdi
movq %r15, %rsi
movq -0x88(%rbp), %rdx
callq 0x1150
incq %r13
movq %r13, -0x48(%rbp)
cmpq -0x78(%rbp), %r13
jne 0x27b5
movq %r15, %rdi
callq 0x1030
movl 0xf87b(%rip), %ecx      # 0x12120
leaq -0x50(%rbp), %rdx
movq %r12, %rdi
movl -0x4c(%rbp), %esi
callq 0x1dce
movq %rax, %r15
movq %r12, %rdi
callq 0x1030
testq %r15, %r15
je 0x2a5b
movslq -0x50(%rbp), %r12
leaq 0x39(%r12), %r14
movq %r14, %rdi
callq 0x1170
testq %rax, %rax
je 0x2a5b
movq -0x58(%rbp), %rcx
movl %r14d, (%rcx)
leaq -0x90(%rbp), %r14
movq %rax, (%r14)
movabsq $0xa1a0a0d474e5089, %rcx # imm = 0xA1A0A0D474E5089
movq %rcx, (%rax)
leaq 0x8(%rax), %rcx
movq %rcx, (%r14)
movl $0xd000000, 0x8(%rax)   # imm = 0xD000000
leaq 0xc(%rax), %rcx
movq %rcx, (%r14)
movl $0x52444849, 0xc(%rax)  # imm = 0x52444849
leaq 0x10(%rax), %rcx
movq %rcx, (%r14)
movq %rax, %rbx
movl -0x30(%rbp), %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)
leaq 0x14(%rbx), %rax
movq %rax, (%r14)
movl -0x2c(%rbp), %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)
leaq 0x19(%rbx), %rax
movq %rax, (%r14)
movb $0x8, 0x18(%rbx)
movslq -0x34(%rbp), %rax
leaq 0xa86b(%rip), %rcx      # 0xd1e0
movb (%rcx,%rax,4), %al
leaq 0x1a(%rbx), %rcx
movq %rcx, (%r14)
movb %al, 0x19(%rbx)
leaq 0x1b(%rbx), %rax
movq %rax, (%r14)
xorl %eax, %eax
movb %al, 0x1a(%rbx)
leaq 0x1c(%rbx), %rcx
movq %rcx, (%r14)
movb %al, 0x1b(%rbx)
leaq 0x1d(%rbx), %rcx
movq %rcx, (%r14)
movb %al, 0x1c(%rbx)
movq %r14, %rdi
movl $0xd, %esi
callq 0x2d22
movq (%r14), %r13
movl %r12d, %eax
bswapl %eax
movl %eax, (%r13)
leaq 0x4(%r13), %rax
movq %rax, (%r14)
movl $0x54414449, 0x4(%r13)  # imm = 0x54414449
leaq 0x8(%r13), %rdi
movq %rdi, (%r14)
movq %r15, %rsi
movq %r12, %rdx
callq 0x11b0
leaq (%r12,%r13), %rax
addq $0x8, %rax
movq %rax, (%r14)
movq %r15, %rdi
callq 0x1030
movq %r14, %rdi
movl %r12d, %esi
callq 0x2d22
movq (%r14), %rax
leaq 0x4(%rax), %rcx
movl $0x0, (%rax)
movq %rcx, (%r14)
movl $0x444e4549, 0x4(%rax)  # imm = 0x444E4549
addq $0x8, %rax
movq %rax, (%r14)
movq %r14, %rdi
xorl %esi, %esi
callq 0x2d22
movq %rbx, %rax
movq -0x58(%rbp), %rcx
movslq (%rcx), %rcx
addq %rbx, %rcx
cmpq %rcx, (%r14)
je 0x2a5d
leaq 0xc1bf(%rip), %rdi      # 0xebfa
leaq 0xc160(%rip), %rsi      # 0xeba2
leaq 0xc1c5(%rip), %rcx      # 0xec0e
movl $0x483, %edx            # imm = 0x483
callq 0x10c0
movq %r12, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq