// File: /workspace/github_c_makefile_star2_O2/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 $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 0x442160
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x411b8f
movq %r14, 0x68(%rsp)
movslq %r12d, %rdi
movq %rdi, 0x30(%rsp)
callq 0x442160
testq %rax, %rax
je 0x411b85
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 0xc3905(%rip), %r9      # 0x4d501c
leaq 0xc38e2(%rip), %r10     # 0x4d5000
movq 0x58(%rsp), %r11
movq %r8, 0x20(%rsp)
movq %r12, 0x90(%rsp)
movq %r13, 0x60(%rsp)
cmpq 0x80(%rsp), %rbp
je 0x411a0a
testq %rbp, %rbp
leaq 0xc39d3(%rip), %rcx     # 0x4d5120
leaq 0xc39ec(%rip), %rax     # 0x4d5140
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 0x4119b8
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 0x41199a
movq 0x98(%rsp), %rax
movl (%rax,%rcx,4), %ebx
xorl %r15d, %r15d
movq %rcx, 0xa0(%rsp)
movl %ebx, %r12d
cmpq %r15, %r13
je 0x411861
cmpl $0x6, %ebx
ja 0x41185c
movslq (%r10,%r12,4), %rax
addq %r10, %rax
jmpq *%rax
movb (%r11,%r15), %r12b
jmp 0x411858
movb (%r11,%r15), %r12b
movq 0x40(%rsp), %rax
movzbl (%rax,%r15), %esi
xorl %edi, %edi
xorl %edx, %edx
callq 0x411ba6
movq 0x38(%rsp), %r11
leaq 0xc37dc(%rip), %r10     # 0x4d5000
leaq 0xc37f1(%rip), %r9      # 0x4d501c
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %r8
jmp 0x411855
movb (%r11,%r15), %r12b
movq 0x40(%rsp), %rax
subb (%rax,%r15), %r12b
jmp 0x411858
movb (%r11,%r15), %r12b
movq 0x40(%rsp), %rax
movb (%rax,%r15), %al
shrb %al
subb %al, %r12b
movb %r12b, (%r14,%r15)
incq %r15
jmp 0x4117e5
movq %r8, %r13
cmpq %rdx, %r13
jge 0x411939
cmpl $0x6, %ebx
ja 0x411931
movslq (%r9,%r12,4), %rax
addq %r9, %rax
jmpq *%rax
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq %r8, %rax
jmp 0x411928
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq %r8, %rax
movb (%rbp,%rax), %al
shrb %al
jmp 0x41190f
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 0x41192d
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 0x411ba6
movq 0x38(%rsp), %r11
leaq 0xc3702(%rip), %r10     # 0x4d5000
leaq 0xc3717(%rip), %r9      # 0x4d501c
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %r8
subb %al, %r15b
jmp 0x41192d
movb (%rbp,%r13), %r15b
jmp 0x41192d
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq 0x10(%rsp), %rax
subb (%rbp,%rax), %r15b
movb %r15b, (%r14,%r13)
incq %r13
jmp 0x411864
movl 0x54(%rsp), %ebx
testl %ebx, %ebx
jne 0x4119ac
xorl %ecx, %ecx
xorl %eax, %eax
movq 0x90(%rsp), %r12
movq 0x60(%rsp), %r13
cmpq %rcx, %r12
je 0x411972
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 0x411952
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 0x4117c5
movl 0xc(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x1c(%rsp)
jmp 0x4119b1
movq 0x60(%rsp), %r13
incl %ebx
jmp 0x41179c
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 0x401060
movq 0x38(%rsp), %r11
leaq 0xc360f(%rip), %r10     # 0x4d5000
leaq 0xc3624(%rip), %r9      # 0x4d501c
movq 0x20(%rsp), %r8
incq %rbp
addq 0x10(%rsp), %r11
jmp 0x411735
movq %r14, %rdi
callq 0x4424a0
leaq 0xac(%rsp), %r12
pushq $0x8
popq %rcx
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movl 0x50(%rsp), %esi
movq %r12, %rdx
callq 0x410d83
movq %rax, %r14
movq %rbx, %rdi
callq 0x4424a0
movslq (%r12), %rbx
leaq 0x39(%rbx), %r15
movq %r15, %rdi
callq 0x442160
testq %rax, %rax
je 0x411b8f
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 0xc3644(%rip), %rax     # 0x4d5100
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 0x411be5
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 0x401060
leaq (%rbx,%r13), %rax
addq $0x8, %rax
movq %rax, (%r15)
movq %r14, %rdi
callq 0x4424a0
movq %r15, %rdi
movl %r13d, %esi
callq 0x411be5
movq (%r15), %rax
movabsq $0x444e454900000000, %rcx # imm = 0x444E454900000000
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, (%r15)
movq %r15, %rdi
xorl %esi, %esi
callq 0x411be5
movslq (%r12), %rax
addq %rbp, %rax
cmpq %rax, (%r15)
je 0x411b91
leaq 0xc3682(%rip), %rdi     # 0x4d51ef
leaq 0xc3625(%rip), %rsi     # 0x4d5199
leaq 0xc3688(%rip), %rcx     # 0x4d5203
movl $0x1a7, %edx            # imm = 0x1A7
callq 0x41d270
movq 0x28(%rsp), %rdi
callq 0x4424a0
xorl %ebp, %ebp
movq %rbp, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq