// File: phillip-h[P]fractal-gen/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)
{
  /*initially, *out must be NULL and outsize 0, if you just give some random *out
  that's pointing to a non allocated buffer, this'll crash*/
  ucvector outv;
  size_t i;
  unsigned error;
  unsigned char* deflatedata = 0;
  size_t deflatesize = 0;

  /*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;

  /*ucvector-controlled version of the output buffer, for dynamic array*/
  ucvector_init_buffer(&outv, *out, *outsize);

  ucvector_push_back(&outv, (unsigned char)(CMFFLG >> 8));
  ucvector_push_back(&outv, (unsigned char)(CMFFLG & 255));

  error = deflate(&deflatedata, &deflatesize, in, insize, settings);

  if(!error)
  {
    unsigned ADLER32 = adler32(in, (unsigned)insize);
    for(i = 0; i != deflatesize; ++i) ucvector_push_back(&outv, deflatedata[i]);
    lodepng_free(deflatedata);
    lodepng_add32bitInt(&outv, ADLER32);
  }

  *out = outv.data;
  *outsize = outv.size;

  return error;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x78, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl 0x14(%rsp), %eax
shll $0x8, %eax
movl 0xc(%rsp), %ecx
shll $0x5, %ecx
addl %ecx, %eax
movl 0x10(%rsp), %ecx
shll $0x6, %ecx
addl %ecx, %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl $0x1f, %ecx
xorl %edx, %edx
divl %ecx
movl $0x1f, %eax
subl %edx, %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
addl 0x8(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x70(%rsp), %rax
movq (%rax), %rsi
movq 0x68(%rsp), %rax
movq (%rax), %rdx
leaq 0x38(%rsp), %rdi
callq 0x7940
movl 0x8(%rsp), %eax
shrl $0x8, %eax
leaq 0x38(%rsp), %rdi
movzbl %al, %esi
callq 0x81d0
movl 0x8(%rsp), %eax
andl $0xff, %eax
leaq 0x38(%rsp), %rdi
movzbl %al, %esi
callq 0x81d0
movq 0x60(%rsp), %rdx
movq 0x58(%rsp), %rcx
movq 0x50(%rsp), %r8
leaq 0x20(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x8240
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jne 0x81ad
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rax
movl %eax, %esi
callq 0x8020
movl %eax, (%rsp)
movq $0x0, 0x30(%rsp)
movq 0x30(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x8196
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rcx
leaq 0x38(%rsp), %rdi
movzbl (%rax,%rcx), %esi
callq 0x81d0
movq 0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x30(%rsp)
jmp 0x8162
movq 0x20(%rsp), %rdi
callq 0x78b0
movl (%rsp), %esi
leaq 0x38(%rsp), %rdi
callq 0x82c0
movq 0x38(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, (%rax)
movq 0x40(%rsp), %rcx
movq 0x68(%rsp), %rax
movq %rcx, (%rax)
movl 0x2c(%rsp), %eax
addq $0x78, %rsp
retq