// File: /workspace/github_c_makefile_star2_O1/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, 0x30(%rsp)
movq %rdi, 0x28(%rsp)
movq 0x200(%rsp), %r15
leaq 0x80(%rsp), %rdi
xorl %r14d, %r14d
movl $0x98, %edx
xorl %esi, %esi
callq 0x3260
movq 0x28(%rsp), %rax
cmpl $0x0, 0x474(%rax,%r14,4)
jne 0xfff4
decq %r14
cmpq $-0x1d, %r14
jne 0xffdb
xorl %r14d, %r14d
jmp 0xfff8
addl $0x1d, %r14d
movl $0x1d, %ecx
movq 0x30(%rsp), %rdx
movl %ecx, %eax
cmpl $0x0, (%rdx,%rax,4)
jne 0x10010
decl %ecx
jne 0x10002
xorl %ecx, %ecx
movq %rbx, 0x20(%rsp)
leal (%r14,%rcx), %esi
addl $0x102, %esi            # imm = 0x102
testl %esi, %esi
movq %r14, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
je 0x104cc
leal 0x101(%r14), %r8d
movl 0x18(%rsp), %eax
orl 0x14(%rsp), %eax
movl %eax, 0x38(%rsp)
leal (%rcx,%r14), %eax
addl $0x102, %eax            # imm = 0x102
movl %eax, 0x3c(%rsp)
movl %r14d, %eax
shlq $0x2, %rax
movq 0x30(%rsp), %rcx
subq %rax, %rcx
addq $-0x400, %rcx           # imm = 0xFC00
movq %rcx, 0x58(%rsp)
movq 0x28(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r9d, %r9d
movq %rsi, 0x68(%rsp)
movq %r8, 0x60(%rsp)
movq %r9, %rax
subq %r8, %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%r9,4), %rcx
movq 0x30(%rsp), %rdx
leaq (%rdx,%rax,4), %rax
cmovbq %rcx, %rax
movzbl (%rax), %eax
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x1c(%rsp)
jne 0x100d1
movl $0x1, %r11d
cmpl $0x0, 0x38(%rsp)
je 0x10134
cmpl $0x0, 0x8(%rsp)
jne 0x10134
leaq 0x1(%r9), %rax
movl $0x1, %r11d
cmpq %rsi, %rax
jae 0x10134
movl 0x3c(%rsp), %eax
movl %eax, %r11d
subl %r9d, %r11d
movq %rsi, %rax
subq %r9, %rax
movq 0x58(%rsp), %rcx
leaq (%rcx,%r9,4), %rdx
movq 0x50(%rsp), %rcx
leaq (%rcx,%r9,4), %rsi
movl $0x1, %ecx
leaq (%r9,%rcx), %rdi
cmpq %r8, %rdi
movq %rdx, %rdi
cmovbq %rsi, %rdi
movq 0x8(%rsp), %r10
cmpl (%rdi), %r10d
jne 0x10131
incq %rcx
addq $0x4, %rdx
addq $0x4, %rsi
cmpq %rcx, %rax
jne 0x10107
jmp 0x10134
movl %ecx, %r11d
cmpl $0x0, 0x8(%rsp)
movq %r9, 0x78(%rsp)
movq %r11, 0x70(%rsp)
jne 0x1020e
cmpl $0x3, %r11d
jb 0x1020e
cmpl $0x0, 0x18(%rsp)
je 0x10216
cmpl $0xb, %r11d
jb 0x10216
movl %r11d, %ebx
movl $0x8a, %eax
cmpl %eax, %ebx
movl $0x8a, %r15d
cmovbl %ebx, %r15d
cmpq $0x0, 0x200(%rsp)
je 0x101f8
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x101b4
testq %r14, %r14
je 0x101a7
leaq (,%r14,8), %rsi
movq %r12, %rdi
callq 0x3430
jmp 0x101b1
movl $0x4, %edi
callq 0x33d0
movq %rax, %r12
movl $0x12, (%r12,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x101e9
testq %rbp, %rbp
je 0x101dc
leaq (,%rbp,8), %rsi
movq %r13, %rdi
callq 0x3430
jmp 0x101e6
movl $0x4, %edi
callq 0x33d0
movq %rax, %r13
incq %r14
leal -0xb(%r15), %eax
movl %eax, (%r13,%rbp,4)
incq %rbp
incq 0x110(%rsp)
subl %r15d, %ebx
cmpl $0xa, %ebx
ja 0x1016b
jmp 0x10219
movl %r11d, %ebx
jmp 0x102ca
movl %r11d, %ebx
cmpl $0x0, 0x14(%rsp)
je 0x102ca
cmpl $0x3, %ebx
jb 0x102ca
cmpl $0xa, %ebx
movl $0xa, %r15d
cmovbl %ebx, %r15d
cmpq $0x0, 0x200(%rsp)
je 0x102b6
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x10272
testq %r14, %r14
je 0x10265
leaq (,%r14,8), %rsi
movq %r12, %rdi
callq 0x3430
jmp 0x1026f
movl $0x4, %edi
callq 0x33d0
movq %rax, %r12
movl $0x11, (%r12,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x102a7
testq %rbp, %rbp
je 0x1029a
leaq (,%rbp,8), %rsi
movq %r13, %rdi
callq 0x3430
jmp 0x102a4
movl $0x4, %edi
callq 0x33d0
movq %rax, %r13
incq %r14
leal -0x3(%r15), %eax
movl %eax, (%r13,%rbp,4)
incq %rbp
incq 0x108(%rsp)
subl %r15d, %ebx
cmpl $0x2, %ebx
ja 0x1022d
cmpl $0x0, 0x1c(%rsp)
je 0x10410
cmpl $0x4, %ebx
jb 0x10410
movq 0x8(%rsp), %rax
incq 0x80(%rsp,%rax,8)
cmpq $0x0, 0x200(%rsp)
je 0x10368
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x10323
testq %r14, %r14
je 0x10316
leaq (,%r14,8), %rsi
movq %r12, %rdi
callq 0x3430
jmp 0x10320
movl $0x4, %edi
callq 0x33d0
movq %rax, %r12
movq 0x8(%rsp), %rax
movl %eax, (%r12,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x10359
testq %rbp, %rbp
je 0x1034c
leaq (,%rbp,8), %rsi
movq %r13, %rdi
callq 0x3430
jmp 0x10356
movl $0x4, %edi
callq 0x33d0
movq %rax, %r13
incq %r14
movl $0x0, (%r13,%rbp,4)
incq %rbp
decl %ebx
cmpl $0x3, %ebx
jb 0x10410
cmpl $0x6, %ebx
movl $0x6, %r15d
cmovbl %ebx, %r15d
cmpq $0x0, 0x200(%rsp)
je 0x103fc
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x103b8
testq %r14, %r14
je 0x103ab
leaq (,%r14,8), %rsi
movq %r12, %rdi
callq 0x3430
jmp 0x103b5
movl $0x4, %edi
callq 0x33d0
movq %rax, %r12
movl $0x10, (%r12,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x103ed
testq %rbp, %rbp
je 0x103e0
leaq (,%rbp,8), %rsi
movq %r13, %rdi
callq 0x3430
jmp 0x103ea
movl $0x4, %edi
callq 0x33d0
movq %rax, %r13
incq %r14
leal -0x3(%r15), %eax
movl %eax, (%r13,%rbp,4)
incq %rbp
incq 0x100(%rsp)
subl %r15d, %ebx
cmpl $0x2, %ebx
ja 0x10373
movl %ebx, %eax
movl 0x8(%rsp), %ecx
addq %rax, 0x80(%rsp,%rcx,8)
movq 0x200(%rsp), %r15
testl %ebx, %ebx
je 0x104a5
testq %r15, %r15
je 0x104a1
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x1045c
testq %r14, %r14
je 0x1044f
leaq (,%r14,8), %rsi
movq %r12, %rdi
callq 0x3430
jmp 0x10459
movl $0x4, %edi
callq 0x33d0
movq %rax, %r12
movq 0x8(%rsp), %rax
movl %eax, (%r12,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x10492
testq %rbp, %rbp
je 0x10485
leaq (,%rbp,8), %rsi
movq %r13, %rdi
callq 0x3430
jmp 0x1048f
movl $0x4, %edi
callq 0x33d0
movq %rax, %r13
incq %r14
movl $0x0, (%r13,%rbp,4)
incq %rbp
decl %ebx
jne 0x1042a
movq 0x70(%rsp), %rax
decl %eax
movq 0x78(%rsp), %r9
addq %rax, %r9
incq %r9
movq 0x68(%rsp), %rsi
cmpq %rsi, %r9
movq 0x60(%rsp), %r8
jb 0x10092
jmp 0x104d5
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r12d, %r12d
leaq 0x80(%rsp), %rdi
leaq 0x120(%rsp), %rcx
movl $0x13, %esi
movl $0x7, %edx
callq 0x11258
testq %r15, %r15
je 0x10518
leaq 0x120(%rsp), %rdi
leaq 0x170(%rsp), %rcx
movl $0x13, %esi
movl $0x7, %edx
callq 0x11010
movl $0xf, %ebx
leaq 0x6acc(%rip), %rcx      # 0x16ff0
movl 0xc(%rcx,%rbx,4), %eax
cmpq $0x0, 0x80(%rsp,%rax,8)
jne 0x1053a
decq %rbx
jne 0x10524
xorl %ebx, %ebx
testq %r15, %r15
je 0x10664
movq 0x48(%rsp), %rdi
movl $0x5, %esi
movq 0x20(%rsp), %r15
movq %r15, %rdx
movq 0x200(%rsp), %rbp
movq %rbp, %rcx
movq 0x208(%rsp), %r8
callq 0x106e2
movq 0x40(%rsp), %rdi
movl $0x5, %esi
movq %r15, %rdx
movq %rbp, %rcx
movq 0x208(%rsp), %r8
callq 0x106e2
movl %ebx, %edi
movl $0x4, %esi
movq %r15, %rdx
movq %rbp, %rcx
movq 0x208(%rsp), %r8
callq 0x106e2
cmpl $-0x4, %ebx
je 0x105f0
movl %ebx, %eax
leaq 0x10(,%rax,4), %rbp
xorl %r15d, %r15d
leaq 0x6a33(%rip), %rax      # 0x16ff0
movl (%r15,%rax), %eax
movl 0x120(%rsp,%rax,4), %edi
movl $0x3, %esi
movq 0x20(%rsp), %rdx
movq 0x200(%rsp), %rcx
movq 0x208(%rsp), %r8
callq 0x106e2
addq $0x4, %r15
cmpq %r15, %rbp
jne 0x105b6
testq %r14, %r14
je 0x10664
xorl %r15d, %r15d
leaq 0x6a3d(%rip), %rbp      # 0x1703c
movl (%r12,%r15,4), %eax
movl 0x170(%rsp,%rax,4), %edi
movl 0x120(%rsp,%rax,4), %esi
movq 0x20(%rsp), %rdx
movq 0x200(%rsp), %rcx
movq 0x208(%rsp), %r8
callq 0x1078f
movl (%r12,%r15,4), %eax
addl $-0x10, %eax
cmpl $0x2, %eax
ja 0x1065c
movl %eax, %eax
movl (%rbp,%rax,4), %esi
movl (%r13,%r15,4), %edi
movq 0x20(%rsp), %rdx
movq 0x200(%rsp), %rcx
movq 0x208(%rsp), %r8
callq 0x106e2
incq %r15
cmpq %r15, %r14
jne 0x105ff
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 0x10670
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 %r13, %rdi
callq 0x3050
movq %rbx, %rax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq