// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq %r8, %rbp
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl $0x70, %esi
callq 0xe8b2b
movq %rax, %r13
leaq 0xd4446(%rip), %rax     # 0x11e183
movq %rax, 0x20(%r13)
movq %r15, (%r13)
movq %r14, 0x8(%r13)
movq %rbx, 0x10(%r13)
movq %r12, 0x10(%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 0x49d8a
movq $0x0, 0x8(%r12)
movq %r15, %rbx
jmp 0x49da4
movl $0x28, %esi
movq %r15, %rdi
callq 0xe8b2b
movq %rax, %rbp
movl (%r12), %eax
movl %eax, (%rbp)
movq (%r14), %rbx
addq $0x38, %r13
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
movl (%r12), %ecx
cmpl (%rbp), %ecx
movq %r15, 0x8(%rsp)
movq %r12, 0x18(%rsp)
jne 0x49e31
xorl %eax, %eax
cmpl $0x5, %ecx
sete %al
xorl $0xf, %eax
movl %eax, 0x68(%r14)
movq 0x8(%rbp), %rax
testq %rax, %rax
jne 0x49dfe
xorl %esi, %esi
cmpl $0x5, %ecx
setne %sil
shll $0xc, %esi
addl $0x1000, %esi           # imm = 0x1000
movq (%r14), %rdi
callq 0xfad8f
movq %rax, 0x8(%rbp)
movq %r14, (%rax)
leaq 0x325(%rip), %rcx       # 0x4a12d
movq %rcx, 0x8(%rax)
movq 0x10(%r14), %rax
cmpb $0x0, 0x8(%rax)
je 0x49e45
testb $0x2, 0x4(%r12)
jne 0x49eed
cmpb $0x0, 0x10(%rbp)
jne 0x49fbd
jmp 0x49f1f
movq 0x18(%r14), %rsi
movq 0x20(%r14), %rdx
leaq 0xd435f(%rip), %rcx     # 0x11e19f
movq %rbx, %rdi
jmp 0x49e57
movq (%r14), %rdi
movq 0x18(%r14), %rsi
movq 0x20(%r14), %rdx
leaq 0xd4378(%rip), %rcx     # 0x11e1cf
xorl %eax, %eax
callq 0xfa21a
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq 0x8(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq 0x18(%rsp), %rax
movq %rbp, 0x8(%rax)
cmpl $0x0, 0x30(%r14)
je 0x49ea5
movq 0x20(%r14), %rdx
leaq 0xd42fd(%rip), %rcx     # 0x11e18c
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0xfa21a
jmp 0x49ed3
movq %r13, %rdi
callq 0xf1c84
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 0xe8be2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbp), %rsi
testq %rsi, %rsi
je 0x49f06
movq %rbx, %rdi
callq 0x104dc7
movq $0x0, 0x18(%rbp)
movq 0x20(%rbp), %rsi
testq %rsi, %rsi
je 0x49f1f
movq %rbx, %rdi
callq 0x104dc7
movq $0x0, 0x20(%rbp)
movb $0x1, 0x10(%rbp)
leaq 0xd42b4(%rip), %rsi     # 0x11e1de
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe87e4
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
leaq 0x18(%rbp), %rsi
movq %r14, %rdi
movl $0x9, %edx
movl $0x5, %ecx
callq 0x4a157
movl %eax, %r15d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
testl %r15d, %r15d
je 0x49e5e
leaq 0xd426e(%rip), %rsi     # 0x11e1e5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe87e4
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
leaq 0x20(%rbp), %rsi
movq %r14, %rdi
movl $0x5, %edx
movl $0x4, %ecx
callq 0x4a157
movl %eax, %r15d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
testl %r15d, %r15d
je 0x49e5e
leaq 0x20(%rsp), %r15
movl $0x20, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0xf1cf3
leaq 0xd4213(%rip), %rsi     # 0x11e1ec
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe87e4
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpb $0x0, 0x50(%r14)
jne 0x49e5e
movq 0x10(%r14), %rax
cmpb $0x0, 0x8(%rax)
je 0x4a016
movq 0x28(%r14), %rcx
cmpq 0x10(%rax), %rcx
jge 0x49e5e
movq 0x18(%rbp), %rsi
movq %r14, %rdi
callq 0x4a2b5
cmpb $0x0, 0x50(%r14)
jne 0x49e5e
movl %eax, %r15d
cmpl $0x3, (%rbx)
jl 0x4a04d
movq 0x28(%r14), %rcx
movq %rbx, %rdi
leaq 0xd41bf(%rip), %rsi     # 0x11e202
movl %r15d, %edx
xorl %eax, %eax
callq 0xe889f
cmpl $0xff, %r15d
ja 0x4a065
movq 0x8(%rbp), %rdi
movzbl %r15b, %esi
callq 0xfae4e
jmp 0x49ff3
movq 0x20(%rbp), %rsi
movq %r14, %rdi
callq 0x4a2b5
cmpb $0x0, 0x50(%r14)
jne 0x49e5e
movl %eax, %r12d
movl 0x68(%r14), %eax
decl %eax
cmpl %eax, %r12d
jne 0x4a08f
movl 0x4(%rbp), %esi
jmp 0x4a0bb
testl %r12d, %r12d
je 0x4a0b6
decl %r12d
movq %r13, %rdi
movl %r12d, %esi
callq 0xf1bed
cmpb $0x0, 0x50(%r14)
jne 0x49e5e
movq %rax, %rsi
btsl %r12d, %esi
jmp 0x4a0b8
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 0xfae7b
jmp 0x49ff3