// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-lzh.c
// Function: lh5x_read_literals_tree
static int lh5x_read_literals_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, 9);
	de_dbg2(c, "num codes in %s tree: %u", name, ncodes);

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

	if(ncodes>cctx->lh5x_literals_tree_max_codes) {
		goto done;
	}
	if(ncodes==0) {
		UI null_val;

		null_val = (UI)lzh_getbits(cctx, 9);
		de_dbg3(c, "val0: %u", null_val);
		if(null_val >= cctx->lh5x_literals_tree_max_codes) goto done;
		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 x;

		x = read_next_code_using_tree(cctx, &cctx->meta_tree);

		if(x<=2) {
			UI sk;

			sk = lh5x_read_a_skip_length(cctx, x);
			de_dbg3(c, "len[%u]: code=%u => skip:range_code=%u,extra_skip=%u",
				curr_idx, x, x, sk);
			curr_idx += 1 + sk;
		}
		else {
			UI symlen;

			symlen = x-2;
			de_dbg3(c, "len[%u]: code=%u => len=%u", curr_idx, x, symlen);
			fmtutil_huffman_record_a_code_length(c, tree->ht->builder, (fmtutil_huffman_valtype)curr_idx, symlen);
			curr_idx++;
		}
	}
	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 0x54f38(%rip), %rsi     # 0x1d6e74
movb $0x0, %al
callq 0x15b350
movq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x9, %esi
callq 0x181a10
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl -0x24(%rbp), %ecx
leaq 0x54f0e(%rip), %rsi     # 0x1d6e82
movb $0x0, %al
callq 0x15b350
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)
movl -0x24(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x80(%rcx), %eax
jbe 0x181fac
jmp 0x182110
cmpl $0x0, -0x24(%rbp)
jne 0x182016
movq -0x8(%rbp), %rdi
movl $0x9, %esi
callq 0x181a10
movl %eax, -0x54(%rbp)
movq -0x20(%rbp), %rdi
movl -0x54(%rbp), %edx
leaq 0x27e03(%rip), %rsi     # 0x1a9dd4
movb $0x0, %al
callq 0x15b400
movl -0x54(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x80(%rcx), %eax
jb 0x181fec
jmp 0x182110
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 0x182110
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x1820d0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x90, %rsi
callq 0x181ae0
movl %eax, -0x58(%rbp)
cmpl $0x2, -0x58(%rbp)
ja 0x182083
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x1823e0
movl %eax, -0x5c(%rbp)
movq -0x20(%rbp), %rdi
movl -0x28(%rbp), %edx
movl -0x58(%rbp), %ecx
movl -0x58(%rbp), %r8d
movl -0x5c(%rbp), %r9d
leaq 0x54e3c(%rip), %rsi     # 0x1d6eaa
movb $0x0, %al
callq 0x15b400
movl -0x5c(%rbp), %eax
addl $0x1, %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x1820cb
movl -0x58(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
movl -0x28(%rbp), %edx
movl -0x58(%rbp), %ecx
movl -0x60(%rbp), %r8d
leaq 0x54e3e(%rip), %rsi     # 0x1d6edf
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 -0x60(%rbp), %ecx
callq 0x187f20
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x18201d
movq -0x8(%rbp), %rax
cmpb $0x0, 0x50(%rax)
je 0x1820dc
jmp 0x182110
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 0x182109
jmp 0x182110
movl $0x1, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x18211f
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
nopw %cs:(%rax,%rax)