// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-exe.c
// Function: detect_exesfx_pak_nogate
static void detect_exesfx_pak_nogate(deark *c,
	struct fmtutil_exe_info *ei, struct fmtutil_specialexe_detection_data *edd)
{
	int found = 0;
	int canextract = 0;
	u8 v16flag = 0;

	read_exe_testbytes(ei);
	if(ei->ovl64b[0]!=0x1a && ei->ovl64b[0]!=0xfe) goto done;

	if(!de_memcmp(ei->ep64b, (const u8*)"\x55\x8b\xec\x83\xec\x50", 6)) {
		// PAK 1.51-2.51
		found = 1;

		// There's a problem with PAK v1.6. If there are any file or archive
		// comments, the payload will not be in proper PAK format.
		// We can convert it, but it needs special detection and handling.
		if(ei->entry_point==2656) {
			v16flag = 1;
		}
		else {
			canextract = 1;
		}
	}
	if(!found && !de_memcmp(ei->ep64b, (const u8*)"\xfb\xba\x53\x03\x2e\x89\x16\x65", 8)) {
		// GSARC 1.0 and PAK 1.0
		found = 1;
		canextract = 1;
	}

	if(!found) goto done;
	if(ei->ovl64b[0] != 0x1a) canextract = 0;

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

	if(v16flag) {
		edd->detected_fmt = DE_SPECIALEXEFMT_PAK16SFX;
	}
	else {
		edd->detected_fmt = DE_SPECIALEXEFMT_SFX;
		edd->payload_valid = canextract;
		edd->payload_file_ext = "pak";
	}

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movb $0x0, -0x21(%rbp)
movq -0x10(%rbp), %rdi
callq 0x189f70
movq -0x10(%rbp), %rax
movzbl 0xb0(%rax), %eax
cmpl $0x1a, %eax
je 0x18ac96
movq -0x10(%rbp), %rax
movzbl 0xb0(%rax), %eax
cmpl $0xfe, %eax
je 0x18ac96
jmp 0x18ad9e
movq -0x10(%rbp), %rdi
addq $0x70, %rdi
leaq 0x4ceac(%rip), %rsi     # 0x1d7b51
movl $0x6, %edx
callq 0x19170
cmpl $0x0, %eax
jne 0x18acd8
movl $0x1, -0x1c(%rbp)
movq -0x10(%rbp), %rax
cmpq $0xa60, 0x40(%rax)      # imm = 0xA60
jne 0x18accf
movb $0x1, -0x21(%rbp)
jmp 0x18acd6
movl $0x1, -0x20(%rbp)
jmp 0x18acd8
cmpl $0x0, -0x1c(%rbp)
jne 0x18ad0f
movq -0x10(%rbp), %rax
addq $0x70, %rax
movq (%rax), %rax
movabsq $0x6516892e0353bafb, %rcx # imm = 0x6516892E0353BAFB
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x18ad0f
movl $0x1, -0x1c(%rbp)
movl $0x1, -0x20(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x18ad1a
jmp 0x18ad9e
movq -0x10(%rbp), %rax
movzbl 0xb0(%rax), %eax
cmpl $0x1a, %eax
je 0x18ad31
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq -0x18(%rbp), %rax
subq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
cmpq $0x2, 0x18(%rax)
jge 0x18ad69
jmp 0x18ad9e
cmpb $0x0, -0x21(%rbp)
je 0x18ad79
movq -0x18(%rbp), %rax
movb $0x67, 0x1(%rax)
jmp 0x18ad9c
movq -0x18(%rbp), %rax
movb $0x64, 0x1(%rax)
movl -0x20(%rbp), %eax
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x18(%rbp), %rax
leaq 0x156d5(%rip), %rcx     # 0x1a046d
movq %rcx, 0x40(%rax)
jmp 0x18ad9e
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x64, %eax
je 0x18adb8
movq -0x18(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x67, %eax
jne 0x18add1
movq -0x18(%rbp), %rdi
addq $0x50, %rdi
leaq 0x4cd9a(%rip), %rsi     # 0x1d7b61
movl $0x28, %edx
callq 0x15aa30
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)