// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/dms.c
// Function: dmslzh_codectype1
static void dmslzh_codectype1(deark *c, struct de_dfilter_in_params *dcmpri,
	struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres,
	void *codec_private_params)
{
	struct dmslzh_params *lzhp = (struct dmslzh_params*)codec_private_params;
	struct lzh_ctx *cctx = NULL;
	struct dmsheavy_cmpr_state *hvst = NULL;

	cctx = de_malloc(c, sizeof(struct lzh_ctx));
	cctx->modname = "undmslzh";
	cctx->c = c;
	cctx->dcmpri = dcmpri;
	cctx->dcmpro = dcmpro;
	cctx->dres = dres;
	cctx->bitrd.f = dcmpri->f;
	cctx->bitrd.curpos = dcmpri->pos;
	cctx->bitrd.endpos = dcmpri->pos + dcmpri->len;

	if(lzhp->heavy_state) {
		// If a previous decompression state exists, use it.
		hvst = lzhp->heavy_state;
		lzhp->heavy_state = NULL;
	}
	else {
		hvst = de_malloc(c, sizeof(struct dmsheavy_cmpr_state));
		hvst->cmpr_type = lzhp->cmpr_type;
	}

	decompress_dms_heavy(cctx, lzhp, hvst);

	hvst->ringbuf->userdata = NULL;
	hvst->ringbuf->writebyte_cb = NULL;
	lzhp->heavy_state = hvst;
	hvst = NULL;

	if(cctx->err_flag) {
		// A default error message
		de_dfilter_set_errorf(c, dres, cctx->modname, "LZH decoding error");
		goto done;
	}

	de_bitreader_skip_to_byte_boundary(&cctx->bitrd);
	cctx->dres->bytes_consumed = cctx->bitrd.curpos - cctx->dcmpri->pos;
	if(cctx->dres->bytes_consumed<0) {
		cctx->dres->bytes_consumed = 0;
	}
	cctx->dres->bytes_consumed_valid = 1;

done:
	if(hvst) destroy_heavy_state(c, hvst);
	de_free(c, cctx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %rbp
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
pushq $0x70
popq %rsi
callq 0xdecaf
movq %rax, %r13
leaq 0xc9678(%rip), %rax     # 0x1110af
movq %rax, 0x20(%r13)
movq %r15, (%r13)
movq %r14, 0x8(%r13)
movq %rbx, 0x10(%r13)
movq %r12, 0x18(%rsp)
movq %r12, 0x18(%r13)
movq %rbp, %r12
movq (%r14), %rax
movq %rax, 0x38(%r13)
movq 0x8(%r14), %rax
movq %rax, 0x40(%r13)
addq 0x10(%r14), %rax
movq %r13, %r14
movq %rax, 0x48(%r13)
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
je 0x47a81
andq $0x0, 0x8(%r12)
movq %r15, %rbx
jmp 0x47a99
pushq $0x28
popq %rsi
movq %r15, %rdi
callq 0xdecaf
movq %rax, %rbp
movl (%r12), %eax
movl %eax, (%rbp)
movq (%r14), %rbx
addq $0x38, %r13
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0xdebae
movl (%r12), %ecx
cmpl (%rbp), %ecx
movq %r15, 0x8(%rsp)
movq %r12, 0x28(%rsp)
jne 0x47b26
xorl %eax, %eax
cmpl $0x5, %ecx
sete %al
xorl $0xf, %eax
movl %eax, 0x68(%r14)
movq 0x8(%rbp), %rax
testq %rax, %rax
jne 0x47af3
xorl %esi, %esi
cmpl $0x5, %ecx
setne %sil
shll $0xc, %esi
addl $0x1000, %esi           # imm = 0x1000
movq (%r14), %rdi
callq 0xef27c
movq %rax, 0x8(%rbp)
movq %r14, (%rax)
leaq 0x2e6(%rip), %rcx       # 0x47de3
movq %rcx, 0x8(%rax)
movq 0x10(%r14), %rax
cmpb $0x0, 0x8(%rax)
je 0x47b3a
testb $0x2, 0x4(%r12)
jne 0x47be2
cmpb $0x0, 0x10(%rbp)
jne 0x47c88
jmp 0x47bfa
movq 0x18(%r14), %rsi
movq 0x20(%r14), %rdx
leaq 0xc9596(%rip), %rcx     # 0x1110cb
movq %rbx, %rdi
jmp 0x47b4c
movq (%r14), %rdi
movq 0x18(%r14), %rsi
movq 0x20(%r14), %rdx
leaq 0xc95af(%rip), %rcx     # 0x1110fb
xorl %eax, %eax
callq 0xee823
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq 0x8(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq 0x28(%rsp), %rax
movq %rbp, 0x8(%rax)
cmpl $0x0, 0x30(%r14)
je 0x47b9a
movq 0x20(%r14), %rdx
leaq 0xc9534(%rip), %rcx     # 0x1110b8
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0xee823
jmp 0x47bc8
movq %r13, %rdi
callq 0xe6abe
movq 0x40(%r14), %rax
movq 0x8(%r14), %rcx
movq 0x18(%r14), %rdx
subq 0x8(%rcx), %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovgq %rax, %rcx
movq %rcx, 0x8(%rdx)
movb $0x1, 0x4(%rdx)
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18(%rbp), %rsi
movq %rbx, %rdi
callq 0x47e12
leaq 0x20(%rbp), %rsi
movq %rbx, %rdi
callq 0x47e12
movb $0x1, 0x10(%rbp)
leaq 0xc9505(%rip), %rsi     # 0x11110a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde9f1
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0x18(%rbp), %rsi
pushq $0x9
popq %rdx
pushq $0x5
popq %rcx
movq %r14, %rdi
callq 0x47e29
movl %eax, %r15d
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
testl %r15d, %r15d
je 0x47b53
leaq 0xc94c7(%rip), %rsi     # 0x111111
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde9f1
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0x20(%rbp), %rsi
pushq $0x5
popq %rdx
pushq $0x4
popq %rcx
movq %r14, %rdi
callq 0x47e29
movl %eax, %r15d
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
testl %r15d, %r15d
je 0x47b53
leaq 0x30(%rsp), %r15
pushq $0x20
popq %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0xe6b25
leaq 0xc9476(%rip), %rsi     # 0x111118
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xde9f1
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0x18(%rbp), %rax
movq %rax, 0x20(%rsp)
leaq 0x20(%rbp), %rax
movq %rax, 0x10(%rsp)
cmpb $0x0, 0x50(%r14)
jne 0x47b53
movq %r14, %rdi
callq 0x47f7c
testl %eax, %eax
jne 0x47b53
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0x47f97
cmpb $0x0, 0x50(%r14)
jne 0x47b53
movl %eax, %r15d
cmpl $0x3, (%rbx)
jl 0x47d1f
movq 0x28(%r14), %rcx
movq %rbx, %rdi
leaq 0xc9419(%rip), %rsi     # 0x11112e
movl %r15d, %edx
xorl %eax, %eax
callq 0xdea83
cmpl $0xff, %r15d
ja 0x47d37
movq 0x8(%rbp), %rdi
movzbl %r15b, %esi
callq 0xef336
jmp 0x47ccc
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x47f97
cmpb $0x0, 0x50(%r14)
jne 0x47b53
movl %eax, %r12d
movl 0x68(%r14), %eax
decl %eax
cmpl %eax, %r12d
jne 0x47d62
movl 0x4(%rbp), %esi
jmp 0x47d8e
testl %r12d, %r12d
je 0x47d89
decl %r12d
movq %r13, %rdi
movl %r12d, %esi
callq 0xe6a4a
cmpb $0x0, 0x50(%r14)
jne 0x47b53
movq %rax, %rsi
btsl %r12d, %esi
jmp 0x47d8b
xorl %esi, %esi
movl %esi, 0x4(%rbp)
addl $0xffffff03, %r15d      # imm = 0xFFFFFF03
movq 0x8(%rbp), %rdi
notl %esi
addl 0x10(%rdi), %esi
movl %r15d, %edx
callq 0xef363
jmp 0x47ccc