// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/lzexe.c
// Function: de_run_lzexe
static void de_run_lzexe(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	const char *s;
	struct fmtutil_specialexe_detection_data edd;

	d = de_malloc(c, sizeof(lctx));

	d->host_ei = de_malloc(c, sizeof(struct fmtutil_exe_info));

	d->raw_mode = (u8)de_get_ext_option_bool(c, "lzexe:raw", 0xff);

	s = de_get_ext_option(c, "execomp:align");
	if(s) {
		d->final_code_alignment = de_atoi(s);
	}
	if(d->final_code_alignment != 512) {
		d->final_code_alignment = 16;
	}

	fmtutil_collect_exe_info(c, c->infile, d->host_ei);

	de_zeromem(&edd, sizeof(struct fmtutil_specialexe_detection_data));
	edd.restrict_to_fmt = DE_SPECIALEXEFMT_LZEXE;
	fmtutil_detect_execomp(c, d->host_ei, &edd);
	if(edd.detected_fmt!=DE_SPECIALEXEFMT_LZEXE) {
		de_err(c, "Not an LZEXE-compressed file");
		goto done;
	}
	de_declare_fmt(c, "LZEXE-compressed EXE");
	d->ver = (int)edd.detected_subfmt;
	de_dbg(c, "LZEXE variant: %s", get_lzexe_subfmt_name(d->ver));
	if(d->ver<=3) {
		d->can_decompress_to_exe = 1;
		d->can_decompress_to_raw = 1;
	}
	else if(d->ver==LZEXE_VER_LHARK_SFX || d->ver==LZEXE_VER_PCX2EXE) {
		d->host_ei->regCS = edd.regCS_2;
		d->host_ei->regIP = edd.regIP_2;
		d->host_ei->entry_point = d->host_ei->start_of_dos_code + d->host_ei->regCS*16 +
			d->host_ei->regIP;
		d->can_decompress_to_exe = 0;
		d->can_decompress_to_raw = 1;
	}

	if(!d->can_decompress_to_raw ||
		(d->raw_mode==0 && !d->can_decompress_to_exe))
	{
		de_err(c, "Unsupported LZEXE variant");
		goto done;
	}

	if(d->raw_mode==0xff && !d->can_decompress_to_exe) {
		de_err(c, "This LZEXE variant is not fully supported");
		de_info(c, "Note: Try \"-opt lzexe:raw\" to decompress the raw data");
		goto done;
	}

	d->guest_reloc_table = dbuf_create_membuf(c, 0, 0);
	d->dcmpr_code = dbuf_create_membuf(c, 0, 0);
	dbuf_enable_wbuffer(d->dcmpr_code);

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

	d->ephdr_pos = d->host_ei->start_of_dos_code + d->host_ei->regCS*16;
	if(d->ephdr_pos > c->infile->len) {
		d->errflag = 1;
		return;
	}
	read_special_hdr(c, d, d->ephdr_pos);
	if(d->errflag) goto done;

	// TODO? Should we do this even if raw_mode==1?
	read_reloc_tbl(c, d);
	if(d->errflag) goto done;

	do_decompress_code(c, d);
	dbuf_flush(d->dcmpr_code);
	if(d->errflag) goto done;

	if(d->raw_mode==1) {
		do_write_data_only(c, d);
	}
	else {
		do_write_dcmpr(c, d);
	}

done:

	if(d) {
		if(d->errflag && !d->errmsg_handled) {
			de_err(c, "LZEXE decompression failed");
		}

		dbuf_close(d->guest_reloc_table);
		dbuf_close(d->dcmpr_code);
		de_free(c, d->host_ei);
		de_free(c, d);
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x90, %rsp
movq %rdi, %rbx
movl $0xa8, %esi
callq 0xe8b2b
movq %rax, %r14
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, 0x18(%r14)
leaq 0x9e5b6(%rip), %rsi     # 0x12f122
movq %rbx, %rdi
movl $0xff, %edx
callq 0xe9bd0
movb %al, 0x10(%r14)
leaq 0x8cf1f(%rip), %rsi     # 0x11daa3
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x90b9f
movq %rax, %rdi
callq 0xe9c2a
movl %eax, 0xc(%r14)
jmp 0x90ba3
movl 0xc(%r14), %eax
cmpl $0x200, %eax            # imm = 0x200
je 0x90bb2
movl $0x10, 0xc(%r14)
movq 0x18(%rbx), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
callq 0x104f96
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r15
movaps %xmm0, (%r15)
movaps %xmm0, 0x60(%r15)
movaps %xmm0, 0x50(%r15)
movaps %xmm0, 0x40(%r15)
movaps %xmm0, 0x30(%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
movq $0x0, 0x70(%r15)
movb $0x1, (%r15)
movq 0x18(%r14), %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x105185
cmpb $0x1, 0x1(%r15)
jne 0x90c5e
leaq 0x9e4f8(%rip), %rsi     # 0x12f10d
movq %rbx, %rdi
callq 0xeb125
movzbl 0x12(%rsp), %eax
movl %eax, (%r14)
leaq 0x9b525(%rip), %rdx     # 0x12c151
cmpl $0x2, %eax
jle 0x90cca
cmpl $0x3, %eax
je 0x90cdd
cmpl $0x66, %eax
je 0x90ce6
cmpl $0xca, %eax
jne 0x90cf6
leaq 0x9e5ba(%rip), %rdx     # 0x12f213
jmp 0x90cf6
leaq 0x9e4c7(%rip), %rsi     # 0x12f12c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
cmpl $0x0, 0x4(%r14)
je 0x90c8e
cmpl $0x0, 0x8(%r14)
jne 0x90c8e
leaq 0x9e551(%rip), %rsi     # 0x12f1d5
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq 0x80(%r14), %rdi
callq 0xf052b
movq 0x88(%r14), %rdi
callq 0xf052b
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x90, %rsp
popq %rbx
popq %r14
popq %r15
retq
cmpl $0x1, %eax
je 0x90cef
cmpl $0x2, %eax
jne 0x90cf6
leaq 0x9e51b(%rip), %rdx     # 0x12f1f6
jmp 0x90cf6
leaq 0x9e518(%rip), %rdx     # 0x12f1fc
jmp 0x90cf6
leaq 0x9e516(%rip), %rdx     # 0x12f203
jmp 0x90cf6
leaq 0x9e4fa(%rip), %rdx     # 0x12f1f0
leaq 0x9e44c(%rip), %rsi     # 0x12f149
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl (%r14), %eax
movb $0x1, %cl
cmpl $0x4, %eax
jl 0x90d48
cmpl $0xca, %eax
je 0x90d21
cmpl $0x66, %eax
jne 0x90e12
movq 0x40(%rsp), %rax
movq 0x18(%r14), %rcx
movq %rax, 0x20(%rcx)
movq 0x48(%rsp), %rdx
movq %rdx, 0x28(%rcx)
shlq $0x4, %rax
addq %rdx, %rax
addq 0x38(%rcx), %rax
movq %rax, 0x40(%rcx)
xorl %ecx, %ecx
movb %cl, 0x11(%r14)
movb $0x1, 0x12(%r14)
movzbl 0x10(%r14), %eax
cmpl $0xff, %eax
je 0x90d6d
testl %eax, %eax
jne 0x90d78
cmpb $0x0, 0x11(%r14)
jne 0x90d78
jmp 0x90e1d
cmpb $0x0, 0x11(%r14)
je 0x90e29
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x80(%r14)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x88(%r14)
movq %rax, %rdi
callq 0xf0855
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xeebc8
movl %eax, 0x20(%r14)
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xeebc8
movl %eax, 0x24(%r14)
movq 0x18(%r14), %rax
movq 0x20(%rax), %rdx
testq %rdx, %rdx
js 0x90e05
cmpl $0x0, 0x4(%r14)
jne 0x90c6f
shlq $0x4, %rdx
addq 0x38(%rax), %rdx
movq %rdx, 0x28(%r14)
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rdx
jle 0x90e50
movl $0x1, 0x4(%r14)
jmp 0x90cbd
movl $0x1, 0x4(%r14)
jmp 0x90c6f
cmpb $0x0, 0x12(%r14)
jne 0x90d51
leaq 0x9e337(%rip), %rsi     # 0x12f15b
jmp 0x90c65
leaq 0x9e345(%rip), %rsi     # 0x12f175
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
leaq 0x9e35e(%rip), %rsi     # 0x12f19f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9393
jmp 0x90c6f
leaq 0x8(%rsp), %r15
movq %rdx, (%r15)
leaq 0x9e3c2(%rip), %rsi     # 0x12f221
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x50(%r14)
leaq 0xba5fa(%rip), %rsi     # 0x14b487
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeecce
movq %rax, 0x48(%r14)
leaq 0xa8e48(%rip), %rsi     # 0x139cf8
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x40(%r14)
leaq 0x8cc59(%rip), %rsi     # 0x11db2c
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeecce
movq %rax, 0x38(%r14)
leaq 0x8cc3d(%rip), %rsi     # 0x11db33
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x58(%r14)
movq %rax, %rcx
shlq $0x4, %rcx
leaq 0x9e31b(%rip), %rsi     # 0x12f23b
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x60(%r14)
leaq 0x9e310(%rip), %rsi     # 0x12f253
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x68(%r14)
leaq 0x9e2f8(%rip), %rsi     # 0x12f25e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
cmpl $0x1, (%r14)
jne 0x90fc3
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x70(%r14)
leaq 0x9e2d5(%rip), %rsi     # 0x12f269
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x78(%r14)
leaq 0x9e2bd(%rip), %rsi     # 0x12f274
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpl $0x0, 0x4(%r14)
jne 0x90c6f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9107b
cmpl $0x0, 0x4(%r14)
jne 0x90c6f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x911e7
movq 0x88(%r14), %rdi
callq 0xf08ba
cmpl $0x0, 0x4(%r14)
jne 0x90c6f
cmpb $0x1, 0x10(%r14)
jne 0x91047
movq 0x88(%r14), %rdi
testq %rdi, %rdi
je 0x90c6f
movq 0x18(%rdi), %rdx
leaq 0xa4788(%rip), %rcx     # 0x1357bd
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xefd48
jmp 0x90c6f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x913f9
jmp 0x90c6f