// File: logicx24[P]RayTracer/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)*/
  {
    blocksize = insize / 8 + 8;
    if(blocksize < 65535) blocksize = 65535;
  }

  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:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x2, (%rax)
jbe 0x1296f
movl $0x3d, -0x4(%rbp)
jmp 0x12b4e
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x12991
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x1d2d0
movl %eax, -0x4(%rbp)
jmp 0x12b4e
movq -0x28(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x129a4
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x129c8
movq -0x20(%rbp), %rax
shrq $0x3, %rax
addq $0x8, %rax
movq %rax, -0x40(%rbp)
cmpq $0xffff, -0x40(%rbp)    # imm = 0xFFFF
jae 0x129c6
movq $0xffff, -0x40(%rbp)    # imm = 0xFFFF
jmp 0x129c8
jmp 0x129ca
jmp 0x129cc
movq -0x20(%rbp), %rax
addq -0x40(%rbp), %rax
subq $0x1, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x40(%rbp)
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x129f3
movq $0x1, -0x48(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %esi
leaq -0x80(%rbp), %rdi
callq 0x1d450
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x12a17
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x12b4e
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpq -0x48(%rbp), %rcx
movb %al, -0x99(%rbp)
je 0x12a40
cmpl $0x0, -0x2c(%rbp)
setne %al
xorb $-0x1, %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x12a4f
jmp 0x12b3f
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x84(%rbp)
movq -0x38(%rbp), %rax
imulq -0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq -0x20(%rbp), %rax
jbe 0x12aa6
movq -0x20(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x12ae9
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq -0x90(%rbp), %r8
movq -0x98(%rbp), %r9
movq -0x28(%rbp), %r10
movl -0x84(%rbp), %eax
leaq -0x50(%rbp), %rsi
leaq -0x80(%rbp), %rdx
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x1d630
movl %eax, -0x2c(%rbp)
jmp 0x12b2c
movq -0x28(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x12b2a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq -0x90(%rbp), %r8
movq -0x98(%rbp), %r9
movq -0x28(%rbp), %r10
movl -0x84(%rbp), %eax
leaq -0x50(%rbp), %rsi
leaq -0x80(%rbp), %rdx
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x1d930
movl %eax, -0x2c(%rbp)
jmp 0x12b2c
jmp 0x12b2e
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x12a1f
leaq -0x80(%rbp), %rdi
callq 0x1e640
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)