// File: /workspace/github_c_makefile_star2_O1/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 %rbx
subq $0x20, %rsp
movq %rdx, %rbx
movq $0x0, 0x8(%rsp)
cmpq $-0x10, 0x10(%rsi)
jne 0x1065e7
movq %rsi, %r14
cmpq $0x100, 0x18(%rsi)      # imm = 0x100
jne 0x1065e7
cmpq $-0x10, 0x20(%r14)
jne 0x1065e7
cmpq $0x100, 0x28(%r14)      # imm = 0x100
jne 0x1065e7
leaq 0x10(%rsp), %rsi
movq $0x20, (%rsi)
movq (%r14), %rdi
callq 0xef14d
cmpb $-0x17, %al
je 0x106479
movzbl %al, %eax
cmpl $0xeb, %eax
jne 0x1065e7
movq (%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xef14d
movzbl %al, %eax
jmp 0x106486
movq (%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xef1ed
leaq 0x10(%rsp), %rsi
addq %rax, (%rsi)
movq (%r14), %rdi
callq 0xef34e
cmpl $0xfcbc0001, %eax       # imm = 0xFCBC0001
jne 0x1065e7
movq (%r14), %rdi
movq 0x10(%rsp), %rdx
leaq 0x18(%rsp), %r15
movl $0x8, %ecx
movq %r15, %rsi
callq 0xeeee8
leaq 0x456d9(%rip), %rsi     # 0x14bb9b
movl $0x8, %edx
movq %r15, %rdi
movl $0x3f, %ecx
xorl %r8d, %r8d
callq 0xec00b
testl %eax, %eax
jne 0x106501
leaq 0x45767(%rip), %rsi     # 0x14bc49
leaq 0x18(%rsp), %rdi
movl $0x7, %edx
movl $0x3f, %ecx
xorl %r8d, %r8d
callq 0xec00b
testl %eax, %eax
je 0x1065e7
movq 0x48(%r14), %rsi
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
movl $0x68, %edx
callq 0x10619f
testl %eax, %eax
jne 0x1065b7
movq 0x48(%r14), %rsi
incq %rsi
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
movl $0x68, %edx
callq 0x10619f
testl %eax, %eax
jne 0x1065b7
movq 0x48(%r14), %rsi
addq $0x3, %rsi
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
movl $0x68, %edx
callq 0x10619f
testl %eax, %eax
jne 0x1065b7
movl $0x4ec, %esi            # imm = 0x4EC
addq 0x40(%r14), %rsi
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
movl $0x68, %edx
callq 0x10619f
testl %eax, %eax
jne 0x1065b7
movl $0x4ef, %esi            # imm = 0x4EF
addq 0x40(%r14), %rsi
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
movl $0x68, %edx
callq 0x10619f
testl %eax, %eax
jne 0x1065b7
movl $0x50a, %esi            # imm = 0x50A
addq 0x40(%r14), %rsi
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
movl $0x68, %edx
callq 0x10619f
testl %eax, %eax
je 0x1065e7
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rbx)
movq (%r14), %rcx
movq 0x18(%rcx), %rcx
subq %rax, %rcx
movq %rcx, 0x18(%rbx)
cmpq $0x15, %rcx
jl 0x1065e7
movb $0x64, 0x1(%rbx)
movb $0x1, 0x3(%rbx)
leaq 0x3e5bf(%rip), %rax     # 0x144ba2
movq %rax, 0x40(%rbx)
cmpb $0x64, 0x1(%rbx)
jne 0x106605
addq $0x50, %rbx
leaq 0x25420(%rip), %rsi     # 0x12ba18
movl $0x28, %edx
movq %rbx, %rdi
callq 0xe8ded
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq