// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-lzh.c
// Function: lh5x_read_codelengths_tree
static int lh5x_read_codelengths_tree(struct lzh_ctx *cctx, struct lzh_tree_wrapper *tree,
	const char *name)
{
	deark *c = cctx->c;
	UI ncodes;
	UI curr_idx;
	int retval = 0;
	int saved_indent_level;
	char pos_descr[32];

	de_dbg_indent_save(c, &saved_indent_level);
	de_bitreader_describe_curpos(&cctx->bitrd, pos_descr, sizeof(pos_descr));
	de_dbg2(c, "%s tree at %s", name, pos_descr);
	de_dbg_indent(c, 1);

	ncodes = (UI)lzh_getbits(cctx, 5);
	de_dbg2(c, "num codes in %s tree: %u", name, ncodes);

	if(ncodes>LH5X_CODELENGTHS_TREE_MAX_CODES) {
		ncodes = LH5X_CODELENGTHS_TREE_MAX_CODES;
	}

	tree->ht = fmtutil_huffman_create_decoder(c, (i64)ncodes, (i64)ncodes);

	if(ncodes==0) {
		UI null_val;

		null_val = (UI)lzh_getbits(cctx, 5);
		de_dbg3(c, "val0: %u", null_val);
		fmtutil_huffman_add_code(c, tree->ht->bk, 0, 0, (fmtutil_huffman_valtype)null_val);
		retval = 1;
		goto done;
	}

	curr_idx = 0;
	while(curr_idx < ncodes) {
		UI symlen;

		symlen = lh5x_read_a_code_length(cctx);
		de_dbg3(c, "len[%u] = %u", curr_idx, symlen);
		fmtutil_huffman_record_a_code_length(c, tree->ht->builder, (fmtutil_huffman_valtype)curr_idx, symlen);
		curr_idx++;

		if(curr_idx==3) {
			UI extraskip;

			// After the first three lengths is a special 2-bit code that may tell us
			// to skip forward in the lengths table.
			// TODO: Verify that it exists when the number of lengths is exactly 3.
			extraskip = (UI)lzh_getbits(cctx, 2);
			if(extraskip>0) {
				de_dbg3(c, "extra skip: %u", extraskip);
				curr_idx += extraskip;
			}
		}
	}
	if(cctx->bitrd.eof_flag) goto done;

	if(!fmtutil_huffman_make_canonical_code(c, tree->ht->bk, tree->ht->builder, 0, NULL)) goto done;

	retval = 1;
done:
	if(!retval) {
		lzh_set_err_flag(cctx);
	}
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
leaq -0x50(%rbp), %rsi
movl $0x20, %edx
callq 0x16a540
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq -0x50(%rbp), %rcx
leaq 0x55158(%rip), %rsi     # 0x1d6e74
movb $0x0, %al
callq 0x15b350
movq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x5, %esi
callq 0x181a10
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl -0x24(%rbp), %ecx
leaq 0x5512e(%rip), %rsi     # 0x1d6e82
movb $0x0, %al
callq 0x15b350
cmpl $0x14, -0x24(%rbp)
jbe 0x181d68
movl $0x14, -0x24(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
movl -0x24(%rbp), %eax
movl %eax, %edx
callq 0x188560
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
cmpl $0x0, -0x24(%rbp)
jne 0x181ddb
movq -0x8(%rbp), %rdi
movl $0x5, %esi
callq 0x181a10
movl %eax, -0x54(%rbp)
movq -0x20(%rbp), %rdi
movl -0x54(%rbp), %edx
leaq 0x2802a(%rip), %rsi     # 0x1a9dd4
movb $0x0, %al
callq 0x15b400
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
movl -0x54(%rbp), %r8d
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x187720
movl $0x1, -0x2c(%rbp)
jmp 0x181eb7
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x181e77
movq -0x8(%rbp), %rdi
callq 0x182340
movl %eax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
movl -0x28(%rbp), %edx
movl -0x58(%rbp), %ecx
leaq 0x27fd2(%rip), %rsi     # 0x1a9ddd
movb $0x0, %al
callq 0x15b400
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rsi
movl -0x28(%rbp), %edx
movl -0x58(%rbp), %ecx
callq 0x187f20
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
cmpl $0x3, -0x28(%rbp)
jne 0x181e72
movq -0x8(%rbp), %rdi
movl $0x2, %esi
callq 0x181a10
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jbe 0x181e70
movq -0x20(%rbp), %rdi
movl -0x5c(%rbp), %edx
leaq 0x5503b(%rip), %rsi     # 0x1d6e9b
movb $0x0, %al
callq 0x15b400
movl -0x5c(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x181e72
jmp 0x181de2
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x181e83
jmp 0x181eb7
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rdx
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0x188020
cmpl $0x0, %eax
jne 0x181eb0
jmp 0x181eb7
movl $0x1, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x181ec6
movq -0x8(%rbp), %rdi
callq 0x1823c0
movq -0x20(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x15b5a0
movl -0x2c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)