// File: /workspace/github_c_makefile_star2_O2/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 $0xb8, %rsp
movq %r9, %r14
movl %r8d, %ebx
movl %esi, %ebp
movq %rdi, 0x58(%rsp)
movl %r8d, %r12d
movl %edx, 0x4c(%rsp)
imull %edx, %r12d
testl %esi, %esi
cmovel %r12d, %ebp
leal 0x1(%r12), %r15d
movl %r15d, %eax
movl %ecx, 0x18(%rsp)
imull %ecx, %eax
movl %eax, 0x50(%rsp)
movslq %eax, %rdi
callq 0x1100
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x2b81
movq %r14, 0x68(%rsp)
movslq %r12d, %rdi
movq %rdi, 0x30(%rsp)
callq 0x1100
testq %rax, %rax
je 0x2b77
movq %rax, %r14
movslq %ebp, %rcx
xorl %ebp, %ebp
testl %ebx, %ebx
movl $0x0, %r13d
cmovgl %ebx, %r13d
movslq %ebx, %r8
testl %r12d, %r12d
cmovlel %ebp, %r12d
movl 0x18(%rsp), %eax
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
movq %rdx, 0x80(%rsp)
movslq %r15d, %rax
movq %rax, 0x78(%rsp)
movq %rcx, 0x10(%rsp)
negq %rcx
movq %rcx, 0x70(%rsp)
leaq 0x5963(%rip), %r9       # 0x806c
leaq 0x5940(%rip), %r10      # 0x8050
movq 0x58(%rsp), %r11
movq %r8, 0x20(%rsp)
movq %r12, 0x90(%rsp)
movq %r13, 0x60(%rsp)
cmpq 0x80(%rsp), %rbp
je 0x29fc
testq %rbp, %rbp
leaq 0x5b51(%rip), %rcx      # 0x8290
leaq 0x5b6a(%rip), %rax      # 0x82b0
cmoveq %rax, %rcx
movq %rcx, 0x98(%rsp)
movq %rbp, 0x88(%rsp)
imulq 0x10(%rsp), %rbp
addq 0x58(%rsp), %rbp
movq 0x70(%rsp), %rax
addq %r11, %rax
movq %rax, 0x40(%rsp)
movl $0x7fffffff, 0x1c(%rsp) # imm = 0x7FFFFFFF
xorl %ebx, %ebx
movl $0x0, 0x4(%rsp)
movq 0x30(%rsp), %rdx
movq %r11, 0x38(%rsp)
cmpl $0x2, %ebx
je 0x29aa
testl %ebx, %ebx
movl 0x4(%rsp), %ecx
movl %ecx, %eax
cmovel %ebx, %eax
cltq
movl %ecx, 0xc(%rsp)
movq %rax, %rcx
movl 0x1c(%rsp), %eax
movl %eax, 0x8(%rsp)
movl %ebx, 0x54(%rsp)
cmpq $0x4, %rcx
jg 0x298c
movq 0x98(%rsp), %rax
movl (%rax,%rcx,4), %ebx
xorl %r15d, %r15d
movq %rcx, 0xa0(%rsp)
movl %ebx, %r12d
cmpq %r15, %r13
je 0x2853
cmpl $0x6, %ebx
ja 0x284e
movslq (%r10,%r12,4), %rax
addq %r10, %rax
jmpq *%rax
movb (%r11,%r15), %r12b
jmp 0x284a
movb (%r11,%r15), %r12b
movq 0x40(%rsp), %rax
movzbl (%rax,%r15), %esi
xorl %edi, %edi
xorl %edx, %edx
callq 0x2b98
movq 0x38(%rsp), %r11
leaq 0x583a(%rip), %r10      # 0x8050
leaq 0x584f(%rip), %r9       # 0x806c
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %r8
jmp 0x2847
movb (%r11,%r15), %r12b
movq 0x40(%rsp), %rax
subb (%rax,%r15), %r12b
jmp 0x284a
movb (%r11,%r15), %r12b
movq 0x40(%rsp), %rax
movb (%rax,%r15), %al
shrb %al
subb %al, %r12b
movb %r12b, (%r14,%r15)
incq %r15
jmp 0x27d7
movq %r8, %r13
cmpq %rdx, %r13
jge 0x292b
cmpl $0x6, %ebx
ja 0x2923
movslq (%r9,%r12,4), %rax
addq %r9, %rax
jmpq *%rax
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq %r8, %rax
jmp 0x291a
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq %r8, %rax
movb (%rbp,%rax), %al
shrb %al
jmp 0x2901
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq %r8, %rax
movzbl (%rbp,%rax), %eax
movq %r13, %rcx
subq 0x10(%rsp), %rcx
movzbl (%rbp,%rcx), %ecx
addl %eax, %ecx
shrl %ecx
subb %cl, %r15b
jmp 0x291f
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq %r8, %rax
movzbl (%rbp,%rax), %edi
movq %r13, %rax
subq 0x10(%rsp), %rax
movzbl (%rbp,%rax), %esi
subq %r8, %rax
movzbl (%rbp,%rax), %edx
callq 0x2b98
movq 0x38(%rsp), %r11
leaq 0x5760(%rip), %r10      # 0x8050
leaq 0x5775(%rip), %r9       # 0x806c
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %r8
subb %al, %r15b
jmp 0x291f
movb (%rbp,%r13), %r15b
jmp 0x291f
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq 0x10(%rsp), %rax
subb (%rbp,%rax), %r15b
movb %r15b, (%r14,%r13)
incq %r13
jmp 0x2856
movl 0x54(%rsp), %ebx
testl %ebx, %ebx
jne 0x299e
xorl %ecx, %ecx
xorl %eax, %eax
movq 0x90(%rsp), %r12
movq 0x60(%rsp), %r13
cmpq %rcx, %r12
je 0x2964
movb (%r14,%rcx), %dil
movl %edi, %esi
sarb $0x7, %sil
xorb %sil, %dil
subb %sil, %dil
movzbl %dil, %esi
addl %esi, %eax
incq %rcx
jmp 0x2944
movl 0x8(%rsp), %edi
cmpl %edi, %eax
movq 0xa0(%rsp), %rcx
movl 0xc(%rsp), %esi
cmovll %ecx, %esi
movl %esi, 0xc(%rsp)
cmovll %eax, %edi
movl %edi, 0x8(%rsp)
incq %rcx
jmp 0x27b7
movl 0xc(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x1c(%rsp)
jmp 0x29a3
movq 0x60(%rsp), %r13
incl %ebx
jmp 0x278e
movq 0x88(%rsp), %rbp
movq %rbp, %rax
imulq 0x78(%rsp), %rax
movq 0x28(%rsp), %rcx
movl 0x4(%rsp), %esi
movb %sil, (%rcx,%rax)
leaq (%rcx,%rax), %rdi
incq %rdi
movq %r14, %rsi
callq 0x10e0
movq 0x38(%rsp), %r11
leaq 0x566d(%rip), %r10      # 0x8050
leaq 0x5682(%rip), %r9       # 0x806c
movq 0x20(%rsp), %r8
incq %rbp
addq 0x10(%rsp), %r11
jmp 0x2727
movq %r14, %rdi
callq 0x1030
leaq 0xac(%rsp), %r12
pushq $0x8
popq %rcx
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movl 0x50(%rsp), %esi
movq %r12, %rdx
callq 0x1dd0
movq %rax, %r14
movq %rbx, %rdi
callq 0x1030
movslq (%r12), %rbx
leaq 0x39(%rbx), %r15
movq %r15, %rdi
callq 0x1100
testq %rax, %rax
je 0x2b81
movq %rax, %rbp
movq 0x68(%rsp), %r12
movl %r15d, (%r12)
movabsq $0xa1a0a0d474e5089, %rax # imm = 0xA1A0A0D474E5089
movq %rax, (%rbp)
movabsq $0x524448490d000000, %rax # imm = 0x524448490D000000
movq %rax, 0x8(%rbp)
movl 0x4c(%rsp), %ecx
movl %ecx, %eax
shrl $0x18, %eax
movb %al, 0x10(%rbp)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x11(%rbp)
movb %ch, 0x12(%rbp)
movb %cl, 0x13(%rbp)
movl 0x18(%rsp), %ecx
movl %ecx, %eax
shrl $0x18, %eax
movb %al, 0x14(%rbp)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x15(%rbp)
movb %ch, 0x16(%rbp)
movb %cl, 0x17(%rbp)
movb $0x8, 0x18(%rbp)
leaq 0x57c2(%rip), %rax      # 0x8270
movq 0x20(%rsp), %rcx
movb (%rax,%rcx,4), %al
movb %al, 0x19(%rbp)
andw $0x0, 0x1a(%rbp)
leaq 0x1d(%rbp), %rax
leaq 0xb0(%rsp), %r15
movq %rax, (%r15)
movb $0x0, 0x1c(%rbp)
pushq $0xd
popq %rsi
movq %r15, %rdi
callq 0x2bd7
movl %ebx, %eax
shrl $0x18, %eax
movq %rbx, %rdx
movq (%r15), %rbx
movb %al, (%rbx)
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0x1(%rbx)
movb %dh, 0x2(%rbx)
movb %dl, 0x3(%rbx)
movl $0x54414449, 0x4(%rbx)  # imm = 0x54414449
leaq 0x8(%rbx), %rdi
movq %r14, %rsi
movq %rdx, %r13
callq 0x1150
leaq (%rbx,%r13), %rax
addq $0x8, %rax
movq %rax, (%r15)
movq %r14, %rdi
callq 0x1030
movq %r15, %rdi
movl %r13d, %esi
callq 0x2bd7
movq (%r15), %rax
movabsq $0x444e454900000000, %rcx # imm = 0x444E454900000000
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, (%r15)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2bd7
movslq (%r12), %rax
addq %rbp, %rax
cmpq %rax, (%r15)
je 0x2b83
leaq 0x751e(%rip), %rdi      # 0xa07d
leaq 0x7404(%rip), %rsi      # 0x9f6a
leaq 0x7524(%rip), %rcx      # 0xa091
movl $0x3e9, %edx            # imm = 0x3E9
callq 0x1080
movq 0x28(%rsp), %rdi
callq 0x1030
xorl %ebp, %ebp
movq %rbp, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq