// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x450, %rsp            # imm = 0x450
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rdi
callq 0x182a50
movl %eax, -0x28(%rbp)
cmpl $0x918, -0x24(%rbp)     # imm = 0x918
jg 0x18286e
jmp 0x18288d
leaq 0x3edbc(%rip), %rdi     # 0x1c1631
leaq 0x3ed77(%rip), %rsi     # 0x1c15f3
movl $0xe0, %edx
leaq 0x3edd4(%rip), %rcx     # 0x1c165c
callq 0x598a0
cmpl $0x0, -0x28(%rbp)
je 0x18289a
cmpq $0x0, -0x10(%rbp)
jne 0x1828a5
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x182a3b
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq (%rcx), %rcx
movq -0x10(%rbp), %rdx
movq 0x20(%rdx), %rdx
movslq 0x18(%rdx), %rdx
shlq $0x2, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jb 0x1829b2
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl 0x18(%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x5b060
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x182917
movl $0x0, -0x4(%rbp)
jmp 0x182a3b
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jle 0x18292a
movl -0x28(%rbp), %eax
movl %eax, -0x44c(%rbp)
jmp 0x182933
movl -0x2c(%rbp), %eax
movl %eax, -0x44c(%rbp)
movl -0x44c(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x38(%rbp), %rax
movslq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x5b060
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x18297b
movq -0x38(%rbp), %rdi
callq 0x5b920
movl $0x0, -0x4(%rbp)
jmp 0x182a3b
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
cmpl $0x200, -0x24(%rbp)     # imm = 0x200
jg 0x1829df
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
leaq -0x440(%rbp), %r8
callq 0x182a50
jmp 0x182a35
movslq -0x24(%rbp), %rdi
movl $0x2, %esi
callq 0x5b060
movq %rax, -0x448(%rbp)
cmpq $0x0, -0x448(%rbp)
jne 0x182a07
movl $0x0, -0x4(%rbp)
jmp 0x182a3b
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
movq -0x448(%rbp), %r8
callq 0x182a50
movq -0x448(%rbp), %rdi
callq 0x5b920
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x450, %rsp            # imm = 0x450
popq %rbp
retq
nopw (%rax,%rax)