// File: /workspace/github_c_makefile_star2_O3/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 0x10616e
movq %rsi, %r14
cmpq $0x100, 0x18(%rsi)      # imm = 0x100
jne 0x10616e
cmpq $-0x10, 0x20(%r14)
jne 0x10616e
cmpq $0x100, 0x28(%r14)      # imm = 0x100
jne 0x10616e
leaq 0x10(%rsp), %rsi
movq $0x20, (%rsi)
movq (%r14), %rdi
callq 0xeeb4b
cmpb $-0x17, %al
je 0x106000
movzbl %al, %eax
cmpl $0xeb, %eax
jne 0x10616e
movq (%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeeb4b
movzbl %al, %eax
jmp 0x10600d
movq (%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeebeb
leaq 0x10(%rsp), %rsi
addq %rax, (%rsi)
movq (%r14), %rdi
callq 0xeed31
cmpl $0xfcbc0001, %eax       # imm = 0xFCBC0001
jne 0x10616e
movq (%r14), %rdi
movq 0x10(%rsp), %rdx
leaq 0x18(%rsp), %r15
movl $0x8, %ecx
movq %r15, %rsi
callq 0xee8d4
leaq 0x46172(%rip), %rsi     # 0x14c1bb
movl $0x8, %edx
movq %r15, %rdi
movl $0x3f, %ecx
xorl %r8d, %r8d
callq 0xeba3e
testl %eax, %eax
jne 0x106088
leaq 0x46200(%rip), %rsi     # 0x14c269
leaq 0x18(%rsp), %rdi
movl $0x7, %edx
movl $0x3f, %ecx
xorl %r8d, %r8d
callq 0xeba3e
testl %eax, %eax
je 0x10616e
movq (%r14), %rdi
movq 0x48(%r14), %rsi
leaq 0x8(%rsp), %rcx
movl $0x68, %edx
callq 0x105d2a
testl %eax, %eax
jne 0x10613e
movq (%r14), %rdi
movq 0x48(%r14), %rsi
incq %rsi
leaq 0x8(%rsp), %rcx
movl $0x68, %edx
callq 0x105d2a
testl %eax, %eax
jne 0x10613e
movq (%r14), %rdi
movq 0x48(%r14), %rsi
addq $0x3, %rsi
leaq 0x8(%rsp), %rcx
movl $0x68, %edx
callq 0x105d2a
testl %eax, %eax
jne 0x10613e
movl $0x4ec, %esi            # imm = 0x4EC
addq 0x40(%r14), %rsi
movq (%r14), %rdi
leaq 0x8(%rsp), %rcx
movl $0x68, %edx
callq 0x105d2a
testl %eax, %eax
jne 0x10613e
movl $0x4ef, %esi            # imm = 0x4EF
addq 0x40(%r14), %rsi
movq (%r14), %rdi
leaq 0x8(%rsp), %rcx
movl $0x68, %edx
callq 0x105d2a
testl %eax, %eax
jne 0x10613e
movl $0x50a, %esi            # imm = 0x50A
addq 0x40(%r14), %rsi
movq (%r14), %rdi
leaq 0x8(%rsp), %rcx
movl $0x68, %edx
callq 0x105d2a
testl %eax, %eax
je 0x10616e
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 0x10616e
movb $0x64, 0x1(%rbx)
movb $0x1, 0x3(%rbx)
leaq 0x3ef18(%rip), %rax     # 0x145082
movq %rax, 0x40(%rbx)
cmpb $0x64, 0x1(%rbx)
jne 0x10618c
addq $0x50, %rbx
leaq 0x25b81(%rip), %rsi     # 0x12bd00
movl $0x28, %edx
movq %rbx, %rdi
callq 0xe8219
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq