// File: /workspace/github_c_makefile_star2_O3/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 %rdi, 0x8(%rsp)
cmpl $0x30, %ecx
jbe 0x1044e5
xorl %eax, %eax
jmp 0x10462c
movq %rsi, %rbp
testl %ecx, %ecx
je 0x10461c
movq %rdx, %r11
movb $0x0, 0x18(%rbp)
cmpl %ecx, 0x28(%rbp)
jae 0x1044ff
movl %ecx, 0x28(%rbp)
leal -0x1(%rcx), %r13d
movl %ecx, %r10d
movq %r13, %rbx
negq %rbx
xorl %r12d, %r12d
xorl %r15d, %r15d
cmpl 0xc(%rbp), %r15d
jb 0x104549
leal 0x1(%r15), %edx
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq %r15, 0x10(%rsp)
movq %r10, %r15
movq %rbx, %r14
movq %r11, %rbx
callq 0x10463b
movq %rbx, %r11
movq %r14, %rbx
movq %r15, %r10
movq 0x10(%rsp), %r15
testl %eax, %eax
je 0x1044de
movl 0x8(%rbp), %r14d
movl %r15d, %edx
cmpl %r14d, %r15d
jb 0x10456a
leal 0x1(%r15), %r14d
movl %r14d, 0x8(%rbp)
movq 0x10(%rbp), %rax
leaq (%rdx,%rdx,2), %rcx
movb %r12b, 0x2(%rax,%rcx,4)
leaq (%rbx,%r12), %rdi
movq %r11, %rax
movl %r13d, %ecx
shrq %cl, %rax
testq %rdi, %rdi
je 0x10459c
movq 0x10(%rbp), %rdi
leaq (%rdx,%rdx,2), %rcx
leaq (%rdi,%rcx,4), %rdx
andl $0x1, %eax
movb (%rax,%rdx), %r8b
cmpb $0x1, %r8b
jne 0x1045c5
movl 0x4(%rdx,%rax,4), %r15d
jmp 0x10460b
movq 0x10(%rbp), %rcx
cmpl %r15d, 0xc(%rbp)
jbe 0x1045f8
andl $0x1, %eax
leaq (%rdx,%rdx,2), %rdi
leaq (%rcx,%rdi,4), %rdi
cmpb $0x2, (%rax,%rdi)
je 0x1045fb
incq 0x20(%rbp)
movb $0x2, (%rdi,%rax)
movq 0x10(%rbp), %rcx
jmp 0x1045fb
movl %r14d, %r9d
cmpq %r9, (%rbp)
jle 0x1044de
cmpl %r15d, 0xc(%rbp)
jbe 0x1045ea
cmpb $0x2, %r8b
jne 0x1045e2
decq 0x20(%rbp)
movb $0x1, (%rdx,%rax)
movq 0x10(%rbp), %rdi
leaq (%rdi,%rcx,4), %rcx
movl %r14d, 0x4(%rcx,%rax,4)
movl %r14d, %r15d
jmp 0x10460b
andl $0x1, %eax
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,4), %rcx
movl 0x4(%rsp), %edx
movl %edx, 0x4(%rcx,%rax,4)
incq %r12
decq %r13
cmpq %r12, %r10
jne 0x104512
jmp 0x104627
movl 0x4(%rsp), %eax
movl %eax, 0x1c(%rbp)
movb $0x1, 0x18(%rbp)
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq