// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x188068
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x188075
leaq 0x4f985(%rip), %rax     # 0x1d79f4
movq %rax, -0x48(%rbp)
jmp 0x188075
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x4f96c(%rip), %rsi     # 0x1d79f0
movb $0x0, %al
callq 0x15b400
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x1880b0
movl $0x1, -0x38(%rbp)
jmp 0x188159
movl -0x1c(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x1880c4
movq -0x18(%rbp), %rdi
callq 0x188170
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpl %ecx, %eax
jae 0x188114
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x30(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %eax
cmpl -0x2c(%rbp), %eax
jbe 0x188107
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x30(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %eax
movl %eax, -0x2c(%rbp)
jmp 0x188109
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1880d2
cmpl $0x30, -0x2c(%rbp)
jbe 0x18811c
jmp 0x188159
movl -0x1c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x188140
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x2c(%rbp), %ecx
callq 0x188210
movl %eax, -0x38(%rbp)
jmp 0x188157
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x2c(%rbp), %ecx
callq 0x1883b0
movl %eax, -0x38(%rbp)
jmp 0x188159
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x15b5a0
movl -0x38(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop