// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %rcx, %r14
movl %edx, %r15d
movq %rsi, 0x10(%rsp)
movq %rdi, %r12
movq (%rdi), %rbx
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0xdebae
movq %r12, 0x18(%rsp)
movq 0x40(%r12), %rcx
leaq 0x48351(%rip), %rsi     # 0x13e339
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xde9f1
cmpl $0x100, %r15d           # imm = 0x100
je 0xf600a
cmpl $0x40, %r15d
jne 0xf6122
movl %r15d, 0x8(%rsp)
movq 0x18(%rsp), %r14
leaq 0x40(%r14), %r15
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
movq 0x38(%r14), %rdi
movq %r15, 0x20(%rsp)
movq %r15, %rsi
callq 0xe3f2a
movzbl %al, %r13d
incl %r13d
leaq 0x48304(%rip), %rsi     # 0x13e348
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xde9f1
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
xorl %r12d, %r12d
movl %r12d, %r14d
subl $0x1, %r13d
jb 0xf60d9
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x20(%rsp), %rsi
callq 0xe3f2a
movzbl %al, %ebp
movl %ebp, %r8d
shrl $0x4, %r8d
leal 0x1(%r8), %r15d
andl $0xf, %ebp
incl %ebp
leal (%r8,%r14), %r12d
incl %r12d
addl %r14d, %r8d
movq %rbx, %rdi
leaq 0x482bb(%rip), %rsi     # 0x13e35c
movl %r15d, %edx
movl %r14d, %ecx
movl %ebp, %r9d
xorl %eax, %eax
callq 0xdea83
cmpl 0x8(%rsp), %r14d
jae 0xf605e
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movl %r15d, %ecx
movq %rbx, %rdi
movl %r14d, %edx
movl %ebp, %r8d
callq 0xf5f56
testl %eax, %eax
jne 0xf605e
xorl %ebp, %ebp
jmp 0xf6122
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0x48293(%rip), %rsi     # 0x13e37e
movq %rbx, %rdi
movl %r14d, %edx
movl 0x8(%rsp), %ecx
xorl %eax, %eax
callq 0xde9f1
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
xorl %r8d, %r8d
callq 0xf8362
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq