// File: alan0201tw[P]RayTracer/include/stb_image_write.h
// Function: stbi_write_png_to_mem
STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
{
   int force_filter = stbi_write_force_png_filter;
   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 j,zlen;

   if (stride_bytes == 0)
      stride_bytes = x * n;

   if (force_filter >= 5) {
      force_filter = -1;
   }

   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) {
      int filter_type;
      if (force_filter > -1) {
         filter_type = force_filter;
         stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, force_filter, line_buffer);
      } else { // Estimate the best filter by running through all of them:
         int best_filter = 0, best_filter_val = 0x7fffffff, est, i;
         for (filter_type = 0; filter_type < 5; filter_type++) {
            stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, filter_type, line_buffer);

            // Estimate the entropy of the line using this filter; the less, the better.
            est = 0;
            for (i = 0; i < x*n; ++i) {
               est += abs((signed char) line_buffer[i]);
            }
            if (est < best_filter_val) {
               best_filter_val = est;
               best_filter = filter_type;
            }
         }
         if (filter_type != best_filter) {  // If the last iteration already got us the best filter, don't redo it
            stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, best_filter, line_buffer);
            filter_type = best_filter;
         }
      }
      // when we get here, filter_type contains the filter type, and line_buffer contains the data
      filt[j*(x*n+1)] = (unsigned char) filter_type;
      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, stbi_write_png_compression_level);
   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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movl 0x57282(%rip), %eax     # 0xb04e8
movl %eax, -0x2c(%rbp)
movq 0x261e0(%rip), %rax     # 0x7f450
movq %rax, -0x40(%rbp)
movq 0x261dd(%rip), %rax     # 0x7f458
movq %rax, -0x38(%rbp)
movl 0x261db(%rip), %eax     # 0x7f460
movl %eax, -0x30(%rbp)
movq 0x27b59(%rip), %rax     # 0x80de8
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x592a3
movl -0x18(%rbp), %eax
imull -0x20(%rbp), %eax
movl %eax, -0x14(%rbp)
cmpl $0x5, -0x2c(%rbp)
jl 0x592b0
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl -0x18(%rbp), %eax
imull -0x20(%rbp), %eax
addl $0x1, %eax
imull -0x1c(%rbp), %eax
movslq %eax, %rdi
callq 0x7650
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x592de
movq $0x0, -0x8(%rbp)
jmp 0x598e4
movl -0x18(%rbp), %eax
imull -0x20(%rbp), %eax
movslq %eax, %rdi
callq 0x7650
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0x5930e
movq -0x60(%rbp), %rdi
callq 0x75e0
movq $0x0, -0x8(%rbp)
jmp 0x598e4
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x594ca
cmpl $-0x1, -0x2c(%rbp)
jle 0x5935d
movl -0x2c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x74(%rbp), %r8d
movl -0x20(%rbp), %r9d
movl -0x2c(%rbp), %r10d
movq -0x70(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x59900
jmp 0x5946d
movl $0x0, -0x80(%rbp)
movl $0x7fffffff, -0x84(%rbp) # imm = 0x7FFFFFFF
movl $0x0, -0x7c(%rbp)
cmpl $0x5, -0x7c(%rbp)
jge 0x59432
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x74(%rbp), %r8d
movl -0x20(%rbp), %r9d
movl -0x7c(%rbp), %r10d
movq -0x70(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x59900
movl $0x0, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl -0x18(%rbp), %ecx
imull -0x20(%rbp), %ecx
cmpl %ecx, %eax
jge 0x59402
movq -0x70(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
addl -0x88(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x593be
movl -0x88(%rbp), %eax
cmpl -0x84(%rbp), %eax
jge 0x59422
movl -0x88(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x80(%rbp)
jmp 0x59424
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x59375
movl -0x7c(%rbp), %eax
cmpl -0x80(%rbp), %eax
je 0x5946b
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x74(%rbp), %r8d
movl -0x20(%rbp), %r9d
movl -0x80(%rbp), %r10d
movq -0x70(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x59900
movl -0x80(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x5946d
movl -0x7c(%rbp), %eax
movb %al, %dl
movq -0x60(%rbp), %rax
movl -0x74(%rbp), %ecx
movl -0x18(%rbp), %esi
imull -0x20(%rbp), %esi
addl $0x1, %esi
imull %esi, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x60(%rbp), %rdi
movl -0x74(%rbp), %eax
movl -0x18(%rbp), %ecx
imull -0x20(%rbp), %ecx
addl $0x1, %ecx
imull %ecx, %eax
cltq
addq %rax, %rdi
addq $0x1, %rdi
movq -0x70(%rbp), %rsi
movl -0x18(%rbp), %eax
imull -0x20(%rbp), %eax
movslq %eax, %rdx
callq 0x77b0
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x59315
movq -0x70(%rbp), %rdi
callq 0x75e0
movq -0x60(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x18(%rbp), %eax
imull -0x20(%rbp), %eax
addl $0x1, %eax
imull %eax, %esi
movl 0x56ff3(%rip), %ecx     # 0xb04e0
leaq -0x78(%rbp), %rdx
callq 0x583b0
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
callq 0x75e0
cmpq $0x0, -0x68(%rbp)
jne 0x59517
movq $0x0, -0x8(%rbp)
jmp 0x598e4
movl -0x78(%rbp), %eax
addl $0x2d, %eax
addl $0xc, %eax
movslq %eax, %rdi
callq 0x7650
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x59540
movq $0x0, -0x8(%rbp)
jmp 0x598e4
movl -0x78(%rbp), %ecx
addl $0x2d, %ecx
addl $0xc, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
leaq -0x48(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq -0x58(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movb $0x0, (%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x1(%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x2(%rax)
movq -0x58(%rbp), %rax
movb $0xd, 0x3(%rax)
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movsbl 0x27d07(%rip), %eax     # 0x812aa
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, (%rax)
movsbl 0x27cf4(%rip), %eax     # 0x812ab
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x1(%rax)
movsbl 0x27ce0(%rip), %eax     # 0x812ac
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x2(%rax)
movsbl 0x27ccc(%rip), %eax     # 0x812ad
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movl -0x18(%rbp), %eax
sarl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, (%rax)
movl -0x18(%rbp), %eax
sarl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0x18(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x18(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movl -0x1c(%rbp), %eax
sarl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, (%rax)
movl -0x1c(%rbp), %eax
sarl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0x1c(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x1c(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
movb $0x8, (%rax)
movslq -0x20(%rbp), %rax
movl -0x40(%rbp,%rax,4), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x58(%rbp)
movb %cl, (%rax)
movq -0x58(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
movb $0x0, (%rax)
movq -0x58(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
movb $0x0, (%rax)
movq -0x58(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
movb $0x0, (%rax)
leaq -0x58(%rbp), %rdi
movl $0xd, %esi
callq 0x59d90
movl -0x78(%rbp), %eax
sarl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, (%rax)
movl -0x78(%rbp), %eax
sarl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0x78(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x78(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movsbl 0x27c0e(%rip), %eax     # 0x8138e
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, (%rax)
movsbl 0x27bfb(%rip), %eax     # 0x8138f
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x1(%rax)
movsbl 0x27be7(%rip), %eax     # 0x81390
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x2(%rax)
movsbl 0x27bd3(%rip), %eax     # 0x81391
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq -0x68(%rbp), %rsi
movslq -0x78(%rbp), %rdx
callq 0x77b0
movl -0x78(%rbp), %ecx
movq -0x58(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x68(%rbp), %rdi
callq 0x75e0
movl -0x78(%rbp), %esi
leaq -0x58(%rbp), %rdi
callq 0x59d90
movq -0x58(%rbp), %rax
movb $0x0, (%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x1(%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x2(%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x3(%rax)
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movsbl 0x276a2(%rip), %eax     # 0x80ee3
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, (%rax)
movsbl 0x2768f(%rip), %eax     # 0x80ee4
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x1(%rax)
movsbl 0x2767b(%rip), %eax     # 0x80ee5
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x2(%rax)
movsbl 0x27667(%rip), %eax     # 0x80ee6
andl $0xff, %eax
movb %al, %cl
movq -0x58(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x59d90
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x28(%rbp), %rdx
movslq (%rdx), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jne 0x598bd
jmp 0x598dc
leaq 0x27624(%rip), %rdi     # 0x80ee8
leaq 0x275ba(%rip), %rsi     # 0x80e85
movl $0x483, %edx            # imm = 0x483
leaq 0x27625(%rip), %rcx     # 0x80efc
callq 0x7310
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)