// File: /workspace/github_c_makefile_star2_O1/imaya[P]zopfli-for-emscripten/deflate.c
// Function: AddDynamicTree
static void AddDynamicTree(const unsigned* ll_lengths,
                           const unsigned* d_lengths,
                           unsigned char* bp,
                           unsigned char** out, size_t* outsize) {
  unsigned* lld_lengths = 0;  /* All litlen and dist lengthts with ending zeros
      trimmed together in one array. */
  unsigned lld_total;  /* Size of lld_lengths. */
  unsigned* rle = 0;  /* Runlength encoded version of lengths of litlen and dist
      trees. */
  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;
  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. */
  unsigned order[19] = {
    16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  };

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

  lld_total = hlit + 257 + hdist + 1;
  lld_lengths = (unsigned*)malloc(sizeof(*lld_lengths) * lld_total);
  if (!lld_lengths) exit(-1); /* Allocation failed. */

  for (i = 0; i < lld_total; i++) {
    lld_lengths[i] = i < 257 + hlit
        ? ll_lengths[i] : d_lengths[i - 257 - hlit];
    assert(lld_lengths[i] < 16);
  }

  for (i = 0; i < lld_total; i++) {
    size_t count = 0;
    for (j = i; j < lld_total && lld_lengths[i] == lld_lengths[j]; j++) {
      count++;
    }
    if (count >= 4 || (count >= 3 && lld_lengths[i] == 0)) {
      if (lld_lengths[i] == 0) {
        if (count > 10) {
          if (count > 138) count = 138;
          ZOPFLI_APPEND_DATA(18, &rle, &rle_size);
          ZOPFLI_APPEND_DATA(count - 11, &rle_bits, &rle_bits_size);
        } else {
          ZOPFLI_APPEND_DATA(17, &rle, &rle_size);
          ZOPFLI_APPEND_DATA(count - 3, &rle_bits, &rle_bits_size);
        }
      } else {
        unsigned repeat = count - 1;  /* Since the first one is hardcoded. */
        ZOPFLI_APPEND_DATA(lld_lengths[i], &rle, &rle_size);
        ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
        while (repeat >= 6) {
          ZOPFLI_APPEND_DATA(16, &rle, &rle_size);
          ZOPFLI_APPEND_DATA(6 - 3, &rle_bits, &rle_bits_size);
          repeat -= 6;
        }
        if (repeat >= 3) {
          ZOPFLI_APPEND_DATA(16, &rle, &rle_size);
          ZOPFLI_APPEND_DATA(3 - 3, &rle_bits, &rle_bits_size);
          repeat -= 3;
        }
        while (repeat != 0) {
          ZOPFLI_APPEND_DATA(lld_lengths[i], &rle, &rle_size);
          ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
          repeat--;
        }
      }

      i += count - 1;
    } else {
      ZOPFLI_APPEND_DATA(lld_lengths[i], &rle, &rle_size);
      ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
    }
    assert(rle[rle_size - 1] <= 18);
  }

  for (i = 0; i < 19; i++) {
    clcounts[i] = 0;
  }
  for (i = 0; i < rle_size; i++) {
    clcounts[rle[i]]++;
  }

  ZopfliCalculateBitLengths(clcounts, 19, 7, clcl);
  ZopfliLengthsToSymbols(clcl, 19, 7, clsymbols);

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

  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);
  }

  free(lld_lengths);
  free(rle);
  free(rle_bits);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %r8, %r15
