// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movq %rbx, (%rax)
leaq 0x43aa8(%rip), %rax     # 0x13f119
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 0x104d0e
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 0x4f4bc(%rip), %rsi     # 0x14ab76
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe87e4
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xfb81d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpl $0x0, 0x30(%r14)
je 0xfb756
movq %r13, %rdi
callq 0xf1c84
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 0xfb730
cmpl $0x0, (%r15)
jne 0xfb730
movq 0x8(%r14), %rdx
leaq 0x4f3b3(%rip), %rcx     # 0x14aad9
movq %r15, %rsi
xorl %eax, %eax
callq 0xfa21a
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0x104dc7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x4, (%rbx)
jl 0xfb767
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0x1047f5
movq 0x28(%r14), %rax
movq 0x8(%rax), %rdi
callq 0x1044a8
testl %eax, %eax
je 0xfb6f1
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
movq %r13, %rdi
callq 0xf1cf3
leaq 0x22a57(%rip), %rsi     # 0x11e1ec
xorl %ebp, %ebp
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0xe8617
movl $0x0, 0xc(%rsp)
cmpb $0x0, 0x8(%r12)
je 0xfb7c1
cmpq 0x10(%r12), %rbp
jge 0xfb6f1
cmpb $0x0, 0x50(%r14)
jne 0xfb6f1
cmpl $0x0, 0x30(%r14)
jne 0xfb6f1
movq 0x28(%r14), %rax
movq 0x8(%rax), %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
xorl %ecx, %ecx
callq 0x1046f3
testl %eax, %eax
je 0xfb805
movq (%r12), %rdi
movzbl 0xc(%rsp), %esi
callq 0xf0bd6
incq %rbp
jmp 0xfb7a6
cmpb $0x0, 0x50(%r14)
jne 0xfb6f1
movl $0x1, 0x30(%r14)
jmp 0xfb6f1