// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pack.c
// Function: read_tree
static int read_tree(deark *c, lctx *d, i64 pos1)
{
	i64 pos = pos1;
	UI lv;
	int retval = 0;

	d->depth = (UI)de_getbyte_p(&pos);
	de_dbg(c, "depth: %u", d->depth);
	if(d->depth>=PCK_MAX_LEVELS) goto done;
	if(d->depth<1) goto done;

	for(lv=0; lv<d->depth; lv++) {
		d->leaves_per_level[lv] = (UI)de_getbyte_p(&pos);
		if(lv==d->depth-1) {
			// The count of leaves in the last level is biased, and the last leaf
			// (for EOF) is virtual (not stored in the following table).
			// The bias is 2 if you think it includes the virtual leaf, otherwise 1.
			d->leaves_per_level[lv] += 2;
		}
		de_dbg2(c, "num length %u codes: %u", (UI)(lv+1), d->leaves_per_level[lv]);
	}

	for(lv=0; lv<d->depth; lv++) {
		UI n_stored_leaves_this_level;
		UI k;

		if(lv==d->depth-1) {
			n_stored_leaves_this_level = d->leaves_per_level[lv] - 1;
		}
		else {
			n_stored_leaves_this_level = d->leaves_per_level[lv];
		}

		for(k=0; k<n_stored_leaves_this_level; k++) {
			u8 ch;

			ch = de_getbyte_p(&pos);
			de_dbg3(c, "lv=%u ch=%u", lv, (UI)ch);
			fmtutil_huffman_record_a_code_length(c, d->ht->builder, (fmtutil_huffman_valtype)ch, lv+1);
		}

		if(lv==d->depth-1) {
			de_dbg3(c, "lv=%u EOF", lv);
			fmtutil_huffman_record_a_code_length(c, d->ht->builder, (fmtutil_huffman_valtype)PCK_EOF_CODE, lv+1);
		}
	}

	if(!fmtutil_huffman_make_canonical_code(c, d->ht->bk, d->ht->builder, FMTUTIL_MCTFLAG_LEFT_ALIGN_BRANCHES, NULL)) {
		de_err(c, "Failed to decode Huffman tree");
	}

	retval = 1;
done:
	d->tree_def_size = pos - pos1;
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x48(%rax), %edx
leaq 0x9e6ea(%rip), %rsi     # 0x1afa8a
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpl $0x30, 0x48(%rax)
jb 0x1113b6
jmp 0x111596
movq -0x10(%rbp), %rax
cmpl $0x1, 0x48(%rax)
jae 0x1113c5
jmp 0x111596
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jae 0x111448
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movzbl %al, %edx
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %edx, 0x4c(%rax,%rcx,4)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x48(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x11141a
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movl 0x4c(%rax,%rcx,4), %edx
addl $0x2, %edx
movl %edx, 0x4c(%rax,%rcx,4)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %edx
addl $0x1, %edx
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movl 0x4c(%rax,%rcx,4), %ecx
leaq 0xb2701(%rip), %rsi     # 0x1c3b37
movb $0x0, %al
callq 0x15b350
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1113cc
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jae 0x11154d
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x48(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x111483
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movl 0x4c(%rax,%rcx,4), %eax
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x111491
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movl 0x4c(%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x1114f7
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, -0x31(%rbp)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %edx
movzbl -0x31(%rbp), %ecx
leaq 0xb2689(%rip), %rsi     # 0x1c3b4f
movb $0x0, %al
callq 0x15b400
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
movzbl -0x31(%rbp), %edx
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
callq 0x187f20
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x111498
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x48(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x11153d
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %edx
leaq 0xb2645(%rip), %rsi     # 0x1c3b5b
movb $0x0, %al
callq 0x15b400
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x187f20
jmp 0x11153f
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x11144f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdx
movl $0x1, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0x188020
cmpl $0x0, %eax
jne 0x11158f
movq -0x8(%rbp), %rdi
leaq 0xb25dd(%rip), %rsi     # 0x1c3b65
movb $0x0, %al
callq 0x15bfb0
movl $0x1, -0x28(%rbp)
movq -0x20(%rbp), %rcx
subq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x28(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop