// File: drummyfish[P]flowchart/src/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;

  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-controlled version of the output buffer, for dynamic array*/
  ucvector_init_buffer(&outv, *out, *outsize);

  ucvector_push_back(&outv, (unsigned char)(CMFFLG / 256));
  ucvector_push_back(&outv, (unsigned char)(CMFFLG % 256));

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

  if(!error)
  {
    ADLER32 = adler32(in, (unsigned)insize);
    for(i = 0; i < deflatesize; i++) ucvector_push_back(&outv, deflatedata[i]);
    free(deflatedata);
    lodepng_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)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x78, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl -0x68(%rbp), %eax
shll $0x8, %eax
movl -0x70(%rbp), %ecx
shll $0x5, %ecx
addl %ecx, %eax
movl -0x6c(%rbp), %ecx
shll $0x6, %ecx
addl %ecx, %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl $0x1f, %ecx
xorl %edx, %edx
divl %ecx
movl $0x1f, %eax
subl %edx, %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
addl -0x74(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq -0x40(%rbp), %rdi
callq 0x40e0
movl -0x74(%rbp), %eax
shrl $0x8, %eax
leaq -0x40(%rbp), %rdi
movzbl %al, %esi
callq 0x4840
movl -0x74(%rbp), %eax
andl $0xff, %eax
leaq -0x40(%rbp), %rdi
movzbl %al, %esi
callq 0x4840
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x48a0
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x4817
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %esi
callq 0x46b0
movl %eax, -0x64(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x4802
movq -0x58(%rbp), %rax
movq -0x48(%rbp), %rcx
leaq -0x40(%rbp), %rdi
movzbl (%rax,%rcx), %esi
callq 0x4840
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x47d5
movq -0x58(%rbp), %rdi
callq 0x32e0
movl -0x64(%rbp), %esi
leaq -0x40(%rbp), %rdi
callq 0x4910
movq -0x40(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0x4c(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)