// File: /workspace/github_c_makefile_star2_O3/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, %r12
movl %edx, %ebp
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
movq (%rdi), %rbx
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
movq %r14, 0x18(%rsp)
movq 0x40(%r14), %rcx
leaq 0x49825(%rip), %rsi     # 0x14b809
xorl %r14d, %r14d
movq %rbx, (%rsp)
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe87e4
cmpl $0x100, %ebp            # imm = 0x100
je 0x102009
cmpl $0x40, %ebp
jne 0x102156
movq 0x18(%rsp), %r14
leaq 0x40(%r14), %r15
movq (%rsp), %rbx
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x38(%r14), %rdi
movq %r15, 0x28(%rsp)
movq %r15, %rsi
callq 0xeeb4b
movzbl %al, %eax
movq %rax, 0x20(%rsp)
leal 0x1(%rax), %edx
leaq 0x497d2(%rip), %rsi     # 0x14b818
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe87e4
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
xorl %eax, %eax
xorl %r13d, %r13d
movl %ebp, 0x8(%rsp)
movq %rax, 0x30(%rsp)
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x28(%rsp), %rsi
callq 0xeeb4b
movzbl %al, %r14d
movl %r14d, %r15d
shrl $0x4, %r15d
leal 0x1(%r15), %r12d
andl $0xf, %r14d
incl %r14d
leal (%r15,%r13), %r8d
movq (%rsp), %rdi
leaq 0x49789(%rip), %rsi     # 0x14b82c
movl %r12d, %edx
movl %r13d, %ecx
movl %r14d, %r9d
xorl %eax, %eax
callq 0xe889f
cmpl %ebp, %r13d
jae 0x1020e5
movq 0x10(%rsp), %rax
movq (%rax), %rbp
movl %r12d, %ebx
movl %r13d, %r12d
movq 0x10(%rbp), %rsi
movq (%rsp), %rdi
movl %r12d, %edx
movl %r14d, %ecx
callq 0x1049cc
testl %eax, %eax
je 0x102153
incl %r12d
decq %rbx
jne 0x1020c6
addl %r15d, %r13d
incl %r13d
movq 0x30(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl 0x20(%rsp), %ecx
movl 0x8(%rsp), %ebp
jne 0x102066
movq (%rsp), %rbx
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
leaq 0x49735(%rip), %rsi     # 0x14b84e
movq %rbx, %rdi
movl %r13d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe87e4
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 0x104a46
xorl %r14d, %r14d
testl %eax, %eax
setne %r14b
jmp 0x102156
xorl %r14d, %r14d
movl 0xc(%rsp), %esi
movq (%rsp), %rdi
callq 0xe8a25
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq