// File: /workspace/github_c_makefile_star2_O2/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 $0x1a8, %rsp            # imm = 0x1A8
movq %r8, 0x38(%rsp)
movq %rcx, %r13
movq %rdx, 0x40(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $-0x1d, %rcx
je 0x282c
cmpl $0x0, 0x474(%r14,%rcx,4)
jne 0x2827
decq %rcx
jmp 0x2811
addl $0x1d, %ecx
movl %ecx, %eax
movl %eax, %eax
movq %rax, 0x50(%rsp)
leaq 0x47c(,%rax,4), %rdi
pushq $0x1d
popq %rdx
movq $-0x11f, %r15           # imm = 0xFEE1
movl %edx, %eax
subl $0x1, %eax
jb 0x285f
movl %edx, %ecx
cmpl $0x0, (%rbx,%rcx,4)
jne 0x285f
incq %r15
addq $-0x4, %rdi
movl %eax, %edx
jmp 0x2847
movl %edx, 0x4c(%rsp)
callq 0x1120
testq %rax, %rax
je 0x3022
movq %rax, %r12
movq 0x50(%rsp), %rcx
movq %rcx, %rax
subq %r15, %rax
movq %rax, %r15
leal 0x101(%rcx), %eax
shlq $0x2, %rcx
subq %rcx, %rbx
addq $-0x404, %rbx           # imm = 0xFBFC
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x28dc
cmpq %rax, %rcx
movq %rbx, %rdx
cmovbq %r14, %rdx
movl (%rdx), %edx
movl %edx, (%r12,%rcx,4)
incq %rcx
addq $0x4, %rbx
addq $0x4, %r14
cmpl $0x10, %edx
jb 0x2898
leaq 0x4a9c(%rip), %rdi      # 0x7360
leaq 0x4a26(%rip), %rsi      # 0x72f1
leaq 0x4aa2(%rip), %rcx      # 0x7374
movl $0x86, %edx
callq 0x10b0
xorl %edi, %edi
xorl %edx, %edx
xorl %ebx, %ebx
xorl %r8d, %r8d
xorl %ebp, %ebp
movq %r12, 0x8(%rsp)
movq %r15, 0x58(%rsp)
movq %r13, 0x30(%rsp)
cmpq %r15, %rbp
movq %rbx, 0x10(%rsp)
jae 0x2e6c
xorl %r13d, %r13d
movq %rbp, %rax
cmpq %r15, %rax
jae 0x2921
movl (%r12,%rbp,4), %ecx
cmpl (%r12,%rax,4), %ecx
jne 0x2921
incq %rax
decq %r13
jmp 0x290a
movq %r13, %rcx
negq %rcx
cmpq $0x3, %rcx
movq %r8, (%rsp)
movq %rcx, 0x20(%rsp)
jbe 0x2975
movl (%r12,%rbp,4), %ebx
testl %ebx, %ebx
je 0x29b2
movq 0x10(%rsp), %rcx
leaq -0x1(%rcx), %rax
testq %rax, %rcx
jne 0x2aa0
movq %rdx, %r14
testq %rcx, %rcx
je 0x2a8e
leaq (,%rcx,8), %rsi
callq 0x1140
movq %rax, %rdi
movl (%r12,%rbp,4), %ebx
jmp 0x2a99
cmpq $-0x3, %r13
jne 0x2982
cmpl $0x0, (%r12,%rbp,4)
je 0x29fc
leaq -0x1(%rbx), %rax
testq %rax, %rbx
movq 0x30(%rsp), %r13
jne 0x2a3e
movq %rdx, %r14
testq %rbx, %rbx
je 0x2a2c
leaq (,%rbx,8), %rsi
callq 0x1140
jmp 0x2a34
cmpq $0xb, %rcx
movq 0x10(%rsp), %rbx
jb 0x29fc
movl $0x8a, %eax
cmpq %rax, %rcx
cmovaeq %rax, %rcx
leaq -0x1(%rbx), %rax
testq %rax, %rbx
movq 0x30(%rsp), %r13
jne 0x2dda
movq %rcx, %r12
movq %rdx, %r14
testq %rbx, %rbx
je 0x2dc0
leaq (,%rbx,8), %rsi
callq 0x1140
jmp 0x2dc8
leaq -0x1(%rbx), %rax
testq %rax, %rbx
movq 0x30(%rsp), %r13
jne 0x2c23
movq %rdx, %r14
testq %rbx, %rbx
je 0x2c0c
leaq (,%rbx,8), %rsi
callq 0x1140
jmp 0x2c14
pushq $0x4
popq %rdi
callq 0x1120
movq %rax, %rdi
movq %r14, %rdx
movq (%rsp), %r8
movl (%r12,%rbp,4), %eax
movl %eax, (%rdi,%rbx,4)
leaq -0x1(%r8), %rax
testq %rax, %r8
jne 0x2a7e
movq %rdi, %r14
testq %r8, %r8
je 0x2a6c
movq (%rsp), %rax
leaq (,%rax,8), %rsi
movq %rdx, %rdi
callq 0x1140
jmp 0x2a74
pushq $0x4
popq %rdi
callq 0x1120
movq (%rsp), %r8
movq %rax, %rdx
movq %r14, %rdi
incq %rbx
andl $0x0, (%rdx,%r8,4)
incq %r8
jmp 0x2e3f
pushq $0x4
popq %rdi
callq 0x1120
movq %rax, %rdi
movq %r14, %rdx
movq (%rsp), %r8
movq 0x10(%rsp), %rax
movl %ebx, (%rdi,%rax,4)
movq %rax, %rbx
leaq -0x1(%r8), %rax
testq %rax, %r8
movq %rbp, 0x18(%rsp)
jne 0x2ae9
movq %rdi, %r14
testq %r8, %r8
je 0x2ad7
movq (%rsp), %rax
leaq (,%rax,8), %rsi
movq %rdx, %rdi
callq 0x1140
jmp 0x2adf
pushq $0x4
popq %rdi
callq 0x1120
movq (%rsp), %r8
movq %rax, %rdx
movq %r14, %rdi
notq %r13
andl $0x0, (%rdx,%r8,4)
leaq 0x8(,%rbx,8), %r15
leaq 0x4(,%rbx,4), %rax
movq %rax, 0x68(%rsp)
leaq 0x8(,%r8,8), %rsi
leaq 0x4(,%r8,4), %rax
movq %rax, 0x60(%rsp)
xorl %ebp, %ebp
leaq (%rbx,%rbp), %rcx
movq %rbx, %rax
leaq (%r8,%rbp), %rbx
leaq (%r8,%rbp), %r12
incq %r12
addq %rbp, %rax
incq %rax
cmpl $0x6, %r13d
jb 0x2be2
testq %rcx, %rax
jne 0x2b72
movq %rsi, 0x28(%rsp)
movq %rdx, %r14
testq %rax, %rax
je 0x2b5b
movq %r15, %rsi
callq 0x1140
jmp 0x2b63
pushq $0x4
popq %rdi
callq 0x1120
movq %rax, %rdi
movq %r14, %rdx
movq (%rsp), %r8
movq 0x28(%rsp), %rsi
movq 0x68(%rsp), %rax
addq %rdi, %rax
movl $0x10, (%rax,%rbp,4)
testq %rbx, %r12
je 0x2b8d
movq 0x10(%rsp), %rbx
jmp 0x2bbf
movq %rdi, %r14
testq %r12, %r12
movq 0x10(%rsp), %rbx
je 0x2ba7
movq %rdx, %rdi
movq %rsi, %r12
callq 0x1140
jmp 0x2bb2
pushq $0x4
popq %rdi
movq %rsi, %r12
callq 0x1120
movq %r12, %rsi
movq %rax, %rdx
movq %r14, %rdi
movq (%rsp), %r8
movq 0x60(%rsp), %rax
addq %rdx, %rax
movl $0x3, (%rax,%rbp,4)
addl $-0x6, %r13d
incq %rbp
addq $0x8, %r15
addq $0x8, %rsi
jmp 0x2b1d
cmpl $0x3, %r13d
jb 0x2cf1
testq %rcx, %rax
jne 0x2c8e
movq %rsi, 0x28(%rsp)
movq %rdx, %r14
testq %rax, %rax
je 0x2c77
movq %r15, %rsi
callq 0x1140
jmp 0x2c7f
pushq $0x4
popq %rdi
callq 0x1120
movq %rax, %rdi
movq %r14, %rdx
movq (%rsp), %r8
movq 0x20(%rsp), %rcx
movl $0x11, (%rdi,%rbx,4)
leaq -0x1(%r8), %rax
testq %rax, %r8
jne 0x2c51
movq %rdi, %r14
testq %r8, %r8
je 0x2c58
movq (%rsp), %rax
leaq (,%rax,8), %rsi
movq %rdx, %rdi
callq 0x1140
jmp 0x2c60
pushq $-0x3
jmp 0x2e0d
pushq $0x4
popq %rdi
callq 0x1120
movq (%rsp), %r8
movq %rax, %rdx
pushq $-0x3
popq %rax
movq %r14, %rdi
movq 0x20(%rsp), %rcx
jmp 0x2e2d
pushq $0x4
popq %rdi
callq 0x1120
movq %rax, %rdi
movq %r14, %rdx
movq (%rsp), %r8
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %rax
leaq (%rdi,%rax,4), %rax
movl $0x10, 0x4(%rax,%rbp,4)
testq %rbx, %r12
je 0x2cab
movq 0x10(%rsp), %rcx
jmp 0x2cd4
movq %rdi, %rbx
testq %r12, %r12
je 0x2cbd
movq %rdx, %rdi
callq 0x1140
jmp 0x2cc5
pushq $0x4
popq %rdi
callq 0x1120
movq %rax, %rdx
movq 0x10(%rsp), %rcx
movq %rbx, %rdi
movq (%rsp), %r8
leaq (%rdx,%r8,4), %rax
andl $0x0, 0x4(%rax,%rbp,4)
leaq (%rcx,%rbp), %rax
addq $0x2, %rax
leaq (%r8,%rbp), %r12
addq $0x2, %r12
addl $-0x3, %r13d
leaq (,%rax,8), %r14
leaq (,%r12,8), %r15
movq %rax, %rbx
movq %r12, %r8
movq 0x8(%rsp), %r12
movq 0x18(%rsp), %rbp
subl $0x1, %r13d
jb 0x2daf
leaq -0x1(%rbx), %rax
testq %rax, %rbx
jne 0x2d54
movq %r8, %r12
movq %rdx, %rbp
testq %rbx, %rbx
je 0x2d39
movq %r14, %rsi
callq 0x1140
jmp 0x2d41
pushq $0x4
popq %rdi
callq 0x1120
movq %rax, %rdi
movq %rbp, %rdx
movq 0x18(%rsp), %rbp
movq %r12, %r8
movq 0x8(%rsp), %r12
movl (%r12,%rbp,4), %eax
movl %eax, (%rdi,%rbx,4)
leaq -0x1(%r8), %rax
testq %rax, %r8
jne 0x2d97
movq %rdi, %rbp
movq %r8, %r12
testq %r8, %r8
je 0x2d7c
movq %rdx, %rdi
movq %r15, %rsi
callq 0x1140
jmp 0x2d84
pushq $0x4
popq %rdi
callq 0x1120
movq %rax, %rdx
movq %rbp, %rdi
movq 0x18(%rsp), %rbp
movq %r12, %r8
movq 0x8(%rsp), %r12
andl $0x0, (%rdx,%r8,4)
incq %r8
addq $0x8, %r14
addq $0x8, %r15
incq %rbx
jmp 0x2d11
movq 0x30(%rsp), %r13
movq 0x58(%rsp), %r15
movq 0x20(%rsp), %rcx
jmp 0x2e39
pushq $0x4
popq %rdi
callq 0x1120
movq %rax, %rdi
movq %r14, %rdx
movq (%rsp), %r8
movq %r12, %rcx
movq 0x8(%rsp), %r12
movl $0x12, (%rdi,%rbx,4)
leaq -0x1(%r8), %rax
testq %rax, %r8
jne 0x2e0b
movq %rcx, %r12
movq %rdi, %r14
testq %r8, %r8
je 0x2e10
movq (%rsp), %rax
leaq (,%rax,8), %rsi
movq %rdx, %rdi
callq 0x1140
jmp 0x2e18
pushq $-0xb
popq %rax
jmp 0x2e2d
pushq $0x4
popq %rdi
callq 0x1120
movq (%rsp), %r8
movq %rax, %rdx
pushq $-0xb
popq %rax
movq %r14, %rdi
movq %r12, %rcx
movq 0x8(%rsp), %r12
incq %rbx
addl %ecx, %eax
movl %eax, (%rdx,%r8,4)
incq %r8
addq %rcx, %rbp
decq %rbp
incq %rbp
cmpl $0x13, -0x4(%rdi,%rbx,4)
jb 0x28f6
leaq 0x458d(%rip), %rdi      # 0x73e1
leaq 0x4496(%rip), %rsi      # 0x72f1
leaq 0x4512(%rip), %rcx      # 0x7374
movl $0xb2, %edx
callq 0x10b0
xorl %eax, %eax
cmpq $0x13, %rax
je 0x2e82
andq $0x0, 0x110(%rsp,%rax,8)
incq %rax
jmp 0x2e6e
xorl %eax, %eax
cmpq %rax, %rbx
je 0x2e99
movl (%rdi,%rax,4), %ecx
incq 0x110(%rsp,%rcx,8)
incq %rax
jmp 0x2e84
movq %rdx, 0x18(%rsp)
movq %rdi, %rbp
leaq 0x110(%rsp), %rdi
pushq $0x13
popq %rbx
pushq $0x7
popq %r14
leaq 0x70(%rsp), %r15
movq %rbx, %rsi
movl %r14d, %edx
movq %r15, %rcx
callq 0x632f
leaq 0xc0(%rsp), %rcx
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x6144
pushq $0xf
popq %rax
leaq 0x497d(%rip), %rbx      # 0x7860
movq %rax, %r15
subq $0x1, %rax
jb 0x2efd
movl 0x10(%rbx,%rax,4), %ecx
cmpq $0x0, 0x110(%rsp,%rcx,8)
je 0x2ee3
jmp 0x2f00
xorl %r15d, %r15d
pushq $0x5
popq %rsi
movq 0x50(%rsp), %rdi
movq 0x40(%rsp), %r12
movq %r12, %rdx
movq %r13, %rcx
movq 0x38(%rsp), %r14
movq %r14, %r8
callq 0x302a
movl 0x4c(%rsp), %edi
pushq $0x5
popq %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x302a
pushq $0x4
popq %rsi
movl %r15d, %edi
movq %r12, %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x302a
movl %r15d, %eax
leaq 0x10(,%rax,4), %r15
xorl %r12d, %r12d
pushq $0x3
popq %r14
cmpq %r12, %r15
je 0x2f83
movl (%r12,%rbx), %eax
movl 0x70(%rsp,%rax,4), %edi
movl %r14d, %esi
movq 0x40(%rsp), %rdx
movq %r13, %rcx
movq 0x38(%rsp), %r8
callq 0x302a
addq $0x4, %r12
jmp 0x2f5b
xorl %ebx, %ebx
leaq 0x4920(%rip), %r14      # 0x78ac
movq 0x40(%rsp), %r15
movq 0x8(%rsp), %r12
movq %rbp, %rcx
cmpq %rbx, 0x10(%rsp)
je 0x2ff3
movl (%rcx,%rbx,4), %eax
movl 0xc0(%rsp,%rax,4), %edi
movl 0x70(%rsp,%rax,4), %esi
movq %r15, %rdx
movq %r13, %rcx
movq 0x38(%rsp), %r8
callq 0x30d4
movq %rbp, %rcx
movl (%rbp,%rbx,4), %eax
addl $-0x10, %eax
cmpl $0x2, %eax
ja 0x2fee
movl %eax, %eax
movl (%r14,%rax,4), %esi
movq 0x18(%rsp), %rax
movl (%rax,%rbx,4), %edi
movq %r15, %rdx
movq %r13, %rcx
movq 0x38(%rsp), %r8
callq 0x302a
movq %rbp, %rcx
incq %rbx
jmp 0x2f99
movq %rcx, %rbx
movq %r12, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rdi
callq 0x1170