// File: /workspace/github_cpp_makefile_star2_O2/srajangarg[P]raytracer/src/core/stb_image_write.hpp
// Function: stbi_write_png_to_mem(unsigned char*, int, int, int, int, int*)
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 ? 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;
      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 0x4420
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x1c5bf
movq %r14, 0x68(%rsp)
movslq %r12d, %rdi
movq %rdi, 0x30(%rsp)
callq 0x4420
testq %rax, %rax
je 0x1c5b5
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 0x68c5(%rip), %r9       # 0x22a0c
leaq 0x68a2(%rip), %r10      # 0x229f0
movq 0x58(%rsp), %r11
movq %r8, 0x20(%rsp)
movq %r12, 0x90(%rsp)
movq %r13, 0x60(%rsp)
cmpq 0x80(%rsp), %rbp
je 0x1c43a
testq %rbp, %rbp
leaq 0x69a3(%rip), %rcx      # 0x22b20
leaq 0x69bc(%rip), %rax      # 0x22b40
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 0x1c3e8
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 0x1c3ca
movq 0x98(%rsp), %rax
movl (%rax,%rcx,4), %ebx
xorl %r15d, %r15d
movq %rcx, 0xa0(%rsp)
movl %ebx, %r12d
cmpq %r15, %r13
je 0x1c291
cmpl $0x6, %ebx
ja 0x1c28c
movslq (%r10,%r12,4), %rax
addq %r10, %rax
jmpq *%rax
movb (%r11,%r15), %r12b
jmp 0x1c288
movb (%r11,%r15), %r12b
movq 0x40(%rsp), %rax
movzbl (%rax,%r15), %esi
xorl %edi, %edi
xorl %edx, %edx
callq 0x1c5d6
movq 0x38(%rsp), %r11
leaq 0x679c(%rip), %r10      # 0x229f0
leaq 0x67b1(%rip), %r9       # 0x22a0c
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %r8
jmp 0x1c285
movb (%r11,%r15), %r12b
movq 0x40(%rsp), %rax
subb (%rax,%r15), %r12b
jmp 0x1c288
movb (%r11,%r15), %r12b
movq 0x40(%rsp), %rax
movb (%rax,%r15), %al
shrb %al
subb %al, %r12b
movb %r12b, (%r14,%r15)
incq %r15
jmp 0x1c215
movq %r8, %r13
cmpq %rdx, %r13
jge 0x1c369
cmpl $0x6, %ebx
ja 0x1c361
movslq (%r9,%r12,4), %rax
addq %r9, %rax
jmpq *%rax
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq %r8, %rax
jmp 0x1c358
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq %r8, %rax
movb (%rbp,%rax), %al
shrb %al
jmp 0x1c33f
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 0x1c35d
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 0x1c5d6
movq 0x38(%rsp), %r11
leaq 0x66c2(%rip), %r10      # 0x229f0
leaq 0x66d7(%rip), %r9       # 0x22a0c
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %r8
subb %al, %r15b
jmp 0x1c35d
movb (%rbp,%r13), %r15b
jmp 0x1c35d
movb (%rbp,%r13), %r15b
movq %r13, %rax
subq 0x10(%rsp), %rax
subb (%rbp,%rax), %r15b
movb %r15b, (%r14,%r13)
incq %r13
jmp 0x1c294
movl 0x54(%rsp), %ebx
testl %ebx, %ebx
jne 0x1c3dc
xorl %ecx, %ecx
xorl %eax, %eax
movq 0x90(%rsp), %r12
movq 0x60(%rsp), %r13
cmpq %rcx, %r12
je 0x1c3a2
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 0x1c382
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 0x1c1f5
movl 0xc(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x1c(%rsp)
jmp 0x1c3e1
movq 0x60(%rsp), %r13
incl %ebx
jmp 0x1c1cc
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 0x42a0
movq 0x38(%rsp), %r11
leaq 0x65cf(%rip), %r10      # 0x229f0
leaq 0x65e4(%rip), %r9       # 0x22a0c
movq 0x20(%rsp), %r8
incq %rbp
addq 0x10(%rsp), %r11
jmp 0x1c165
movq %r14, %rdi
callq 0x43a0
leaq 0xac(%rsp), %r12
pushq $0x8
popq %rcx
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movl 0x50(%rsp), %esi
movq %r12, %rdx
callq 0x1b834
movq %rax, %r14
movq %rbx, %rdi
callq 0x43a0
movslq (%r12), %rbx
leaq 0x39(%rbx), %r15
movq %r15, %rdi
callq 0x4420
testq %rax, %rax
je 0x1c5bf
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 0x6614(%rip), %rax      # 0x22b00
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 0x1c615
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 0x4510
leaq (%rbx,%r13), %rax
addq $0x8, %rax
movq %rax, (%r15)
movq %r14, %rdi
callq 0x43a0
movq %r15, %rdi
movl %r13d, %esi
callq 0x1c615
movq (%r15), %rax
movabsq $0x444e454900000000, %rcx # imm = 0x444E454900000000
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, (%r15)
movq %r15, %rdi
xorl %esi, %esi
callq 0x1c615
movslq (%r12), %rax
addq %rbp, %rax
cmpq %rax, (%r15)
je 0x1c5c1
leaq 0x6aed(%rip), %rdi      # 0x2308a
leaq 0x69ce(%rip), %rsi      # 0x22f72
leaq 0x6af3(%rip), %rcx      # 0x2309e
movl $0x3d8, %edx            # imm = 0x3D8
callq 0x4210
movq 0x28(%rsp), %rdi
callq 0x43a0
xorl %ebp, %ebp
movq %rbp, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq