// File: /workspace/github_c_makefile_star2_O3/nilesr[P]dotter/./stb/stb_image_write.h
// Function: stbi_write_png_to_mem
unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
{
   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 i,j,k,p,zlen;

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

   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) {
      static int mapping[] = { 0,1,2,3,4 };
      static int firstmap[] = { 0,1,0,5,6 };
      int *mymap = (j != 0) ? mapping : firstmap;
      int best = 0, bestval = 0x7fffffff;
      for (p=0; p < 2; ++p) {
         for (k= p?best:0; k < 5; ++k) { // @TODO: clarity: rewrite this to go 0..5, and 'continue' the unwanted ones during 2nd pass
            int type = mymap[k],est=0;
            unsigned char *z = pixels + stride_bytes*j;
            for (i=0; i < n; ++i)
               switch (type) {
                  case 0: line_buffer[i] = z[i]; break;
                  case 1: line_buffer[i] = z[i]; break;
                  case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break;
                  case 3: line_buffer[i] = z[i] - (z[i-stride_bytes]>>1); break;
                  case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-stride_bytes],0)); break;
                  case 5: line_buffer[i] = z[i]; break;
                  case 6: line_buffer[i] = z[i]; break;
               }
            for (i=n; i < x*n; ++i) {
               switch (type) {
                  case 0: line_buffer[i] = z[i]; break;
                  case 1: line_buffer[i] = z[i] - z[i-n]; break;
                  case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break;
                  case 3: line_buffer[i] = z[i] - ((z[i-n] + z[i-stride_bytes])>>1); break;
                  case 4: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-stride_bytes], z[i-stride_bytes-n]); break;
                  case 5: line_buffer[i] = z[i] - (z[i-n]>>1); break;
                  case 6: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break;
               }
            }
            if (p) break;
            for (i=0; i < x*n; ++i)
               est += abs((signed char) line_buffer[i]);
            if (est < bestval) { bestval = est; best = k; }
         }
      }
      // when we get here, best contains the filter type, and line_buffer contains the data
      filt[j*(x*n+1)] = (unsigned char) best;
      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, 8); // increase 8 to get smaller but use more memory
   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 $0xa8, %rsp
