// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: lodepng_deflatev(ucvector*, unsigned char const*, unsigned long, LodePNGCompressSettings const*)
static unsigned lodepng_deflatev(ucvector* out, const unsigned char* in, size_t insize,
                                 const LodePNGCompressSettings* settings)
{
  unsigned error = 0;
  size_t i, blocksize, numdeflateblocks;
  size_t bp = 0; /*the bit pointer*/
  Hash hash;

  if(settings->btype > 2) return 61;
  else if(settings->btype == 0) return deflateNoCompression(out, in, insize);
  else if(settings->btype == 1) blocksize = insize;
  else /*if(settings->btype == 2)*/
  {
    /*on PNGs, deflate blocks of 65-262k seem to give most dense encoding*/
    blocksize = insize / 8 + 8;
    if(blocksize < 65536) blocksize = 65536;
    if(blocksize > 262144) blocksize = 262144;
  }

  numdeflateblocks = (insize + blocksize - 1) / blocksize;
  if(numdeflateblocks == 0) numdeflateblocks = 1;

  error = hash_init(&hash, settings->windowsize);
  if(error) return error;

  for(i = 0; i != numdeflateblocks && !error; ++i)
  {
    unsigned final = (i == numdeflateblocks - 1);
    size_t start = i * blocksize;
    size_t end = start + blocksize;
    if(end > insize) end = insize;

    if(settings->btype == 1) error = deflateFixed(out, &bp, &hash, in, start, end, settings, final);
    else if(settings->btype == 2) error = deflateDynamic(out, &bp, &hash, in, start, end, settings, final);
  }

  hash_cleanup(&hash);

  return error;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movl $0x0, 0x8c(%rsp)
movq $0x0, 0x68(%rsp)
movq 0x90(%rsp), %rax
cmpl $0x2, (%rax)
jbe 0x7ba8
movl $0x3d, 0xb4(%rsp)
jmp 0x7e1c
movq 0x90(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x7bde
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x135e0
movl %eax, 0xb4(%rsp)
jmp 0x7e1c
movq 0x90(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x7bfa
movq 0x98(%rsp), %rax
movq %rax, 0x78(%rsp)
jmp 0x7c39
movq 0x98(%rsp), %rax
shrq $0x3, %rax
addq $0x8, %rax
movq %rax, 0x78(%rsp)
cmpq $0x10000, 0x78(%rsp)    # imm = 0x10000
jae 0x7c23
movq $0x10000, 0x78(%rsp)    # imm = 0x10000
cmpq $0x40000, 0x78(%rsp)    # imm = 0x40000
jbe 0x7c37
movq $0x40000, 0x78(%rsp)    # imm = 0x40000
jmp 0x7c39
jmp 0x7c3b
jmp 0x7c3d
movq 0x98(%rsp), %rax
addq 0x78(%rsp), %rax
subq $0x1, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x78(%rsp)
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x7c6d
movq $0x1, 0x70(%rsp)
movq 0x90(%rsp), %rax
movl 0x8(%rax), %esi
leaq 0x38(%rsp), %rdi
callq 0x13790
movl %eax, 0x8c(%rsp)
cmpl $0x0, 0x8c(%rsp)
je 0x7ca6
movl 0x8c(%rsp), %eax
movl %eax, 0xb4(%rsp)
jmp 0x7e1c
movq $0x0, 0x80(%rsp)
movq 0x80(%rsp), %rcx
xorl %eax, %eax
cmpq 0x70(%rsp), %rcx
movb %al, 0x1f(%rsp)
je 0x7cd8
cmpl $0x0, 0x8c(%rsp)
setne %al
xorb $-0x1, %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x7ce5
jmp 0x7e04
movq 0x80(%rsp), %rax
movq 0x70(%rsp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x34(%rsp)
movq 0x80(%rsp), %rax
imulq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
addq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq 0x98(%rsp), %rax
jbe 0x7d43
movq 0x98(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x90(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x7d96
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rcx
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
movq 0x90(%rsp), %r10
movl 0x34(%rsp), %eax
leaq 0x68(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x13990
movl %eax, 0x8c(%rsp)
jmp 0x7de9
movq 0x90(%rsp), %rax
cmpl $0x2, (%rax)
jne 0x7de7
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rcx
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
movq 0x90(%rsp), %r10
movl 0x34(%rsp), %eax
leaq 0x68(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x13d60
movl %eax, 0x8c(%rsp)
jmp 0x7de9
jmp 0x7deb
movq 0x80(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x80(%rsp)
jmp 0x7cb2
leaq 0x38(%rsp), %rdi
callq 0x14b40
movl 0x8c(%rsp), %eax
movl %eax, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopl (%rax,%rax)