// File: /workspace/github_c_makefile_star2_O1/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 0x2cf7
movl %ebx, 0x34(%rsp)
movq %r14, 0x18(%rsp)
movslq %r14d, %rdi
movq %rdi, 0x40(%rsp)
callq 0x1100
testq %rax, %rax
je 0x2ced
movq %rax, %rbx
movl %r15d, 0x30(%rsp)
movslq 0x8(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x2b30
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), %rdi
movq %rax, %rdx
movq %rcx, 0x70(%rsp)
subq %rcx, %rdx
addq %r13, %rdx
movq 0x40(%rsp), %r12
subq %rax, %r12
xorl %eax, %eax
leaq 0x67f2(%rip), %rsi      # 0x9050
leaq 0x6807(%rip), %rcx      # 0x906c
movq %r15, 0x90(%rsp)
movq %rax, 0x48(%rsp)
testq %rax, %rax
leaq 0x6a24(%rip), %r8       # 0x92a0
leaq 0x6a3d(%rip), %rax      # 0x92c0
cmoveq %rax, %r8
movq %r8, 0x80(%rsp)
movl $0x7fffffff, 0xc(%rsp)  # imm = 0x7FFFFFFF
movl $0x0, (%rsp)
xorl %r8d, %r8d
movq %rdi, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
testl %r8d, %r8d
movl (%rsp), %eax
cmovel %r8d, %eax
cmpl $0x4, %eax
jg 0x2ab7
movslq %eax, %r9
movq %r8, 0x88(%rsp)
movq 0x80(%rsp), %rax
movq %r9, 0x50(%rsp)
movl (%rax,%r9,4), %r8d
movl %r8d, %r9d
cmpl $0x0, 0x8(%rsp)
jle 0x292a
xorl %r10d, %r10d
cmpl $0x6, %r8d
ja 0x2922
movslq (%rsi,%r9,4), %rax
addq %rsi, %rax
jmpq *%rax
movb (%r13,%r10), %al
jmp 0x291e
movb (%r13,%r10), %al
subb (%rbp,%r10), %al
jmp 0x291e
movb (%r13,%r10), %al
movb (%rbp,%r10), %cl
shrb %cl
subb %cl, %al
leaq 0x674e(%rip), %rcx      # 0x906c
movb %al, (%rbx,%r10)
incq %r10
cmpq %r10, %r15
jne 0x28e7
movq 0x18(%rsp), %rax
cmpl 0x8(%rsp), %eax
jle 0x2a29
xorl %r10d, %r10d
cmpl $0x6, %r8d
ja 0x2a1d
movslq (%rcx,%r9,4), %rax
addq %rcx, %rax
jmpq *%rax
movb (%rdi,%r10), %r11b
subb (%r13,%r10), %r11b
jmp 0x2a19
movb (%rdi,%r10), %r11b
movb (%r13,%r10), %al
shrb %al
subb %al, %r11b
jmp 0x2a19
movb (%rdi,%r10), %r11b
movzbl (%r13,%r10), %eax
movzbl (%rdx,%r10), %ecx
addl %eax, %ecx
shrl %ecx
subb %cl, %r11b
jmp 0x2a02
movq %r12, %r15
movq %r14, %r12
movzbl (%r13,%r10), %eax
movzbl (%rdx,%r10), %r11d
movq %rbp, %r14
movzbl (%rbp,%r10), %ecx
leal (%r11,%rax), %edi
subl %ecx, %edi
movl %edi, %esi
subl %eax, %esi
movl %esi, %edx
negl %edx
cmovsl %esi, %edx
movl %edi, %ebp
subl %r11d, %ebp
movl %ebp, %esi
negl %esi
cmovsl %ebp, %esi
subl %ecx, %edi
movl %edi, %ebp
negl %ebp
cmovsl %edi, %ebp
cmpl %ebp, %esi
ja 0x29cf
movl %r11d, %ecx
movq 0x28(%rsp), %rdi
movb (%rdi,%r10), %r11b
cmpl %ebp, %edx
cmoval %ecx, %eax
cmpl %esi, %edx
cmoval %ecx, %eax
subb %al, %r11b
movq %r14, %rbp
movq %r12, %r14
movq 0x20(%rsp), %rdx
movq %r15, %r12
movq 0x90(%rsp), %r15
leaq 0x664e(%rip), %rsi      # 0x9050
leaq 0x6663(%rip), %rcx      # 0x906c
jmp 0x2a19
movb (%rdi,%r10), %r11b
jmp 0x2a19
movb (%rdi,%r10), %r11b
subb (%rdx,%r10), %r11b
movb %r11b, (%r14,%r10)
incq %r10
cmpq %r10, %r12
jne 0x293c
movq 0x88(%rsp), %r8
testl %r8d, %r8d
je 0x2a3d
movq 0x50(%rsp), %r9
jmp 0x2aa1
cmpl $0x0, 0x18(%rsp)
movq 0x78(%rsp), %rdi
movq 0x50(%rsp), %r9
jle 0x2a70
xorl %ecx, %ecx
xorl %eax, %eax
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 0x2a52
jmp 0x2a72
xorl %eax, %eax
movl 0xc(%rsp), %edx
cmpl %edx, %eax
movl (%rsp), %ecx
cmovll %r9d, %ecx
movl %ecx, (%rsp)
cmovll %eax, %edx
movl %edx, 0xc(%rsp)
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rdx
leaq 0x65b6(%rip), %rsi      # 0x9050
leaq 0x65cb(%rip), %rcx      # 0x906c
testl %r8d, %r8d
jne 0x2ab7
leaq 0x1(%r9), %rax
cmpq $0x4, %r9
movq %rax, %r9
jl 0x28c9
leal 0x1(%r8), %eax
testl %r8d, %r8d
movl %eax, %r8d
je 0x28ab
movq 0x48(%rsp), %rax
imulq 0x68(%rsp), %rax
movq 0x10(%rsp), %rcx
movl (%rsp), %edx
movb %dl, (%rcx,%rax)
leaq (%rcx,%rax), %rdi
incq %rdi
movq %rbx, %rsi
movq 0x40(%rsp), %rdx
callq 0x10e0
leaq 0x6574(%rip), %rcx      # 0x906c
leaq 0x6551(%rip), %rsi      # 0x9050
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rdi
movq 0x48(%rsp), %r8
incq %r8
movq 0x70(%rsp), %rax
addq %rax, %rbp
addq %rax, %r13
addq %rax, %rdi
addq %rax, %rdx
movq %r8, %rax
cmpq 0x60(%rsp), %r8
jne 0x286d
movq %rbx, %rdi
callq 0x1030
leaq 0x9c(%rsp), %rbx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movl 0x30(%rsp), %esi
movq %rbx, %rdx
movl $0x8, %ecx
callq 0x1e36
movq %rax, %r12
movq %r14, %rdi
callq 0x1030
movslq (%rbx), %r15
leaq 0x39(%r15), %r14
movq %r14, %rdi
callq 0x1100
testq %rax, %rax
je 0x2cf7
movq %rax, %rbx
movq 0x58(%rsp), %rbp
movl %r14d, (%rbp)
leaq 0xa0(%rsp), %r14
movq %rax, (%r14)
movabsq $0xa1a0a0d474e5089, %rax # imm = 0xA1A0A0D474E5089
movq %rax, (%rbx)
leaq 0x8(%rbx), %rax
movq %rax, (%r14)
movl $0xd000000, 0x8(%rbx)   # imm = 0xD000000
leaq 0xc(%rbx), %rax
movq %rax, (%r14)
movl $0x52444849, 0xc(%rbx)  # imm = 0x52444849
leaq 0x10(%rbx), %rax
movq %rax, (%r14)
movl 0x34(%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)
leaq 0x14(%rbx), %rax
movq %rax, (%r14)
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)
leaq 0x19(%rbx), %rax
movq %rax, (%r14)
movb $0x8, 0x18(%rbx)
leaq 0x6670(%rip), %rax      # 0x9280
movq 0x38(%rsp), %rcx
movb (%rax,%rcx,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 0x2d0e
movq (%r14), %r13
movl %r15d, %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 %r12, %rsi
movq %r15, %rdx
callq 0x1150
leaq (%r15,%r13), %rax
addq $0x8, %rax
movq %rax, (%r14)
movq %r12, %rdi
callq 0x1030
movq %r14, %rdi
movl %r15d, %esi
callq 0x2d0e
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 0x2d0e
movslq (%rbp), %rax
addq %rbx, %rax
cmpq %rax, (%r14)
je 0x2cf9
leaq 0x83a8(%rip), %rdi      # 0xb07d
leaq 0x828e(%rip), %rsi      # 0xaf6a
leaq 0x83ae(%rip), %rcx      # 0xb091
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