// File: /workspace/github_c_makefile_star2_O1/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 0x4449d0
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x414093
movl %ebx, 0x3c(%rsp)
movq %r14, 0x20(%rsp)
movslq %r14d, %rdi
movq %rdi, 0x48(%rsp)
callq 0x4449d0
testq %rax, %rax
je 0x414089
movq %rax, %rbx
movl %r15d, 0x38(%rsp)
movslq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x413edc
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), %rdi
movq %rax, %rdx
movq %rcx, 0x78(%rsp)
subq %rcx, %rdx
addq %r13, %rdx
movq 0x48(%rsp), %r12
subq %rax, %r12
xorl %eax, %eax
leaq 0xc441e(%rip), %rsi     # 0x4d8020
leaq 0xc4433(%rip), %rcx     # 0x4d803c
movq %r15, 0x98(%rsp)
movq %rax, 0x50(%rsp)
testq %rax, %rax
leaq 0xc4520(%rip), %r8      # 0x4d8140
leaq 0xc4539(%rip), %rax     # 0x4d8160
cmoveq %rax, %r8
movq %r8, 0x88(%rsp)
movl $0x7fffffff, 0x14(%rsp) # imm = 0x7FFFFFFF
movl $0x0, 0x8(%rsp)
xorl %r8d, %r8d
movq %rdi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
testl %r8d, %r8d
movl 0x8(%rsp), %eax
cmovel %r8d, %eax
cmpl $0x4, %eax
jg 0x413e62
movslq %eax, %r9
movq %r8, 0x90(%rsp)
movq 0x88(%rsp), %rax
movq %r9, 0x58(%rsp)
movl (%rax,%r9,4), %r8d
movl %r8d, %r9d
cmpl $0x0, 0x10(%rsp)
jle 0x413cd0
xorl %r10d, %r10d
cmpl $0x6, %r8d
ja 0x413cc8
movslq (%rsi,%r9,4), %rax
addq %rsi, %rax
jmpq *%rax
movb (%r13,%r10), %al
jmp 0x413cc4
movb (%r13,%r10), %al
subb (%rbp,%r10), %al
jmp 0x413cc4
movb (%r13,%r10), %al
movb (%rbp,%r10), %cl
shrb %cl
subb %cl, %al
leaq 0xc4378(%rip), %rcx     # 0x4d803c
movb %al, (%rbx,%r10)
incq %r10
cmpq %r10, %r15
jne 0x413c8d
movq 0x20(%rsp), %rax
cmpl 0x10(%rsp), %eax
jle 0x413dcf
xorl %r10d, %r10d
cmpl $0x6, %r8d
ja 0x413dc3
movslq (%rcx,%r9,4), %rax
addq %rcx, %rax
jmpq *%rax
movb (%rdi,%r10), %r11b
subb (%r13,%r10), %r11b
jmp 0x413dbf
movb (%rdi,%r10), %r11b
movb (%r13,%r10), %al
shrb %al
subb %al, %r11b
jmp 0x413dbf
movb (%rdi,%r10), %r11b
movzbl (%r13,%r10), %eax
movzbl (%rdx,%r10), %ecx
addl %eax, %ecx
shrl %ecx
subb %cl, %r11b
jmp 0x413da8
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 0x413d75
movl %r11d, %ecx
movq 0x30(%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 0x28(%rsp), %rdx
movq %r15, %r12
movq 0x98(%rsp), %r15
leaq 0xc4278(%rip), %rsi     # 0x4d8020
leaq 0xc428d(%rip), %rcx     # 0x4d803c
jmp 0x413dbf
movb (%rdi,%r10), %r11b
jmp 0x413dbf
movb (%rdi,%r10), %r11b
subb (%rdx,%r10), %r11b
movb %r11b, (%r14,%r10)
incq %r10
cmpq %r10, %r12
jne 0x413ce2
movq 0x90(%rsp), %r8
testl %r8d, %r8d
je 0x413de3
movq 0x58(%rsp), %r9
jmp 0x413e4c
cmpl $0x0, 0x20(%rsp)
movq 0x80(%rsp), %rdi
movq 0x58(%rsp), %r9
jle 0x413e19
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 0x413dfb
jmp 0x413e1b
xorl %eax, %eax
movl 0x14(%rsp), %edx
cmpl %edx, %eax
movl 0x8(%rsp), %ecx
cmovll %r9d, %ecx
movl %ecx, 0x8(%rsp)
cmovll %eax, %edx
movl %edx, 0x14(%rsp)
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rdx
leaq 0xc41db(%rip), %rsi     # 0x4d8020
leaq 0xc41f0(%rip), %rcx     # 0x4d803c
testl %r8d, %r8d
jne 0x413e62
leaq 0x1(%r9), %rax
cmpq $0x4, %r9
movq %rax, %r9
jl 0x413c6f
leal 0x1(%r8), %eax
testl %r8d, %r8d
movl %eax, %r8d
je 0x413c50
movq 0x50(%rsp), %rax
imulq 0x70(%rsp), %rax
movq 0x18(%rsp), %rcx
movl 0x8(%rsp), %edx
movb %dl, (%rcx,%rax)
leaq (%rcx,%rax), %rdi
incq %rdi
movq %rbx, %rsi
movq 0x48(%rsp), %rdx
callq 0x401058
leaq 0xc4198(%rip), %rcx     # 0x4d803c
leaq 0xc4175(%rip), %rsi     # 0x4d8020
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rdi
movq 0x50(%rsp), %r8
incq %r8
movq 0x78(%rsp), %rax
addq %rax, %rbp
addq %rax, %r13
addq %rax, %rdi
addq %rax, %rdx
movq %r8, %rax
cmpq 0x68(%rsp), %r8
jne 0x413c11
movq %rbx, %rdi
callq 0x444d10
leaq 0xa4(%rsp), %rbx
movq 0x18(%rsp), %r15
movq %r15, %rdi
movl 0x38(%rsp), %esi
movq %rbx, %rdx
movl $0x8, %ecx
callq 0x413184
movq %rax, %r14
movq %r15, %rdi
callq 0x444d10
movslq (%rbx), %rax
movq %rax, %r13
leaq 0x39(%rax), %r15
movq %r15, %rdi
callq 0x4449d0
testq %rax, %rax
je 0x414093
movq %rax, %rbx
movq 0x60(%rsp), %r12
movl %r15d, (%r12)
movabsq $0xa1a0a0d474e5089, %rax # imm = 0xA1A0A0D474E5089
movq %rax, (%rbx)
movabsq $0x524448490d000000, %rax # imm = 0x524448490D000000
movq %rax, 0x8(%rbx)
leaq 0xc(%rbx), %rdi
movl 0x3c(%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 0xc418b(%rip), %rax     # 0x4d8120
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 0x413aa5
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 0x444d10
leal 0x4(%r13), %esi
movq %r15, %rdi
callq 0x413aa5
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 0x413aa5
bswapl %eax
movl %eax, 0x35(%rbx,%r13)
leaq (%rbx,%r13), %rax
addq $0x39, %rax
movslq (%r12), %rcx
addq %rbx, %rcx
cmpq %rcx, %rax
je 0x414095
leaq 0xc419e(%rip), %rdi     # 0x4d820f
leaq 0xc4141(%rip), %rsi     # 0x4d81b9
leaq 0xc41a4(%rip), %rcx     # 0x4d8223
movl $0x1a7, %edx            # imm = 0x1A7
callq 0x41fae0
movq 0x18(%rsp), %rdi
callq 0x444d10
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq