// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/fmtutil-cmpr.c
// Function: fmtutil_huff_packit_codectype1
void fmtutil_huff_packit_codectype1(deark *c, struct de_dfilter_in_params *dcmpri,
	struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres, void *rsvd)
{
	struct sit_huffctx *hctx = NULL;
	i64 nbytes_written = 0;
	char pos_descr[32];

	hctx = de_malloc(c, sizeof(struct sit_huffctx));
	hctx->c = c;
	hctx->modname = "huffman";
	hctx->dcmpri = dcmpri;
	hctx->dcmpro = dcmpro;
	hctx->dres = dres;
	hctx->ht = fmtutil_huffman_create_decoder(c, 256, 512);

	hctx->bitrd.f = dcmpri->f;
	hctx->bitrd.curpos = dcmpri->pos;
	hctx->bitrd.endpos = dcmpri->pos + dcmpri->len;

	// Read the tree definition
	de_dbg2(c, "interpreted huffman codebook:");
	de_dbg_indent(c, 1);
	sit_huff_read_tree(hctx, 0, 0);
	de_dbg_indent(c, -1);
	if(hctx->errflag) goto done;
	if(c->debug_level>=4) {
		fmtutil_huffman_dump(c, hctx->ht);
	}
	if(fmtutil_huffman_get_max_bits(hctx->ht->bk)<1) {
		goto done;
	}

	// Read the data section
	de_bitreader_describe_curpos(&hctx->bitrd, pos_descr, sizeof(pos_descr));
	de_dbg(c, "cmpr data codes at %s", pos_descr);
	while(1) {
		int ret;
		fmtutil_huffman_valtype val = 0;

		if(dcmpro->len_known) {
			if(nbytes_written >= dcmpro->expected_len) break;
		}

		if(hctx->bitrd.eof_flag || hctx->errflag) break;

		ret = fmtutil_huffman_read_next_value(hctx->ht->bk, &hctx->bitrd, &val, NULL);
		if(!ret) {
			if(hctx->bitrd.eof_flag) break;
			hctx->errflag = 1;
			break;
		}

		dbuf_writebyte(dcmpro->f, (u8)val);
		nbytes_written++;
	}

done:
	de_bitreader_skip_to_byte_boundary(&hctx->bitrd);
	dres->bytes_consumed = hctx->bitrd.curpos - hctx->dcmpri->pos;
	dres->bytes_consumed_valid = 1;

	if(hctx->errflag) {
		de_dfilter_set_generic_error(c, dres, hctx->modname);
	}
	if(hctx) {
		fmtutil_huffman_destroy_decoder(c, hctx->ht);
		de_free(c, hctx);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %rbx
movl $0x68, %esi
callq 0xe969b
movq %rax, %r14
movq %rbx, (%rax)
leaq 0x43143(%rip), %rax     # 0x13ed19
movq %rax, 0x8(%r14)
movq %rbp, 0x10(%r14)
movq %r12, 0x18(%r14)
movq %r15, 0x20(%r14)
movl $0x100, %esi            # imm = 0x100
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
callq 0x105165
movq %rax, 0x28(%r14)
movq (%rbp), %rax
leaq 0x38(%r14), %r13
movq %rax, 0x38(%r14)
movq 0x8(%rbp), %rax
movq %rax, 0x40(%r14)
addq 0x10(%rbp), %rax
movq %rax, 0x48(%r14)
leaq 0x4e9a7(%rip), %rsi     # 0x14a5c6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe93cf
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xfbd7c
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpl $0x0, 0x30(%r14)
je 0xfbcbb
movq %r13, %rdi
callq 0xf22f1
movq 0x10(%r14), %rax
movq 0x40(%r14), %rcx
subq 0x8(%rax), %rcx
movq %rcx, 0x8(%r15)
movb $0x1, 0x4(%r15)
cmpl $0x0, 0x30(%r14)
je 0xfbc95
cmpl $0x0, (%r15)
jne 0xfbc95
movq 0x8(%r14), %rdx
leaq 0x4e89e(%rip), %rcx     # 0x14a529
movq %r15, %rsi
xorl %eax, %eax
callq 0xfa71e
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0x10521e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x4, (%rbx)
jl 0xfbccc
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0x104c1c
movq 0x28(%r14), %rax
movq 0x8(%rax), %rdi
callq 0x1048e8
testl %eax, %eax
je 0xfbc56
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
movq %r13, %rdi
callq 0xf2360
leaq 0x223e2(%rip), %rsi     # 0x11e0dc
xorl %ebp, %ebp
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0xe9226
movl $0x0, 0xc(%rsp)
cmpb $0x0, 0x8(%r12)
je 0xfbd22
cmpq 0x10(%r12), %rbp
jge 0xfbd30
cmpb $0x0, 0x50(%r14)
jne 0xfbd30
cmpl $0x0, 0x30(%r14)
je 0xfbd3b
xorl %eax, %eax
testb %al, %al
jne 0xfbd0b
jmp 0xfbc56
movq 0x28(%r14), %rax
movq 0x8(%rax), %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
xorl %ecx, %ecx
callq 0x104b4d
testl %eax, %eax
je 0xfbd6b
movq (%r12), %rdi
movzbl 0xc(%rsp), %esi
callq 0xf11be
incq %rbp
movb $0x1, %al
jmp 0xfbd32
cmpb $0x0, 0x50(%r14)
jne 0xfbd30
movl $0x1, 0x30(%r14)
jmp 0xfbd30