// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %r14
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, 0x18(%r14)
leaq 0x9da0b(%rip), %rsi     # 0x12ee3f
movq %rbx, %rdi
movl $0xff, %edx
callq 0xea727
movb %al, 0x10(%r14)
leaq 0x8c547(%rip), %rsi     # 0x11d993
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x91465
movq %rax, %rdi
callq 0xea781
movl %eax, 0xc(%r14)
cmpl $0x200, 0xc(%r14)       # imm = 0x200
je 0x91477
movl $0x10, 0xc(%r14)
movq 0x18(%rbx), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
callq 0x1053ee
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 0x1055db
cmpb $0x1, 0x1(%r15)
jne 0x91510
leaq 0x9d950(%rip), %rsi     # 0x12ee2a
movq %rbx, %rdi
callq 0xeb6d6
movzbl 0x12(%rsp), %eax
movl %eax, (%r14)
leaq 0x9a978(%rip), %rdx     # 0x12be69
cmpl $0x2, %eax
jle 0x9151c
cmpl $0x3, %eax
je 0x9152f
cmpl $0x66, %eax
je 0x91538
cmpl $0xca, %eax
jne 0x91548
leaq 0x9da22(%rip), %rdx     # 0x12ef30
jmp 0x91548
leaq 0x9d932(%rip), %rsi     # 0x12ee49
jmp 0x9166d
cmpl $0x1, %eax
je 0x91541
cmpl $0x2, %eax
jne 0x91548
leaq 0x9d9e6(%rip), %rdx     # 0x12ef13
jmp 0x91548
leaq 0x9d9e3(%rip), %rdx     # 0x12ef19
jmp 0x91548
leaq 0x9d9e1(%rip), %rdx     # 0x12ef20
jmp 0x91548
leaq 0x9d9c5(%rip), %rdx     # 0x12ef0d
leaq 0x9d917(%rip), %rsi     # 0x12ee66
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movl (%r14), %eax
movb $0x1, %cl
cmpl $0x4, %eax
jl 0x91596
cmpl $0xca, %eax
je 0x9156f
cmpl $0x66, %eax
jne 0x9159f
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)
cmpb $0x0, 0x12(%r14)
je 0x91666
movzbl 0x10(%r14), %eax
cmpl $0xff, %eax
je 0x915c9
testl %eax, %eax
jne 0x915d4
cmpb $0x0, 0x11(%r14)
je 0x91666
cmpb $-0x1, %al
jne 0x915d4
cmpb $0x0, 0x11(%r14)
je 0x916d7
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf0faf
movq %rax, 0x80(%r14)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf0faf
movq %rax, 0x88(%r14)
movq %rax, %rdi
callq 0xf0e3f
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xef1ca
movl %eax, 0x20(%r14)
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xef1ca
movl %eax, 0x24(%r14)
movq 0x18(%r14), %rax
movq 0x20(%rax), %rdx
testq %rdx, %rdx
jns 0x9163b
movl $0x1, 0x4(%r14)
cmpl $0x0, 0x4(%r14)
jne 0x91677
shlq $0x4, %rdx
addq 0x38(%rax), %rdx
movq %rdx, 0x28(%r14)
movq 0x18(%rbx), %rax
cmpq 0x18(%rax), %rdx
jle 0x916fe
movl $0x1, 0x4(%r14)
jmp 0x916ca
leaq 0x9d80b(%rip), %rsi     # 0x12ee78
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
testq %r14, %r14
je 0x916ca
cmpl $0x0, 0x4(%r14)
je 0x9169b
cmpl $0x0, 0x8(%r14)
jne 0x9169b
leaq 0x9d861(%rip), %rsi     # 0x12eef2
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq 0x80(%r14), %rdi
callq 0xf0b13
movq 0x88(%r14), %rdi
callq 0xf0b13
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x90, %rsp
popq %rbx
popq %r14
popq %r15
retq
leaq 0x9d7b4(%rip), %rsi     # 0x12ee92
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
leaq 0x9d7cd(%rip), %rsi     # 0x12eebc
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9ef5
jmp 0x91677
leaq 0x8(%rsp), %r15
movq %rdx, (%r15)
leaq 0x9d831(%rip), %rsi     # 0x12ef3e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x50(%r14)
leaq 0xb9755(%rip), %rsi     # 0x14ae90
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef2dd
movq %rax, 0x48(%r14)
leaq 0xa81fa(%rip), %rsi     # 0x139958
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x40(%r14)
leaq 0x8c29b(%rip), %rsi     # 0x11da1c
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef2dd
movq %rax, 0x38(%r14)
leaq 0x8c27f(%rip), %rsi     # 0x11da23
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x58(%r14)
movq %rax, %rcx
shlq $0x4, %rcx
leaq 0x9d78a(%rip), %rsi     # 0x12ef58
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x60(%r14)
leaq 0x9d77f(%rip), %rsi     # 0x12ef70
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x68(%r14)
leaq 0x9d767(%rip), %rsi     # 0x12ef7b
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
cmpl $0x1, (%r14)
jne 0x91871
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x70(%r14)
leaq 0x9d744(%rip), %rsi     # 0x12ef86
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x78(%r14)
leaq 0x9d72c(%rip), %rsi     # 0x12ef91
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpl $0x0, 0x4(%r14)
jne 0x91677
movq %rbx, %rdi
movq %r14, %rsi
callq 0x91929
cmpl $0x0, 0x4(%r14)
jne 0x91677
movq %rbx, %rdi
movq %r14, %rsi
callq 0x91ad3
movq 0x88(%r14), %rdi
callq 0xf0ea4
cmpl $0x0, 0x4(%r14)
jne 0x91677
cmpb $0x1, 0x10(%r14)
jne 0x918f5
movq 0x88(%r14), %rdi
testq %rdi, %rdi
je 0x91677
movq 0x18(%rdi), %rdx
leaq 0xa3b4e(%rip), %rcx     # 0x135431
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xf0321
jmp 0x91677
movq %rbx, %rdi
movq %r14, %rsi
callq 0x91d67
jmp 0x91677