// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/diet.c
// Function: find_v100_mz_pos
static void find_v100_mz_pos(deark *c, lctx *d, struct exe_dcmpr_ctx *ectx,
	i64 mz_pos_approx)
{
	i64 cmpr_endpos;
	i64 params_pos = 0;
	i64 reloc_tbl_rel = 0;
	i64 nrelocs_r = 0;
	i64 nbytes_to_search;
	int found_count = 0;
	u8 fclass = 0; // 0=unknown, 1=has params at params_pos, 2=no reloc table
	i64 i;
	i64 n;
	i64 foundpos;
	int ret;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "[searching for MZ pos in intermed. data]");
	de_dbg_indent(c, 1);

	cmpr_endpos = d->cmpr_pos + d->cmpr_len;
	de_dbg(c, "cmpr data end: %"I64_FMT, cmpr_endpos);

	// Sanity check. This part of the decompressor always seems to start with
	// these bytes.
	n = de_getu32be(cmpr_endpos);
	if(n != 0xd1edfecaU) {
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	// We're looking for some parameters in the part of the code that
	// appears after the compressed data. (But if there are 0 relocations,
	// then the params won't be present, and we have to detect that as
	// a special case.)
	// Probing at precise offsets doesn't seem to be robust enough, so we
	// resort to doing a search for characteristic byte patterns.

	nbytes_to_search = de_min_int(d->host_ei->end_of_dos_code - cmpr_endpos, 1000);

	if(fclass==0) {
		ret = dbuf_search(c->infile, (const u8*)"\x5d\x0e\x1f\xbe", 4, cmpr_endpos,
			nbytes_to_search, &foundpos);
		if(ret) {
			fclass = 1;
			params_pos = foundpos+4;
		}
	}

	if(fclass==0) {
		// The case where there are no relocations
		ret = dbuf_search(c->infile, (const u8*)"\x5d\x07\x1f\x81", 4, cmpr_endpos,
			nbytes_to_search, &foundpos);
		if(ret) {
			fclass = 2;
			nrelocs_r = 0;
		}
	}

	if(fclass==0) {
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	if(fclass==1) {
		de_dbg(c, "params pos: %"I64_FMT" (b+%"I64_FMT"; e-%"I64_FMT")",
			params_pos, params_pos - cmpr_endpos,
			d->host_ei->end_of_dos_code - params_pos);
		reloc_tbl_rel = de_getu16le(params_pos);
		de_dbg(c, "reloc tbl intermed. pos: approx_MZ+%"I64_FMT, reloc_tbl_rel);
		nrelocs_r =  de_getu16le(params_pos+3);
	}

	de_dbg(c, "nrelocs (reported): %"I64_FMT, nrelocs_r);

	// Search for the MZ header, hopefully avoiding false positives.
	for(i=0; i<16; i++) {
		int sig;

		// Look for "MZ" or "ZM"
		sig = (int)dbuf_getbyte(d->dcmpr_code, mz_pos_approx+i);
		if(sig!='M' && sig!='Z') continue;
		sig += (int)dbuf_getbyte(d->dcmpr_code, mz_pos_approx+i+1);
		if(sig != 'M'+'Z') continue;

		// Validate the reloc count
		n = dbuf_getu16le(d->dcmpr_code, mz_pos_approx+i+6);
		if(n!=nrelocs_r) continue;

		// Validate the reloc pos if possible
		if(fclass==1) {
			n = dbuf_getu16le(d->dcmpr_code, mz_pos_approx+i+24);
			if(i+n!=reloc_tbl_rel) continue;
		}

		found_count++;
		if(found_count>1) {
			d->errflag = 1;
			d->need_errmsg = 1;
			goto done;
		}
		ectx->mz_pos = mz_pos_approx+i;
		break;
	}

	if(found_count!=1) {
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	de_dbg(c, "MZ header found at %"I64_FMT, ectx->mz_pos);

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x4c(%rbp)
movb $0x0, -0x4d(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
leaq 0x13f08f(%rip), %rsi    # 0x1aa335
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x30(%rcx), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x13f080(%rip), %rsi    # 0x1aa35e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x165470
movq %rax, -0x60(%rbp)
movl $0xd1edfeca, %eax       # imm = 0xD1EDFECA
cmpq %rax, -0x60(%rbp)
je 0x6b31a
movq -0x10(%rbp), %rax
movb $0x1, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x29(%rax)
jmp 0x6b5b9
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x48(%rax), %rdi
subq -0x28(%rbp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x15ccf0
movq %rax, -0x48(%rbp)
movzbl -0x4d(%rbp), %eax
cmpl $0x0, %eax
jne 0x6b381
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rcx
movq -0x48(%rbp), %r8
leaq 0x13f019(%rip), %rsi    # 0x1aa371
movl $0x4, %edx
leaq -0x68(%rbp), %r9
callq 0x169370
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
je 0x6b37f
movb $0x1, -0x4d(%rbp)
movq -0x68(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
jmp 0x6b381
movzbl -0x4d(%rbp), %eax
cmpl $0x0, %eax
jne 0x6b3c6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rcx
movq -0x48(%rbp), %r8
leaq 0x13efd5(%rip), %rsi    # 0x1aa376
movl $0x4, %edx
leaq -0x68(%rbp), %r9
callq 0x169370
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
je 0x6b3c4
movb $0x2, -0x4d(%rbp)
movq $0x0, -0x40(%rbp)
jmp 0x6b3c6
movzbl -0x4d(%rbp), %eax
cmpl $0x0, %eax
jne 0x6b3e4
movq -0x10(%rbp), %rax
movb $0x1, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x29(%rax)
jmp 0x6b5b9
movzbl -0x4d(%rbp), %eax
cmpl $0x1, %eax
jne 0x6b45f
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x30(%rbp), %rcx
subq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x48(%rax), %r8
subq -0x30(%rbp), %r8
leaq 0x13ef67(%rip), %rsi    # 0x1aa37b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165280
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x13ef5b(%rip), %rsi    # 0x1aa39a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x3, %rsi
callq 0x165280
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
leaq 0x13ef53(%rip), %rsi    # 0x1aa3c1
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x58(%rbp)
cmpq $0x10, -0x58(%rbp)
jge 0x6b588
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x20(%rbp), %rsi
addq -0x58(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
movl %eax, -0x74(%rbp)
cmpl $0x4d, -0x74(%rbp)
je 0x6b4b4
cmpl $0x5a, -0x74(%rbp)
je 0x6b4b4
jmp 0x6b577
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x20(%rbp), %rsi
addq -0x58(%rbp), %rsi
addq $0x1, %rsi
callq 0x165060
movzbl %al, %eax
addl -0x74(%rbp), %eax
movl %eax, -0x74(%rbp)
cmpl $0xa7, -0x74(%rbp)
je 0x6b4e4
jmp 0x6b577
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x20(%rbp), %rsi
addq -0x58(%rbp), %rsi
addq $0x6, %rsi
callq 0x165280
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0x6b50d
jmp 0x6b577
movzbl -0x4d(%rbp), %eax
cmpl $0x1, %eax
jne 0x6b545
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x20(%rbp), %rsi
addq -0x58(%rbp), %rsi
addq $0x18, %rsi
callq 0x165280
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
addq -0x60(%rbp), %rax
cmpq -0x38(%rbp), %rax
je 0x6b543
jmp 0x6b577
jmp 0x6b545
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
cmpl $0x1, -0x4c(%rbp)
jle 0x6b566
movq -0x10(%rbp), %rax
movb $0x1, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x29(%rax)
jmp 0x6b5b9
movq -0x20(%rbp), %rcx
addq -0x58(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x6b588
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x6b47d
cmpl $0x1, -0x4c(%rbp)
je 0x6b5a0
movq -0x10(%rbp), %rax
movb $0x1, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x29(%rax)
jmp 0x6b5b9
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
leaq 0x13ee27(%rip), %rsi    # 0x1aa3d9
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl -0x70(%rbp), %esi
callq 0x15b5a0
addq $0x80, %rsp
popq %rbp
retq
nop