// File: Lut99[P]RayTracer/src/lib/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;
  Hash hash;
  LodePNGBitWriter writer;

  LodePNGBitWriter_init(&writer, out);

  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 / 8u + 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(&writer, &hash, in, start, end, settings, final);
    else if(settings->btype == 2) error = deflateDynamic(&writer, &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 0xa8(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x13cf90
movq 0x90(%rsp), %rax
cmpl $0x2, (%rax)
jbe 0x13e5b1
movl $0x3d, 0xb4(%rsp)
jmp 0x13e80b
movq 0x90(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x13e5e7
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x14d380
movl %eax, 0xb4(%rsp)
jmp 0x13e80b
movq 0x90(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x13e603
movq 0x98(%rsp), %rax
movq %rax, 0x78(%rsp)
jmp 0x13e642
movq 0x98(%rsp), %rax
shrq $0x3, %rax
addq $0x8, %rax
movq %rax, 0x78(%rsp)
cmpq $0x10000, 0x78(%rsp)    # imm = 0x10000
jae 0x13e62c
movq $0x10000, 0x78(%rsp)    # imm = 0x10000
cmpq $0x40000, 0x78(%rsp)    # imm = 0x40000
jbe 0x13e640
movq $0x40000, 0x78(%rsp)    # imm = 0x40000
jmp 0x13e642
jmp 0x13e644
jmp 0x13e646
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 0x13e676
movq $0x1, 0x70(%rsp)
movq 0x90(%rsp), %rax
movl 0x8(%rax), %esi
leaq 0x40(%rsp), %rdi
callq 0x14d530
movl %eax, 0x8c(%rsp)
cmpl $0x0, 0x8c(%rsp)
je 0x13e6af
movl 0x8c(%rsp), %eax
movl %eax, 0xb4(%rsp)
jmp 0x13e80b
movq $0x0, 0x80(%rsp)
movq 0x80(%rsp), %rcx
xorl %eax, %eax
cmpq 0x70(%rsp), %rcx
movb %al, 0x17(%rsp)
je 0x13e6e1
cmpl $0x0, 0x8c(%rsp)
setne %al
xorb $-0x1, %al
movb %al, 0x17(%rsp)
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x13e6ee
jmp 0x13e7f3
movq 0x80(%rsp), %rax
movq 0x70(%rsp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x2c(%rsp)
movq 0x80(%rsp), %rax
imulq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
addq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
cmpq 0x98(%rsp), %rax
jbe 0x13e74c
movq 0x98(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x90(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x13e792
movq 0xa0(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
movq 0x90(%rsp), %r9
movl 0x2c(%rsp), %eax
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movl %eax, (%rsp)
callq 0x14d730
movl %eax, 0x8c(%rsp)
jmp 0x13e7d8
movq 0x90(%rsp), %rax
cmpl $0x2, (%rax)
jne 0x13e7d6
movq 0xa0(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
movq 0x90(%rsp), %r9
movl 0x2c(%rsp), %eax
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movl %eax, (%rsp)
callq 0x14d9b0
movl %eax, 0x8c(%rsp)
jmp 0x13e7d8
jmp 0x13e7da
movq 0x80(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x80(%rsp)
jmp 0x13e6bb
leaq 0x40(%rsp), %rdi
callq 0x14e610
movl 0x8c(%rsp), %eax
movl %eax, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopw (%rax,%rax)