// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-lzh.c
// Function: distilled_read_nodetable
static void distilled_read_nodetable(deark *c, struct lzh_ctx *cctx)
{
	struct distilled_nodetable_decoder *ntd = NULL;

	UI k;
	UI code_len;
	UI num_codes_in_tree;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	ntd = de_malloc(c, sizeof(struct distilled_nodetable_decoder));
	ntd->c = c;

	de_dbg2(c, "[node table]");
	de_dbg_indent(c, 1);

	ntd->nodecount = (UI)de_bitreader_getbits(&cctx->bitrd, 16);
	de_dbg2(c, "node count: %u", ntd->nodecount);
	// Expected to be an even number from 2 to 628.
	// There are 315 codes: 256 literal, 1 special, 58 distance (3..60).
	// Max table entries needed is (315-1)*2 = 628.
	if(ntd->nodecount<2 || ntd->nodecount>DISTILLED_MAX_NODES ||
		(ntd->nodecount & 0x1))
	{
		cctx->err_flag = 1;
		goto done;
	}

	num_codes_in_tree = 1 + ntd->nodecount/2;

	code_len = (UI)de_bitreader_getbits(&cctx->bitrd, 8);
	de_dbg2(c, "code len: %u", code_len);
	if(code_len<1 || code_len>12) { // Expecting 9 or 10
		cctx->err_flag = 1;
		goto done;
	}

	de_dbg2(c, "[node table entries]");
	de_dbg_indent(c, 1);
	for(k=0; k<ntd->nodecount; k++) {
		UI val;

		val = (UI)de_bitreader_getbits(&cctx->bitrd, code_len);
		ntd->node[k].dval = val;
		de_dbg2(c, "node[%u] = %u", k, val);
	}
	de_dbg_indent(c, -1);

	de_dbg_indent(c, -1);
	de_dbg3(c, "[codebook for literals/lengths]");
	de_dbg_indent(c, 1);

	cctx->literals_tree.ht = fmtutil_huffman_create_decoder(c,
		(i64)num_codes_in_tree, (i64)num_codes_in_tree);

	ntd->ht = cctx->literals_tree.ht;
	distilled_interpret_node_pair(ntd, ntd->nodecount-2, 0, 0);

done:
	if(ntd) {
		de_free(c, ntd);
	}
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0xa30, %esi            # imm = 0xA30
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
leaq 0x5707a(%rip), %rsi     # 0x1d72f7
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
movl $0x10, %esi
callq 0x16a390
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x57045(%rip), %rsi     # 0x1d7304
movb $0x0, %al
callq 0x15b350
movq -0x18(%rbp), %rax
cmpl $0x2, 0x8(%rax)
jb 0x1802ec
movq -0x18(%rbp), %rax
cmpl $0x274, 0x8(%rax)       # imm = 0x274
ja 0x1802ec
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x1802fc
movq -0x10(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x180455
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
shrl %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
movl $0x8, %esi
callq 0x16a390
movl %eax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %edx
leaq 0x56fe5(%rip), %rsi     # 0x1d7313
movb $0x0, %al
callq 0x15b350
cmpl $0x1, -0x20(%rbp)
jb 0x180341
cmpl $0xc, -0x20(%rbp)
jbe 0x180351
movq -0x10(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x180455
movq -0x8(%rbp), %rdi
leaq 0x56fc4(%rip), %rsi     # 0x1d7320
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x1803cc
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
movl -0x20(%rbp), %esi
callq 0x16a390
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movw %ax, %dx
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
movw %dx, 0x1a(%rax,%rcx,4)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %edx
movl -0x2c(%rbp), %ecx
leaq 0x56f7b(%rip), %rsi     # 0x1d7335
movb $0x0, %al
callq 0x15b350
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x180378
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
leaq 0x56f50(%rip), %rsi     # 0x1d7343
movb $0x0, %al
callq 0x15b400
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%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, 0x98(%rax)
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
subl $0x2, %esi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x183a30
cmpq $0x0, -0x18(%rbp)
je 0x180469
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x15b5a0
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)