// File: /workspace/github_c_makefile_star2_O2/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, %r12
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
leaq 0x14(%rsp), %rsi
callq 0xdebae
testq %r12, %r12
leaq 0x467c0(%rip), %rdx     # 0x13eb54
cmovneq %r12, %rdx
leaq 0x467b1(%rip), %rsi     # 0x13eb50
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdea83
pushq $0x1
popq %r15
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movl %r15d, %esi
callq 0xdeba7
cmpq $0x0, 0x10(%r14)
je 0xf85db
testb $0x2, %bpl
jne 0xf84cd
movl 0x8(%r14), %eax
xorl %ecx, %ecx
xorl %r12d, %r12d
cmpq %rcx, %rax
je 0xf83f4
movq 0x10(%r14), %rdx
movl 0x4(%rdx,%rcx,8), %edx
cmpl %r12d, %edx
cmoval %edx, %r12d
incq %rcx
jmp 0xf83db
xorl %r15d, %r15d
cmpl $0x30, %r12d
ja 0xf85db
testb $0x1, %bpl
jne 0xf851f
incl %r12d
pushq $0x1
popq %rbp
xorl %r15d, %r15d
xorl %edx, %edx
xorl %r13d, %r13d
cmpl %r12d, %ebp
je 0xf85fb
xorl %ebx, %ebx
movl %eax, %ecx
cmpq %rcx, %rbx
jae 0xf84c6
movq 0x10(%r14), %rsi
cmpl %ebp, 0x4(%rsi,%rbx,8)
jne 0xf84be
testl %r15d, %r15d
je 0xf8455
incq %r13
movl %ebp, %ecx
subl %edx, %ecx
movq 0x8(%rsp), %rdi
jbe 0xf845d
shlq %cl, %r13
jmp 0xf845d
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
cmpl $0x3, (%rdi)
jl 0xf849a
leaq 0x20(%rsp), %rdi
pushq $0x48
popq %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0xdfc03
movq 0x10(%r14), %rcx
movl (%rcx,%rbx,8), %ecx
movq 0x8(%rsp), %rdi
leaq 0x18f09(%rip), %rsi     # 0x111390
movq %rax, %rdx
xorl %eax, %eax
callq 0xdea83
movq 0x8(%rsp), %rdi
movq 0x10(%r14), %rsi
movl (%rsi,%rbx,8), %r8d
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0xf7e56
testl %eax, %eax
je 0xf85d8
incl %r15d
movl 0x8(%r14), %eax
movl %ebp, %edx
incq %rbx
jmp 0xf8424
incl %ebp
jmp 0xf8419
movq 0x8(%r14), %rax
pushq $0x2
popq %rcx
cqto
idivq %rcx
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
pushq $-0x8
popq %rdx
cmpq %rcx, %rax
je 0xf83d2
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdi
movq (%rdi,%rcx,8), %r8
leaq (%rdi,%rsi,8), %rsi
movq (%rdx,%rsi), %rsi
movq %rsi, (%rdi,%rcx,8)
movq 0x8(%r14), %rsi
shlq $0x3, %rsi
addq 0x10(%r14), %rsi
movq %r8, (%rdx,%rsi)
incq %rcx
addq $-0x8, %rdx
jmp 0xf84e5
xorl %r15d, %r15d
xorl %ecx, %ecx
xorl %ebp, %ebp
movl %r12d, %r13d
subl $0x1, %r13d
jb 0xf85fb
xorl %ebx, %ebx
movl %eax, %edx
cmpq %rdx, %rbx
jae 0xf85d0
movq 0x10(%r14), %rdx
cmpl %r12d, 0x4(%rdx,%rbx,8)
jne 0xf85c8
subl %r12d, %ecx
shrq %cl, %rbp
movq %rbp, %rax
incq %rax
testl %r15d, %r15d
movl $0x0, %ebp
cmovneq %rax, %rbp
movq 0x8(%rsp), %rdi
cmpl $0x3, (%rdi)
jl 0xf85a6
leaq 0x20(%rsp), %rdi
pushq $0x48
popq %rsi
movq %rbp, %rdx
movl %r12d, %ecx
callq 0xdfc03
movq 0x10(%r14), %rcx
movl (%rcx,%rbx,8), %ecx
movq 0x8(%rsp), %rdi
leaq 0x18dfd(%rip), %rsi     # 0x111390
movq %rax, %rdx
xorl %eax, %eax
callq 0xdea83
movq 0x8(%rsp), %rdi
movq 0x10(%r14), %rdx
movl (%rdx,%rbx,8), %r8d
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
movl %r12d, %ecx
callq 0xf7e56
testl %eax, %eax
je 0xf85d8
incl %r15d
movl 0x8(%r14), %eax
movl %r12d, %ecx
incq %rbx
jmp 0xf8535
movl %r13d, %r12d
jmp 0xf8529
xorl %r15d, %r15d
movl 0x14(%rsp), %esi
movq 0x8(%rsp), %rdi
callq 0xdebb7
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %r15
jmp 0xf85db