// File: /workspace/github_c_makefile_star2_O3/archer1357[P]Offline-Raytracer/stb_image_write.c
// 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 *) malloc((x*n+1) * y); if (!filt) return 0;
   line_buffer = (signed char *) malloc(x * n); if (!line_buffer) { 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 ? mapping : firstmap;
      int best = 0, bestval = 0x7fffffff;
      for (p=0; p < 2; ++p) {
         for (k= p?best:0; k < 5; ++k) {
            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;
      memcpy(filt+j*(x*n+1)+1, line_buffer, x*n);
   }
   free(line_buffer);
   zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, 8); // increase 8 to get smaller but use more memory
   free(filt);
   if (!zlib) return 0;

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

   o=out;
   memcpy(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++ = (unsigned char) ctype[n];
   *o++ = 0;
   *o++ = 0;
   *o++ = 0;
   stbiw__wpcrc(&o,13);

   stbiw__wp32(o, zlen);
   stbiw__wptag(o, "IDAT");
   memcpy(o, zlib, zlen); o += zlen; free(zlib);
   stbiw__wpcrc(&o, zlen);

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

   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, 0x60(%rsp)
movl %edx, %ebx
movl %esi, %r12d
movq %rdi, %r13
movl %r8d, 0x10(%rsp)
movl %r8d, %r14d
imull %edx, %r14d
testl %esi, %esi
cmovel %r14d, %r12d
leal 0x1(%r14), %ebp
movl %ebp, %r15d
movl %ecx, 0xc(%rsp)
imull %ecx, %r15d
movslq %r15d, %rdi
callq 0x4458c0
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x415253
movl %ebx, 0x34(%rsp)
movq %r14, 0x20(%rsp)
movslq %r14d, %rdi
movq %rdi, 0x48(%rsp)
callq 0x4458c0
testq %rax, %rax
je 0x415249
movq %rax, %rbx
movl %r15d, 0x30(%rsp)
movslq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x41509c
movslq %r12d, %rcx
movslq %ebp, %rax
movq %rax, 0x70(%rsp)
movl 0xc(%rsp), %eax
movq %rax, 0x68(%rsp)
movq 0x40(%rsp), %rax
movl %eax, %r15d
movl 0x20(%rsp), %edx
movq %rdx, 0x80(%rsp)
movq %r13, %rbp
subq %rcx, %rbp
movq %rbx, %r14
addq %rax, %r14
leaq (%rax,%r13), %r8
movq %rax, %rdx
movq %rcx, 0x78(%rsp)
subq %rcx, %rdx
addq %r13, %rdx
movq 0x48(%rsp), %r12
subq %rax, %r12
leaq 0xc43dc(%rip), %rsi     # 0x4d9190
leaq 0xc43f1(%rip), %rdi     # 0x4d91ac
xorl %eax, %eax
movq %r15, 0x98(%rsp)
movq %rax, 0x50(%rsp)
testq %rax, %rax
leaq 0xc44dc(%rip), %rcx     # 0x4d92b0
leaq 0xc44f5(%rip), %rax     # 0x4d92d0
cmoveq %rax, %rcx
movq %rcx, 0x88(%rsp)
movb $0x1, %r9b
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
xorl %r11d, %r11d
movq %r8, 0x58(%rsp)
movq %rdx, 0x28(%rsp)
testb $0x1, %r9b
movl %r11d, %eax
movl $0x0, %ecx
cmovnel %ecx, %eax
cmpl $0x4, %eax
jg 0x415012
movslq %eax, %rcx
movl %r11d, 0x8(%rsp)
movl %r9d, 0x3c(%rsp)
movl %r11d, 0x38(%rsp)
movq %rcx, 0x90(%rsp)
movl %r10d, 0x14(%rsp)
movq 0x88(%rsp), %rax
movl (%rax,%rcx,4), %r9d
movl %r9d, %r10d
cmpl $0x0, 0x10(%rsp)
jle 0x414e89
xorl %r11d, %r11d
cmpl $0x6, %r9d
ja 0x414e81
movslq (%rsi,%r10,4), %rax
addq %rsi, %rax
jmpq *%rax
movb (%r13,%r11), %al
jmp 0x414e7d
movb (%r13,%r11), %al
subb (%rbp,%r11), %al
jmp 0x414e7d
movb (%r13,%r11), %al
movb (%rbp,%r11), %cl
shrb %cl
subb %cl, %al
movb %al, (%rbx,%r11)
incq %r11
cmpq %r11, %r15
jne 0x414e4d
movq 0x20(%rsp), %rax
cmpl 0x10(%rsp), %eax
jle 0x414f88
xorl %r11d, %r11d
cmpl $0x6, %r9d
ja 0x414f7c
movslq (%rdi,%r10,4), %rax
addq %rdi, %rax
jmpq *%rax
movb (%r8,%r11), %sil
subb (%r13,%r11), %sil
jmp 0x414f78
movb (%r8,%r11), %sil
movb (%r13,%r11), %al
shrb %al
subb %al, %sil
jmp 0x414f78
movb (%r8,%r11), %sil
movzbl (%r13,%r11), %eax
movzbl (%rdx,%r11), %ecx
addl %eax, %ecx
shrl %ecx
subb %cl, %sil
jmp 0x414f78
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 0x414f35
movl %esi, %ecx
movq 0x58(%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 0x28(%rsp), %rdx
movq %r15, %r12
movq 0x98(%rsp), %r15
leaq 0xc4244(%rip), %rdi     # 0x4d91ac
jmp 0x414f78
movb (%r8,%r11), %sil
jmp 0x414f78
movb (%r8,%r11), %sil
subb (%rdx,%r11), %sil
movb %sil, (%r14,%r11)
incq %r11
cmpq %r11, %r12
jne 0x414e9b
movl 0x3c(%rsp), %r9d
testb $0x1, %r9b
movq 0x80(%rsp), %rdi
je 0x415030
cmpl $0x0, 0x20(%rsp)
jle 0x414fcd
xorl %ecx, %ecx
xorl %eax, %eax
movl 0x14(%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 0x414faf
jmp 0x414fd4
xorl %eax, %eax
movl 0x14(%rsp), %r10d
cmpl %r10d, %eax
movl 0x8(%rsp), %ecx
movq 0x90(%rsp), %rdx
cmovll %edx, %ecx
movl %ecx, 0x8(%rsp)
movq %rdx, %rcx
cmovll %eax, %r10d
incq %rcx
cmpl $0x5, %ecx
movq 0x28(%rsp), %rdx
leaq 0xc418d(%rip), %rsi     # 0x4d9190
leaq 0xc41a2(%rip), %rdi     # 0x4d91ac
jne 0x414e27
jmp 0x415017
movl %r11d, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, %r11d
testb $0x1, %r9b
movl $0x0, %r9d
jne 0x414dfd
jmp 0x415034
movl 0x38(%rsp), %eax
movl %eax, %edx
movq 0x50(%rsp), %rax
imulq 0x70(%rsp), %rax
movq 0x18(%rsp), %rcx
movb %dl, (%rcx,%rax)
leaq (%rcx,%rax), %rdi
incq %rdi
movq %rbx, %rsi
movq 0x48(%rsp), %rdx
callq 0x401058
leaq 0xc4148(%rip), %rdi     # 0x4d91ac
leaq 0xc4125(%rip), %rsi     # 0x4d9190
movq 0x28(%rsp), %rdx
movq 0x58(%rsp), %r8
movq 0x50(%rsp), %rcx
incq %rcx
movq 0x78(%rsp), %rax
addq %rax, %rbp
addq %rax, %r13
addq %rax, %r8
addq %rax, %rdx
movq %rcx, %rax
cmpq 0x68(%rsp), %rcx
jne 0x414dc5
movq %rbx, %rdi
callq 0x445c00
leaq 0xa4(%rsp), %rbx
movq 0x18(%rsp), %r15
movq %r15, %rdi
movl 0x30(%rsp), %esi
movq %rbx, %rdx
movl $0x8, %ecx
callq 0x4143ac
movq %rax, %r14
movq %r15, %rdi
callq 0x445c00
movslq (%rbx), %rax
movq %rax, %r13
leaq 0x39(%rax), %r15
movq %r15, %rdi
callq 0x4458c0
testq %rax, %rax
je 0x415253
movq %rax, %rbx
movq 0x60(%rsp), %r12
movl %r15d, (%r12)
movabsq $0xa1a0a0d474e5089, %rax # imm = 0xA1A0A0D474E5089
movq %rax, (%rbx)
leaq 0xc(%rbx), %rdi
movabsq $0x524448490d000000, %rax # imm = 0x524448490D000000
movq %rax, 0x8(%rbx)
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)
movl 0xc(%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 0xc413b(%rip), %rax     # 0x4d9290
movq 0x40(%rsp), %rcx
movb (%rax,%rcx,4), %al
movb %al, 0x19(%rbx)
movw $0x0, 0x1a(%rbx)
movb $0x0, 0x1c(%rbx)
movl $0x11, %esi
callq 0x414c5c
movl %eax, %ecx
shrl $0x18, %ecx
movb %cl, 0x1d(%rbx)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x1e(%rbx)
movb %ah, 0x1f(%rbx)
movb %al, 0x20(%rbx)
movq %r13, %rdx
movl %edx, %eax
shrl $0x18, %eax
movb %al, 0x21(%rbx)
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0x22(%rbx)
movb %dh, 0x23(%rbx)
movb %dl, 0x24(%rbx)
leaq 0x25(%rbx), %r15
movl $0x54414449, 0x25(%rbx) # imm = 0x54414449
leaq 0x29(%rbx), %rdi
movq %r14, %rsi
callq 0x401058
movq %r14, %rdi
callq 0x445c00
leal 0x4(%r13), %esi
movq %r15, %rdi
callq 0x414c5c
movl %eax, %ecx
shrl $0x18, %ecx
movq %r13, %rdx
movb %cl, 0x29(%rbx,%r13)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x2a(%rbx,%r13)
movb %ah, 0x2b(%rbx,%rdx)
movb %al, 0x2c(%rbx,%r13)
leaq (%rbx,%r13), %rdi
addq $0x31, %rdi
movabsq $0x444e454900000000, %rax # imm = 0x444E454900000000
movq %rax, 0x2d(%rbx,%r13)
movl $0x4, %esi
callq 0x414c5c
bswapl %eax
movl %eax, 0x35(%rbx,%r13)
leaq (%rbx,%r13), %rax
addq $0x39, %rax
movslq (%r12), %rcx
addq %rbx, %rcx
cmpq %rcx, %rax
je 0x415255
leaq 0xc414e(%rip), %rdi     # 0x4d937f
leaq 0xc40f1(%rip), %rsi     # 0x4d9329
leaq 0xc4154(%rip), %rcx     # 0x4d9393
movl $0x1a7, %edx            # imm = 0x1A7
callq 0x4209d0
movq 0x18(%rsp), %rdi
callq 0x445c00
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq