// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pklite.c
// Function: reconstruct_header
static void reconstruct_header(deark *c, lctx *d)
{
	i64 num_relocs;
	const i64 reloc_table_start = 28;
	i64 start_of_dos_code;
	i64 end_of_dos_code;
	i64 minmem; // in 16-byte units
	i64 maxmem;

	// "MZ" should already be written
	if(d->hdr_for_dcmpr_file->len!=2 || !d->footer_pos) {
		d->errflag = 1;
		return;
	}

	// By default, keep the same values as the container. These are likely to
	// be higher than the original, but it's better to be too high than too low.
	minmem = de_getu16le(10);
	maxmem = de_getu16le(12);
	if(maxmem==0) {
		// Unlikely, but could possibly happen for beta files with the
		// load-high option
		maxmem = 65535;
	}
	find_min_mem_needed(c, d, &minmem);
	// TODO: For maxmem, it may be possible to do better.
	if(maxmem<minmem) maxmem = minmem;

	num_relocs = d->guest_reloc_table->len / 4;
	start_of_dos_code = de_pad_to_n(reloc_table_start + num_relocs*4,
		(i64)d->default_code_alignment);
	end_of_dos_code = start_of_dos_code + d->dcmpr_code->len;
	dbuf_writeu16le(d->hdr_for_dcmpr_file, end_of_dos_code%512);
	dbuf_writeu16le(d->hdr_for_dcmpr_file, (end_of_dos_code+511)/512);
	dbuf_writeu16le(d->hdr_for_dcmpr_file, num_relocs);
	dbuf_writeu16le(d->hdr_for_dcmpr_file, start_of_dos_code/16);
	dbuf_writeu16le(d->hdr_for_dcmpr_file, minmem);
	dbuf_writeu16le(d->hdr_for_dcmpr_file, maxmem);
	dbuf_writei16le(d->hdr_for_dcmpr_file, d->footer.regSS);
	dbuf_writeu16le(d->hdr_for_dcmpr_file, d->footer.regSP);
	dbuf_writeu16le(d->hdr_for_dcmpr_file, 0); // checksum
	dbuf_writeu16le(d->hdr_for_dcmpr_file, d->footer.regIP);
	dbuf_writei16le(d->hdr_for_dcmpr_file, d->footer.regCS);
	dbuf_writeu16le(d->hdr_for_dcmpr_file, reloc_table_start);
	dbuf_writeu16le(d->hdr_for_dcmpr_file, 0); // overlay indicator

	fmtutil_collect_exe_info(c, d->hdr_for_dcmpr_file, d->guest_ei);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x1c, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rax
cmpq $0x2, 0x18(%rax)
jne 0x116c58
movq -0x10(%rbp), %rax
cmpq $0x0, 0xa0(%rax)
jne 0x116c64
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x116e7d
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xa, %esi
callq 0x165280
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xc, %esi
callq 0x165280
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x116c9f
movq $0xffff, -0x40(%rbp)    # imm = 0xFFFF
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x116e90
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x116cc2
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rax
movq 0x18(%rax), %rax
movl $0x4, %ecx
cqto
idivq %rcx
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
shlq $0x2, %rdi
addq $0x1c, %rdi
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rsi
callq 0x15ce90
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0xd8(%rcx), %rcx
addq 0x18(%rcx), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x30(%rbp), %rax
movl $0x200, %ecx            # imm = 0x200
cqto
idivq %rcx
movq %rdx, %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x30(%rbp), %rax
addq $0x1ff, %rax            # imm = 0x1FF
movl $0x200, %ecx            # imm = 0x200
cqto
idivq %rcx
movq %rax, %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x28(%rbp), %rax
movl $0x10, %ecx
cqto
idivq %rcx
movq %rax, %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rsi
callq 0x168a90
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xb0(%rax), %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xc0(%rax), %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rsi
callq 0x168a90
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movl $0x1c, %esi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168a10
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x188930
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)