// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/fmtutil-lzh.c
// Function: implode_read_a_tree
static int implode_read_a_tree(struct lzh_ctx *cctx,
	struct lzh_tree_wrapper *tree, UI num_values_expected, const char *name)
{
	UI n;
	UI num_rle_items;
	UI i;
	UI next_val = 0;
	deark *c = cctx->c;
	int saved_indent_level;
	int retval = 0;

	de_dbg_indent_save(c, &saved_indent_level);

	de_dbg2(c, "%s tree at %"I64_FMT, name, cctx->bitrd.curpos);
	if(num_values_expected!=64 && num_values_expected!=256) goto done;
	de_dbg_indent(c, 1);
	n = (UI)dbuf_getbyte_p(cctx->bitrd.f, &cctx->bitrd.curpos);
	num_rle_items = n + 1;
	de_dbg2(c, "num RLE entries: %u", num_rle_items);

	de_dbg_indent(c, 1);
	for(i=0; i<num_rle_items; i++) {
		u8 b;
		UI this_bit_length;
		UI num_codes_with_this_bit_length;

		b = dbuf_getbyte_p(cctx->bitrd.f, &cctx->bitrd.curpos);
		num_codes_with_this_bit_length = ((UI)b >> 4) + 1;
		this_bit_length = ((UI)b & 0x0f) + 1;
		de_dbg3(c, "%u items (%u..%u) w/bit length %u", num_codes_with_this_bit_length,
			next_val, (UI)(next_val+num_codes_with_this_bit_length-1), this_bit_length);

		if(next_val < num_values_expected) {
			if(!huffman_record_len_for_range(c, tree->ht, (fmtutil_huffman_valtype)next_val,
				(i64)num_codes_with_this_bit_length, this_bit_length))
			{
				goto done;
			}
		}
		next_val += num_codes_with_this_bit_length;
	}
	de_dbg_indent(c, -1);

	// The Implode specification apparently requires the trees to always be fully
	// populated (with 256 or 64 items), even though that's inefficient, and there's
	// nothing about the format that really demands it.
	de_dbg2(c, "number of items: %u (expected %u)", next_val, num_values_expected);

	if(!fmtutil_huffman_make_canonical_code(c, tree->ht->bk, tree->ht->builder,
		FMTUTIL_MCTFLAG_LEFT_ALIGN_BRANCHES | FMTUTIL_MCTFLAG_LAST_CODE_FIRST, NULL))
	{
		goto done;
	}

	retval = 1;
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 $0x38, %rsp
movq %rcx, %r14
movl %edx, %r12d
movq %rsi, 0x10(%rsp)
movq %rdi, %r15
movq (%rdi), %rbx
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0xe958c
movq %r15, 0x18(%rsp)
movq 0x40(%r15), %rcx
leaq 0x48960(%rip), %rsi     # 0x14b212
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xe93cf
cmpl $0x100, %r12d           # imm = 0x100
je 0x1028d4
cmpl $0x40, %r12d
jne 0x102a49
movq 0x18(%rsp), %r14
leaq 0x40(%r14), %r15
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x38(%r14), %rdi
movq %r15, 0x30(%rsp)
movq %r15, %rsi
callq 0xef14d
movzbl %al, %r14d
leal 0x1(%r14), %edx
leaq 0x48917(%rip), %rsi     # 0x14b221
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe93cf
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq $0x0, 0x20(%rsp)
xorl %ebp, %ebp
movl %r12d, 0x8(%rsp)
movq %r14, 0x28(%rsp)
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x30(%rsp), %rsi
callq 0xef14d
movzbl %al, %r13d
movl %r13d, %r15d
shrl $0x4, %r15d
leal 0x1(%r15), %edx
andl $0xf, %r13d
incl %r13d
leal (%r15,%rbp), %eax
incl %eax
movl %eax, 0x4(%rsp)
leal (%r15,%rbp), %r8d
movq %rbx, %rdi
leaq 0x488be(%rip), %rsi     # 0x14b235
movl %ebp, %ecx
movl %r13d, %r9d
xorl %eax, %eax
callq 0xe9461
movb $0x1, %al
cmpl %r12d, %ebp
jae 0x1029d7
movl %ebp, %ebp
movq 0x10(%rsp), %rax
movq (%rax), %r14
xorl %r12d, %r12d
movq 0x10(%r14), %rsi
leal (%r12,%rbp), %edx
movq %rbx, %rdi
movl %r13d, %ecx
callq 0x104de7
testl %eax, %eax
je 0x1029bb
leaq 0x1(%r12), %rcx
cmpq %r12, %r15
movq %rcx, %r12
jne 0x102997
testl %eax, %eax
movl 0x8(%rsp), %r12d
movq 0x28(%rsp), %r14
movl 0x4(%rsp), %r15d
movb $0x1, %al
jne 0x1029dc
xorl %eax, %eax
movl %ebp, %r15d
jmp 0x1029dc
movl 0x4(%rsp), %r15d
testb %al, %al
je 0x102a47
movq 0x20(%rsp), %rcx
leal 0x1(%rcx), %eax
movl %r15d, %ebp
cmpl %r14d, %ecx
movq %rax, 0x20(%rsp)
jne 0x102936
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
leaq 0x4884a(%rip), %rsi     # 0x14b257
movq %rbx, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe93cf
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq %rbx, %rdi
movl $0x3, %ecx
xorl %r8d, %r8d
callq 0x104e5d
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
jmp 0x102a49
xorl %ebp, %ebp
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq