// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/diet.c
// Function: identify_diet_fmt
static void identify_diet_fmt(deark *c, struct diet_identify_data *idd, u8 idmode,
	struct fmtutil_exe_info *ei)
{
	static const u8 *sig_9d89 = (const u8*)"\x9d\x89";
	static const u8 *sig_dlz = (const u8*)"dlz";
	static const u8 *sig_int21 = (const u8*)"\xb4\x4c\xcd\x21";
	static const u8 *sig_old = (const u8*)"\xfd\xf3\xa5\xfc\x8b\xf7\xbf\x00";
	static const u8 *sig_8edb = (const u8*)"\x8e\xdb\x8e\xc0\x33\xf6\x33\xff\xb9";
	u8 buf[9];

	de_read(buf, 0, sizeof(buf));

	if(buf[0]==0xbe) {
		if(!dbuf_memcmp(c->infile, 35, sig_dlz, 3)) {
			if(!dbuf_memcmp(c->infile, 17, sig_old, 8))
			{
				idd->ftype = FTYPE_COM;
				idd->fmt = FMT_COM_102;
				idd->dlz_pos_known = 1;
				idd->dlz_pos = 35;
				goto done;
			}
		}
	}

	if(buf[0]==0xbf) {
		if(!dbuf_memcmp(c->infile, 17, sig_old, 8))
		{
			idd->ftype = FTYPE_COM;
			idd->fmt = FMT_COM_100;
			idd->crc_pos_known = 1;
			idd->crc_pos = 35;
			idd->cmpr_pos_known = 1;
			idd->cmpr_pos = 37;
			goto done;
		}
	}

	if(buf[0]==0xf9) {
		if(!dbuf_memcmp(c->infile, 65, sig_dlz, 3)) {
			if(!dbuf_memcmp(c->infile, 10, sig_9d89, 2)) {
				idd->ftype = FTYPE_COM;
				idd->fmt = FMT_COM_144;
				idd->dlz_pos_known = 1;
				idd->dlz_pos = 65;
				goto done;
			}
		}
	}

	if(buf[0]==0xb4) {
		if(!de_memcmp(&buf[0], sig_int21, 4)) {
			if(!de_memcmp(&buf[4], sig_9d89, 2)) {
				idd->ftype = FTYPE_DATA;
				if(!de_memcmp(&buf[6], sig_dlz, 3)) {
					idd->fmt = FMT_DATA_144;
					idd->dlz_pos_known = 1;
					idd->dlz_pos =  6;
					goto done;
				}
				idd->fmt = FMT_DATA_100;
				idd->crc_pos_known = 1;
				idd->crc_pos = 6;
				idd->cmpr_pos_known = 1;
				idd->cmpr_pos = 8;
				goto done;
			}
		}
	}

	if(buf[0]==0x9d) {
		if(!de_memcmp(&buf[0], sig_9d89, 2)) {
			if(!de_memcmp(&buf[2], sig_dlz, 3)) {
				idd->ftype = FTYPE_DATA;
				idd->fmt = FMT_DATA_102;
				idd->dlz_pos_known = 1;
				idd->dlz_pos = 2;
				goto done;
			}
		}
	}

	// Don't autodetect EXE -- It's handled by the "exe" module.
	if(idmode) goto done;

	if((buf[0]=='M' && buf[1]=='Z') || (buf[0]=='Z' && buf[1]=='M')) {
		i64 codestart;
		i64 sig_8edb_pos_rel = 0;
		u8 x;

		// TODO?: Probing for the 8e db 8e... byte pattern is good enough for
		// all the DIET-EXE files I've encountered. But it probably ought to be
		// improved, somehow.
		// I've found some files in which the "dlz" signature has been modified,
		// so checking for it wouldn't help much.

		codestart = 16 * de_getu16le(8); // Expected to be 32

		if(!dbuf_memcmp(c->infile, codestart-32+77, sig_8edb, 8)) {
			sig_8edb_pos_rel = 77-32;
		}
		else if(!dbuf_memcmp(c->infile, codestart-32+72, sig_8edb, 8)) {
			sig_8edb_pos_rel = 72-32;
		}
		else if(!dbuf_memcmp(c->infile, codestart-32+52, sig_8edb, 8)) {
			sig_8edb_pos_rel = 52-32;
		}
		else if(!dbuf_memcmp(c->infile, codestart-32+55, sig_8edb, 8)) {
			sig_8edb_pos_rel = 55-32;
		}

		if(sig_8edb_pos_rel==0) goto done;

		x = de_getbyte(codestart+sig_8edb_pos_rel+26);
		if(x==0x95) {
			idd->maybe_lglz = 1;
		}

		if(sig_8edb_pos_rel == 77-32) {
			idd->ftype = FTYPE_EXE;
			idd->fmt = FMT_EXE_145F;
			idd->dlz_pos_known = 1;
			idd->dlz_pos = codestart-32+108;
			check_for_com2exe(c, idd, ei);
			goto done;
		}

		if(sig_8edb_pos_rel == 72-32) {
			idd->ftype = FTYPE_EXE;
			idd->fmt = FMT_EXE_144;
			idd->dlz_pos_known = 1;
			idd->dlz_pos = codestart-32+107;
			check_for_com2exe(c, idd, ei);
			goto done;
		}

		if(sig_8edb_pos_rel == 52-32) {
			idd->ftype = FTYPE_EXE;
			idd->fmt = FMT_EXE_102;
			idd->dlz_pos_known = 1;
			idd->dlz_pos = codestart-32+87;
			goto done;
		}

		if(sig_8edb_pos_rel == 55-32) {
			idd->ftype = FTYPE_EXE;
			idd->fmt = FMT_EXE_100;
			idd->crc_pos_known = 1;
			idd->crc_pos = 18;
			idd->cmpr_pos_known = 1;
			idd->cmpr_pos = codestart-32+90;
			goto done;
		}
	}

done:
	if(idd->dlz_pos_known) {
		idd->crc_pos_known = 1;
		idd->cmpr_pos_known = 1;
		idd->crc_pos = idd->dlz_pos + 6;
		idd->cmpr_pos = idd->dlz_pos + 11;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x29(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x9, %ecx
callq 0x164d10
movzbl -0x29(%rbp), %eax
cmpl $0xbe, %eax
jne 0x699a6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq 0x1bbd1b(%rip), %rdx    # 0x225658
movl $0x23, %esi
movl $0x3, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x699a4
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq 0x1bbd08(%rip), %rdx    # 0x225668
movl $0x11, %esi
movl $0x8, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x699a2
movq -0x10(%rbp), %rax
movl $0x2, 0x4(%rax)
movq -0x10(%rbp), %rax
movl $0x5, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x10(%rbp), %rax
movq $0x23, 0x10(%rax)
jmp 0x69e85
jmp 0x699a4
jmp 0x699a6
movzbl -0x29(%rbp), %eax
cmpl $0xbf, %eax
jne 0x69a18
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq 0x1bbca8(%rip), %rdx    # 0x225668
movl $0x11, %esi
movl $0x8, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x69a16
movq -0x10(%rbp), %rax
movl $0x2, 0x4(%rax)
movq -0x10(%rbp), %rax
movl $0x4, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x9(%rax)
movq -0x10(%rbp), %rax
movq $0x23, 0x18(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0xa(%rax)
movq -0x10(%rbp), %rax
movq $0x25, 0x20(%rax)
jmp 0x69e85
jmp 0x69a18
movzbl -0x29(%rbp), %eax
cmpl $0xf9, %eax
jne 0x69a9b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq 0x1bbc26(%rip), %rdx    # 0x225658
movl $0x41, %esi
movl $0x3, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x69a99
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq 0x1bbbfb(%rip), %rdx    # 0x225650
movl $0xa, %esi
movl $0x2, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x69a97
movq -0x10(%rbp), %rax
movl $0x2, 0x4(%rax)
movq -0x10(%rbp), %rax
movl $0x6, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x10(%rbp), %rax
movq $0x41, 0x10(%rax)
jmp 0x69e85
jmp 0x69a99
jmp 0x69a9b
movzbl -0x29(%rbp), %eax
cmpl $0xb4, %eax
jne 0x69b78
leaq -0x29(%rbp), %rax
movq 0x1bbbab(%rip), %rcx    # 0x225660
movl (%rcx), %ecx
movl (%rax), %eax
subl %ecx, %eax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x69b76
leaq -0x29(%rbp), %rax
addq $0x4, %rax
movq 0x1bbb77(%rip), %rcx    # 0x225650
movw (%rcx), %cx
movw (%rax), %ax
subw %cx, %ax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x69b74
movq -0x10(%rbp), %rax
movl $0x1, 0x4(%rax)
leaq -0x29(%rbp), %rdi
addq $0x6, %rdi
movq 0x1bbb4d(%rip), %rsi    # 0x225658
movl $0x3, %edx
callq 0x19170
cmpl $0x0, %eax
jne 0x69b3d
movq -0x10(%rbp), %rax
movl $0x3, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x10(%rbp), %rax
movq $0x6, 0x10(%rax)
jmp 0x69e85
movq -0x10(%rbp), %rax
movl $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x9(%rax)
movq -0x10(%rbp), %rax
movq $0x6, 0x18(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0xa(%rax)
movq -0x10(%rbp), %rax
movq $0x8, 0x20(%rax)
jmp 0x69e85
jmp 0x69b76
jmp 0x69b78
movzbl -0x29(%rbp), %eax
cmpl $0x9d, %eax
jne 0x69bf2
leaq -0x29(%rbp), %rax
movq 0x1bbac2(%rip), %rcx    # 0x225650
movw (%rcx), %cx
movw (%rax), %ax
subw %cx, %ax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x69bf0
leaq -0x29(%rbp), %rdi
addq $0x2, %rdi
movq 0x1bbaa7(%rip), %rsi    # 0x225658
movl $0x3, %edx
callq 0x19170
cmpl $0x0, %eax
jne 0x69bee
movq -0x10(%rbp), %rax
movl $0x1, 0x4(%rax)
movq -0x10(%rbp), %rax
movl $0x2, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x10(%rbp), %rax
movq $0x2, 0x10(%rax)
jmp 0x69e85
jmp 0x69bf0
jmp 0x69bf2
cmpb $0x0, -0x11(%rbp)
je 0x69bfd
jmp 0x69e85
movzbl -0x29(%rbp), %eax
cmpl $0x4d, %eax
jne 0x69c0f
movzbl -0x28(%rbp), %eax
cmpl $0x5a, %eax
je 0x69c29
movzbl -0x29(%rbp), %eax
cmpl $0x5a, %eax
jne 0x69e83
movzbl -0x28(%rbp), %eax
cmpl $0x4d, %eax
jne 0x69e83
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x8, %esi
callq 0x165280
shlq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
subq $0x20, %rsi
addq $0x4d, %rsi
movq 0x1bba0a(%rip), %rdx    # 0x225670
movl $0x8, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x69c82
movq $0x2d, -0x40(%rbp)
jmp 0x69d22
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
subq $0x20, %rsi
addq $0x48, %rsi
movq 0x1bb9d3(%rip), %rdx    # 0x225670
movl $0x8, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x69cb6
movq $0x28, -0x40(%rbp)
jmp 0x69d20
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
subq $0x20, %rsi
addq $0x34, %rsi
movq 0x1bb99f(%rip), %rdx    # 0x225670
movl $0x8, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x69cea
movq $0x14, -0x40(%rbp)
jmp 0x69d1e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
subq $0x20, %rsi
addq $0x37, %rsi
movq 0x1bb96b(%rip), %rdx    # 0x225670
movl $0x8, %ecx
callq 0x166ab0
cmpl $0x0, %eax
jne 0x69d1c
movq $0x17, -0x40(%rbp)
jmp 0x69d1e
jmp 0x69d20
jmp 0x69d22
cmpq $0x0, -0x40(%rbp)
jne 0x69d2e
jmp 0x69e85
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
addq -0x40(%rbp), %rsi
addq $0x1a, %rsi
callq 0x165060
movb %al, -0x41(%rbp)
movzbl -0x41(%rbp), %eax
cmpl $0x95, %eax
jne 0x69d5d
movq -0x10(%rbp), %rax
movb $0x1, 0xc(%rax)
cmpq $0x2d, -0x40(%rbp)
jne 0x69dab
movq -0x10(%rbp), %rax
movl $0x3, 0x4(%rax)
movq -0x10(%rbp), %rax
movl $0xa, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x38(%rbp), %rcx
subq $0x20, %rcx
addq $0x6c, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x6ab50
jmp 0x69e85
cmpq $0x28, -0x40(%rbp)
jne 0x69df9
movq -0x10(%rbp), %rax
movl $0x3, 0x4(%rax)
movq -0x10(%rbp), %rax
movl $0x9, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x38(%rbp), %rcx
subq $0x20, %rcx
addq $0x6b, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x6ab50
jmp 0x69e85
cmpq $0x14, -0x40(%rbp)
jne 0x69e33
movq -0x10(%rbp), %rax
movl $0x3, 0x4(%rax)
movq -0x10(%rbp), %rax
movl $0x8, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x38(%rbp), %rcx
subq $0x20, %rcx
addq $0x57, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x69e85
cmpq $0x17, -0x40(%rbp)
jne 0x69e81
movq -0x10(%rbp), %rax
movl $0x3, 0x4(%rax)
movq -0x10(%rbp), %rax
movl $0x7, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x9(%rax)
movq -0x10(%rbp), %rax
movq $0x12, 0x18(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0xa(%rax)
movq -0x38(%rbp), %rcx
subq $0x20, %rcx
addq $0x5a, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x69e85
jmp 0x69e83
jmp 0x69e85
movq -0x10(%rbp), %rax
cmpb $0x0, 0x8(%rax)
je 0x69ec7
movq -0x10(%rbp), %rax
movb $0x1, 0x9(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0xa(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x6, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0xb, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)