// File: Lut99[P]RayTracer/src/lib/LodePNG.cpp
// Function: deflateFixed(LodePNGBitWriter*, Hash*, unsigned char const*, unsigned long, unsigned long, LodePNGCompressSettings const*, unsigned int)
static unsigned deflateFixed(LodePNGBitWriter* writer, Hash* hash,
                             const unsigned char* data,
                             size_t datapos, size_t dataend,
                             const LodePNGCompressSettings* settings, unsigned final) {
  HuffmanTree tree_ll; /*tree for literal values and length codes*/
  HuffmanTree tree_d; /*tree for distance codes*/

  unsigned BFINAL = final;
  unsigned error = 0;
  size_t i;

  HuffmanTree_init(&tree_ll);
  HuffmanTree_init(&tree_d);

  generateFixedLitLenTree(&tree_ll);
  generateFixedDistanceTree(&tree_d);

  writeBits(writer, BFINAL, 1);
  writeBits(writer, 1, 1); /*first bit of BTYPE*/
  writeBits(writer, 0, 1); /*second bit of BTYPE*/

  if(settings->use_lz77) /*LZ77 encoded*/ {
    uivector lz77_encoded;
    uivector_init(&lz77_encoded);
    error = encodeLZ77(&lz77_encoded, hash, data, datapos, dataend, settings->windowsize,
                       settings->minmatch, settings->nicematch, settings->lazymatching);
    if(!error) writeLZ77data(writer, &lz77_encoded, &tree_ll, &tree_d);
    uivector_cleanup(&lz77_encoded);
  } else /*no LZ77, but still will be Huffman compressed*/ {
    for(i = datapos; i < dataend; ++i) {
      writeBitsReversed(writer, HuffmanTree_getCode(&tree_ll, data[i]), HuffmanTree_getLength(&tree_ll, data[i]));
    }
  }
  /*add END code*/
  if(!error) writeBitsReversed(writer, HuffmanTree_getCode(&tree_ll, 256), HuffmanTree_getLength(&tree_ll, 256));

  /*cleanup*/
  HuffmanTree_cleanup(&tree_ll);
  HuffmanTree_cleanup(&tree_d);

  return error;
}

// Assembly:
subq $0xe8, %rsp
movl 0xf0(%rsp), %eax
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq %rdx, 0xd0(%rsp)
movq %rcx, 0xc8(%rsp)
movq %r8, 0xc0(%rsp)
movq %r9, 0xb8(%rsp)
movl 0xf0(%rsp), %eax
movl %eax, 0x64(%rsp)
movl $0x0, 0x60(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x14bf60
leaq 0x68(%rsp), %rdi
callq 0x14bf60
leaq 0x90(%rsp), %rdi
callq 0x14c710
leaq 0x68(%rsp), %rdi
callq 0x14c840
movq 0xe0(%rsp), %rdi
movl 0x64(%rsp), %esi
movl $0x1, %edx
callq 0x14e6e0
movq 0xe0(%rsp), %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x14e6e0
movq 0xe0(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x14e6e0
movq 0xb8(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x14d8ab
leaq 0x40(%rsp), %rdi
callq 0x14e820
movq 0xd8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
movq 0xc8(%rsp), %rcx
movq 0xc0(%rsp), %r8
movq 0xb8(%rsp), %rax
movl 0x8(%rax), %r9d
movq 0xb8(%rsp), %rax
movl 0xc(%rax), %r11d
movq 0xb8(%rsp), %rax
movl 0x10(%rax), %r10d
movq 0xb8(%rsp), %rax
movl 0x14(%rax), %eax
leaq 0x40(%rsp), %rdi
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x14e850
movl %eax, 0x60(%rsp)
cmpl $0x0, 0x60(%rsp)
jne 0x14d89c
movq 0xe0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x90(%rsp), %rdx
leaq 0x68(%rsp), %rcx
callq 0x14f140
leaq 0x40(%rsp), %rdi
callq 0x14f300
jmp 0x14d938
movq 0xc8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rax
cmpq 0xc0(%rsp), %rax
jae 0x14d936
movq 0xe0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xd0(%rsp), %rax
movq 0x58(%rsp), %rcx
movzbl (%rax,%rcx), %esi
leaq 0x90(%rsp), %rdi
callq 0x14f410
movl %eax, 0x3c(%rsp)
movq 0xd0(%rsp), %rax
movq 0x58(%rsp), %rcx
movzbl (%rax,%rcx), %esi
leaq 0x90(%rsp), %rdi
callq 0x14f430
movq 0x30(%rsp), %rdi
movl 0x3c(%rsp), %esi
movl %eax, %eax
movl %eax, %edx
callq 0x14f340
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
jmp 0x14d8b8
jmp 0x14d938
cmpl $0x0, 0x60(%rsp)
jne 0x14d986
movq 0xe0(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x90(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x14f410
movl %eax, 0x2c(%rsp)
leaq 0x90(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x14f430
movq 0x20(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl %eax, %eax
movl %eax, %edx
callq 0x14f340
leaq 0x90(%rsp), %rdi
callq 0x14c6d0
leaq 0x68(%rsp), %rdi
callq 0x14c6d0
movl 0x60(%rsp), %eax
addq $0xe8, %rsp
retq
nopl (%rax)