// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/fmtutil-huffman.c
// Function: fmtutil_huffman_make_canonical_code
int fmtutil_huffman_make_canonical_code(deark *c, struct fmtutil_huffman_codebook *bk,
	struct fmtutil_huffman_code_builder *builder, UI flags, const char *title)
{
	UI max_sym_len_used;
	UI i;
	int saved_indent_level;
	int retval = 0;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg3(c, "%s:", (title?title:"derived huffman codebook"));
	de_dbg_indent(c, 1);

	if(!builder->lengths_arr) {
		retval = 1;
		goto done;
	}

	if(flags & FMTUTIL_MCTFLAG_LAST_CODE_FIRST) {
		// Bit of a hack. Instead of each worker function having to have a way
		// to read the array from back to front, reverse the order of items in
		// the array, so we don't have to deal with it later.
		reverse_lengths_array(builder);
	}

	// Find the maximum length
	max_sym_len_used = 0;
	for(i=0; i<(UI)builder->lengths_arr_numused; i++) {
		if(builder->lengths_arr[i].len > max_sym_len_used) {
			max_sym_len_used = builder->lengths_arr[i].len;
		}
	}
	if(max_sym_len_used>FMTUTIL_HUFFMAN_MAX_CODE_LENGTH) {
		goto done;
	}

	if(flags & FMTUTIL_MCTFLAG_LEFT_ALIGN_BRANCHES) {
		retval = fmtutil_huffman_make_canonical_tree2(c, bk, builder, max_sym_len_used);
	}
	else {
		retval = fmtutil_huffman_make_canonical_tree1(c, bk, builder, max_sym_len_used);
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r13
movl %ecx, %r12d
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
leaq 0x14(%rsp), %rsi
callq 0xe958c
testq %r13, %r13
leaq 0x46ba4(%rip), %rdx     # 0x14ba34
cmovneq %r13, %rdx
leaq 0x46b95(%rip), %rsi     # 0x14ba30
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9461
movl $0x1, %r15d
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpq $0x0, 0x10(%r14)
je 0x105145
testb $0x2, %r12b
je 0x104f22
movq 0x8(%r14), %rcx
cmpq $0x2, %rcx
jl 0x104f22
movq %rcx, %rax
shrq $0x3f, %rax
addq %rcx, %rax
sarq %rax
movq $-0x8, %rcx
xorl %edx, %edx
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdi
movq (%rdi,%rdx,8), %r8
leaq (%rdi,%rsi,8), %rsi
movq (%rcx,%rsi), %rsi
movq %rsi, (%rdi,%rdx,8)
movq 0x8(%r14), %rsi
shlq $0x3, %rsi
addq 0x10(%r14), %rsi
movq %r8, (%rcx,%rsi)
incq %rdx
addq $-0x8, %rcx
cmpq %rdx, %rax
jne 0x104eee
movl 0x8(%r14), %eax
xorl %r15d, %r15d
movl $0x0, %ebp
testq %rax, %rax
je 0x104f4c
movq 0x10(%r14), %rcx
xorl %edx, %edx
xorl %ebp, %ebp
movl 0x4(%rcx,%rdx,8), %esi
cmpl %ebp, %esi
cmoval %esi, %ebp
incq %rdx
cmpq %rdx, %rax
jne 0x104f3b
cmpl $0x30, %ebp
ja 0x105145
testb $0x1, %r12b
jne 0x105057
testl %ebp, %ebp
je 0x10513a
movl $0x1, %r13d
xorl %r12d, %r12d
xorl %eax, %eax
xorl %r15d, %r15d
cmpl $0x0, 0x8(%r14)
je 0x105038
xorl %ebx, %ebx
movq 0x10(%r14), %rdx
movl $0x7, %ecx
cmpl %r13d, 0x4(%rdx,%rbx,8)
jne 0x105016
testl %r15d, %r15d
je 0x104faa
incq %r12
movl %r13d, %ecx
subl %eax, %ecx
jbe 0x104fad
shlq %cl, %r12
jmp 0x104fad
xorl %r12d, %r12d
movq 0x8(%rsp), %rdi
cmpl $0x3, (%rdi)
jl 0x104fee
movl $0x48, %esi
leaq 0x20(%rsp), %rdi
movq %r12, %rdx
movl %r13d, %ecx
callq 0xea85a
movq 0x10(%r14), %rcx
movl (%rcx,%rbx,8), %ecx
movq 0x8(%rsp), %rdi
leaq 0x19375(%rip), %rsi     # 0x11e354
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9461
movq 0x8(%rsp), %rdi
incl %r15d
movq 0x10(%r14), %rax
movl (%rax,%rbx,8), %r8d
movq 0x18(%rsp), %rsi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x104901
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
shll $0x3, %ecx
movl %r13d, %eax
cmpl $0x7, %ecx
je 0x10501f
testl %ecx, %ecx
jne 0x105031
incq %rbx
movl 0x8(%r14), %ecx
cmpq %rcx, %rbx
jb 0x104f82
xorl %ecx, %ecx
testl %ecx, %ecx
sete %cl
jmp 0x10503a
movb $0x1, %cl
testb %cl, %cl
je 0x105142
leal 0x1(%r13), %ecx
cmpl %ebp, %r13d
movl %ecx, %r13d
jne 0x104f75
jmp 0x10513a
movl $0x1, %r15d
testl %ebp, %ebp
je 0x105145
leaq 0x192e8(%rip), %r12     # 0x11e354
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %r15d, %r15d
cmpl $0x0, 0x8(%r14)
je 0x10512c
xorl %ebx, %ebx
movq 0x10(%r14), %rax
movl $0x7, %edx
cmpl %ebp, 0x4(%rax,%rbx,8)
jne 0x10510a
subl %ebp, %ecx
shrq %cl, %r13
movq %r13, %rax
incq %rax
testl %r15d, %r15d
movl $0x0, %r13d
cmovneq %rax, %r13
movq 0x8(%rsp), %rdi
cmpl $0x3, (%rdi)
jl 0x1050e4
movl $0x48, %esi
leaq 0x20(%rsp), %rdi
movq %r13, %rdx
movl %ebp, %ecx
callq 0xea85a
movq 0x10(%r14), %rcx
movl (%rcx,%rbx,8), %ecx
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9461
movq 0x8(%rsp), %rdi
incl %r15d
movq 0x10(%r14), %rax
movl (%rax,%rbx,8), %r8d
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x104901
xorl %edx, %edx
testl %eax, %eax
sete %dl
shll $0x3, %edx
movl %ebp, %ecx
cmpl $0x7, %edx
je 0x105113
testl %edx, %edx
jne 0x105125
incq %rbx
movl 0x8(%r14), %eax
cmpq %rax, %rbx
jb 0x105081
xorl %edx, %edx
testl %edx, %edx
sete %al
jmp 0x10512e
movb $0x1, %al
testb %al, %al
je 0x105142
decl %ebp
jne 0x105074
movl $0x1, %r15d
jmp 0x105145
xorl %r15d, %r15d
movl 0x14(%rsp), %esi
movq 0x8(%rsp), %rdi
callq 0xe9595
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq