// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/exectext.c
// Function: de_run_texe
static void de_run_texe(deark *c, de_module_params *mparams)
{
	struct texe_ctx *tctx = NULL;
	lctx *d = NULL;
	dbuf *tmpdbuf = NULL;

	d = create_lctx(c);

	tctx = de_malloc(c, sizeof(struct texe_ctx));
	tctx->msgpfx = "[TEXE] ";
	tctx->ei = de_malloc(c, sizeof(struct fmtutil_exe_info));

	exectext_set_common_enc_opts(c, d, DE_ENCODING_CP437);

	d->chartypes[10] = ETCT_CONTROL;
	d->chartypes[13] = ETCT_CONTROL;

	fmtutil_collect_exe_info(c, c->infile, tctx->ei);

	tctx->edd.restrict_to_fmt = DE_SPECIALEXEFMT_TEXE;
	fmtutil_detect_specialexe(c, tctx->ei, &tctx->edd);
	if(tctx->edd.detected_fmt!=DE_SPECIALEXEFMT_TEXE) {
		d->need_errmsg = 1;
		goto done;
	}

	if(tctx->ei->overlay_len<2 || tctx->ei->overlay_len > 640*1024) {
		d->need_errmsg = 1;
		goto done;
	}

	texe_find_text(c, tctx, d);
	if(d->errflag) goto done;

	if(tctx->use_cr_hack) {
		// The first byte of the file seems lost, due to an apparent bug in
		// TEXE.
		// But if the second byte is LF, we assume the first byte was CR, and
		// can fix it.
		// (Here we make a modified copy of the data.)
		tmpdbuf = dbuf_create_membuf(c, d->tlen, 0);
		dbuf_writebyte(tmpdbuf, 0x0d);
		dbuf_copy(c->infile, d->tpos+1, d->tlen-1, tmpdbuf);
		dbuf_flush(tmpdbuf);
		d->inf = tmpdbuf;
		d->tpos = 0;
	}

	exectext_extract_default(c, d);

done:
	if(d) {
		if(d->need_errmsg && tctx) {
			de_err(c, "%sBad or unsupported TEXE file", tctx->msgpfx);
		}
		de_free(c, d);
		d = NULL;
	}
	if(tctx) {
		de_free(c, tctx->ei);
		de_free(c, tctx);
		tctx = NULL;
	}
	dbuf_close(tmpdbuf);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
callq 0x5fc9f
movq %rax, %r15
movl $0x90, %esi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r14
leaq 0xb7555(%rip), %rax     # 0x116b50
movq %rax, (%r14)
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, 0x10(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5fcb7
movb $0x1, %al
movb %al, 0x42(%r15)
movb %al, 0x45(%r15)
movq 0x18(%rbx), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xf8849
leaq 0x18(%r14), %rdx
movb $-0x35, 0x18(%r14)
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0xf90d3
cmpb $-0x35, 0x19(%r14)
jne 0x5f668
movq 0x10(%r14), %rax
movq $-0xa0001, %rcx         # imm = 0xFFF5FFFF
addq 0x50(%rax), %rcx
cmpq $-0xa0000, %rcx         # imm = 0xFFF60000
ja 0x5f6c3
movb $0x1, 0x13(%r15)
xorl %r12d, %r12d
cmpb $0x0, 0x13(%r15)
je 0x5f68b
movq (%r14), %rdx
leaq 0xb74d7(%rip), %rsi     # 0x116b58
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movq %r12, %rdi
callq 0xe5591
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x48(%rax), %rdx
movq %rdx, 0x28(%r15)
leaq 0xb73ae(%rip), %rsi     # 0x116a80
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq 0x28(%r15), %rdx
leaq 0xe(%rsp), %r12
pushq $0x2
popq %rcx
movq %r12, %rsi
callq 0xe3cb7
cmpb $0xa, 0x1(%r12)
jne 0x5f708
cmpb $0xd, 0xe(%rsp)
je 0x5f708
movb $0x1, 0x8(%r14)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %r12
leaq -0x1(%r12), %r13
movq %r13, %rsi
callq 0xe3eb4
cmpb $0x20, %al
cmoveq %r13, %r12
subq 0x28(%r15), %r12
movq %r12, 0x30(%r15)
cmpb $0x0, 0x12(%r15)
jne 0x5f66d
cmpb $0x0, 0x8(%r14)
je 0x5f785
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r12
pushq $0xd
popq %rsi
movq %rax, %rdi
callq 0xe5bc5
movq 0x18(%rbx), %rdi
movq 0x28(%r15), %rsi
movq 0x30(%r15), %rdx
incq %rsi
decq %rdx
movq %r12, %rcx
callq 0xe45f4
movq %r12, %rdi
callq 0xe58e8
movq %r12, 0x20(%r15)
andq $0x0, 0x28(%r15)
jmp 0x5f788
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5fd39
jmp 0x5f670