// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/fmtutil-huffman.c
// Function: fmtutil_huffman_add_code
int fmtutil_huffman_add_code(deark *c, struct fmtutil_huffman_codebook *bk,
	u64 code, UI code_nbits, fmtutil_huffman_valtype val)
{
	UI k;
	NODE_REF_TYPE curr_noderef = 0; // Note that this may temporarily point to an unallocated node
	int retval = 0;

	if(code_nbits>FMTUTIL_HUFFMAN_MAX_CODE_LENGTH) goto done;

	if(code_nbits<1) {
		bk->value_of_null_code = val;
		bk->has_null_code = 1;
		retval = 1;
		goto done;
	}
	bk->has_null_code = 0;

	if(code_nbits > bk->max_bits) {
		bk->max_bits = code_nbits;
	}

	// Iterate through the bits, high bit first.
	for(k=0; k<code_nbits; k++) {
		UI child_idx; // 0 or 1

		// Make sure the current node exists
		if(curr_noderef >= bk->nodes_alloc) {
			if(!huffman_ensure_alloc(c, bk, curr_noderef+1)) goto done;
		}
		// Claim the current node, if necessary
		if(curr_noderef >= bk->next_avail_node) {
			bk->next_avail_node = curr_noderef+1;
			bk->nodes[curr_noderef].depth = (u8)k;
		}

		child_idx = (code>>(code_nbits-1-k))&0x1;

		if(k==code_nbits-1) {
			// Reached the "leaf" node. Set the value for this child_idx.
			huffman_setchildstatus(bk, curr_noderef, child_idx, CHILDSTATUS_VALUE);
			bk->nodes[curr_noderef].child[child_idx].hnvd.value = val;
		}
		else {
			// Not at the leaf node yet.
			if(bk->nodes[curr_noderef].child_status[child_idx]==CHILDSTATUS_POINTER) {
				// It's already a pointer.
				curr_noderef = bk->nodes[curr_noderef].child[child_idx].hnpd.noderef;
			}
			else {
				NODE_REF_TYPE next_noderef;

				// It's not a pointer -- make it one.
				if(bk->next_avail_node >= bk->max_nodes) goto done;
				next_noderef = bk->next_avail_node;
				huffman_setchildstatus(bk, curr_noderef, child_idx, CHILDSTATUS_POINTER);
				bk->nodes[curr_noderef].child[child_idx].hnpd.noderef = next_noderef;
				curr_noderef = next_noderef;
			}
		}
	}

	retval = 1;

done:
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, 0x4(%rsp)
movq %rdx, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
cmpl $0x30, %ecx
jbe 0x10492a
xorl %eax, %eax
jmp 0x104a74
movl %ecx, %r15d
movq %rsi, %r14
testl %ecx, %ecx
je 0x104a62
movb $0x0, 0x18(%r14)
cmpl %r15d, 0x28(%r14)
jae 0x104947
movl %r15d, 0x28(%r14)
leal -0x1(%r15), %ebx
movslq %ebx, %rbp
cmpl $0x1, %r15d
adcl $0x0, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
cmpl 0xc(%r14), %r12d
jb 0x10497c
leal 0x1(%r12), %edx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x104a83
testl %eax, %eax
je 0x104a47
movl %r12d, %edx
cmpl 0x8(%r14), %r12d
jb 0x10499b
leal 0x1(%r12), %eax
movl %eax, 0x8(%r14)
movq 0x10(%r14), %rax
leaq (%rdx,%rdx,2), %rcx
movb %r13b, 0x2(%rax,%rcx,4)
movq 0x10(%rsp), %rax
movl %ebp, %ecx
shrq %cl, %rax
cmpq %r13, %rbx
jne 0x1049e8
cmpl %r12d, 0xc(%r14)
jbe 0x1049cf
movl %eax, %ecx
andl $0x1, %ecx
leaq (%rdx,%rdx,2), %rsi
shlq $0x2, %rsi
addq 0x10(%r14), %rsi
cmpb $0x2, (%rcx,%rsi)
je 0x1049cf
incq 0x20(%r14)
movb $0x2, (%rsi,%rcx)
andl $0x1, %eax
leaq (%rdx,%rdx,2), %rcx
shlq $0x2, %rcx
addq 0x10(%r14), %rcx
movl 0x4(%rsp), %edx
movl %edx, 0x4(%rcx,%rax,4)
jmp 0x104a07
leaq (%rdx,%rdx,2), %rdx
shlq $0x2, %rdx
movq 0x10(%r14), %rdi
addq %rdx, %rdi
movl %eax, %esi
andl $0x1, %esi
cmpb $0x1, (%rsi,%rdi)
jne 0x104a0b
movl 0x4(%rdi,%rsi,4), %r12d
movb $0x1, %cl
jmp 0x104a49
movl 0x8(%r14), %r8d
cmpq %r8, (%r14)
setg %cl
jle 0x104a49
cmpl %r12d, 0xc(%r14)
jbe 0x104a39
andl $0x1, %eax
movzbl (%rdi,%rax), %r9d
cmpl $0x1, %r9d
je 0x104a39
cmpl $0x2, %r9d
jne 0x104a35
decq 0x20(%r14)
movb $0x1, (%rdi,%rax)
addq 0x10(%r14), %rdx
movl %r8d, 0x4(%rdx,%rsi,4)
movl %r8d, %r12d
jmp 0x104a49
xorl %ecx, %ecx
testb %cl, %cl
je 0x104923
incq %r13
decq %rbp
cmpq %r13, %r15
jne 0x10495c
jmp 0x104a6f
movl 0x4(%rsp), %eax
movl %eax, 0x1c(%r14)
movb $0x1, 0x18(%r14)
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq