// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-huffman.c
// Function: fmtutil_huffman_make_canonical_tree1
static int fmtutil_huffman_make_canonical_tree1(deark *c, struct fmtutil_huffman_codebook *bk,
	struct fmtutil_huffman_code_builder *builder, UI max_sym_len_used)
{
	UI symlen;
	UI codes_count_total = 0;
	UI prev_code_bit_length = 0;
	u64 prev_code = 0;
	int retval = 0;
	char b2buf[72];

	// For each possible symbol length...
	for(symlen=1; symlen<=max_sym_len_used; symlen++) {
		UI k;

		// Find all the codes that use this symbol length, in order
		for(k=0; k<(UI)builder->lengths_arr_numused; k++) {
			int ret;
			u64 thiscode;

			if(builder->lengths_arr[k].len != symlen) continue;
			// Found a code of the length we're looking for.

			if(codes_count_total==0) {
				thiscode = 0;
			}
			else {
				thiscode = prev_code + 1;
				if(symlen > prev_code_bit_length) {
					thiscode <<= (symlen - prev_code_bit_length);
				}
			}

			prev_code = thiscode;
			prev_code_bit_length = symlen;
			codes_count_total++;

			if(c->debug_level>=3) {
				de_dbg3(c, "code: \"%s\" = %d",
					de_print_base2_fixed(b2buf, sizeof(b2buf), thiscode, symlen),
					(int)builder->lengths_arr[k].val);
			}
			ret = fmtutil_huffman_add_code(c, bk, thiscode, symlen, builder->lengths_arr[k].val);
			if(!ret) {
				goto done;
			}
		}
	}
	retval = 1;

done:
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x1, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
ja 0x18854b
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpl %ecx, %eax
jae 0x18853b
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x84(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %eax
cmpl -0x20(%rbp), %eax
je 0x188436
jmp 0x188527
cmpl $0x0, -0x24(%rbp)
jne 0x188449
movq $0x0, -0x90(%rbp)
jmp 0x18847b
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jbe 0x188479
movl -0x20(%rbp), %ecx
subl -0x28(%rbp), %ecx
movq -0x90(%rbp), %rax
movl %ecx, %ecx
shlq %cl, %rax
movq %rax, -0x90(%rbp)
jmp 0x18847b
movq -0x90(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x3, (%rax)
jl 0x1884ea
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq -0x80(%rbp), %rdi
movq -0x90(%rbp), %rdx
movl -0x20(%rbp), %ecx
movl $0x48, %esi
callq 0x15cf60
movq -0x98(%rbp), %rdi
movq %rax, %rdx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x84(%rbp), %ecx
movl (%rax,%rcx,8), %ecx
leaq 0x21b31(%rip), %rsi     # 0x1aa014
movb $0x0, %al
callq 0x15b400
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x90(%rbp), %rdx
movl -0x20(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x84(%rbp), %r8d
movl (%rax,%r8,8), %r8d
callq 0x187720
movl %eax, -0x88(%rbp)
cmpl $0x0, -0x88(%rbp)
jne 0x188525
jmp 0x188552
jmp 0x188527
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x188404
jmp 0x18853d
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1883ee
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nop