// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: writeLZ77data(unsigned long*, ucvector*, uivector const*, HuffmanTree const*, HuffmanTree const*)
static void writeLZ77data(size_t* bp, ucvector* out, 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];
    addHuffmanSymbol(bp, out, 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];

      addBitsToStream(bp, out, length_extra_bits, n_length_extra_bits);
      addHuffmanSymbol(bp, out, HuffmanTree_getCode(tree_d, distance_code),
                       HuffmanTree_getLength(tree_d, distance_code));
      addBitsToStream(bp, out, distance_extra_bits, n_distance_extra_bits);
    }
  }
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %r8, 0x60(%rsp)
movq $0x0, 0x58(%rsp)
movq $0x0, 0x58(%rsp)
movq 0x58(%rsp), %rax
movq 0x70(%rsp), %rcx
cmpq 0x8(%rcx), %rax
je 0x15680
movq 0x70(%rsp), %rax
movq (%rax), %rax
movq 0x58(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x54(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x68(%rsp), %rdi
movl 0x54(%rsp), %esi
callq 0x156d0
movl %eax, 0x34(%rsp)
movq 0x68(%rsp), %rdi
movl 0x54(%rsp), %esi
callq 0x156f0
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl 0x34(%rsp), %edx
movl %eax, %ecx
callq 0x15690
cmpl $0x100, 0x54(%rsp)      # imm = 0x100
jbe 0x1566b
movl 0x54(%rsp), %eax
subl $0x101, %eax            # imm = 0x101
movl %eax, 0x50(%rsp)
movl 0x50(%rsp), %eax
movl %eax, %ecx
leaq 0x709f(%rip), %rax      # 0x1c600
movl (%rax,%rcx,4), %eax
movl %eax, 0x4c(%rsp)
movq 0x70(%rsp), %rax
movq (%rax), %rax
movq 0x58(%rsp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x58(%rsp)
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0x48(%rsp)
movq 0x70(%rsp), %rax
movq (%rax), %rax
movq 0x58(%rsp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x58(%rsp)
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %eax
movl %eax, %ecx
leaq 0x7141(%rip), %rax      # 0x1c700
movl (%rax,%rcx,4), %eax
movl %eax, 0x3c(%rsp)
movq 0x70(%rsp), %rax
movq (%rax), %rax
movq 0x58(%rsp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, 0x58(%rsp)
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0x38(%rsp)
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %rsi
movl 0x48(%rsp), %edx
movl 0x4c(%rsp), %eax
movl %eax, %ecx
callq 0x15b80
movq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x60(%rsp), %rdi
movl 0x44(%rsp), %esi
callq 0x156d0
movl %eax, 0x1c(%rsp)
movq 0x60(%rsp), %rdi
movl 0x44(%rsp), %esi
callq 0x156f0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl 0x1c(%rsp), %edx
movl %eax, %ecx
callq 0x15690
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %rsi
movl 0x38(%rsp), %edx
movl 0x3c(%rsp), %eax
movl %eax, %ecx
callq 0x15b80
jmp 0x1566d
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
jmp 0x154c5
addq $0x88, %rsp
retq
nopl (%rax,%rax)