// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x15b580
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rcx
leaq 0x53b01(%rip), %rsi     # 0x1d7235
movb $0x0, %al
callq 0x15b350
cmpl $0x40, -0x14(%rbp)
je 0x18374f
cmpl $0x100, -0x14(%rbp)     # imm = 0x100
je 0x18374f
jmp 0x1838ba
movq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x8(%rbp), %rsi
addq $0x38, %rsi
addq $0x8, %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x38(%rbp), %rdi
movl -0x28(%rbp), %edx
leaq 0x53ab1(%rip), %rsi     # 0x1d7244
movb $0x0, %al
callq 0x15b350
movq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x18385d
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x8(%rbp), %rsi
addq $0x38, %rsi
addq $0x8, %rsi
callq 0x165170
movb %al, -0x41(%rbp)
movzbl -0x41(%rbp), %eax
shrl $0x4, %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movzbl -0x41(%rbp), %eax
andl $0xf, %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movl -0x4c(%rbp), %edx
movl -0x30(%rbp), %ecx
movl -0x30(%rbp), %r8d
addl -0x4c(%rbp), %r8d
subl $0x1, %r8d
movl -0x48(%rbp), %r9d
leaq 0x53a46(%rip), %rsi     # 0x1d7258
movb $0x0, %al
callq 0x15b400
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x183846
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl -0x30(%rbp), %edx
movl -0x4c(%rbp), %eax
movl %eax, %ecx
movl -0x48(%rbp), %r8d
callq 0x183540
cmpl $0x0, %eax
jne 0x183844
jmp 0x1838ba
jmp 0x183846
movl -0x4c(%rbp), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1837af
movq -0x38(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %edx
movl -0x14(%rbp), %ecx
leaq 0x539fe(%rip), %rsi     # 0x1d727a
movb $0x0, %al
callq 0x15b350
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rdx
movl $0x3, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0x188020
cmpl $0x0, %eax
jne 0x1838b3
jmp 0x1838ba
movl $0x1, -0x40(%rbp)
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x15b5a0
movl -0x40(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop