// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pklite.c
// Function: read_orig_header
static int read_orig_header(deark *c, lctx *d)
{
	i64 orig_hdr_len;
	i64 orig_reloc_pos;
	i64 n1, n2;
	i64 dcmpr_bytes_expected;
	i64 orig_hdr_pos;
	const char *name;
	enum ohdisp_enum {
		OHDISP_MISSING_E, OHDISP_MISSING, OHDISP_PRESENT, OHDISP_BAD
	} ohdisp;

	if(d->dparams.extra_cmpr) {
		ohdisp = OHDISP_MISSING_E;
		goto done;
	}
	orig_hdr_pos = d->host_ei->reloc_table_pos + 4*d->host_ei->num_relocs;

	orig_hdr_len = d->host_ei->start_of_dos_code - orig_hdr_pos; // tentative
	if(orig_hdr_len < 26) {
		ohdisp = OHDISP_MISSING;
		goto done;
	}

	// Peek at the reloc table offs field to figure out how much to read
	orig_reloc_pos = de_getu16le(orig_hdr_pos + 22);
	if(orig_reloc_pos>=28 && orig_reloc_pos<2+orig_hdr_len) {
		orig_hdr_len = orig_reloc_pos-2;
	}

	de_dbg(c, "orig. hdr: at %"I64_FMT", len=(2+)%"I64_FMT, orig_hdr_pos, orig_hdr_len);

	n1 = de_getu16le(orig_hdr_pos); // len of final block
	n2 = de_getu16le(orig_hdr_pos+2); // numBlocks
	if(n1>511 || n2==0) {
		ohdisp = OHDISP_BAD;
		goto done;
	}

	dbuf_copy(c->infile, orig_hdr_pos, orig_hdr_len, d->hdr_for_dcmpr_file);

	fmtutil_collect_exe_info(c, d->hdr_for_dcmpr_file, d->guest_ei);
	if(d->guest_ei->reloc_table_pos<28) {
		d->guest_ei->reloc_table_pos = 28;
	}

	if((d->guest_ei->regSS != d->footer.regSS) ||
		(d->guest_ei->regSP != d->footer.regSP) ||
		(d->guest_ei->regCS != d->footer.regCS) ||
		(d->guest_ei->regIP != d->footer.regIP))
	{
		ohdisp = OHDISP_BAD;
		goto done;
	}

	if(d->guest_ei->num_relocs != (d->guest_reloc_table->len / 4)) {
		ohdisp = OHDISP_BAD;
		goto done;
	}

	dcmpr_bytes_expected = d->guest_ei->end_of_dos_code - d->guest_ei->start_of_dos_code;

	if(d->dcmpr_code->len != dcmpr_bytes_expected) {
		const char *note2;

		if(d->dcmpr_code->len < dcmpr_bytes_expected) {
			// If the original file's reported file size is larger than its
			// actual size, PKLITE can still correctly compress it (with a
			// misleading warning that it "may contain overlays"), and can
			// decompress it as well. There is unfortunately no way for us
			// to distinguish this not-really-an-error situation from some
			// sort of decompression failure that really ought to be
			// reported.
			note2 = ". (This could mean the original file was slightly "
				"malformed, before PKLITE compressed it.)";
		}
		else {
			note2 = "";
		}

		de_warn(c, "Expected %"I64_FMT" decompressed bytes, got %"I64_FMT"%s",
			dcmpr_bytes_expected, d->dcmpr_code->len, note2);
	}

	ohdisp = OHDISP_PRESENT;

done:
	switch(ohdisp) {
	case OHDISP_PRESENT: name="present"; break;
	case OHDISP_MISSING_E: name="n/a"; break;
	case OHDISP_MISSING: name="missing"; break;
	default: name="bad"; break;
	}
	de_dbg(c, "copy of orig hdr: %s", name);
	if(ohdisp==OHDISP_BAD) {
		de_warn(c, "Original header seems bad. Ignoring it.");
	}
	return (ohdisp==OHDISP_PRESENT);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x18(%rax)
je 0x116906
movl $0x0, -0x4c(%rbp)
jmp 0x116b8c
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq 0x8(%rcx), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x38(%rax), %rax
subq -0x40(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x1a, -0x18(%rbp)
jge 0x116950
movl $0x1, -0x4c(%rbp)
jmp 0x116b8c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
addq $0x16, %rsi
callq 0x165280
movq %rax, -0x20(%rbp)
cmpq $0x1c, -0x20(%rbp)
jl 0x11698d
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
addq $0x2, %rcx
cmpq %rcx, %rax
jge 0x11698d
movq -0x20(%rbp), %rax
subq $0x2, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rcx
leaq 0xadb95(%rip), %rsi     # 0x1c4535
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x165280
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
addq $0x2, %rsi
callq 0x165280
movq %rax, -0x30(%rbp)
cmpq $0x1ff, -0x28(%rbp)     # imm = 0x1FF
jg 0x1169e6
cmpq $0x0, -0x30(%rbp)
jne 0x1169f2
movl $0x3, -0x4c(%rbp)
jmp 0x116b8c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rcx
callq 0x166070
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x188930
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
cmpq $0x1c, 0x30(%rax)
jge 0x116a4d
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq $0x1c, 0x30(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0xa8(%rcx), %rax
jne 0x116ab1
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0xb0(%rcx), %rax
jne 0x116ab1
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0xb8(%rcx), %rax
jne 0x116ab1
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0xc0(%rcx), %rax
je 0x116abd
movl $0x3, -0x4c(%rbp)
jmp 0x116b8c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rax
movq 0x18(%rax), %rax
movl $0x4, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
je 0x116afe
movl $0x3, -0x4c(%rbp)
jmp 0x116b8c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x48(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
subq 0x38(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x18(%rax), %rax
cmpq -0x38(%rbp), %rax
je 0x116b85
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x18(%rax), %rax
cmpq -0x38(%rbp), %rax
jge 0x116b51
leaq 0xada09(%rip), %rax     # 0x1c4554
movq %rax, -0x58(%rbp)
jmp 0x116b5c
leaq 0xba078(%rip), %rax     # 0x1d0bd0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x58(%rbp), %r8
leaq 0xada31(%rip), %rsi     # 0x1c45af
movb $0x0, %al
callq 0x15bc10
movl $0x2, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x64(%rbp)
testl %eax, %eax
je 0x116bb9
jmp 0x116b98
movl -0x64(%rbp), %eax
subl $0x1, %eax
je 0x116bc6
jmp 0x116ba2
movl -0x64(%rbp), %eax
subl $0x2, %eax
jne 0x116bd3
jmp 0x116bac
leaq 0x92e31(%rip), %rax     # 0x1a99e4
movq %rax, -0x48(%rbp)
jmp 0x116bde
leaq 0x8ed8f(%rip), %rax     # 0x1a594f
movq %rax, -0x48(%rbp)
jmp 0x116bde
leaq 0xada0d(%rip), %rax     # 0x1c45da
movq %rax, -0x48(%rbp)
jmp 0x116bde
leaq 0xa5609(%rip), %rax     # 0x1bc1e3
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0xad9f5(%rip), %rsi     # 0x1c45e2
movb $0x0, %al
callq 0x15b0d0
cmpl $0x3, -0x4c(%rbp)
jne 0x116c0c
movq -0x8(%rbp), %rdi
leaq 0xad9f2(%rip), %rsi     # 0x1c45f7
movb $0x0, %al
callq 0x15bc10
cmpl $0x2, -0x4c(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
addq $0x70, %rsp
popq %rbp
retq
nop