movq %rcx, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
xorl %ecx, %ecx
cmpl $0x0, 0x474(%r14,%rcx,4)
jne 0x2ff0
decq %rcx
cmpq $-0x1d, %rcx
jne 0x2fd8
xorl %ecx, %ecx
jmp 0x2ff3
addl $0x1d, %ecx
movl $0x1d, %edx
movl %edx, %eax
cmpl $0x0, (%rbx,%rax,4)
jne 0x3006
decl %edx
jne 0x2ff8
xorl %edx, %edx
movq %rcx, 0x30(%rsp)
movq %rdx, 0x48(%rsp)
leal (%rcx,%rdx), %r12d
addl $0x102, %r12d           # imm = 0x102
leaq (,%r12,4), %rdi
callq 0x1120
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x37f6
testq %r12, %r12
je 0x35e7
movq 0x30(%rsp), %rcx
leal 0x101(%rcx), %eax
movl %eax, %eax
movl %ecx, %ecx
shlq $0x2, %rcx
subq %rcx, %rbx
addq $-0x404, %rbx           # imm = 0xFBFC
xorl %ecx, %ecx
movq 0x10(%rsp), %rsi
cmpq %rax, %rcx
movq %rbx, %rdx
cmovbq %r14, %rdx
movl (%rdx), %edx
movl %edx, (%rsi,%rcx,4)
cmpl $0x10, %edx
jae 0x37b8
incq %rcx
addq $0x4, %rbx
addq $0x4, %r14
cmpq %rcx, %r12
jne 0x3063
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
movq %r12, 0x38(%rsp)
movq %r15, 0x20(%rsp)
movq 0x10(%rsp), %rcx
leaq (%rcx,%rbp,4), %rax
movl (%rcx,%rbp,4), %ecx
xorl %r13d, %r13d
cmpl (%rax,%r13,4), %ecx
jne 0x30c9
leaq 0x1(,%r13), %rdx
addq %rbp, %rdx
incq %r13
cmpq %r12, %rdx
jb 0x30b0
cmpq $0x3, %r13
movq %rsi, (%rsp)
jbe 0x3110
movq 0x10(%rsp), %rax
cmpl $0x0, (%rax,%rbp,4)
je 0x314f
leaq -0x1(%rsi), %rax
testq %rax, %rsi
jne 0x3240
movq %rdi, %rbx
testq %rsi, %rsi
je 0x322c
movq (%rsp), %rax
leaq (,%rax,8), %rsi
movq %r8, %rdi
callq 0x1140
jmp 0x3236
jne 0x311d
movq 0x10(%rsp), %rax
cmpl $0x0, (%rax,%rbp,4)
je 0x30d3
leaq -0x1(%rsi), %rax
testq %rax, %rsi
jne 0x31d9
movq %rdi, %rbx
testq %rsi, %rsi
je 0x31c5
movq (%rsp), %rax
leaq (,%rax,8), %rsi
movq %r8, %rdi
callq 0x1140
jmp 0x31cf
cmpq $0xb, %r13
jb 0x3193
movl $0x8a, %eax
cmpq %rax, %r13
cmovaeq %rax, %r13
leaq -0x1(%rsi), %rax
testq %rax, %rsi
jne 0x3509
movq %rdi, %rbx
testq %rsi, %rsi
je 0x34f5
movq (%rsp), %rax
leaq (,%rax,8), %rsi
movq %r8, %rdi
callq 0x1140
jmp 0x34ff
leaq -0x1(%rsi), %rax
testq %rax, %rsi
jne 0x354e
movq %rdi, %rbx
testq %rsi, %rsi
je 0x353a
movq (%rsp), %rax
leaq (,%rax,8), %rsi
movq %r8, %rdi
callq 0x1140
jmp 0x3544
movl $0x4, %edi
callq 0x1120
movq (%rsp), %rsi
movq %rax, %r8
movq %rbx, %rdi
movq 0x10(%rsp), %rax
movl (%rax,%rbp,4), %eax
movl %eax, (%r8,%rsi,4)
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x3219
movq %r8, %rbx
testq %r14, %r14
je 0x3205
leaq (,%r14,8), %rsi
callq 0x1140
jmp 0x320f
movl $0x4, %edi
callq 0x1120
movq %rax, %rdi
movq %rbx, %r8
movq (%rsp), %rsi
incq %rsi
movl $0x0, (%rdi,%r14,4)
incq %r14
jmp 0x35cd
movl $0x4, %edi
callq 0x1120
movq (%rsp), %rsi
movq %rax, %r8
movq %rbx, %rdi
movq 0x10(%rsp), %rax
movl (%rax,%rbp,4), %eax
movl %eax, (%r8,%rsi,4)
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x3280
movq %r8, %rbx
testq %r14, %r14
je 0x326c
leaq (,%r14,8), %rsi
callq 0x1140
jmp 0x3276
movl $0x4, %edi
callq 0x1120
movq %rax, %rdi
movq %rbx, %r8
movq (%rsp), %rsi
movq %r13, 0x40(%rsp)
leal -0x1(%r13), %ebx
movl $0x0, (%rdi,%r14,4)
cmpl $0x6, %ebx
movq %rbp, 0x8(%rsp)
movq %r14, 0x18(%rsp)
jb 0x339a
leaq 0x4(,%rsi,4), %rax
movq %rax, 0x28(%rsp)
leaq 0x8(,%rsi,8), %r15
leaq 0x4(,%r14,4), %r13
leaq 0x8(,%r14,8), %rbp
xorl %r12d, %r12d
leaq (%rsi,%r12), %rcx
leaq (%rsi,%r12), %rax
incq %rax
testq %rcx, %rax
jne 0x330a
movq %rdi, %r14
testq %rax, %rax
je 0x32f1
movq %r8, %rdi
movq %r15, %rsi
callq 0x1140
jmp 0x32fb
movl $0x4, %edi
callq 0x1120
movq %rax, %r8
movq %r14, %rdi
movq (%rsp), %rsi
movq 0x18(%rsp), %r14
leaq (%r14,%r12), %rcx
leaq (%r14,%r12), %rax
incq %rax
movq 0x28(%rsp), %rdx
addq %r8, %rdx
movl $0x10, (%rdx,%r12,4)
testq %rcx, %rax
jne 0x3355
movq %r8, %r14
testq %rax, %rax
je 0x333c
movq %rbp, %rsi
callq 0x1140
jmp 0x3346
movl $0x4, %edi
callq 0x1120
movq %rax, %rdi
movq %r14, %r8
movq (%rsp), %rsi
movq 0x18(%rsp), %r14
leaq (%rdi,%r13), %rax
movl $0x3, (%rax,%r12,4)
addl $-0x6, %ebx
incq %r12
addq $0x8, %r15
addq $0x8, %rbp
cmpl $0x5, %ebx
ja 0x32cc
leaq (%rsi,%r12), %r13
incq %r13
addq %r12, %rsi
movq %r14, %rax
addq %r12, %r14
incq %r14
addq %r12, %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %r15
jmp 0x33a1
incq %r14
leaq 0x1(%rsi), %r13
cmpl $0x3, %ebx
jb 0x343c
testq %rsi, %r13
jne 0x33e5
movq %rsi, %rbp
movq %r15, %r12
movq %rdi, %r15
testq %r13, %r13
je 0x33cf
leaq (,%r13,8), %rsi
movq %r8, %rdi
callq 0x1140
jmp 0x33d9
movl $0x4, %edi
callq 0x1120
movq %rax, %r8
movq %r15, %rdi
movq %r12, %r15
movq %rbp, %rsi
movl $0x10, (%r8,%r13,4)
testq %r14, 0x18(%rsp)
jne 0x3421
movq %rsi, %r13
movq %r8, %r12
testq %r14, %r14
je 0x340e
leaq (,%r14,8), %rsi
callq 0x1140
jmp 0x3418
movl $0x4, %edi
callq 0x1120
movq %rax, %rdi
movq %r12, %r8
movq %r13, %rsi
addq $0x2, %rsi
movl $0x0, (%rdi,%r14,4)
movq 0x18(%rsp), %r14
addq $0x2, %r14
addl $-0x3, %ebx
movq %rsi, %r13
testl %ebx, %ebx
je 0x34e1
leaq (,%r14,8), %r15
leaq (,%r13,8), %rbp
leaq -0x1(%r13), %rax
testq %rax, %r13
jne 0x3482
movq %rdi, %r12
testq %r13, %r13
je 0x3472
movq %r8, %rdi
movq %rbp, %rsi
callq 0x1140
jmp 0x347c
movl $0x4, %edi
callq 0x1120
movq %rax, %r8
movq %r12, %rdi
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, (%r8,%r13,4)
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x34be
movq %r8, %r12
testq %r14, %r14
je 0x34ae
movq %r15, %rsi
callq 0x1140
jmp 0x34b8
movl $0x4, %edi
callq 0x1120
movq %rax, %rdi
movq %r12, %r8
movl $0x0, (%rdi,%r14,4)
incq %r14
addq $0x8, %r15
addq $0x8, %rbp
incq %r13
decl %ebx
jne 0x3454
movq 0x20(%rsp), %r15
movq 0x38(%rsp), %r12
movq 0x8(%rsp), %rbp
movq 0x40(%rsp), %rax
jmp 0x35c4
movl $0x4, %edi
callq 0x1120
movq (%rsp), %rsi
movq %rax, %r8
movq %rbx, %rdi
movl $0x12, (%r8,%rsi,4)
leaq -0x1(%r14), %rax
movl $0xfffffff5, %ebx       # imm = 0xFFFFFFF5
testq %rax, %r14
jne 0x35b0
movq %r8, %r15
testq %r14, %r14
je 0x357e
leaq (,%r14,8), %rsi
callq 0x1140
jmp 0x3588
movl $0x4, %edi
callq 0x1120
movq (%rsp), %rsi
movq %rax, %r8
movq %rbx, %rdi
movl $0x11, (%r8,%rsi,4)
leaq -0x1(%r14), %rax
movl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
testq %rax, %r14
jne 0x35b0
movq %r14, %r15
movq %r8, %r14
testq %r15, %r15
je 0x3594
leaq (,%r15,8), %rsi
callq 0x1140
jmp 0x359e
movl $0x4, %edi
callq 0x1120
movq %rax, %rdi
movq %r15, %r8
movq (%rsp), %rsi
jmp 0x35ab
movl $0x4, %edi
callq 0x1120
movq %rax, %rdi
movq %r14, %r8
movq (%rsp), %rsi
movq %r15, %r14
movq 0x20(%rsp), %r15
incq %rsi
leal (%rbx,%r13), %eax
movl %eax, (%rdi,%r14,4)
incq %r14
movq %r13, %rax
movq %rsi, %r13
addq %rax, %rbp
decq %rbp
movq %r13, %rsi
cmpl $0x13, -0x4(%r8,%rsi,4)
jae 0x37d7
incq %rbp
cmpq %r12, %rbp
jb 0x30a1
jmp 0x35ee
xorl %r8d, %r8d
xorl %edi, %edi
xorl %esi, %esi
movq %r8, 0x8(%rsp)
movq %rsi, (%rsp)
movq %r15, %r12
movq %rdi, 0x28(%rsp)
leaq 0x100(%rsp), %rdi
movl $0x98, %edx
xorl %esi, %esi
callq 0x10c0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
testq %rsi, %rsi
je 0x3636
xorl %eax, %eax
movl (%rdx,%rax,4), %ecx
incq 0x100(%rsp,%rcx,8)
incq %rax
cmpq %rax, %rsi
jne 0x3623
leaq 0x100(%rsp), %rdi
leaq 0x60(%rsp), %r15
movl $0x13, %esi
movl $0x7, %edx
movq %r15, %rcx
callq 0x89c8
leaq 0xb0(%rsp), %rcx
movl $0x13, %esi
movq %r15, %rdi
movl $0x7, %edx
callq 0x878c
movl $0xf, %r15d
leaq 0x72a4(%rip), %rbx      # 0xa920
movq 0x30(%rsp), %rdi
movl 0xc(%rbx,%r15,4), %eax
cmpq $0x0, 0x100(%rsp,%rax,8)
jne 0x3699
decq %r15
jne 0x3681
xorl %r15d, %r15d
movl $0x5, %esi
movq 0x50(%rsp), %rbp
movq %rbp, %rdx
movq 0x58(%rsp), %r13
movq %r13, %rcx
movq %r12, %r8
callq 0x3800
movq 0x48(%rsp), %rdi
movl $0x5, %esi
movq %rbp, %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x3800
movl %r15d, %edi
movl $0x4, %esi
movq %rbp, %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x3800
cmpl $-0x4, %r15d
je 0x371c
movl %r15d, %eax
leaq 0x10(,%rax,4), %r14
xorl %r15d, %r15d
movl (%r15,%rbx), %eax
movl 0x60(%rsp,%rax,4), %edi
movl $0x3, %esi
movq %rbp, %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x3800
addq $0x4, %r15
cmpq %r15, %r14
jne 0x36f8
cmpq $0x0, (%rsp)
movq 0x8(%rsp), %rcx
je 0x3787
xorl %ebx, %ebx
leaq 0x723b(%rip), %r14      # 0xa96c
movl (%rcx,%rbx,4), %eax
movl 0xb0(%rsp,%rax,4), %edi
movl 0x60(%rsp,%rax,4), %esi
movq %rbp, %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x38a9
movq 0x8(%rsp), %rcx
movl (%rcx,%rbx,4), %eax
addl $-0x10, %eax
cmpl $0x2, %eax
ja 0x377e
movl %eax, %eax
movl (%r14,%rax,4), %esi
movq 0x28(%rsp), %rax
movl (%rax,%rbx,4), %edi
movq %rbp, %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x3800
movq 0x8(%rsp), %rcx
incq %rbx
cmpq %rbx, (%rsp)
jne 0x3731
movq 0x10(%rsp), %rdi
movq %rcx, %rbx
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq 0x28(%rsp), %rdi
callq 0x1030
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6c51(%rip), %rdi      # 0xa410
leaq 0x6bdb(%rip), %rsi      # 0xa3a1
leaq 0x6c57(%rip), %rcx      # 0xa424
movl $0x86, %edx
callq 0x10b0
leaq 0x6cb3(%rip), %rdi      # 0xa491
leaq 0x6bbc(%rip), %rsi      # 0xa3a1
leaq 0x6c38(%rip), %rcx      # 0xa424
movl $0xb2, %edx
callq 0x10b0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1170