// File: Lut99[P]RayTracer/src/lib/LodePNG.cpp
// Function: lodepng_zlib_compress(unsigned char**, unsigned long*, unsigned char const*, unsigned long, LodePNGCompressSettings const*)
unsigned lodepng_zlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in,
                               size_t insize, const LodePNGCompressSettings* settings) {
  size_t i;
  unsigned error;
  unsigned char* deflatedata = 0;
  size_t deflatesize = 0;

  error = deflate(&deflatedata, &deflatesize, in, insize, settings);

  *out = NULL;
  *outsize = 0;
  if(!error) {
    *outsize = deflatesize + 6;
    *out = (unsigned char*)lodepng_malloc(*outsize);
    if(!*out) error = 83; /*alloc fail*/
  }

  if(!error) {
    unsigned ADLER32 = adler32(in, (unsigned)insize);
    /*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/
    unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/
    unsigned FLEVEL = 0;
    unsigned FDICT = 0;
    unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64;
    unsigned FCHECK = 31 - CMFFLG % 31;
    CMFFLG += FCHECK;

    (*out)[0] = (unsigned char)(CMFFLG >> 8);
    (*out)[1] = (unsigned char)(CMFFLG & 255);
    for(i = 0; i != deflatesize; ++i) (*out)[i + 2] = deflatedata[i];
    lodepng_set32bitInt(&(*out)[*outsize - 4], ADLER32);
  }

  lodepng_free(deflatedata);
  return error;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x50(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x40(%rsp), %r8
leaq 0x28(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x13ec50
movl %eax, 0x34(%rsp)
movq 0x60(%rsp), %rax
movq $0x0, (%rax)
movq 0x58(%rsp), %rax
movq $0x0, (%rax)
cmpl $0x0, 0x34(%rsp)
jne 0x13eb2e
movq 0x20(%rsp), %rcx
addq $0x6, %rcx
movq 0x58(%rsp), %rax
movq %rcx, (%rax)
movq 0x58(%rsp), %rax
movq (%rax), %rdi
callq 0x13ce80
movq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, (%rax)
movq 0x60(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x13eb2c
movl $0x53, 0x34(%rsp)
jmp 0x13eb2e
cmpl $0x0, 0x34(%rsp)
jne 0x13ec33
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rax
movl %eax, %esi
callq 0x13ea50
movl %eax, 0x1c(%rsp)
movl $0x78, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movl 0x18(%rsp), %eax
shll $0x8, %eax
movl 0x10(%rsp), %ecx
shll $0x5, %ecx
addl %ecx, %eax
movl 0x14(%rsp), %ecx
shll $0x6, %ecx
addl %ecx, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl $0x1f, %ecx
xorl %edx, %edx
divl %ecx
movl $0x1f, %eax
subl %edx, %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
addl 0xc(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
shrl $0x8, %eax
movb %al, %cl
movq 0x60(%rsp), %rax
movq (%rax), %rax
movb %cl, (%rax)
movl 0xc(%rsp), %eax
andl $0xff, %eax
movb %al, %cl
movq 0x60(%rsp), %rax
movq (%rax), %rax
movb %cl, 0x1(%rax)
movq $0x0, 0x38(%rsp)
movq 0x38(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x13ec13
movq 0x28(%rsp), %rax
movq 0x38(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x60(%rsp), %rax
movq (%rax), %rax
movq 0x38(%rsp), %rcx
movb %dl, 0x2(%rax,%rcx)
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x13ebd9
movq 0x60(%rsp), %rax
movq (%rax), %rdi
movq 0x58(%rsp), %rax
movq (%rax), %rax
subq $0x4, %rax
addq %rax, %rdi
movl 0x1c(%rsp), %esi
callq 0x13ecd0
movq 0x28(%rsp), %rdi
callq 0x13e2d0
movl 0x34(%rsp), %eax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)