movq %r9, 0x58(%rsp)
movl %edx, %ebx
movl %esi, %r12d
movq %rdi, %r13
movl %r8d, 0x8(%rsp)
movl %r8d, %r14d
imull %edx, %r14d
testl %esi, %esi
cmovel %r14d, %r12d
leal 0x1(%r14), %ebp
movl %ebp, %r15d
movl %ecx, 0x4(%rsp)
imull %ecx, %r15d
movslq %r15d, %rdi
callq 0x1100
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x2bd9
movl %ebx, 0x2c(%rsp)
movq %r14, 0x18(%rsp)
movslq %r14d, %rdi
movq %rdi, 0x40(%rsp)
callq 0x1100
testq %rax, %rax
je 0x2bcf
movq %rax, %rbx
movl %r15d, 0x28(%rsp)
movslq 0x8(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x2a4d
movslq %r12d, %rcx
movslq %ebp, %rax
movq %rax, 0x68(%rsp)
movl 0x4(%rsp), %eax
movq %rax, 0x60(%rsp)
movq 0x38(%rsp), %rax
movl %eax, %r15d
movl 0x18(%rsp), %edx
movq %rdx, 0x78(%rsp)
movq %r13, %rbp
subq %rcx, %rbp
movq %rbx, %r14
addq %rax, %r14
leaq (%rax,%r13), %r8
movq %rax, %rdx
movq %rcx, 0x70(%rsp)
subq %rcx, %rdx
addq %r13, %rdx
movq 0x40(%rsp), %r12
subq %rax, %r12
leaq 0x68e3(%rip), %rsi      # 0x9050
leaq 0x68f8(%rip), %rdi      # 0x906c
xorl %eax, %eax
movq %r15, 0x90(%rsp)
movq %rax, 0x48(%rsp)
testq %rax, %rax
leaq 0x6b13(%rip), %rcx      # 0x92a0
leaq 0x6b2c(%rip), %rax      # 0x92c0
cmoveq %rax, %rcx
movq %rcx, 0x80(%rsp)
movb $0x1, %r9b
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
xorl %r11d, %r11d
movq %r8, 0x50(%rsp)
movq %rdx, 0x20(%rsp)
testb $0x1, %r9b
movl %r11d, %eax
movl $0x0, %ecx
cmovnel %ecx, %eax
cmpl $0x4, %eax
jg 0x29c5
movslq %eax, %rcx
movl %r11d, (%rsp)
movl %r9d, 0x34(%rsp)
movl %r11d, 0x30(%rsp)
movq %rcx, 0x88(%rsp)
movl %r10d, 0xc(%rsp)
movq 0x80(%rsp), %rax
movl (%rax,%rcx,4), %r9d
movl %r9d, %r10d
cmpl $0x0, 0x8(%rsp)
jle 0x2841
xorl %r11d, %r11d
cmpl $0x6, %r9d
ja 0x2839
movslq (%rsi,%r10,4), %rax
addq %rsi, %rax
jmpq *%rax
movb (%r13,%r11), %al
jmp 0x2835
movb (%r13,%r11), %al
subb (%rbp,%r11), %al
jmp 0x2835
movb (%r13,%r11), %al
movb (%rbp,%r11), %cl
shrb %cl
subb %cl, %al
movb %al, (%rbx,%r11)
incq %r11
cmpq %r11, %r15
jne 0x2805
movq 0x18(%rsp), %rax
cmpl 0x8(%rsp), %eax
jle 0x2940
xorl %r11d, %r11d
cmpl $0x6, %r9d
ja 0x2934
movslq (%rdi,%r10,4), %rax
addq %rdi, %rax
jmpq *%rax
movb (%r8,%r11), %sil
subb (%r13,%r11), %sil
jmp 0x2930
movb (%r8,%r11), %sil
movb (%r13,%r11), %al
shrb %al
subb %al, %sil
jmp 0x2930
movb (%r8,%r11), %sil
movzbl (%r13,%r11), %eax
movzbl (%rdx,%r11), %ecx
addl %eax, %ecx
shrl %ecx
subb %cl, %sil
jmp 0x2930
movq %r12, %r15
movq %r14, %r12
movzbl (%r13,%r11), %eax
movzbl (%rdx,%r11), %esi
movq %rbp, %r14
movzbl (%rbp,%r11), %ecx
leal (%rsi,%rax), %r8d
subl %ecx, %r8d
movl %r8d, %edx
subl %eax, %edx
movl %edx, %edi
negl %edi
cmovsl %edx, %edi
movl %r8d, %ebp
subl %esi, %ebp
movl %ebp, %edx
negl %edx
cmovsl %ebp, %edx
subl %ecx, %r8d
movl %r8d, %ebp
negl %ebp
cmovsl %r8d, %ebp
cmpl %ebp, %edx
ja 0x28ed
movl %esi, %ecx
movq 0x50(%rsp), %r8
movb (%r8,%r11), %sil
cmpl %ebp, %edi
cmoval %ecx, %eax
cmpl %edx, %edi
cmoval %ecx, %eax
subb %al, %sil
movq %r14, %rbp
movq %r12, %r14
movq 0x20(%rsp), %rdx
movq %r15, %r12
movq 0x90(%rsp), %r15
leaq 0x674c(%rip), %rdi      # 0x906c
jmp 0x2930
movb (%r8,%r11), %sil
jmp 0x2930
movb (%r8,%r11), %sil
subb (%rdx,%r11), %sil
movb %sil, (%r14,%r11)
incq %r11
cmpq %r11, %r12
jne 0x2853
movl 0x34(%rsp), %r9d
testb $0x1, %r9b
movq 0x78(%rsp), %rdi
je 0x29e1
cmpl $0x0, 0x18(%rsp)
jle 0x2982
xorl %ecx, %ecx
xorl %eax, %eax
movl 0xc(%rsp), %r10d
movb (%rbx,%rcx), %dl
movl %edx, %esi
sarb $0x7, %sil
xorb %sil, %dl
subb %sil, %dl
movzbl %dl, %edx
addl %edx, %eax
incq %rcx
cmpq %rcx, %rdi
jne 0x2964
jmp 0x2989
xorl %eax, %eax
movl 0xc(%rsp), %r10d
cmpl %r10d, %eax
movl (%rsp), %ecx
movq 0x88(%rsp), %rdx
cmovll %edx, %ecx
movl %ecx, (%rsp)
movq %rdx, %rcx
cmovll %eax, %r10d
incq %rcx
cmpl $0x5, %ecx
movq 0x20(%rsp), %rdx
leaq 0x669a(%rip), %rsi      # 0x9050
leaq 0x66af(%rip), %rdi      # 0x906c
jne 0x27df
jmp 0x29c9
movl %r11d, (%rsp)
movl (%rsp), %eax
movl %eax, %r11d
testb $0x1, %r9b
movl $0x0, %r9d
jne 0x27b6
jmp 0x29e5
movl 0x30(%rsp), %eax
movl %eax, %edx
movq 0x48(%rsp), %rax
imulq 0x68(%rsp), %rax
movq 0x10(%rsp), %rcx
movb %dl, (%rcx,%rax)
leaq (%rcx,%rax), %rdi
incq %rdi
movq %rbx, %rsi
movq 0x40(%rsp), %rdx
callq 0x10e0
leaq 0x6657(%rip), %rdi      # 0x906c
leaq 0x6634(%rip), %rsi      # 0x9050
movq 0x20(%rsp), %rdx
movq 0x50(%rsp), %r8
movq 0x48(%rsp), %rcx
incq %rcx
movq 0x70(%rsp), %rax
addq %rax, %rbp
addq %rax, %r13
addq %rax, %r8
addq %rax, %rdx
movq %rcx, %rax
cmpq 0x60(%rsp), %rcx
jne 0x277e
movq %rbx, %rdi
callq 0x1030
leaq 0x9c(%rsp), %rbx
movq 0x10(%rsp), %r15
movq %r15, %rdi
movl 0x28(%rsp), %esi
movq %rbx, %rdx
movl $0x8, %ecx
callq 0x1da4
movq %rax, %r14
movq %r15, %rdi
callq 0x1030
movslq (%rbx), %rax
movq %rax, %r13
leaq 0x39(%rax), %r15
movq %r15, %rdi
callq 0x1100
testq %rax, %rax
je 0x2bd9
movq %rax, %rbx
movq 0x58(%rsp), %r12
movl %r15d, (%r12)
movabsq $0xa1a0a0d474e5089, %rax # imm = 0xA1A0A0D474E5089
movq %rax, (%rbx)
movabsq $0x524448490d000000, %rax # imm = 0x524448490D000000
movq %rax, 0x8(%rbx)
movl 0x2c(%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 0x4(%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)
leaq 0x677e(%rip), %rax      # 0x9280
movq 0x38(%rsp), %rcx
movb (%rax,%rcx,4), %al
movb %al, 0x19(%rbx)
movw $0x0, 0x1a(%rbx)
leaq 0x1d(%rbx), %rax
leaq 0xa0(%rsp), %r15
movq %rax, (%r15)
movb $0x0, 0x1c(%rbx)
movq %r15, %rdi
movl $0xd, %esi
callq 0x2bf0
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 0x1150
leaq 0x8(,%r13), %rax
addq %rbp, %rax
movq %rax, (%r15)
movq %r14, %rdi
callq 0x1030
movq %r15, %rdi
movl %r13d, %esi
callq 0x2bf0
movq (%r15), %rax
movabsq $0x444e454900000000, %rcx # imm = 0x444E454900000000
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, (%r15)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2bf0
movslq (%r12), %rax
addq %rbx, %rax
cmpq %rax, (%r15)
je 0x2bdb
leaq 0x8526(%rip), %rdi      # 0xb0dd
leaq 0x840c(%rip), %rsi      # 0xafca
leaq 0x852c(%rip), %rcx      # 0xb0f1
movl $0x3e9, %edx            # imm = 0x3E9
callq 0x1080
movq 0x10(%rsp), %rdi
callq 0x1030
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq