// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil-exe.c
// Function: detect_exesfx_lha
static void detect_exesfx_lha(deark *c,
	struct fmtutil_exe_info *ei, struct fmtutil_specialexe_detection_data *edd)
{
	u8 b[8];
	u8 x;
	int found;
	i64 foundpos = 0;
	i64 pos;
	i64 j;

	if(ei->regSS != -16) goto done;
	if(ei->regSP != 256) goto done;
	if(ei->regCS != -16) goto done;
	if(ei->regIP != 256) goto done;

	pos = 32;
	x = dbuf_getbyte_p(ei->f, &pos);
	if(x==0xeb) {
		j = (i64)dbuf_getbyte_p(ei->f, &pos);
	}
	else if(x==0xe9) {
		j = dbuf_getu16le_p(ei->f, &pos);
	}
	else {
		goto done;
	}
	pos += j;

	if((u32)dbuf_getu32be_p(ei->f, &pos) != (u32)0xfcbc0001U) {
		goto done;
	}

	dbuf_read(ei->f, b, pos, sizeof(b));
	if(de_memmatch(b, (const u8*)"\xbb?\x01\xe8??\x8c\xc8", 8, '?', 0)) {
		;
	}
	else if(de_memmatch(b, (const u8*)"\x8c\xc8\x05?\x00\x8e\xc0", 7, '?', 0)) {
		;
	}
	else {
		goto done;
	}

	found =
		is_lha_data_at(ei, ei->end_of_dos_code, &foundpos) ||
		is_lha_data_at(ei, ei->end_of_dos_code+1, &foundpos) ||
		is_lha_data_at(ei, ei->end_of_dos_code+3, &foundpos) ||
		is_lha_data_at(ei, ei->entry_point + 1292-32, &foundpos) ||
		is_lha_data_at(ei, ei->entry_point + 1295-32, &foundpos) ||
		is_lha_data_at(ei, ei->entry_point + 1322-32, &foundpos);
	if(!found) goto done;

	edd->payload_pos = foundpos;
	edd->payload_len = ei->f->len - edd->payload_pos;
	if(edd->payload_len<21) goto done;

	edd->detected_fmt = DE_SPECIALEXEFMT_SFX;
	edd->payload_valid = 1;
	edd->payload_file_ext = "lzh";

done:
	if(edd->detected_fmt==DE_SPECIALEXEFMT_SFX) {
		de_strlcpy(edd->detected_fmt_name, "LHA", sizeof(edd->detected_fmt_name));
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x18, %rsp
andq $0x0, (%rsp)
movq %rdx, %rbx
cmpq $-0x10, 0x10(%rsi)
jne 0xf9954
movq %rsi, %r14
cmpq $0x100, 0x18(%rsi)      # imm = 0x100
jne 0xf9954
cmpq $-0x10, 0x20(%r14)
jne 0xf9954
cmpq $0x100, 0x28(%r14)      # imm = 0x100
jne 0xf9954
leaq 0x8(%rsp), %rsi
movq $0x20, (%rsi)
movq (%r14), %rdi
callq 0xe3f2a
cmpb $-0x17, %al
je 0xf981b
movzbl %al, %eax
cmpl $0xeb, %eax
jne 0xf9954
movq (%r14), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe3f2a
movzbl %al, %eax
jmp 0xf9828
movq (%r14), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe3fc4
leaq 0x8(%rsp), %rsi
addq %rax, (%rsi)
movq (%r14), %rdi
callq 0xe4071
cmpl $0xfcbc0001, %eax       # imm = 0xFCBC0001
jne 0xf9954
movq (%r14), %rdi
movq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %r15
pushq $0x8
popq %r12
movq %r15, %rsi
movq %r12, %rcx
callq 0xe3cb7
leaq 0x45455(%rip), %rsi     # 0x13ecbb
pushq $0x3f
popq %rcx
movq %r15, %rdi
movq %r12, %rdx
xorl %r8d, %r8d
callq 0xe0fff
testl %eax, %eax
jne 0xf989d
leaq 0x454e7(%rip), %rsi     # 0x13ed69
leaq 0x10(%rsp), %rdi
pushq $0x7
popq %rdx
pushq $0x3f
popq %rcx
xorl %r8d, %r8d
callq 0xe0fff
testl %eax, %eax
je 0xf9954
movq 0x48(%r14), %rsi
movq %rsp, %rdx
movq %r14, %rdi
callq 0xf9ea2
testl %eax, %eax
jne 0xf9925
movq 0x48(%r14), %rsi
incq %rsi
movq %rsp, %rdx
movq %r14, %rdi
callq 0xf9ea2
testl %eax, %eax
jne 0xf9925
movq 0x48(%r14), %rsi
addq $0x3, %rsi
movq %rsp, %rdx
movq %r14, %rdi
callq 0xf9ea2
testl %eax, %eax
jne 0xf9925
movl $0x4ec, %esi            # imm = 0x4EC
addq 0x40(%r14), %rsi
movq %rsp, %rdx
movq %r14, %rdi
callq 0xf9ea2
testl %eax, %eax
jne 0xf9925
movl $0x4ef, %esi            # imm = 0x4EF
addq 0x40(%r14), %rsi
movq %rsp, %rdx
movq %r14, %rdi
callq 0xf9ea2
testl %eax, %eax
jne 0xf9925
movl $0x50a, %esi            # imm = 0x50A
addq 0x40(%r14), %rsi
movq %rsp, %rdx
movq %r14, %rdi
callq 0xf9ea2
testl %eax, %eax
je 0xf9954
movq (%rsp), %rax
movq %rax, 0x10(%rbx)
movq (%r14), %rcx
movq 0x18(%rcx), %rcx
subq %rax, %rcx
movq %rcx, 0x18(%rbx)
cmpq $0x15, %rcx
jl 0xf9954
movb $0x64, 0x1(%rbx)
movb $0x1, 0x3(%rbx)
leaq 0x3e2e2(%rip), %rax     # 0x137c32
movq %rax, 0x40(%rbx)
cmpb $0x64, 0x1(%rbx)
jne 0xf9970
addq $0x50, %rbx
leaq 0x2512b(%rip), %rsi     # 0x11ea90
pushq $0x28
popq %rdx
movq %rbx, %rdi
callq 0xde492
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq