// File: /workspace/github_c_makefile_star2_O3/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 $0x1b8, %rsp            # imm = 0x1B8
movq %r8, 0x60(%rsp)
movq %rcx, %r12
movq %rdx, 0x58(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
xorl %ecx, %ecx
cmpl $0x0, 0x474(%r14,%rcx,4)
jne 0x2ff1
decq %rcx
cmpq $-0x1d, %rcx
jne 0x2fd9
xorl %ecx, %ecx
jmp 0x2ff4
addl $0x1d, %ecx
movl $0x1d, %edx
movl %edx, %eax
cmpl $0x0, (%rbx,%rax,4)
jne 0x3007
decl %edx
jne 0x2ff9
xorl %edx, %edx
movq %rcx, 0x38(%rsp)
movq %rdx, 0x50(%rsp)
leal (%rcx,%rdx), %r13d
addl $0x102, %r13d           # imm = 0x102
leaq (,%r13,4), %rdi
callq 0x1130
testq %rax, %rax
je 0x386c
movq %rax, %r15
testq %r13, %r13
movq %rax, 0x28(%rsp)
je 0x365e
movq 0x38(%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
cmpq %rax, %rcx
movq %rbx, %rdx
cmovbq %r14, %rdx
movl (%rdx), %edx
movl %edx, (%r15,%rcx,4)
cmpl $0x10, %edx
jae 0x382e
incq %rcx
addq $0x4, %rbx
addq $0x4, %r14
cmpq %rcx, %r13
jne 0x3062
testl %r13d, %r13d
je 0x365e
xorl %r14d, %r14d
xorl %r9d, %r9d
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
movq %r12, 0x68(%rsp)
movq %r13, 0x40(%rsp)
leaq (%r15,%r14,4), %rcx
movl (%r15,%r14,4), %ebx
xorl %ebp, %ebp
cmpl (%rcx,%rbp,4), %ebx
jne 0x30c9
leaq (%r14,%rbp), %rax
incq %rax
incq %rbp
cmpq %r13, %rax
jb 0x30b5
cmpq $0x3, %rbp
movq %rsi, 0x8(%rsp)
movq %r14, 0x30(%rsp)
movq %r9, 0x10(%rsp)
jbe 0x3135
testl %ebx, %ebx
je 0x317f
leaq -0x1(%rsi), %rax
testq %rax, %rsi
movq %rcx, 0x18(%rsp)
jne 0x323b
movq %rdi, %r14
testq %rsi, %rsi
je 0x321c
movq 0x8(%rsp), %rax
leaq (,%rax,8), %rsi
movq %r8, %rdi
callq 0x1150
movq 0x8(%rsp), %rsi
movq %rax, %r8
movq 0x18(%rsp), %rcx
movl (%rcx), %ebx
movq %r14, %rdi
movq 0x10(%rsp), %r9
jmp 0x323b
jne 0x313f
testl %ebx, %ebx
je 0x31c4
leaq -0x1(%rsi), %rax
testq %rax, %rsi
jne 0x33e7
movq %rdi, %r14
testq %rsi, %rsi
je 0x33c8
movq 0x8(%rsp), %rax
leaq (,%rax,8), %rsi
movq %r8, %rdi
movq %rcx, %rbx
callq 0x1150
movq 0x8(%rsp), %rsi
movq %rax, %r8
movl (%rbx), %ebx
jmp 0x33da
cmpq $0xb, %rbp
jb 0x31c4
movl $0x8a, %eax
cmpq %rax, %rbp
cmovaeq %rax, %rbp
leaq -0x1(%rsi), %rax
testq %rax, %rsi
jne 0x357c
movq %rdi, %rbx
testq %rsi, %rsi
je 0x3562
movq 0x8(%rsp), %rax
leaq (,%rax,8), %rsi
movq %r8, %rdi
callq 0x1150
jmp 0x356c
leaq -0x1(%rsi), %rax
testq %rax, %rsi
jne 0x3206
movq %rdi, %rbx
testq %rsi, %rsi
je 0x31ec
movq 0x8(%rsp), %rax
leaq (,%rax,8), %rsi
movq %r8, %rdi
callq 0x1150
jmp 0x31f6
movl $0x4, %edi
callq 0x1130
movq 0x8(%rsp), %rsi
movq %rax, %r8
movq %rbx, %rdi
movq 0x10(%rsp), %r9
movl $0x11, (%r8,%rsi,4)
leaq -0x1(%r9), %rax
movl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
jmp 0x358d
movl $0x4, %edi
callq 0x1130
movq 0x8(%rsp), %rsi
movq %rax, %r8
movq %r14, %rdi
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %rcx
movl %ebx, (%r8,%rsi,4)
leaq -0x1(%r9), %rax
testq %rax, %r9
jne 0x327e
movq %r8, %rbx
testq %r9, %r9
je 0x325f
leaq (,%r9,8), %rsi
callq 0x1150
jmp 0x3269
movl $0x4, %edi
callq 0x1130
movq 0x10(%rsp), %r9
movq %rax, %rdi
movq %rbx, %r8
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rcx
movq %rbp, 0x48(%rsp)
leal -0x1(%rbp), %r13d
movl $0x0, (%rdi,%r9,4)
cmpl $0x6, %r13d
jb 0x3389
leaq 0x4(,%rsi,4), %rax
movq %rax, 0x20(%rsp)
leaq 0x8(,%rsi,8), %r15
leaq 0x4(,%r9,4), %rbp
leaq 0x8(,%r9,8), %r12
xorl %ebx, %ebx
leaq (%rsi,%rbx), %rcx
leaq (%rsi,%rbx), %rax
incq %rax
testq %rcx, %rax
jne 0x32ff
movq %rdi, %r14
testq %rax, %rax
je 0x32e5
movq %r8, %rdi
movq %r15, %rsi
callq 0x1150
jmp 0x32ef
movl $0x4, %edi
callq 0x1130
movq %rax, %r8
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %r9
leaq (%r9,%rbx), %rcx
leaq (%r9,%rbx), %rax
incq %rax
movq 0x20(%rsp), %rdx
addq %r8, %rdx
movl $0x10, (%rdx,%rbx,4)
testq %rcx, %rax
jne 0x334a
movq %r8, %r14
testq %rax, %rax
je 0x3330
movq %r12, %rsi
callq 0x1150
jmp 0x333a
movl $0x4, %edi
callq 0x1130
movq %rax, %rdi
movq %r14, %r8
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %r9
leaq (%rdi,%rbp), %rax
movl $0x3, (%rax,%rbx,4)
addl $-0x6, %r13d
incq %rbx
addq $0x8, %r15
addq $0x8, %r12
cmpl $0x5, %r13d
ja 0x32c0
leaq (%rsi,%rbx), %r14
incq %r14
addq %rbx, %rsi
leaq (%r9,%rbx), %rbp
incq %rbp
addq %rbx, %r9
movq 0x18(%rsp), %rcx
jmp 0x3391
leaq 0x1(%r9), %rbp
leaq 0x1(%rsi), %r14
cmpl $0x3, %r13d
jb 0x34b1
testq %rsi, %r14
jne 0x3453
movq %r9, %r12
movq %rsi, %r15
movq %rdi, %rbx
testq %r14, %r14
je 0x3438
leaq (,%r14,8), %rsi
movq %r8, %rdi
callq 0x1150
jmp 0x3442
movl $0x4, %edi
callq 0x1130
movq 0x8(%rsp), %rsi
movq %rax, %r8
movq %r14, %rdi
movq 0x30(%rsp), %r14
movq 0x10(%rsp), %r9
movl %ebx, (%r8,%rsi,4)
leaq -0x1(%r9), %rax
testq %rax, %r9
jne 0x3425
movq %r8, %rbx
testq %r9, %r9
je 0x340b
leaq (,%r9,8), %rsi
callq 0x1150
jmp 0x3415
movl $0x4, %edi
callq 0x1130
movq 0x10(%rsp), %r9
movq %rax, %rdi
movq %rbx, %r8
movq 0x8(%rsp), %rsi
incq %rsi
movl $0x0, (%rdi,%r9,4)
incq %r9
jmp 0x35f2
movl $0x4, %edi
callq 0x1130
movq %rax, %r8
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %r9
movq 0x18(%rsp), %rcx
movl $0x10, (%r8,%r14,4)
testq %r9, %rbp
jne 0x3498
movq %r9, %r15
movq %rsi, %r14
movq %r8, %rbx
testq %rbp, %rbp
je 0x347d
leaq (,%rbp,8), %rsi
callq 0x1150
jmp 0x3487
movl $0x4, %edi
callq 0x1130
movq %rax, %rdi
movq %rbx, %r8
movq %r14, %rsi
movq %r15, %r9
movq 0x18(%rsp), %rcx
addq $0x2, %rsi
movl $0x0, (%rdi,%rbp,4)
addq $0x2, %r9
addl $-0x3, %r13d
movq %rsi, %r14
movq %r9, %rbp
testl %r13d, %r13d
je 0x3551
leaq (,%rbp,8), %r15
leaq (,%r14,8), %r12
leaq -0x1(%r14), %rax
testq %rax, %r14
jne 0x34fd
movq %rdi, %rbx
testq %r14, %r14
je 0x34e8
movq %r8, %rdi
movq %r12, %rsi
callq 0x1150
jmp 0x34f2
movl $0x4, %edi
callq 0x1130
movq %rax, %r8
movq %rbx, %rdi
movq 0x18(%rsp), %rcx
movl (%rcx), %eax
movl %eax, (%r8,%r14,4)
leaq -0x1(%rbp), %rax
testq %rax, %rbp
jne 0x3533
movq %r8, %rbx
testq %rbp, %rbp
je 0x351e
movq %r15, %rsi
callq 0x1150
jmp 0x3528
movl $0x4, %edi
callq 0x1130
movq %rax, %rdi
movq %rbx, %r8
movq 0x18(%rsp), %rcx
movl $0x0, (%rdi,%rbp,4)
incq %rbp
addq $0x8, %r15
addq $0x8, %r12
incq %r14
decl %r13d
jne 0x34ca
movq 0x28(%rsp), %r15
movq 0x40(%rsp), %r13
movq 0x48(%rsp), %rcx
jmp 0x35d9
movl $0x4, %edi
callq 0x1130
movq 0x8(%rsp), %rsi
movq %rax, %r8
movq %rbx, %rdi
movq 0x10(%rsp), %r9
movl $0x12, (%r8,%rsi,4)
leaq -0x1(%r9), %rax
movl $0xfffffff5, %ebx       # imm = 0xFFFFFFF5
testq %rax, %r9
jne 0x35c3
movq %r8, %r14
testq %r9, %r9
je 0x35a9
leaq (,%r9,8), %rsi
callq 0x1150
jmp 0x35b3
movl $0x4, %edi
callq 0x1130
movq 0x10(%rsp), %r9
movq %rax, %rdi
movq %r14, %r8
movq 0x8(%rsp), %rsi
incq %rsi
leal (%rbx,%rbp), %eax
movl %eax, (%rdi,%r9,4)
incq %r9
movq %rsi, %r14
movq %rbp, %rcx
movq %r9, %rbp
movq 0x30(%rsp), %rax
addq %rcx, %rax
decq %rax
movq %r14, %rsi
movq %rax, %r14
movq %rbp, %r9
movq 0x68(%rsp), %r12
cmpl $0x13, -0x4(%r8,%rsi,4)
jae 0x384d
incq %r14
cmpq %r13, %r14
jb 0x30ab
movq %r8, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0xc0(%rsp), %rdi
movl $0x98, %edx
movq %rsi, 0x8(%rsp)
xorl %esi, %esi
callq 0x10c0
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
sete %al
je 0x381b
movb %al, 0x20(%rsp)
xorl %eax, %eax
movq 0x18(%rsp), %rdx
movl (%rdx,%rax,4), %ecx
incq 0xc0(%rsp,%rcx,8)
incq %rax
cmpq %rax, %rsi
jne 0x3649
jmp 0x3692
leaq 0xc0(%rsp), %rdi
movq $0x0, 0x8(%rsp)
movl $0x98, %edx
xorl %esi, %esi
callq 0x10c0
movb $0x1, 0x20(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x70(%rsp), %r15
movl $0x13, %esi
movl $0x7, %edx
movq %r15, %rcx
callq 0x8b29
leaq 0x160(%rsp), %rcx
movl $0x13, %esi
movq %r15, %rdi
movl $0x7, %edx
callq 0x88e8
movl $0xf, %r15d
leaq 0x7238(%rip), %r14      # 0xa910
movq 0x38(%rsp), %rdi
movl 0xc(%r14,%r15,4), %eax
cmpq $0x0, 0xc0(%rsp,%rax,8)
jne 0x36f5
decq %r15
jne 0x36dd
xorl %r15d, %r15d
movl $0x5, %esi
movq 0x58(%rsp), %rbx
movq %rbx, %rdx
movq %r12, %r13
movq %r12, %rcx
movq 0x60(%rsp), %rbp
movq %rbp, %r8
callq 0x3876
movq 0x50(%rsp), %rdi
movl $0x5, %esi
movq %rbx, %rdx
movq %r12, %rcx
movq %rbp, %r8
callq 0x3876
movl %r15d, %edi
movl $0x4, %esi
movq %rbx, %rdx
movq %r12, %rcx
movq %rbp, %r8
callq 0x3876
cmpl $-0x4, %r15d
je 0x377b
movl %r15d, %eax
leaq 0x10(,%rax,4), %r15
xorl %r12d, %r12d
movl (%r12,%r14), %eax
movl 0x70(%rsp,%rax,4), %edi
movl $0x3, %esi
movq %rbx, %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0x3876
addq $0x4, %r12
cmpq %r12, %r15
jne 0x3757
cmpb $0x0, 0x20(%rsp)
movq 0x18(%rsp), %rcx
jne 0x37ea
movq %rbx, %r15
xorl %ebx, %ebx
leaq 0x71c9(%rip), %r14      # 0xa95c
movl (%rcx,%rbx,4), %eax
movl 0x160(%rsp,%rax,4), %edi
movl 0x70(%rsp,%rax,4), %esi
movq %r15, %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0x392d
movq 0x18(%rsp), %rcx
movl (%rcx,%rbx,4), %eax
addl $-0x10, %eax
cmpl $0x2, %eax
ja 0x37e0
movl %eax, %eax
movl (%r14,%rax,4), %esi
movq 0x10(%rsp), %rax
movl (%rax,%rbx,4), %edi
movq %r15, %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0x3876
movq 0x18(%rsp), %rcx
incq %rbx
cmpq %rbx, 0x8(%rsp)
jne 0x3793
movq 0x28(%rsp), %rdi
movq %rcx, %rbx
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq 0x10(%rsp), %rdi
callq 0x1030
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
jmp 0x3692
leaq 0x6bdb(%rip), %rdi      # 0xa410
leaq 0x6b65(%rip), %rsi      # 0xa3a1
leaq 0x6be1(%rip), %rcx      # 0xa424
movl $0x86, %edx
callq 0x10b0
leaq 0x6c3d(%rip), %rdi      # 0xa491
leaq 0x6b46(%rip), %rsi      # 0xa3a1
leaq 0x6bc2(%rip), %rcx      # 0xa424
movl $0xb2, %edx
callq 0x10b0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1180