// File: /workspace/github_c_makefile_star2_O1/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
movl $0x138, %esi            # imm = 0x138
callq 0xe969b
movq %rax, %r15
movq 0x18(%rbx), %rax
movq %rax, 0x20(%r15)
movl $0x90, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
leaq 0xc0db5(%rip), %rax     # 0x123b30
movq %rax, (%r14)
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, 0x10(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x634f2
movb $0x1, %al
movb %al, 0x42(%r15)
movb %al, 0x45(%r15)
movq 0x18(%rbx), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0x1053ee
leaq 0x18(%r14), %rdx
movb $-0x35, 0x18(%r14)
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x105ccc
cmpb $-0x35, 0x19(%r14)
jne 0x62de8
movq 0x10(%r14), %rax
movq $-0xa0001, %rcx         # imm = 0xFFF5FFFF
addq 0x50(%rax), %rcx
cmpq $-0xa0000, %rcx         # imm = 0xFFF60000
ja 0x62e48
movb $0x1, 0x13(%r15)
xorl %r12d, %r12d
testq %r15, %r15
je 0x62e1b
cmpb $0x0, 0x13(%r15)
je 0x62e10
movq (%r14), %rdx
leaq 0xc0d32(%rip), %rsi     # 0x123b38
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movq %r12, %rdi
callq 0xf0b13
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x48(%rax), %rdx
movq %rdx, 0x28(%r15)
leaq 0xc0c09(%rip), %rsi     # 0x123a60
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq 0x28(%r15), %rdx
leaq 0xe(%rsp), %r12
movl $0x2, %ecx
movq %r12, %rsi
callq 0xeeee8
cmpb $0xa, 0x1(%r12)
jne 0x62e8f
cmpb $0xd, 0xe(%rsp)
je 0x62e8f
movb $0x1, 0x8(%r14)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %r12
leaq -0x1(%r12), %r13
movq %r13, %rsi
callq 0xef0d5
cmpb $0x20, %al
cmoveq %r13, %r12
subq 0x28(%r15), %r12
movq %r12, 0x30(%r15)
cmpb $0x0, 0x12(%r15)
jne 0x62ded
cmpb $0x0, 0x8(%r14)
je 0x62f11
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r12
movq %rax, %rdi
movl $0xd, %esi
callq 0xf11be
movq 0x18(%rbx), %rdi
movq 0x28(%r15), %rsi
movq 0x30(%r15), %rdx
incq %rsi
decq %rdx
movq %r12, %rcx
callq 0xefabc
movq %r12, %rdi
callq 0xf0ea4
movq %r12, 0x20(%r15)
movq $0x0, 0x28(%r15)
jmp 0x62f14
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x63581
jmp 0x62df0