// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %r8, %rbp
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl $0x70, %esi
callq 0xe969b
movq %rax, %r13
leaq 0xd42d6(%rip), %rax     # 0x11e073
movq %rax, 0x20(%r13)
movq %r15, (%r13)
movq %r14, 0x8(%r13)
movq %rbx, 0x10(%r13)
movq %rbp, %rbx
movq %r12, 0x30(%rsp)
movq %r12, 0x18(%r13)
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
movq %r15, 0x20(%rsp)
je 0x49deb
movq $0x0, 0x8(%rbx)
jmp 0x49e00
movl $0x28, %esi
movq %r15, %rdi
callq 0xe969b
movq %rax, %rbp
movl (%rbx), %eax
movl %eax, (%rbp)
addq $0x38, %r13
movq (%r14), %r15
leaq 0x1c(%rsp), %rsi
movq %r15, %rdi
callq 0xe958c
movl (%rbx), %eax
cmpl (%rbp), %eax
jne 0x4a01e
xorl %ecx, %ecx
cmpl $0x5, %eax
sete %cl
xorl $0xf, %ecx
movl %ecx, 0x68(%r14)
cmpq $0x0, 0x8(%rbp)
jne 0x49e53
xorl %esi, %esi
cmpl $0x5, %eax
setne %sil
shll $0xc, %esi
addl $0x1000, %esi           # imm = 0x1000
movq (%r14), %rdi
callq 0xfb2c3
movq %rax, 0x8(%rbp)
movq 0x8(%rbp), %rax
movq %r14, (%rax)
leaq 0x372(%rip), %rcx       # 0x4a1d3
movq %rcx, 0x8(%rax)
movq 0x10(%r14), %rax
cmpb $0x0, 0x8(%rax)
je 0x4a032
testb $0x2, 0x4(%rbx)
je 0x49eaf
movq 0x18(%rbp), %rsi
testq %rsi, %rsi
je 0x49e92
movq %r15, %rdi
callq 0x10521e
movq $0x0, 0x18(%rbp)
movq 0x20(%rbp), %rsi
testq %rsi, %rsi
je 0x49eab
movq %r15, %rdi
callq 0x10521e
movq $0x0, 0x20(%rbp)
movb $0x0, 0x10(%rbp)
cmpb $0x0, 0x10(%rbp)
movq %rbx, 0x10(%rsp)
je 0x4a0d5
leaq 0x40(%rsp), %rbx
movl $0x20, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0xf2360
leaq 0xd4202(%rip), %rsi     # 0x11e0dc
movq %r15, %rdi
movq %rbx, %rdx
movq 0x10(%rsp), %rbx
xorl %eax, %eax
callq 0xe93cf
movq %r15, %rdi
movl $0x1, %esi
callq 0xe9585
leaq 0x18(%rbp), %rax
movq %rax, 0x38(%rsp)
leaq 0x20(%rbp), %rax
movq %rax, 0x28(%rsp)
cmpb $0x0, 0x50(%r14)
jne 0x49f3a
movq 0x10(%r14), %rax
cmpb $0x0, 0x8(%rax)
je 0x49f26
movq 0x28(%r14), %rcx
cmpq 0x10(%rax), %rcx
jge 0x49f3a
movq %r14, %rdi
movq 0x38(%rsp), %rsi
callq 0x4a34b
cmpb $0x0, 0x50(%r14)
je 0x49f45
xorl %ecx, %ecx
testb %cl, %cl
jne 0x49f0b
jmp 0x4a04b
movl %eax, %r12d
cmpl $0x3, (%r15)
jl 0x49f66
movq 0x28(%r14), %rcx
movq %r15, %rdi
leaq 0xd4196(%rip), %rsi     # 0x11e0f2
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9461
cmpl $0xff, %r12d
ja 0x49f81
movq 0x8(%rbp), %rdi
movzbl %r12b, %esi
callq 0xfb382
jmp 0x4a017
movq %r14, %rdi
movq 0x28(%rsp), %rsi
callq 0x4a34b
cmpb $0x0, 0x50(%r14)
je 0x49f9e
xorl %ecx, %ecx
movq 0x10(%rsp), %rbx
jmp 0x49f3c
movl %eax, %ebx
movl 0x68(%r14), %eax
decl %eax
cmpl %eax, %ebx
jne 0x49fb4
movl 0x4(%rbp), %esi
movq 0x10(%rsp), %rbx
jmp 0x49ffb
testl %ebx, %ebx
je 0x49fe7
decl %ebx
movq %r13, %rdi
movl %ebx, %esi
callq 0xf2271
btsl %ebx, %eax
cmpb $0x0, 0x50(%r14)
sete %cl
movl 0xc(%rsp), %edx
cmovel %eax, %edx
movl %edx, 0xc(%rsp)
movq 0x10(%rsp), %rbx
jne 0x49f3c
jmp 0x49ff4
movl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rbx
movl 0xc(%rsp), %esi
movl %esi, 0x4(%rbp)
addl $0xffffff03, %r12d      # imm = 0xFFFFFF03
movq 0x8(%rbp), %rdi
movl %esi, 0xc(%rsp)
notl %esi
addl 0x10(%rdi), %esi
movl %r12d, %edx
callq 0xfb3af
movb $0x1, %cl
jmp 0x49f3c
movq 0x18(%r14), %rsi
movq 0x20(%r14), %rdx
leaq 0xd4062(%rip), %rcx     # 0x11e08f
movq %r15, %rdi
jmp 0x4a044
movq (%r14), %rdi
movq 0x18(%r14), %rsi
movq 0x20(%r14), %rdx
leaq 0xd407b(%rip), %rcx     # 0x11e0bf
xorl %eax, %eax
callq 0xfa71e
movl 0x1c(%rsp), %esi
movq %r15, %rdi
callq 0xe9595
movq 0x8(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rbp, 0x8(%rbx)
cmpl $0x0, 0x30(%r14)
je 0x4a08d
movq 0x20(%r14), %rdx
leaq 0xd4005(%rip), %rcx     # 0x11e07c
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0xfa71e
jmp 0x4a0bb
movq %r13, %rdi
callq 0xf22f1
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 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x10(%rbp)
leaq 0xd3fee(%rip), %rsi     # 0x11e0ce
movq %r15, %rdi
xorl %eax, %eax
callq 0xe93cf
movq %r15, %rdi
movl $0x1, %esi
callq 0xe9585
leaq 0x18(%rbp), %rsi
movq %r14, %rdi
movl $0x9, %edx
movl $0x5, %ecx
callq 0x4a1fd
movl %eax, %ebx
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
testl %ebx, %ebx
movq 0x10(%rsp), %rbx
je 0x4a04b
leaq 0xd3fa5(%rip), %rsi     # 0x11e0d5
movq %r15, %rdi
xorl %eax, %eax
callq 0xe93cf
movq %r15, %rdi
movl $0x1, %esi
callq 0xe9585
leaq 0x20(%rbp), %rsi
movq %r14, %rdi
movl $0x5, %edx
movl $0x4, %ecx
callq 0x4a1fd
movl %eax, %ebx
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
testl %ebx, %ebx
movq 0x10(%rsp), %rbx
jne 0x49ebe
jmp 0x4a04b