// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x68
popq %rsi
callq 0xdecaf
movq %rax, %r14
movq %rbx, (%rax)
leaq 0x423de(%rip), %rax     # 0x131de9
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 0xf8601
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 0x4dc52(%rip), %rsi     # 0x13d6a6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde9f1
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xefb9b
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpl $0x0, 0x30(%r14)
je 0xefadd
movq %r13, %rdi
callq 0xe6abe
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 0xefab7
movq 0x8(%r14), %rdx
movq %r15, %rsi
callq 0xee91c
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0xf86b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x4, (%rbx)
jl 0xefaee
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0xf811c
movq 0x28(%r14), %rax
movq 0x8(%rax), %rdi
callq 0xf7e40
testl %eax, %eax
je 0xefa87
leaq 0x10(%rsp), %rsi
pushq $0x20
popq %rdx
movq %r13, %rdi
callq 0xe6b25
leaq 0x21602(%rip), %rsi     # 0x111118
xorl %ebp, %ebp
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0xde896
andl $0x0, 0xc(%rsp)
cmpb $0x0, 0x8(%r12)
je 0xefb3f
cmpq 0x10(%r12), %rbp
jge 0xefa87
cmpb $0x0, 0x50(%r14)
jne 0xefa87
cmpl $0x0, 0x30(%r14)
jne 0xefa87
movq 0x28(%r14), %rax
movq 0x8(%rax), %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
xorl %ecx, %ecx
callq 0xf8071
testl %eax, %eax
je 0xefb83
movq (%r12), %rdi
movzbl 0xc(%rsp), %esi
callq 0xe5bc5
incq %rbp
jmp 0xefb27
cmpb $0x0, 0x50(%r14)
jne 0xefa87
movl $0x1, 0x30(%r14)
jmp 0xefa87