// File: /workspace/github_c_makefile_star2_O3/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
movq %r9, %rbx
movl %r8d, 0x18(%rsp)
movl %ecx, 0x14(%rsp)
movl %edx, 0x1c(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x200(%rsp), %r13
leaq 0x80(%rsp), %rdi
xorl %r14d, %r14d
movl $0x98, %edx
xorl %esi, %esi
callq 0x3260
movq 0x20(%rsp), %rax
cmpl $0x0, 0x474(%rax,%r14,4)
jne 0x1044c
decq %r14
cmpq $-0x1d, %r14
jne 0x10433
xorl %r14d, %r14d
jmp 0x10450
addl $0x1d, %r14d
movl $0x1d, %ecx
movq 0x28(%rsp), %rdx
movl %ecx, %eax
cmpl $0x0, (%rdx,%rax,4)
jne 0x10468
decl %ecx
jne 0x1045a
xorl %ecx, %ecx
movq %rbx, 0x50(%rsp)
leal (%r14,%rcx), %esi
addl $0x102, %esi            # imm = 0x102
testl %esi, %esi
movq %r14, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
je 0x109a5
leal 0x101(%r14), %r8d
movl 0x18(%rsp), %eax
orl 0x14(%rsp), %eax
movl %eax, 0x30(%rsp)
leal (%rcx,%r14), %eax
addl $0x102, %eax            # imm = 0x102
movl %eax, 0x34(%rsp)
movl %r14d, %eax
shlq $0x2, %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
addq $-0x400, %rcx           # imm = 0xFC00
movq %rcx, 0x60(%rsp)
movq 0x20(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x58(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r9d, %r9d
movq %rsi, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movq %r9, %rax
subq %r8, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%r9,4), %rcx
movq 0x28(%rsp), %rdx
leaq (%rdx,%rax,4), %rax
cmovbq %rcx, %rax
movzbl (%rax), %eax
cmpl $0x0, 0x1c(%rsp)
movq %r9, 0x38(%rsp)
je 0x1051a
movl %eax, 0x4(%rsp)
jmp 0x1052d
cmpl $0x0, 0x30(%rsp)
je 0x1058b
movl $0x0, 0x4(%rsp)
testl %eax, %eax
jne 0x1058b
leaq 0x1(%r9), %rax
cmpq %rsi, %rax
jae 0x10587
movl 0x34(%rsp), %eax
movl %eax, %r11d
subl %r9d, %r11d
movq %rsi, %rax
subq %r9, %rax
movq 0x60(%rsp), %rcx
leaq (%rcx,%r9,4), %rdx
movq 0x58(%rsp), %rcx
leaq (%rcx,%r9,4), %rsi
movl $0x1, %ecx
leaq (%r9,%rcx), %rdi
cmpq %r8, %rdi
movq %rdx, %rdi
cmovbq %rsi, %rdi
movl 0x4(%rsp), %r10d
cmpl (%rdi), %r10d
jne 0x105a4
incq %rcx
addq $0x4, %rdx
addq $0x4, %rsi
cmpq %rcx, %rax
jne 0x1055d
jmp 0x105a7
movl 0x4(%rsp), %eax
movl %eax, %ecx
incq 0x80(%rsp,%rcx,8)
movl $0x1, %r15d
movl %eax, 0x4(%rsp)
jmp 0x1090f
movl %ecx, %r11d
cmpl $0x0, 0x4(%rsp)
movq %r11, 0x68(%rsp)
jne 0x106a4
cmpl $0x3, %r11d
jb 0x106a4
cmpl $0x0, 0x18(%rsp)
je 0x106fc
cmpl $0xb, %r11d
jb 0x106fc
movq %rbx, 0x8(%rsp)
movq 0x110(%rsp), %rbx
movl %r11d, %r15d
movl $0x8a, %eax
cmpl %eax, %r15d
movl $0x8a, %r13d
cmovbl %r15d, %r13d
cmpq $0x0, 0x200(%rsp)
je 0x1067d
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x10630
testq %r14, %r14
je 0x10623
leaq (,%r14,8), %rsi
movq %r12, %rdi
callq 0x3440
jmp 0x1062d
movl $0x4, %edi
callq 0x33e0
movq %rax, %r12
movl $0x12, (%r12,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x1065a
testq %rbp, %rbp
je 0x10661
leaq (,%rbp,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x3440
jmp 0x1066b
movq 0x8(%rsp), %rax
jmp 0x1066b
movl $0x4, %edi
callq 0x33e0
incq %r14
leal -0xb(%r13), %ecx
movq %rax, 0x8(%rsp)
movl %ecx, (%rax,%rbp,4)
incq %rbp
incq %rbx
subl %r13d, %r15d
cmpl $0xa, %r15d
ja 0x105e6
movq %rbx, 0x110(%rsp)
movq 0x200(%rsp), %r13
movq 0x8(%rsp), %rbx
jmp 0x106ff
movl %r11d, %r15d
cmpl $0x0, 0x1c(%rsp)
je 0x108ef
cmpl $0x4, %r15d
jb 0x108ef
movl 0x4(%rsp), %eax
incq 0x80(%rsp,%rax,8)
testq %r13, %r13
je 0x107d1
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x107e5
testq %r14, %r14
je 0x107d8
leaq (,%r14,8), %rsi
movq %r12, %rdi
callq 0x3440
jmp 0x107e2
movl %r11d, %r15d
cmpl $0x0, 0x14(%rsp)
je 0x106a7
cmpl $0x3, %r15d
jb 0x106a7
movq %rbx, 0x8(%rsp)
movq 0x108(%rsp), %rbx
cmpl $0xa, %r15d
movl $0xa, %r13d
cmovbl %r15d, %r13d
cmpq $0x0, 0x200(%rsp)
je 0x107ac
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x1075f
testq %r14, %r14
je 0x10752
leaq (,%r14,8), %rsi
movq %r12, %rdi
callq 0x3440
jmp 0x1075c
movl $0x4, %edi
callq 0x33e0
movq %rax, %r12
movl $0x11, (%r12,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x10789
testq %rbp, %rbp
je 0x10790
leaq (,%rbp,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x3440
jmp 0x1079a
movq 0x8(%rsp), %rax
jmp 0x1079a
movl $0x4, %edi
callq 0x33e0
incq %r14
leal -0x3(%r13), %ecx
movq %rax, 0x8(%rsp)
movl %ecx, (%rax,%rbp,4)
incq %rbp
incq %rbx
subl %r13d, %r15d
cmpl $0x2, %r15d
ja 0x10719
movq %rbx, 0x108(%rsp)
movl $0x0, 0x4(%rsp)
jmp 0x108e2
movq %rbx, 0x8(%rsp)
jmp 0x1082c
movl $0x4, %edi
callq 0x33e0
movq %rax, %r12
movl 0x4(%rsp), %eax
movl %eax, (%r12,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x1081a
testq %rbp, %rbp
je 0x1080d
leaq (,%rbp,8), %rsi
movq %rbx, %rdi
callq 0x3440
jmp 0x10817
movl $0x4, %edi
callq 0x33e0
movq %rax, %rbx
incq %r14
movq %rbx, 0x8(%rsp)
movl $0x0, (%rbx,%rbp,4)
incq %rbp
decl %r15d
movq 0x100(%rsp), %rbx
cmpl $0x6, %r15d
movl $0x6, %r13d
cmovbl %r15d, %r13d
cmpq $0x0, 0x200(%rsp)
je 0x108ca
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x1087d
testq %r14, %r14
je 0x10870
leaq (,%r14,8), %rsi
movq %r12, %rdi
callq 0x3440
jmp 0x1087a
movl $0x4, %edi
callq 0x33e0
movq %rax, %r12
movl $0x10, (%r12,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x108a7
testq %rbp, %rbp
je 0x108ae
leaq (,%rbp,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x3440
jmp 0x108b8
movq 0x8(%rsp), %rax
jmp 0x108b8
movl $0x4, %edi
callq 0x33e0
incq %r14
leal -0x3(%r13), %ecx
movq %rax, 0x8(%rsp)
movl %ecx, (%rax,%rbp,4)
incq %rbp
incq %rbx
subl %r13d, %r15d
cmpl $0x2, %r15d
ja 0x10837
movq %rbx, 0x100(%rsp)
movq 0x200(%rsp), %r13
movq 0x8(%rsp), %rbx
movq 0x68(%rsp), %rax
decl %eax
addq %rax, 0x38(%rsp)
movl %r15d, %eax
movl 0x4(%rsp), %ecx
addq %rax, 0x80(%rsp,%rcx,8)
testl %r15d, %r15d
je 0x10988
testq %r13, %r13
je 0x10983
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x10941
testq %r14, %r14
je 0x10934
leaq (,%r14,8), %rsi
movq %r12, %rdi
callq 0x3440
jmp 0x1093e
movl $0x4, %edi
callq 0x33e0
movq %rax, %r12
movl 0x4(%rsp), %eax
movl %eax, (%r12,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x10976
testq %rbp, %rbp
je 0x10969
leaq (,%rbp,8), %rsi
movq %rbx, %rdi
callq 0x3440
jmp 0x10973
movl $0x4, %edi
callq 0x33e0
movq %rax, %rbx
incq %r14
movl $0x0, (%rbx,%rbp,4)
incq %rbp
decl %r15d
jne 0x1090f
movq 0x38(%rsp), %r9
incq %r9
movq 0x78(%rsp), %rsi
cmpq %rsi, %r9
movq 0x70(%rsp), %r8
jb 0x104e9
jmp 0x109ad
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %rbx, 0x8(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x120(%rsp), %rcx
movl $0x13, %esi
movl $0x7, %edx
callq 0x117ac
testq %r13, %r13
je 0x109f5
leaq 0x120(%rsp), %rdi
leaq 0x170(%rsp), %rcx
movl $0x13, %esi
movl $0x7, %edx
callq 0x11534
movl $0xf, %ebx
leaq 0x662f(%rip), %rcx      # 0x17030
movq 0x50(%rsp), %r13
movl 0xc(%rcx,%rbx,4), %eax
cmpq $0x0, 0x80(%rsp,%rax,8)
jne 0x10a1c
decq %rbx
jne 0x10a06
xorl %ebx, %ebx
cmpq $0x0, 0x200(%rsp)
je 0x10b44
movq 0x48(%rsp), %rdi
movl $0x5, %esi
movq %r13, %rdx
movq 0x200(%rsp), %r15
movq %r15, %rcx
movq 0x208(%rsp), %rbp
movq %rbp, %r8
callq 0x10bc4
movq 0x40(%rsp), %rdi
movl $0x5, %esi
movq %r13, %rdx
movq %r15, %rcx
movq %rbp, %r8
callq 0x10bc4
movl %ebx, %edi
movl $0x4, %esi
movq %r13, %rdx
movq %r15, %rcx
movq %rbp, %r8
callq 0x10bc4
cmpl $-0x4, %ebx
movq %r13, %r15
je 0x10acc
movl %ebx, %eax
leaq 0x10(,%rax,4), %r13
xorl %ebp, %ebp
leaq 0x6595(%rip), %rax      # 0x17030
movl (%rbp,%rax), %eax
movl 0x120(%rsp,%rax,4), %edi
movl $0x3, %esi
movq %r15, %rdx
movq 0x200(%rsp), %rcx
movq 0x208(%rsp), %r8
callq 0x10bc4
addq $0x4, %rbp
cmpq %rbp, %r13
jne 0x10a94
testq %r14, %r14
je 0x10b44
movq %r15, %rbp
xorl %r15d, %r15d
leaq 0x659e(%rip), %r13      # 0x1707c
movl (%r12,%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 0x10c7a
movl (%r12,%r15,4), %eax
addl $-0x10, %eax
cmpl $0x2, %eax
ja 0x10b3c
movl %eax, %eax
movl (%r13,%rax,4), %esi
movq 0x8(%rsp), %rax
movl (%rax,%r15,4), %edi
movq %rbp, %rdx
movq 0x200(%rsp), %rcx
movq 0x208(%rsp), %r8
callq 0x10bc4
incq %r15
cmpq %r15, %r14
jne 0x10ade
leal (%rbx,%rbx,2), %eax
addl $0xc, %eax
addq $0xe, %rax
xorl %ecx, %ecx
movl 0x120(%rsp,%rcx,4), %edx
imulq 0x80(%rsp,%rcx,8), %rdx
addq %rdx, %rax
incq %rcx
cmpq $0x13, %rcx
jne 0x10b50
movq 0x100(%rsp), %rcx
movq 0x108(%rsp), %rdx
leaq (%rax,%rcx,2), %rax
leaq (%rdx,%rdx,2), %rcx
movq 0x110(%rsp), %rdx
leaq (,%rdx,8), %rbx
subq %rdx, %rbx
addq %rcx, %rbx
addq %rax, %rbx
movq %r12, %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