// File: /workspace/github_cpp_makefile_star2_O1/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 $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 0x4400
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x25286
movl %ebx, 0x34(%rsp)
movq %r14, 0x18(%rsp)
movslq %r14d, %rdi
movq %rdi, 0x40(%rsp)
callq 0x4400
testq %rax, %rax
je 0x2527c
movq %rax, %rbx
movl %r15d, 0x30(%rsp)
movslq 0x8(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x250bf
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 0x8c43(%rip), %rsi      # 0x2da30
leaq 0x8c58(%rip), %rcx      # 0x2da4c
movq %r15, 0x90(%rsp)
movq %rax, 0x48(%rsp)
testq %rax, %rax
leaq 0x8d55(%rip), %r8       # 0x2db60
leaq 0x8d6e(%rip), %rax      # 0x2db80
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 0x25046
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 0x24eb9
xorl %r10d, %r10d
cmpl $0x6, %r8d
ja 0x24eb1
movslq (%rsi,%r9,4), %rax
addq %rsi, %rax
jmpq *%rax
movb (%r13,%r10), %al
jmp 0x24ead
movb (%r13,%r10), %al
subb (%rbp,%r10), %al
jmp 0x24ead
movb (%r13,%r10), %al
movb (%rbp,%r10), %cl
shrb %cl
subb %cl, %al
leaq 0x8b9f(%rip), %rcx      # 0x2da4c
movb %al, (%rbx,%r10)
incq %r10
cmpq %r10, %r15
jne 0x24e76
movq 0x18(%rsp), %rax
cmpl 0x8(%rsp), %eax
jle 0x24fb8
xorl %r10d, %r10d
cmpl $0x6, %r8d
ja 0x24fac
movslq (%rcx,%r9,4), %rax
addq %rcx, %rax
jmpq *%rax
movb (%rdi,%r10), %r11b
subb (%r13,%r10), %r11b
jmp 0x24fa8
movb (%rdi,%r10), %r11b
movb (%r13,%r10), %al
shrb %al
subb %al, %r11b
jmp 0x24fa8
movb (%rdi,%r10), %r11b
movzbl (%r13,%r10), %eax
movzbl (%rdx,%r10), %ecx
addl %eax, %ecx
shrl %ecx
subb %cl, %r11b
jmp 0x24f91
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 0x24f5e
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 0x8a9f(%rip), %rsi      # 0x2da30
leaq 0x8ab4(%rip), %rcx      # 0x2da4c
jmp 0x24fa8
movb (%rdi,%r10), %r11b
jmp 0x24fa8
movb (%rdi,%r10), %r11b
subb (%rdx,%r10), %r11b
movb %r11b, (%r14,%r10)
incq %r10
cmpq %r10, %r12
jne 0x24ecb
movq 0x88(%rsp), %r8
testl %r8d, %r8d
je 0x24fcc
movq 0x50(%rsp), %r9
jmp 0x25030
cmpl $0x0, 0x18(%rsp)
movq 0x78(%rsp), %rdi
movq 0x50(%rsp), %r9
jle 0x24fff
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 0x24fe1
jmp 0x25001
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 0x8a07(%rip), %rsi      # 0x2da30
leaq 0x8a1c(%rip), %rcx      # 0x2da4c
testl %r8d, %r8d
jne 0x25046
leaq 0x1(%r9), %rax
cmpq $0x4, %r9
movq %rax, %r9
jl 0x24e58
leal 0x1(%r8), %eax
testl %r8d, %r8d
movl %eax, %r8d
je 0x24e3a
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 0x42a0
leaq 0x89c5(%rip), %rcx      # 0x2da4c
leaq 0x89a2(%rip), %rsi      # 0x2da30
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 0x24dfc
movq %rbx, %rdi
callq 0x4390
leaq 0x9c(%rsp), %rbx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movl 0x30(%rsp), %esi
movq %rbx, %rdx
movl $0x8, %ecx
callq 0x243e3
movq %rax, %r12
movq %r14, %rdi
callq 0x4390
movslq (%rbx), %r15
leaq 0x39(%r15), %r14
movq %r14, %rdi
callq 0x4400
testq %rax, %rax
je 0x25286
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 0x89a1(%rip), %rax      # 0x2db40
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 0x2529d
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 0x4500
leaq (%r15,%r13), %rax
addq $0x8, %rax
movq %rax, (%r14)
movq %r12, %rdi
callq 0x4390
movq %r14, %rdi
movl %r15d, %esi
callq 0x2529d
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 0x2529d
movslq (%rbp), %rax
addq %rbx, %rax
cmpq %rax, (%r14)
je 0x25288
leaq 0x8e66(%rip), %rdi      # 0x2e0ca
leaq 0x8d47(%rip), %rsi      # 0x2dfb2
leaq 0x8e6c(%rip), %rcx      # 0x2e0de
movl $0x3d8, %edx            # imm = 0x3D8
callq 0x4200
movq 0x10(%rsp), %rdi
callq 0x4390
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq