// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/diet.c
// Function: acquire_new_exe_header
static void acquire_new_exe_header(deark *c, lctx *d, struct exe_dcmpr_ctx *ectx,
	dbuf *newhdr)
{
	i64 iparam1;
	i64 n;
	i64 ioffset1;
	i64 mz_pos_approx;
	u8 byte3;

	if(d->errflag) return;

	switch(d->idd.fmt) {
	case FMT_EXE_100:
	case FMT_EXE_102:
		ioffset1 = 53;
		break;
	case FMT_EXE_144:
		ioffset1 = 73;
		break;
	case FMT_EXE_145F:
		ioffset1 = 26;
		break;
	default:
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	iparam1 = de_getu16le(d->host_ei->entry_point + ioffset1);
	mz_pos_approx = iparam1 * 16;
	de_dbg(c, "approx MZ pos in intermed. data: %"I64_FMT, mz_pos_approx);

	if(d->idd.fmt==FMT_EXE_100) {
		find_v100_mz_pos(c, d, ectx, mz_pos_approx);
		if(d->errflag) goto done;
	}
	else {
		if(!d->orig_len_known || !(d->hdr_flags1 & 0x20)) {
			d->errflag = 1;
			d->need_errmsg = 1;
			goto done;
		}

		ectx->mz_pos = mz_pos_approx + (d->orig_len % 16);
		de_dbg(c, "expected MZ pos in intermed. data: %"I64_FMT, ectx->mz_pos);
		// Verify that this seems to be the right place.
		n = dbuf_getu16be(d->dcmpr_code, ectx->mz_pos);
		if(n!=0x4d5a && n!=0x5a4d) {
			d->errflag = 1;
			d->need_errmsg = 1;
			goto done;
		}
	}

	// Note: DIET elides trailing 0-valued bytes from the intermediate format
	// we store in dcmpr_code.
	// In some cases, dcmpr_code ends even before the end of the 28-byte
	// MZ header.
	// So, this and later calls to dbuf_copy() may read beyond the end of
	// dcmpr_code. That's by design -- we rely on dbuf_copy to replace
	// missing bytes with 0-valued bytes.
	dbuf_copy(d->dcmpr_code, ectx->mz_pos, 28, newhdr);

	byte3 = dbuf_getbyte(newhdr, 3);
	dbuf_writebyte_at(newhdr, 3, (byte3 & 0x01));

	fmtutil_collect_exe_info(c, newhdr, &ectx->guest_ei);

	// collect_exe_info() will not have calculated the overlay len, because we
	// didn't tell it the correct file size. So, patch it up here.
	ectx->guest_ei.overlay_len = ectx->guest_ei.start_of_dos_code + ectx->mz_pos -
		ectx->guest_ei.end_of_dos_code;
	if(ectx->guest_ei.overlay_len<0) ectx->guest_ei.overlay_len = 0;

	ectx->encoded_reloc_tbl_pos = ectx->mz_pos + ectx->guest_ei.reloc_table_pos;

	if(ectx->guest_ei.num_relocs==0) {
		ectx->cdata1_size = ectx->guest_ei.start_of_dos_code - 28;
		ectx->cdata2_size = 0;
	}
	else {
		ectx->cdata1_size = ectx->guest_ei.reloc_table_pos - 28;
		ectx->cdata2_size = ectx->guest_ei.start_of_dos_code - (ectx->guest_ei.reloc_table_pos +
			4*ectx->guest_ei.num_relocs);
	}
	if(ectx->cdata1_size<0 || ectx->cdata2_size<0) {
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

done:
	if(d->errflag && d->need_errmsg) {
		de_err(c, "Unsupported variety of DIET-EXE file");
		d->need_errmsg = 0;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
je 0x6ae37
jmp 0x6b122
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x48(%rbp)
addl $-0x7, %eax
subl $0x2, %eax
jb 0x6ae5e
jmp 0x6ae4a
movl -0x48(%rbp), %eax
subl $0x9, %eax
je 0x6ae68
jmp 0x6ae54
movl -0x48(%rbp), %eax
subl $0xa, %eax
je 0x6ae72
jmp 0x6ae7c
movq $0x35, -0x38(%rbp)
jmp 0x6ae91
movq $0x49, -0x38(%rbp)
jmp 0x6ae91
movq $0x1a, -0x38(%rbp)
jmp 0x6ae91
movq -0x10(%rbp), %rax
movb $0x1, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x29(%rax)
jmp 0x6b0ee
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x40(%rax), %rsi
addq -0x38(%rbp), %rsi
callq 0x165280
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
shlq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
leaq 0x13f3f7(%rip), %rsi    # 0x1aa2c4
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpl $0x7, (%rax)
jne 0x6af06
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x40(%rbp), %rcx
callq 0x6b250
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
je 0x6af01
jmp 0x6b0ee
jmp 0x6afb1
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2b(%rax)
je 0x6af20
movq -0x10(%rbp), %rax
movzbl 0x2d(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0x6af35
movq -0x10(%rbp), %rax
movb $0x1, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x29(%rax)
jmp 0x6b0ee
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl $0x10, %esi
cqto
idivq %rsi
addq %rdx, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
leaq 0x13f382(%rip), %rsi    # 0x1aa2e9
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
callq 0x1651d0
movq %rax, -0x30(%rbp)
cmpq $0x4d5a, -0x30(%rbp)    # imm = 0x4D5A
je 0x6afaf
cmpq $0x5a4d, -0x30(%rbp)    # imm = 0x5A4D
je 0x6afaf
movq -0x10(%rbp), %rax
movb $0x1, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x29(%rax)
jmp 0x6b0ee
jmp 0x6afb1
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rcx
movl $0x1c, %edx
callq 0x166070
movq -0x20(%rbp), %rdi
movl $0x3, %esi
callq 0x165060
movb %al, -0x41(%rbp)
movq -0x20(%rbp), %rdi
movzbl -0x41(%rbp), %eax
andl $0x1, %eax
movl $0x3, %esi
movzbl %al, %edx
callq 0x168790
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
addq $0x28, %rdx
callq 0x188930
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq -0x18(%rbp), %rax
subq 0x70(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x78(%rax)
jge 0x6b042
movq -0x18(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
addq 0x58(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x6b086
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
subq $0x1c, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x20(%rax)
jmp 0x6b0c4
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rcx
subq $0x1c, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x18(%rbp), %rdx
movq 0x30(%rdx), %rdx
shlq $0x2, %rdx
addq %rdx, %rax
subq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jl 0x6b0da
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jge 0x6b0ec
movq -0x10(%rbp), %rax
movb $0x1, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x29(%rax)
jmp 0x6b0ee
jmp 0x6b0ee
movq -0x10(%rbp), %rax
movzbl 0x28(%rax), %eax
cmpl $0x0, %eax
je 0x6b122
movq -0x10(%rbp), %rax
movzbl 0x29(%rax), %eax
cmpl $0x0, %eax
je 0x6b122
movq -0x8(%rbp), %rdi
leaq 0x13f1fd(%rip), %rsi    # 0x1aa310
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x0, 0x29(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)