// File: mhe[P]raytracerframework_cpp/lodepng.cpp
// Function: LodeZlib_decompress(unsigned char**, unsigned long*, unsigned char const*, unsigned long, LodeZlib_DecompressSettings const*)
unsigned LodeZlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize, const LodeZlib_DecompressSettings* settings)
{
  unsigned error = 0;
  unsigned CM, CINFO, FDICT;
  ucvector outv;
  
  if(insize < 2) { error = 53; return error; } /*error, size of zlib data too small*/
  /*read information from zlib header*/
  if((in[0] * 256 + in[1]) % 31 != 0) { error = 24; return error; } /*error: 256 * in[0] + in[1] must be a multiple of 31, the FCHECK value is supposed to be made that way*/

  CM = in[0] & 15;
  CINFO = (in[0] >> 4) & 15;
  /*FCHECK = in[1] & 31; //FCHECK is already tested above*/
  FDICT = (in[1] >> 5) & 1;
  /*FLEVEL = (in[1] >> 6) & 3; //not really important, all it does it to give a compiler warning about unused variable, we don't care what encoding setting the encoder used*/
  
  if(CM != 8 || CINFO > 7) { error = 25; return error; } /*error: only compression method 8: inflate with sliding window of 32k is supported by the PNG spec*/
  if(FDICT != 0) { error = 26; return error; } /*error: the specification of PNG says about the zlib stream: "The additional flags shall not specify a preset dictionary."*/
  
  ucvector_init_buffer(&outv, *out, *outsize); /*ucvector-controlled version of the output buffer, for dynamic array*/
  error = LodeFlate_inflate(&outv, in, insize, 2);
  *out = outv.data;
  *outsize = outv.size;
  if(error) return error;
  
  if(!settings->ignoreAdler32)
  {
    unsigned ADLER32 = LodeZlib_read32bitInt(&in[insize - 4]);
    unsigned checksum = adler32(outv.data, (unsigned)outv.size);
    if(checksum != ADLER32) { error = 58; return error; }
  }
  
  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
cmpq $0x2, -0x28(%rbp)
jae 0x2564c
movl $0x35, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x25788
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq -0x20(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
addl %ecx, %eax
movl $0x1f, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x2567f
movl $0x18, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x25788
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
andl $0xf, %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
sarl $0x4, %eax
andl $0xf, %eax
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %eax
sarl $0x5, %eax
andl $0x1, %eax
movl %eax, -0x40(%rbp)
cmpl $0x8, -0x38(%rbp)
jne 0x256b9
cmpl $0x7, -0x3c(%rbp)
jbe 0x256cb
movl $0x19, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x25788
cmpl $0x0, -0x40(%rbp)
je 0x256e3
movl $0x1a, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x25788
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
leaq -0x58(%rbp), %rdi
callq 0x257a0
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x58(%rbp), %rdi
movl $0x2, %ecx
callq 0x23ce0
movl %eax, -0x34(%rbp)
movq -0x58(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
cmpl $0x0, -0x34(%rbp)
je 0x25737
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x25788
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x25782
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rax
subq $0x4, %rax
addq %rax, %rdi
callq 0x255d0
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %esi
callq 0x257e0
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x5c(%rbp), %eax
je 0x25780
movl $0x3a, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x25788
jmp 0x25782
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)