// File: /workspace/github_c_makefile_star2_O2/madler[P]pigz/zopfli/src/zopfli/deflate.c
// Function: EncodeTree
static size_t EncodeTree(const unsigned* ll_lengths,
                         const unsigned* d_lengths,
                         int use_16, int use_17, int use_18,
                         unsigned char* bp,
                         unsigned char** out, size_t* outsize) {
  unsigned lld_total;  /* Total amount of literal, length, distance codes. */
  /* Runlength encoded version of lengths of litlen and dist trees. */
  unsigned* rle = 0;
  unsigned* rle_bits = 0;  /* Extra bits for rle values 16, 17 and 18. */
  size_t rle_size = 0;  /* Size of rle array. */
  size_t rle_bits_size = 0;  /* Should have same value as rle_size. */
  unsigned hlit = 29;  /* 286 - 257 */
  unsigned hdist = 29;  /* 32 - 1, but gzip does not like hdist > 29.*/
  unsigned hclen;
  unsigned hlit2;
  size_t i, j;
  size_t clcounts[19];
  unsigned clcl[19];  /* Code length code lengths. */
  unsigned clsymbols[19];
  /* The order in which code length code lengths are encoded as per deflate. */
  static const unsigned order[19] = {
    16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  };
  int size_only = !out;
  size_t result_size = 0;

  for(i = 0; i < 19; i++) clcounts[i] = 0;

  /* Trim zeros. */
  while (hlit > 0 && ll_lengths[257 + hlit - 1] == 0) hlit--;
  while (hdist > 0 && d_lengths[1 + hdist - 1] == 0) hdist--;
  hlit2 = hlit + 257;

  lld_total = hlit2 + hdist + 1;

  for (i = 0; i < lld_total; i++) {
    /* This is an encoding of a huffman tree, so now the length is a symbol */
    unsigned char symbol = i < hlit2 ? ll_lengths[i] : d_lengths[i - hlit2];
    unsigned count = 1;
    if(use_16 || (symbol == 0 && (use_17 || use_18))) {
      for (j = i + 1; j < lld_total && symbol ==
          (j < hlit2 ? ll_lengths[j] : d_lengths[j - hlit2]); j++) {
        count++;
      }
    }
    i += count - 1;

    /* Repetitions of zeroes */
    if (symbol == 0 && count >= 3) {
      if (use_18) {
        while (count >= 11) {
          unsigned count2 = count > 138 ? 138 : count;
          if (!size_only) {
            ZOPFLI_APPEND_DATA(18, &rle, &rle_size);
            ZOPFLI_APPEND_DATA(count2 - 11, &rle_bits, &rle_bits_size);
          }
          clcounts[18]++;
          count -= count2;
        }
      }
      if (use_17) {
        while (count >= 3) {
          unsigned count2 = count > 10 ? 10 : count;
          if (!size_only) {
            ZOPFLI_APPEND_DATA(17, &rle, &rle_size);
            ZOPFLI_APPEND_DATA(count2 - 3, &rle_bits, &rle_bits_size);
          }
          clcounts[17]++;
          count -= count2;
        }
      }
    }

    /* Repetitions of any symbol */
    if (use_16 && count >= 4) {
      count--;  /* Since the first one is hardcoded. */
      clcounts[symbol]++;
      if (!size_only) {
        ZOPFLI_APPEND_DATA(symbol, &rle, &rle_size);
        ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
      }
      while (count >= 3) {
        unsigned count2 = count > 6 ? 6 : count;
        if (!size_only) {
          ZOPFLI_APPEND_DATA(16, &rle, &rle_size);
          ZOPFLI_APPEND_DATA(count2 - 3, &rle_bits, &rle_bits_size);
        }
        clcounts[16]++;
        count -= count2;
      }
    }

    /* No or insufficient repetition */
    clcounts[symbol] += count;
    while (count > 0) {
      if (!size_only) {
        ZOPFLI_APPEND_DATA(symbol, &rle, &rle_size);
        ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
      }
      count--;
    }
  }

  ZopfliCalculateBitLengths(clcounts, 19, 7, clcl);
  if (!size_only) ZopfliLengthsToSymbols(clcl, 19, 7, clsymbols);

  hclen = 15;
  /* Trim zeros. */
  while (hclen > 0 && clcounts[order[hclen + 4 - 1]] == 0) hclen--;

  if (!size_only) {
    AddBits(hlit, 5, bp, out, outsize);
    AddBits(hdist, 5, bp, out, outsize);
    AddBits(hclen, 4, bp, out, outsize);

    for (i = 0; i < hclen + 4; i++) {
      AddBits(clcl[order[i]], 3, bp, out, outsize);
    }

    for (i = 0; i < rle_size; i++) {
      unsigned symbol = clsymbols[rle[i]];
      AddHuffmanBits(symbol, clcl[rle[i]], bp, out, outsize);
      /* Extra bits. */
      if (rle[i] == 16) AddBits(rle_bits[i], 2, bp, out, outsize);
      else if (rle[i] == 17) AddBits(rle_bits[i], 3, bp, out, outsize);
      else if (rle[i] == 18) AddBits(rle_bits[i], 7, bp, out, outsize);
    }
  }

  result_size += 14;  /* hlit, hdist, hclen bits */
  result_size += (hclen + 4) * 3;  /* clcl bits */
  for(i = 0; i < 19; i++) {
    result_size += clcl[i] * clcounts[i];
  }
  /* Extra bits. */
  result_size += clcounts[16] * 2;
  result_size += clcounts[17] * 3;
  result_size += clcounts[18] * 7;

  /* Note: in case of "size_only" these are null pointers so no effect. */
  free(rle);
  free(rle_bits);

  return result_size;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movl %r8d, 0x18(%rsp)
movl %ecx, 0x14(%rsp)
movl %edx, 0x1c(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x200(%rsp), %rbx
xorl %eax, %eax
cmpq $0x13, %rax
je 0xf6bf
andq $0x0, 0x80(%rsp,%rax,8)
incq %rax
jmp 0xf6ab
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x30(%rsp), %rdx
cmpq $-0x1d, %rcx
je 0xf6e2
cmpl $0x0, 0x474(%rdx,%rcx,4)
jne 0xf6dd
decq %rcx
jmp 0xf6c8
addl $0x1d, %ecx
movl %ecx, %eax
movl %eax, %edi
pushq $0x1d
popq %rsi
movq $-0x11f, %rax           # imm = 0xFEE1
movl %esi, %ecx
movq 0x38(%rsp), %r8
subl $0x1, %ecx
jb 0xf70a
movl %esi, %edx
cmpl $0x0, (%r8,%rdx,4)
jne 0xf70a
incq %rax
movl %ecx, %esi
jmp 0xf6f5
movl %esi, 0x48(%rsp)
movq %r9, 0x28(%rsp)
leal 0x101(%rdi), %r9d
movq %rdi, %r10
subq %rax, %r10
movl 0x18(%rsp), %eax
orl 0x14(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0x30(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x60(%rsp)
movq %rdi, 0x50(%rsp)
leaq (,%rdi,4), %rax
movq 0x38(%rsp), %rcx
subq %rax, %rcx
addq $-0x400, %rcx           # imm = 0xFC00
movq %rcx, 0x58(%rsp)
xorl %edi, %edi
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r8d, %r8d
xorl %r13d, %r13d
movq %r9, 0x70(%rsp)
movq %r10, 0x68(%rsp)
cmpq %r10, %rdi
jae 0xfbed
movq %rdi, %rax
subq %r9, %rax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rdi,4), %rcx
movq 0x38(%rsp), %rdx
leaq (%rdx,%rax,4), %rax
cmovbq %rcx, %rax
movzbl (%rax), %edx
cmpl $0x0, 0x1c(%rsp)
je 0xf7a5
movl %edx, %eax
jmp 0xf7bc
cmpl $0x0, 0x4c(%rsp)
je 0xfa1b
xorl %eax, %eax
pushq $0x1
popq %rcx
movl %ecx, %r11d
testl %edx, %edx
jne 0xf7f8
movq 0x60(%rsp), %rcx
leaq (%rcx,%rdi,4), %rcx
movq 0x58(%rsp), %rdx
leaq (%rdx,%rdi,4), %rdx
pushq $0x1
popq %r11
leaq (%rdi,%r11), %rsi
cmpq %r10, %rsi
jae 0xf7f6
cmpq %r9, %rsi
movq %rdx, %rsi
cmovbq %rcx, %rsi
cmpl (%rsi), %eax
jne 0xf7f6
incq %r11
addq $0x4, %rcx
addq $0x4, %rdx
jmp 0xf7d2
movl %eax, %edx
testl %edx, %edx
sete %al
cmpl $0x3, %r11d
setae %cl
andb %al, %cl
movl %edx, %r12d
cmpb $0x1, %cl
movq %rdi, 0x78(%rsp)
movq %r11, 0x40(%rsp)
movl %edx, 0x8(%rsp)
jne 0xf902
cmpl $0x0, 0x18(%rsp)
setne %al
cmpl $0xb, %r11d
setae %cl
andb %al, %cl
cmpb $0x1, %cl
movq %r12, 0x20(%rsp)
jne 0xf959
movq 0x110(%rsp), %rbx
movl %r11d, %r15d
movl $0x8a, %eax
cmpl %eax, %r15d
movl $0x8a, %r12d
cmovbl %r15d, %r12d
cmpq $0x0, 0x200(%rsp)
je 0xf8db
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0xf898
movq %r13, %rdi
movq %r8, %r13
testq %r14, %r14
je 0xf88a
leaq (,%r14,8), %rsi
callq 0x3430
jmp 0xf892
pushq $0x4
popq %rdi
callq 0x33d0
movq %r13, %r8
movq %rax, %r13
movl $0x12, (%r13,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0xf8cc
testq %rbp, %rbp
je 0xf8c1
leaq (,%rbp,8), %rsi
movq %r8, %rdi
callq 0x3430
jmp 0xf8c9
pushq $0x4
popq %rdi
callq 0x33d0
movq %rax, %r8
incq %r14
leal -0xb(%r12), %eax
movl %eax, (%r8,%rbp,4)
incq %rbp
incq %rbx
subl %r12d, %r15d
cmpl $0xa, %r15d
ja 0xf84a
movq %rbx, 0x110(%rsp)
movq 0x200(%rsp), %rbx
movq 0x20(%rsp), %r12
jmp 0xf95c
movl %r11d, %r15d
cmpl $0x0, 0x1c(%rsp)
je 0xfb3a
cmpl $0x4, %r15d
jb 0xfb3a
incq 0x80(%rsp,%r12,8)
testq %rbx, %rbx
je 0xfa73
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0xfa34
movq %r8, %rbx
testq %r14, %r14
je 0xfa26
leaq (,%r14,8), %rsi
movq %r13, %rdi
callq 0x3430
jmp 0xfa2e
movl %r11d, %r15d
cmpl $0x0, 0x14(%rsp)
je 0xf905
cmpl $0x3, %r15d
jb 0xf905
movq 0x108(%rsp), %rbx
cmpl $0xa, %r15d
pushq $0xa
popq %rax
movl %eax, %r12d
cmovbl %r15d, %r12d
cmpq $0x0, 0x200(%rsp)
je 0xf9fe
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0xf9bb
movq %r13, %rdi
movq %r8, %r13
testq %r14, %r14
je 0xf9ad
leaq (,%r14,8), %rsi
callq 0x3430
jmp 0xf9b5
pushq $0x4
popq %rdi
callq 0x33d0
movq %r13, %r8
movq %rax, %r13
movl $0x11, (%r13,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0xf9ef
testq %rbp, %rbp
je 0xf9e4
leaq (,%rbp,8), %rsi
movq %r8, %rdi
callq 0x3430
jmp 0xf9ec
pushq $0x4
popq %rdi
callq 0x33d0
movq %rax, %r8
incq %r14
leal -0x3(%r12), %eax
movl %eax, (%r8,%rbp,4)
incq %rbp
incq %rbx
subl %r12d, %r15d
cmpl $0x2, %r15d
ja 0xf971
movq %rbx, 0x108(%rsp)
jmp 0xfb2d
pushq $0x1
popq %rax
movl %eax, %r11d
jmp 0xf7f8
pushq $0x4
popq %rdi
callq 0x33d0
movq %rax, %r13
movq %rbx, %r8
movl 0x8(%rsp), %eax
movl %eax, (%r13,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0xfa68
testq %rbp, %rbp
je 0xfa5d
leaq (,%rbp,8), %rsi
movq %r8, %rdi
callq 0x3430
jmp 0xfa65
pushq $0x4
popq %rdi
callq 0x33d0
movq %rax, %r8
incq %r14
andl $0x0, (%r8,%rbp,4)
incq %rbp
movq %r12, 0x20(%rsp)
decl %r15d
movq 0x100(%rsp), %rbx
cmpl $0x3, %r15d
jb 0xfb25
cmpl $0x6, %r15d
pushq $0x6
popq %rax
movl %eax, %r12d
cmovbl %r15d, %r12d
cmpq $0x0, 0x200(%rsp)
je 0xfb1a
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0xfad7
movq %r13, %rdi
movq %r8, %r13
testq %r14, %r14
je 0xfac9
leaq (,%r14,8), %rsi
callq 0x3430
jmp 0xfad1
pushq $0x4
popq %rdi
callq 0x33d0
movq %r13, %r8
movq %rax, %r13
movl $0x10, (%r13,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0xfb0b
testq %rbp, %rbp
je 0xfb00
leaq (,%rbp,8), %rsi
movq %r8, %rdi
callq 0x3430
jmp 0xfb08
pushq $0x4
popq %rdi
callq 0x33d0
movq %rax, %r8
incq %r14
leal -0x3(%r12), %eax
movl %eax, (%r8,%rbp,4)
incq %rbp
incq %rbx
subl %r12d, %r15d
jmp 0xfa83
movq %rbx, 0x100(%rsp)
movq 0x200(%rsp), %rbx
movq 0x20(%rsp), %r12
movq 0x40(%rsp), %rax
decl %eax
movq %rax, 0x40(%rsp)
movl %r15d, %eax
addq %rax, 0x80(%rsp,%r12,8)
subl $0x1, %r15d
jb 0xfbce
testq %rbx, %rbx
je 0xfb51
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0xfb8d
movq %r8, %r12
testq %r14, %r14
je 0xfb7f
leaq (,%r14,8), %rsi
movq %r13, %rdi
callq 0x3430
jmp 0xfb87
pushq $0x4
popq %rdi
callq 0x33d0
movq %rax, %r13
movq %r12, %r8
movl 0x8(%rsp), %eax
movl %eax, (%r13,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0xfbc1
testq %rbp, %rbp
je 0xfbb6
leaq (,%rbp,8), %rsi
movq %r8, %rdi
callq 0x3430
jmp 0xfbbe
pushq $0x4
popq %rdi
callq 0x33d0
movq %rax, %r8
incq %r14
andl $0x0, (%r8,%rbp,4)
incq %rbp
jmp 0xfb51
movq 0x78(%rsp), %rdi
movq 0x40(%rsp), %rax
addq %rax, %rdi
incq %rdi
movq 0x70(%rsp), %r9
movq 0x68(%rsp), %r10
jmp 0xf772
movq %r8, 0x8(%rsp)
leaq 0x80(%rsp), %rdi
pushq $0x13
popq %rsi
pushq $0x7
popq %rdx
leaq 0x120(%rsp), %rcx
callq 0x109be
testq %rbx, %rbx
je 0xfc2d
leaq 0x120(%rsp), %rdi
pushq $0x13
popq %rsi
pushq $0x7
popq %rdx
leaq 0x170(%rsp), %rcx
callq 0x107cc
pushq $0xf
popq %rax
movq 0x28(%rsp), %r15
movq %rax, %rbx
subq $0x1, %rax
jb 0xfc56
leaq 0x63eb(%rip), %rcx      # 0x16030
movl 0x10(%rcx,%rax,4), %ecx
cmpq $0x0, 0x80(%rsp,%rcx,8)
je 0xfc35
jmp 0xfc58
xorl %ebx, %ebx
cmpq $0x0, 0x200(%rsp)
je 0xfd76
pushq $0x5
popq %rsi
movq 0x50(%rsp), %rdi
movq %r15, %rdx
movq 0x200(%rsp), %rbp
movq %rbp, %rcx
movq 0x208(%rsp), %r12
movq %r12, %r8
callq 0xfdeb
movl 0x48(%rsp), %edi
pushq $0x5
popq %rsi
movq %r15, %rdx
movq %rbp, %rcx
movq %r12, %r8
callq 0xfdeb
pushq $0x4
popq %rsi
movl %ebx, %edi
movq %r15, %rdx
movq %rbp, %rcx
movq %r12, %r8
callq 0xfdeb
movl %ebx, %eax
leaq 0x10(,%rax,4), %r12
xorl %r15d, %r15d
pushq $0x3
popq %rbp
cmpq %r15, %r12
je 0xfcfe
leaq 0x635f(%rip), %rax      # 0x16030
movl (%r15,%rax), %eax
movl 0x120(%rsp,%rax,4), %edi
movl %ebp, %esi
movq 0x28(%rsp), %rdx
movq 0x200(%rsp), %rcx
movq 0x208(%rsp), %r8
callq 0xfdeb
addq $0x4, %r15
jmp 0xfcc5
xorl %r15d, %r15d
leaq 0x6374(%rip), %r12      # 0x1607c
movq 0x28(%rsp), %rbp
cmpq %r15, %r14
je 0xfd76
movl (%r13,%r15,4), %eax
movl 0x170(%rsp,%rax,4), %edi
movl 0x120(%rsp,%rax,4), %esi
movq %rbp, %rdx
movq 0x200(%rsp), %rcx
movq 0x208(%rsp), %r8
callq 0xfe98
movl (%r13,%r15,4), %eax
addl $-0x10, %eax
cmpl $0x2, %eax
ja 0xfd71
movl %eax, %eax
movl (%r12,%rax,4), %esi
movq 0x8(%rsp), %rax
movl (%rax,%r15,4), %edi
movq %rbp, %rdx
movq 0x200(%rsp), %rcx
movq 0x208(%rsp), %r8
callq 0xfdeb
incq %r15
jmp 0xfd0d
leal (%rbx,%rbx,2), %eax
addl $0xc, %eax
addq $0xe, %rax
xorl %ecx, %ecx
cmpq $0x13, %rcx
je 0xfda0
movl 0x120(%rsp,%rcx,4), %edx
imulq 0x80(%rsp,%rcx,8), %rdx
addq %rdx, %rax
incq %rcx
jmp 0xfd82
movq 0x100(%rsp), %rcx
leaq (%rax,%rcx,2), %rax
imulq $0x3, 0x108(%rsp), %rcx
addq %rax, %rcx
imulq $0x7, 0x110(%rsp), %rbx
addq %rcx, %rbx
movq %r13, %rdi
callq 0x3050
movq 0x8(%rsp), %rdi
callq 0x3050
movq %rbx, %rax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq