// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/exectext.c
// Function: de_run_ascom
static void de_run_ascom(deark *c, de_module_params *mparams)
{
	i64 n;
	lctx *d = NULL;
	dbuf *tmpdbuf = NULL;

	d = create_lctx(c);
	exectext_set_common_enc_opts(c, d, DE_ENCODING_CP437);
	de_declare_fmt(c, "ASCOM (executable text)");
	d->chartypes[10] = ETCT_CONTROL;
	d->chartypes[13] = ETCT_CONTROL;

	// Some arbitrary tests, to try to make sure we have the right version
	if(dbuf_memcmp(c->infile, 50, (const u8*)"\x72\x41\xc2\xfe\xd1\x7f\xca\xfe", 8) ||
		dbuf_memcmp(c->infile, 2048, (const u8*)"\xee\x40\x20\xf2\x47\xae\xfe\x43", 8))
	{
		d->need_errmsg = 1;
		goto done;
	}

	d->is_encrypted = 1;
	// TODO?: Figure out how to read this offset from the file.
	d->tpos = 3132;
	de_dbg(c, "tpos: %"I64_FMT, d->tpos);
	n = de_getu16le(48);
	d->tlen = 0xf400 - n;
	de_dbg(c, "tlen: %"I64_FMT, d->tlen);
	exectext_check_tpos(c, d);
	if(d->errflag) goto done;

	// Note: The heading is around offset 3040, XORed with 0xfe.

	tmpdbuf = dbuf_create_membuf(c, d->tlen, 0);
	dbuf_enable_wbuffer(tmpdbuf);
	ascom_decrypt(c, d, tmpdbuf);
	d->inf = tmpdbuf;
	d->tpos = 0;

	exectext_extract_default(c, d);

done:
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Not an ASCOM file, or unsupported version");
		}
		destroy_lctx(c, d);
	}
	dbuf_close(tmpdbuf);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl $0x138, %esi            # imm = 0x138
callq 0xe8b2b
movq %rax, %r14
movq 0x18(%rbx), %rax
movq %rax, 0x20(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x63336
leaq 0xc0dbf(%rip), %rsi     # 0x123d6e
movq %rbx, %rdi
callq 0xeb125
movb $0x1, %al
movb %al, 0x42(%r14)
movb %al, 0x45(%r14)
movq 0x18(%rbx), %rdi
leaq 0xc0dba(%rip), %rdx     # 0x123d86
movl $0x32, %esi
movl $0x8, %ecx
callq 0xefc85
testl %eax, %eax
jne 0x62ffd
movq 0x18(%rbx), %rdi
leaq 0xc0da5(%rip), %rdx     # 0x123d8f
movl $0x800, %esi            # imm = 0x800
movl $0x8, %ecx
callq 0xefc85
testl %eax, %eax
je 0x6303b
movb $0x1, 0x13(%r14)
xorl %r15d, %r15d
cmpb $0x0, 0x13(%r14)
je 0x6301d
leaq 0xc0d85(%rip), %rsi     # 0x123d98
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xf052b
movb $0x1, 0x15(%r14)
movq $0xc3c, 0x28(%r14)      # imm = 0xC3C
leaq 0xc0bf1(%rip), %rsi     # 0x123c40
xorl %r15d, %r15d
movl $0xc3c, %edx            # imm = 0xC3C
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x30, %esi
callq 0xeebc8
movl $0xf400, %edx           # imm = 0xF400
subq %rax, %rdx
movq %rdx, 0x30(%r14)
leaq 0xc09aa(%rip), %rsi     # 0x123a2c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r14, %rsi
callq 0x63572
cmpb $0x0, 0x12(%r14)
jne 0x63005
movq 0x30(%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r15
movq %rax, %rdi
callq 0xf0855
cmpq $0x0, 0x30(%r14)
jle 0x630eb
xorl %r12d, %r12d
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rsi
addq %r12, %rsi
callq 0xeead3
xorb $0x1, %al
movzbl %al, %esi
movq %r15, %rdi
callq 0xf0bd6
incq %r12
cmpq 0x30(%r14), %r12
jl 0x630c5
movq %r15, %rdi
callq 0xf08ba
movq %r15, 0x20(%r14)
movq $0x0, 0x28(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x633b6
jmp 0x63005