// File: /workspace/github_c_makefile_star2_O3/enzo1982[P]smooth/libraries/libwebp/src/dec/vp8l_dec.c
// Function: ReadHuffmanCodesHelper
int ReadHuffmanCodesHelper(int color_cache_bits, int num_htree_groups,
                           int num_htree_groups_max, const int* const mapping,
                           VP8LDecoder* const dec,
                           HuffmanTables* const huffman_tables,
                           HTreeGroup** const htree_groups) {
  int i, j, ok = 0;
  const int max_alphabet_size =
      kAlphabetSize[0] + ((color_cache_bits > 0) ? 1 << color_cache_bits : 0);
  const int table_size = kTableSize[color_cache_bits];
  int* code_lengths = NULL;

  if ((mapping == NULL && num_htree_groups != num_htree_groups_max) ||
      num_htree_groups > num_htree_groups_max) {
    goto Error;
  }

  code_lengths =
      (int*)WebPSafeCalloc((uint64_t)max_alphabet_size, sizeof(*code_lengths));
  *htree_groups = VP8LHtreeGroupsNew(num_htree_groups);

  if (*htree_groups == NULL || code_lengths == NULL ||
      !VP8LHuffmanTablesAllocate(num_htree_groups * table_size,
                                 huffman_tables)) {
    VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY);
    goto Error;
  }

  for (i = 0; i < num_htree_groups_max; ++i) {
    // If the index "i" is unused in the Huffman image, just make sure the
    // coefficients are valid but do not store them.
    if (mapping != NULL && mapping[i] == -1) {
      for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
        int alphabet_size = kAlphabetSize[j];
        if (j == 0 && color_cache_bits > 0) {
          alphabet_size += (1 << color_cache_bits);
        }
        // Passing in NULL so that nothing gets filled.
        if (!ReadHuffmanCode(alphabet_size, dec, code_lengths, NULL)) {
          goto Error;
        }
      }
    } else {
      HTreeGroup* const htree_group =
          &(*htree_groups)[(mapping == NULL) ? i : mapping[i]];
      HuffmanCode** const htrees = htree_group->htrees;
      int size;
      int total_size = 0;
      int is_trivial_literal = 1;
      int max_bits = 0;
      for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
        int alphabet_size = kAlphabetSize[j];
        if (j == 0 && color_cache_bits > 0) {
          alphabet_size += (1 << color_cache_bits);
        }
        size =
            ReadHuffmanCode(alphabet_size, dec, code_lengths, huffman_tables);
        htrees[j] = huffman_tables->curr_segment->curr_table;
        if (size == 0) {
          goto Error;
        }
        if (is_trivial_literal && kLiteralMap[j] == 1) {
          is_trivial_literal = (htrees[j]->bits == 0);
        }
        total_size += htrees[j]->bits;
        huffman_tables->curr_segment->curr_table += size;
        if (j <= ALPHA) {
          int local_max_bits = code_lengths[0];
          int k;
          for (k = 1; k < alphabet_size; ++k) {
            if (code_lengths[k] > local_max_bits) {
              local_max_bits = code_lengths[k];
            }
          }
          max_bits += local_max_bits;
        }
      }
      htree_group->is_trivial_literal = is_trivial_literal;
      htree_group->is_trivial_code = 0;
      if (is_trivial_literal) {
        const int red = htrees[RED][0].value;
        const int blue = htrees[BLUE][0].value;
        const int alpha = htrees[ALPHA][0].value;
        htree_group->literal_arb = ((uint32_t)alpha << 24) | (red << 16) | blue;
        if (total_size == 0 && htrees[GREEN][0].value < NUM_LITERAL_CODES) {
          htree_group->is_trivial_code = 1;
          htree_group->literal_arb |= htrees[GREEN][0].value << 8;
        }
      }
      htree_group->use_packed_table =
          !htree_group->is_trivial_code && (max_bits < HUFFMAN_PACKED_BITS);
      if (htree_group->use_packed_table) BuildPackedTable(htree_group);
    }
  }
  ok = 1;

 Error:
  WebPSafeFree(code_lengths);
  if (!ok) {
    VP8LHuffmanTablesDeallocate(huffman_tables);
    VP8LHtreeGroupsFree(*htree_groups);
    *htree_groups = NULL;
  }
  return ok;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x18(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, %r8
movl %edi, %ecx
movl $0x1, %eax
movl %edi, 0x4(%rsp)
shll %cl, %eax
movq %rax, 0x20(%rsp)
testq %r8, %r8
sete %al
xorl %ebp, %ebp
movq 0x80(%rsp), %r14
movl $0x0, %r13d
cmpl %edx, %esi
setne %cl
jg 0xd3b32
andb %al, %cl
jne 0xd3b32
movl %edx, %ebx
movl %esi, %r12d
movq %r8, 0x40(%rsp)
movl 0x4(%rsp), %edx
movslq %edx, %rax
leaq 0x5607e(%rip), %rcx     # 0x129820
movzwl (%rcx,%rax,2), %r15d
movl $0x118, %eax            # imm = 0x118
movq 0x20(%rsp), %rcx
addl %eax, %ecx
testl %edx, %edx
cmovlel %eax, %ecx
movslq %ecx, %rdi
movl $0x4, %esi
callq 0x56580
movq %rax, %r13
movl %r12d, %edi
callq 0xf0140
movq %rax, (%r14)
testq %rax, %rax
je 0xd3afc
testq %r13, %r13
je 0xd3afc
imull %r12d, %r15d
movl %r15d, %edi
movq 0x18(%rsp), %rsi
callq 0xf0927
testl %eax, %eax
je 0xd3afc
testl %ebx, %ebx
jle 0xd3aed
movl %ebx, %eax
movq %rax, 0x30(%rsp)
xorl %ecx, %ecx
movq %r13, 0x28(%rsp)
movq 0x40(%rsp), %rax
testq %rax, %rax
movq %rcx, 0x38(%rsp)
je 0xd387a
movl (%rax,%rcx,4), %eax
cmpl $-0x1, %eax
jne 0xd387c
xorl %ebx, %ebx
testq %rbx, %rbx
movl $0x0, %edi
cmovel 0x20(%rsp), %edi
cmpl $0x0, 0x4(%rsp)
leaq 0x55fca(%rip), %rax     # 0x129810
movzwl (%rbx,%rax), %eax
movl $0x0, %ecx
cmovlel %ecx, %edi
addl %eax, %edi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0xd3b64
testl %eax, %eax
je 0xd3b2a
addq $0x2, %rbx
cmpq $0xa, %rbx
jne 0xd382d
jmp 0xd3ad2
movl %ecx, %eax
cltq
imulq $0x238, %rax, %r8       # imm = 0x238
addq (%r14), %r8
movl $0x1, %r15d
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r10d, %r10d
movq %r8, 0x8(%rsp)
movq %r13, %rdx
movl %r10d, %r13d
testq %r14, %r14
movl $0x0, %ebx
cmovel 0x20(%rsp), %ebx
cmpl $0x0, 0x4(%rsp)
leaq 0x55f56(%rip), %rax     # 0x129810
movzwl (%rax,%r14,2), %eax
movl $0x0, %ecx
cmovlel %ecx, %ebx
addl %eax, %ebx
movl %ebx, %edi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %r12
movq %r12, %rcx
callq 0xd3b64
movq 0x8(%rsp), %r8
movq 0x20(%r12), %rcx
movq 0x8(%rcx), %rcx
movq %rcx, (%r8,%r14,8)
testl %eax, %eax
je 0xd3b19
testl %r15d, %r15d
je 0xd390f
movb (%rcx), %cl
testb $0x3, %r14b
je 0xd3916
xorl %r15d, %r15d
testb %cl, %cl
sete %r15b
jmp 0xd391c
movb (%rcx), %cl
xorl %r15d, %r15d
jmp 0xd391c
movl $0x1, %r15d
movl %r13d, %r10d
movzbl %cl, %ecx
addl %ecx, %r10d
movq 0x18(%rsp), %rcx
movq 0x20(%rcx), %rcx
cltq
shlq $0x2, %rax
addq %rax, 0x8(%rcx)
cmpq $0x4, %r14
movq 0x28(%rsp), %r13
je 0xd3974
movl (%r13), %eax
cmpl $0x2, %ebx
jl 0xd3965
movl %ebx, %ecx
movl $0x1, %edx
movl (%r13,%rdx,4), %esi
cmpl %eax, %esi
cmovgl %esi, %eax
incq %rdx
cmpq %rdx, %rcx
jne 0xd3953
addl %eax, %ebp
incq %r14
cmpq $0x5, %r14
jne 0xd389b
movl %r15d, 0x28(%r8)
movl $0x0, 0x30(%r8)
testl %r15d, %r15d
je 0xd39d7
movq 0x8(%r8), %rax
movq 0x10(%r8), %rcx
movzwl 0x2(%rax), %eax
movzwl 0x2(%rcx), %ecx
movq 0x18(%r8), %rdx
movzwl 0x2(%rdx), %edx
shll $0x18, %edx
shll $0x10, %eax
orl %ecx, %eax
orl %edx, %eax
movl %eax, 0x2c(%r8)
testl %r10d, %r10d
jne 0xd39d7
movq (%r8), %rcx
movzwl 0x2(%rcx), %ecx
cmpl $0xff, %ecx
ja 0xd39d7
movq $0x1, 0x30(%r8)
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, 0x2c(%r8)
xorl %ebp, %ebp
jmp 0xd3ad2
xorl %eax, %eax
cmpl $0x6, %ebp
setl %al
movl %eax, 0x34(%r8)
movl $0x0, %ebp
jge 0xd3ad2
movq $-0x40, %r14
movl $0x38, %r15d
movq (%r8), %rcx
movzbl 0x100(%rcx,%r14,4), %eax
movzwl 0x102(%rcx,%r14,4), %edi
cmpl $0x100, %edi            # imm = 0x100
jb 0xd3a32
orl $0x100, %eax            # imm = 0x100
movl %eax, 0x238(%r8,%r14,8)
movl %edi, 0x23c(%r8,%r14,8)
jmp 0xd3ac5
leaq (%r8,%r15), %rbx
movzbl 0x101(%rcx,%r14,4), %ecx
movq $0x0, 0x238(%r8,%r14,8)
shll $0x10, %edi
shll $0x8, %ecx
orl %eax, %edi
orl %ecx, %edi
movl $0x8, %esi
movq %rbx, %rdx
callq 0xd6757
leal 0x40(%r14), %r12d
movl %eax, %ecx
shrl %cl, %r12d
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movl (%rax,%r12,4), %edi
movl $0x10, %esi
movq %rbx, %rdx
callq 0xd6757
movl %eax, %ecx
shrl %cl, %r12d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax,%r12,4), %edi
xorl %esi, %esi
movq %rbx, %rdx
callq 0xd6757
movl %eax, %ecx
shrl %cl, %r12d
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movl (%rax,%r12,4), %edi
movl $0x18, %esi
movq %rbx, %rdx
callq 0xd6757
movq 0x8(%rsp), %r8
addq $0x8, %r15
incq %r14
jne 0xd39fb
movq 0x38(%rsp), %rcx
incq %rcx
cmpq 0x30(%rsp), %rcx
movq 0x80(%rsp), %r14
jne 0xd3814
movq %r13, %rdi
callq 0x57290
movl $0x1, %ebp
jmp 0xd3b53
movq 0x10(%rsp), %rax
movl (%rax), %eax
cmpl $0x5, %eax
je 0xd3b0c
testl %eax, %eax
jne 0xd3b32
movq 0x10(%rsp), %rax
movl $0x1, (%rax)
jmp 0xd3b32
xorl %ebp, %ebp
movq 0x80(%rsp), %r14
movq 0x28(%rsp), %r13
jmp 0xd3b32
movq 0x80(%rsp), %r14
movq %r13, %rdi
callq 0x57290
movq 0x18(%rsp), %rdi
callq 0xf0967
movq (%r14), %rdi
callq 0xf0185
movq $0x0, (%r14)
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq