// File: /workspace/github_c_makefile_star2_O1/enzo1982[P]smooth/libraries/libwebp/src/utils/huffman_utils.c
// Function: VP8LBuildHuffmanTable
int VP8LBuildHuffmanTable(HuffmanTables* const root_table, int root_bits,
                          const int code_lengths[], int code_lengths_size) {
  const int total_size =
      BuildHuffmanTable(NULL, root_bits, code_lengths, code_lengths_size, NULL);
  assert(code_lengths_size <= MAX_CODE_LENGTHS_SIZE);
  if (total_size == 0 || root_table == NULL) return total_size;

  if (root_table->curr_segment->curr_table + total_size >=
      root_table->curr_segment->start + root_table->curr_segment->size) {
    // If 'root_table' does not have enough memory, allocate a new segment.
    // The available part of root_table->curr_segment is left unused because we
    // need a contiguous buffer.
    const int segment_size = root_table->curr_segment->size;
    struct HuffmanTablesSegment* next =
        (HuffmanTablesSegment*)WebPSafeMalloc(1, sizeof(*next));
    if (next == NULL) return 0;
    // Fill the new segment.
    // We need at least 'total_size' but if that value is small, it is better to
    // allocate a big chunk to prevent more allocations later. 'segment_size' is
    // therefore chosen (any other arbitrary value could be chosen).
    next->size = total_size > segment_size ? total_size : segment_size;
    next->start =
        (HuffmanCode*)WebPSafeMalloc(next->size, sizeof(*next->start));
    if (next->start == NULL) {
      WebPSafeFree(next);
      return 0;
    }
    next->curr_table = next->start;
    next->next = NULL;
    // Point to the new segment.
    root_table->curr_segment->next = next;
    root_table->curr_segment = next;
  }
  if (code_lengths_size <= SORTED_SIZE_CUTOFF) {
    // use local stack-allocated array.
    uint16_t sorted[SORTED_SIZE_CUTOFF];
    BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits,
                      code_lengths, code_lengths_size, sorted);
  } else {  // rare case. Use heap allocation.
    uint16_t* const sorted =
        (uint16_t*)WebPSafeMalloc(code_lengths_size, sizeof(*sorted));
    if (sorted == NULL) return 0;
    BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits,
                      code_lengths, code_lengths_size, sorted);
    WebPSafeFree(sorted);
  }
  return total_size;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movl %ecx, %ebx
movq %rdx, %r13
movl %esi, %r14d
movq %rdi, %r12
xorl %edi, %edi
xorl %r8d, %r8d
callq 0xf10dc
cmpl $0x919, %ebx            # imm = 0x919
jge 0xf10bd
movl %eax, %ebp
testl %eax, %eax
sete %al
testq %r12, %r12
sete %cl
orb %al, %cl
jne 0xf1096
movq 0x20(%r12), %rax
movslq %ebp, %rcx
shlq $0x2, %rcx
addq 0x8(%rax), %rcx
movslq 0x18(%rax), %r15
leaq (,%r15,4), %rdx
addq (%rax), %rdx
cmpq %rdx, %rcx
jb 0xf1039
movl %r14d, 0x4(%rsp)
movq %r13, 0x8(%rsp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x56b20
xorl %r13d, %r13d
testq %rax, %rax
je 0xf1099
movq %rax, %r14
cmpl %r15d, %ebp
cmovgl %ebp, %r15d
movl %r15d, 0x18(%rax)
movslq %r15d, %rdi
movl $0x4, %esi
callq 0x56b20
movq %rax, (%r14)
testq %rax, %rax
je 0xf10b3
movq %rax, 0x8(%r14)
movq $0x0, 0x10(%r14)
movq 0x20(%r12), %rax
movq %r14, 0x10(%rax)
movq %r14, 0x20(%r12)
movq 0x8(%rsp), %r13
movl 0x4(%rsp), %r14d
cmpl $0x200, %ebx            # imm = 0x200
jg 0xf105e
movq 0x20(%r12), %rax
movq 0x8(%rax), %rdi
leaq 0x10(%rsp), %r8
movl %r14d, %esi
movq %r13, %rdx
movl %ebx, %ecx
callq 0xf10dc
jmp 0xf1096
movl %r14d, %r15d
movl %ebx, %edi
movl $0x2, %esi
callq 0x56b20
testq %rax, %rax
je 0xf10ae
movq %rax, %r14
movq 0x20(%r12), %rax
movq 0x8(%rax), %rdi
movl %r15d, %esi
movq %r13, %rdx
movl %ebx, %ecx
movq %r14, %r8
callq 0xf10dc
movq %r14, %rdi
callq 0x57290
movl %ebp, %r13d
movl %r13d, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
jmp 0xf1099
movq %r14, %rdi
callq 0x57290
jmp 0xf1099
leaq 0x3d99d(%rip), %rdi     # 0x12ea61
leaq 0x3d958(%rip), %rsi     # 0x12ea23
leaq 0x3d9ba(%rip), %rcx     # 0x12ea8c
movl $0xe0, %edx
callq 0x55710