// File: mhe[P]raytracerframework_cpp/lodepng.cpp
// Function: LodeZlib_compress(unsigned char**, unsigned long*, unsigned char const*, unsigned long, LodeZlib_DeflateSettings const*)
unsigned LodeZlib_compress(unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize, const LodeZlib_DeflateSettings* 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 deflatedata, outv;
  size_t i;
  unsigned error;
  
  unsigned ADLER32;
  /*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_init_buffer(&outv, *out, *outsize); /*ucvector-controlled version of the output buffer, for dynamic array*/
  
  ucvector_push_back(&outv, (unsigned char)(CMFFLG / 256));
  ucvector_push_back(&outv, (unsigned char)(CMFFLG % 256));
  
  ucvector_init(&deflatedata);
  error = LodeFlate_deflate(&deflatedata, in, insize, settings);
  
  if(!error)
  {
    ADLER32 = adler32(in, (unsigned)insize);
    for(i = 0; i < deflatedata.size; i++) ucvector_push_back(&outv, deflatedata.data[i]);
    ucvector_cleanup(&deflatedata);
    LodeZlib_add32bitInt(&outv, ADLER32);
  }
  
  *out = outv.data;
  *outsize = outv.size;
  
  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl $0x78, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x6c(%rbp), %eax
shll $0x8, %eax
movl -0x74(%rbp), %ecx
shll $0x5, %ecx
addl %ecx, %eax
movl -0x70(%rbp), %ecx
shll $0x6, %ecx
addl %ecx, %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
movl $0x1f, %ecx
xorl %edx, %edx
divl %ecx
movl $0x1f, %eax
subl %edx, %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
addl -0x78(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq -0x58(%rbp), %rdi
callq 0x257a0
movl -0x78(%rbp), %eax
shrl $0x8, %eax
leaq -0x58(%rbp), %rdi
movzbl %al, %esi
callq 0x25570
movl -0x78(%rbp), %eax
andl $0xff, %eax
leaq -0x58(%rbp), %rdi
movzbl %al, %esi
callq 0x25570
leaq -0x40(%rbp), %rdi
callq 0x25960
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x40(%rbp), %rdi
callq 0x24510
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jne 0x2593c
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %esi
callq 0x257e0
movl %eax, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x25927
movq -0x40(%rbp), %rax
movq -0x60(%rbp), %rcx
leaq -0x58(%rbp), %rdi
movzbl (%rax,%rcx), %esi
callq 0x25570
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x258fa
leaq -0x40(%rbp), %rdi
callq 0x25990
movl -0x68(%rbp), %esi
leaq -0x58(%rbp), %rdi
callq 0x25500
movq -0x58(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0x64(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nop