// File: Lut99[P]RayTracer/src/lib/LodePNG.cpp
// Function: writeLZ77data(LodePNGBitWriter*, uivector const*, HuffmanTree const*, HuffmanTree const*)
static void writeLZ77data(LodePNGBitWriter* writer, const uivector* lz77_encoded,
                          const HuffmanTree* tree_ll, const HuffmanTree* tree_d) {
  size_t i = 0;
  for(i = 0; i != lz77_encoded->size; ++i) {
    unsigned val = lz77_encoded->data[i];
    writeBitsReversed(writer, HuffmanTree_getCode(tree_ll, val), HuffmanTree_getLength(tree_ll, val));
    if(val > 256) /*for a length code, 3 more things have to be added*/ {
      unsigned length_index = val - FIRST_LENGTH_CODE_INDEX;
      unsigned n_length_extra_bits = LENGTHEXTRA[length_index];
      unsigned length_extra_bits = lz77_encoded->data[++i];

      unsigned distance_code = lz77_encoded->data[++i];

      unsigned distance_index = distance_code;
      unsigned n_distance_extra_bits = DISTANCEEXTRA[distance_index];
      unsigned distance_extra_bits = lz77_encoded->data[++i];

      writeBits(writer, length_extra_bits, n_length_extra_bits);
      writeBitsReversed(writer, HuffmanTree_getCode(tree_d, distance_code),
                       HuffmanTree_getLength(tree_d, distance_code));
      writeBits(writer, distance_extra_bits, n_distance_extra_bits);
    }
  }
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq 0x58(%rsp), %rcx
cmpq 0x8(%rcx), %rax
je 0x14f2f3
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x40(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x3c(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x50(%rsp), %rdi
movl 0x3c(%rsp), %esi
callq 0x14f410
movl %eax, 0x1c(%rsp)
movq 0x50(%rsp), %rdi
movl 0x3c(%rsp), %esi
callq 0x14f430
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %esi
movl %eax, %eax
movl %eax, %edx
callq 0x14f340
cmpl $0x100, 0x3c(%rsp)      # imm = 0x100
jbe 0x14f2de
movl 0x3c(%rsp), %eax
subl $0x101, %eax            # imm = 0x101
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
movl %eax, %ecx
leaq 0x2727a(%rip), %rax     # 0x176470
movl (%rax,%rcx,4), %eax
movl %eax, 0x34(%rsp)
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x40(%rsp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0x30(%rsp)
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x40(%rsp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, %ecx
leaq 0x2731c(%rip), %rax     # 0x176570
movl (%rax,%rcx,4), %eax
movl %eax, 0x24(%rsp)
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x40(%rsp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0x20(%rsp)
movq 0x60(%rsp), %rdi
movl 0x30(%rsp), %esi
movl 0x34(%rsp), %eax
movl %eax, %edx
callq 0x14e6e0
movq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq 0x48(%rsp), %rdi
movl 0x2c(%rsp), %esi
callq 0x14f410
movl %eax, 0xc(%rsp)
movq 0x48(%rsp), %rdi
movl 0x2c(%rsp), %esi
callq 0x14f430
movq (%rsp), %rdi
movl 0xc(%rsp), %esi
movl %eax, %eax
movl %eax, %edx
callq 0x14f340
movq 0x60(%rsp), %rdi
movl 0x20(%rsp), %esi
movl 0x24(%rsp), %eax
movl %eax, %edx
callq 0x14e6e0
jmp 0x14f2e0
movq 0x40(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x40(%rsp)
jmp 0x14f16a
addq $0x68, %rsp
retq
nopl (%rax,%rax)