// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r15
movq 0x18(%rbx), %rax
movq %rax, 0x20(%r15)
movl $0x90, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
leaq 0xc1140(%rip), %rax     # 0x123d10
movq %rax, (%r14)
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, 0x10(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x63336
movb $0x1, %al
movb %al, 0x42(%r15)
movb %al, 0x45(%r15)
movq 0x18(%rbx), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0x104f96
leaq 0x18(%r14), %rdx
movb $-0x35, 0x18(%r14)
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x105864
cmpb $-0x35, 0x19(%r14)
jne 0x62c3d
movq 0x10(%r14), %rax
movq $-0xa0001, %rcx         # imm = 0xFFF5FFFF
addq 0x50(%rax), %rcx
cmpq $-0xa0000, %rcx         # imm = 0xFFF60000
ja 0x62c98
movb $0x1, 0x13(%r15)
xorl %r12d, %r12d
cmpb $0x0, 0x13(%r15)
je 0x62c60
movq (%r14), %rdx
leaq 0xc10c2(%rip), %rsi     # 0x123d18
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
movq %r12, %rdi
callq 0xf052b
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x48(%rax), %rdx
movq %rdx, 0x28(%r15)
leaq 0xc0f99(%rip), %rsi     # 0x123c40
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq 0x28(%r15), %rdx
leaq 0xe(%rsp), %r12
movl $0x2, %ecx
movq %r12, %rsi
callq 0xee8d4
cmpb $0xa, 0x1(%r12)
jne 0x62cdf
cmpb $0xd, 0xe(%rsp)
je 0x62cdf
movb $0x1, 0x8(%r14)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %r12
leaq -0x1(%r12), %r13
movq %r13, %rsi
callq 0xeead3
cmpb $0x20, %al
cmoveq %r13, %r12
subq 0x28(%r15), %r12
movq %r12, 0x30(%r15)
cmpb $0x0, 0x12(%r15)
jne 0x62c42
cmpb $0x0, 0x8(%r14)
je 0x62d61
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r12
movq %rax, %rdi
movl $0xd, %esi
callq 0xf0bd6
movq 0x18(%rbx), %rdi
movq 0x28(%r15), %rsi
movq 0x30(%r15), %rdx
incq %rsi
decq %rdx
movq %r12, %rcx
callq 0xef481
movq %r12, %rdi
callq 0xf08ba
movq %r12, 0x20(%r15)
movq $0x0, 0x28(%r15)
jmp 0x62d64
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x633b6
jmp 0x62c45