// File: /workspace/github_c_makefile_star2_O3/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, %r13d
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
leaq 0x14(%rsp), %rsi
callq 0xe8a1c
testq %r12, %r12
leaq 0x475d7(%rip), %rdx     # 0x14c050
cmovneq %r12, %rdx
leaq 0x475c8(%rip), %rsi     # 0x14c04c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe889f
movl $0x1, %r12d
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpq $0x0, 0x10(%r14)
je 0x104cee
testb $0x2, %r13b
je 0x104b0b
movq 0x8(%r14), %rcx
cmpq $0x2, %rcx
jl 0x104b0b
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 0x104ad7
movl 0x8(%r14), %eax
testq %rax, %rax
je 0x104c1e
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 0x104b20
xorl %r12d, %r12d
cmpl $0x30, %ebp
ja 0x104cee
testb $0x1, %r13b
jne 0x104c2d
testl %ebp, %ebp
je 0x104c22
movl $0x1, %r12d
xorl %r13d, %r13d
xorl %edx, %edx
xorl %r15d, %r15d
testl %eax, %eax
je 0x104c0b
xorl %ebx, %ebx
movq 0x10(%r14), %rsi
cmpl %r12d, 0x4(%rsi,%rbx,8)
jne 0x104bfd
testl %r15d, %r15d
je 0x104b8f
incq %r13
movl %r12d, %ecx
subl %edx, %ecx
movq 0x8(%rsp), %rdi
jbe 0x104b97
shlq %cl, %r13
jmp 0x104b97
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
cmpl $0x3, (%rdi)
jl 0x104bd7
movl $0x48, %esi
leaq 0x20(%rsp), %rdi
movq %r13, %rdx
movl %r12d, %ecx
callq 0xe9d03
movq 0x10(%r14), %rcx
movl (%rcx,%rbx,8), %ecx
movq 0x8(%rsp), %rdi
leaq 0x198a0(%rip), %rsi     # 0x11e464
movq %rax, %rdx
xorl %eax, %eax
callq 0xe889f
movq 0x8(%rsp), %rdi
movq 0x10(%r14), %rsi
movl (%rsi,%rbx,8), %r8d
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x1044c1
testl %eax, %eax
je 0x104ceb
incl %r15d
movl 0x8(%r14), %eax
movl %r12d, %edx
incq %rbx
movl %eax, %ecx
cmpq %rcx, %rbx
jb 0x104b67
leal 0x1(%r12), %ecx
cmpl %ebp, %r12d
movl %ecx, %r12d
jne 0x104b5d
jmp 0x104c22
testb $0x1, %r13b
movl $0x1, %r12d
jmp 0x104cee
movl $0x1, %r12d
testl %ebp, %ebp
je 0x104cee
xorl %r13d, %r13d
xorl %ecx, %ecx
xorl %r15d, %r15d
testl %eax, %eax
je 0x104ce1
xorl %ebx, %ebx
movq 0x10(%r14), %rdx
cmpl %ebp, 0x4(%rdx,%rbx,8)
jne 0x104cd3
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 0x104cb3
movl $0x48, %esi
leaq 0x20(%rsp), %rdi
movq %r13, %rdx
movl %ebp, %ecx
callq 0xe9d03
movq 0x10(%r14), %rcx
movl (%rcx,%rbx,8), %ecx
movq 0x8(%rsp), %rdi
leaq 0x197c4(%rip), %rsi     # 0x11e464
movq %rax, %rdx
xorl %eax, %eax
callq 0xe889f
movq 0x8(%rsp), %rdi
movq 0x10(%r14), %rdx
movl (%rdx,%rbx,8), %r8d
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x1044c1
testl %eax, %eax
je 0x104ceb
incl %r15d
movl 0x8(%r14), %eax
movl %ebp, %ecx
incq %rbx
movl %eax, %edx
cmpq %rdx, %rbx
jb 0x104c4d
decl %ebp
jne 0x104c43
jmp 0x104cee
xorl %r12d, %r12d
movl 0x14(%rsp), %esi
movq 0x8(%rsp), %rdi
callq 0xe8a25
movl %r12d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq