// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movl %r8d, 0x4(%rsp)
movq %rdx, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
cmpl $0x30, %ecx
jbe 0xf7e7f
xorl %eax, %eax
jmp 0xf7f8d
movq %rsi, %r14
testl %ecx, %ecx
je 0xf7f7d
movb $0x0, 0x18(%r14)
cmpl %ecx, 0x28(%r14)
jae 0xf7e99
movl %ecx, 0x28(%r14)
leal -0x1(%rcx), %eax
movq %rax, 0x8(%rsp)
movl %ecx, %eax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpq %rbx, 0x18(%rsp)
je 0xf7f8a
cmpl 0xc(%r14), %ebp
jb 0xf7ed1
leal 0x1(%rbp), %edx
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0xf7f9c
testl %eax, %eax
je 0xf7e78
movl 0x8(%r14), %r15d
movl %ebp, %r13d
cmpl %r15d, %ebp
jb 0xf7ef1
leal 0x1(%rbp), %r15d
movl %r15d, 0x8(%r14)
movq 0x10(%r14), %rax
imulq $0xc, %r13, %rcx
movb %bl, 0x2(%rax,%rcx)
movq 0x20(%rsp), %r12
movq 0x8(%rsp), %rcx
shrq %cl, %r12
movl %r12d, %edx
andl $0x1, %edx
subq $0x1, %rcx
movq %rcx, 0x8(%rsp)
jae 0xf7f33
movq %r14, %rdi
movl %ebp, %esi
pushq $0x2
popq %rcx
callq 0xf7ff7
andl $0x1, %r12d
imulq $0xc, %r13, %rax
addq 0x10(%r14), %rax
movl 0x4(%rsp), %ecx
movl %ecx, 0x4(%rax,%r12,4)
jmp 0xf7f75
imulq $0xc, %r13, %r13
movq 0x10(%r14), %rax
addq %r13, %rax
andl $0x1, %r12d
cmpb $0x1, (%r12,%rax)
jne 0xf7f50
movl 0x4(%rax,%r12,4), %ebp
jmp 0xf7f75
movl %r15d, %eax
cmpq %rax, (%r14)
jle 0xf7e78
movq %r14, %rdi
movl %ebp, %esi
pushq $0x1
popq %rcx
callq 0xf7ff7
addq 0x10(%r14), %r13
movl %r15d, 0x4(%r13,%r12,4)
movl %r15d, %ebp
incq %rbx
jmp 0xf7eac
movl 0x4(%rsp), %eax
movl %eax, 0x1c(%r14)
movb $0x1, 0x18(%r14)
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq