// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/stuffit.c
// Function: sit_fixedhuff_init_tree
static void sit_fixedhuff_init_tree(struct sit_fixedhuffctx *hctx)
{
	deark *c = hctx->c;
	size_t i, k;
	size_t cdlen_curpos;
	UI prev_code_bit_length = 0;
	u64 prev_code = 0; // valid if prev_code_bit_length>0
	int saved_indent_level;
	char b2buf[72];
	static const u8 cdlen_RLEcounts [13] = {1, 1, 4,12,32,16,49, 2,2,40,95, 2, 1};
	static const u8 cdlen_RLElengths[13] = {3, 4, 5, 6, 7, 8, 9,10,9,10,11,13,12};
	u8 code_lengths[FIXEDHUFF_NUMCODES];

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbgx(c, 4, "standard huffman codebook:");
	de_dbg_indent(c, 1);

	// "Decompress" cdlen_RLE*[] to code_lengths[].
	cdlen_curpos = 0;
	for(i=0; i<DE_ARRAYCOUNT(cdlen_RLEcounts); i++) {
		for(k=0; k<(size_t)cdlen_RLEcounts[i]; k++) {
			if(cdlen_curpos>=FIXEDHUFF_NUMCODES) goto done;
			code_lengths[cdlen_curpos++] = cdlen_RLElengths[i];
		}
	}

	// This is similar to fmtutil_huffman_make_canonical_tree(), but different.
	// Maybe it would be a useful library function.
	for(i=0; i<FIXEDHUFF_NUMCODES; i++) {
		u64 thiscode;
		UI symlen;
		int ret;

		symlen = (UI)code_lengths[i];

		if(prev_code_bit_length==0) { // this is the first code
			thiscode = 0;
		}
		else if(symlen < prev_code_bit_length) {
			thiscode = prev_code >> (prev_code_bit_length - symlen);
			thiscode++;
		}
		else {
			thiscode = prev_code + 1;
			if(symlen > prev_code_bit_length) {
				thiscode <<= (symlen - prev_code_bit_length);
			}
		}

		prev_code_bit_length = symlen;
		prev_code = thiscode;

		if(c->debug_level>=4) {
			de_dbg3(c, "code: \"%s\" = %d",
				de_print_base2_fixed(b2buf, sizeof(b2buf), thiscode, symlen), (int)i);
		}
		ret = fmtutil_huffman_add_code(c, hctx->ht->bk, thiscode, symlen, (fmtutil_huffman_valtype)i);
		if(!ret) {
			hctx->errflag = 1;
			goto done;
		}
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x15b580
movq -0x10(%rbp), %rdi
movl $0x4, %esi
leaq 0x8d211(%rip), %rdx     # 0x1cdf87
movb $0x0, %al
callq 0x15b4b0
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq $0x0, -0x28(%rbp)
movq $0x0, -0x18(%rbp)
cmpq $0xd, -0x18(%rbp)
jae 0x140e13
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rdx
leaq 0x8d524(%rip), %rcx     # 0x1ce2dd
movzbl (%rcx,%rdx), %ecx
cmpq %rcx, %rax
jae 0x140e03
cmpq $0x101, -0x28(%rbp)     # imm = 0x101
jb 0x140dd1
jmp 0x140f7e
movq -0x18(%rbp), %rcx
leaq 0x8d50e(%rip), %rax     # 0x1ce2ea
movb (%rax,%rcx), %cl
movq -0x28(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x28(%rbp)
movb %cl, -0x1a0(%rbp,%rax)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x140daa
jmp 0x140e05
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x140d9b
movq $0x0, -0x18(%rbp)
cmpq $0x101, -0x18(%rbp)     # imm = 0x101
jae 0x140f7c
movq -0x18(%rbp), %rax
movzbl -0x1a0(%rbp,%rax), %eax
movl %eax, -0x1ac(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x140e4e
movq $0x0, -0x1a8(%rbp)
jmp 0x140ec0
movl -0x1ac(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x140e86
movq -0x38(%rbp), %rax
movl -0x2c(%rbp), %ecx
subl -0x1ac(%rbp), %ecx
movl %ecx, %ecx
shrq %cl, %rax
movq %rax, -0x1a8(%rbp)
movq -0x1a8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1a8(%rbp)
jmp 0x140ebe
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1a8(%rbp)
movl -0x1ac(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jbe 0x140ebc
movl -0x1ac(%rbp), %ecx
subl -0x2c(%rbp), %ecx
movq -0x1a8(%rbp), %rax
movl %ecx, %ecx
shlq %cl, %rax
movq %rax, -0x1a8(%rbp)
jmp 0x140ebe
jmp 0x140ec0
movl -0x1ac(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x1a8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x4, (%rax)
jl 0x140f24
movq -0x10(%rbp), %rax
movq %rax, -0x1b8(%rbp)
leaq -0x90(%rbp), %rdi
movq -0x1a8(%rbp), %rdx
movl -0x1ac(%rbp), %ecx
movl $0x48, %esi
callq 0x15cf60
movq -0x1b8(%rbp), %rdi
movq %rax, %rdx
movq -0x18(%rbp), %rax
movl %eax, %ecx
leaq 0x690f7(%rip), %rsi     # 0x1aa014
movb $0x0, %al
callq 0x15b400
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rsi
movq -0x1a8(%rbp), %rdx
movl -0x1ac(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %eax, %r8d
callq 0x187720
movl %eax, -0x1b0(%rbp)
cmpl $0x0, -0x1b0(%rbp)
jne 0x140f69
movq -0x8(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x140f7e
jmp 0x140f6b
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x140e1b
jmp 0x140f7e
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x15b5a0
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
nopw %cs:(%rax,%rax)