// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pklite.c
// Function: do_write_dcmpr
static void do_write_dcmpr(deark *c, lctx *d)
{
	dbuf *outf = NULL;
	i64 amt_to_copy;

	if(d->errflag || !d->guest_ei || !d->hdr_for_dcmpr_file || !d->dcmpr_code ||
		!d->guest_reloc_table)
	{
		return;
	}
	de_dbg(c, "generating decompressed EXE file");
	de_dbg_indent(c, 1);

	outf = dbuf_create_output_file(c, "exe", NULL, 0);
	d->wrote_exe = 1;

	// Write the original header, up to the relocation table
	amt_to_copy = de_min_int(d->hdr_for_dcmpr_file->len, d->guest_ei->reloc_table_pos);
	dbuf_copy(d->hdr_for_dcmpr_file, 0, amt_to_copy, outf);
	dbuf_truncate(outf, d->guest_ei->reloc_table_pos);

	// Write the relocation table
	dbuf_copy(d->guest_reloc_table, 0, d->guest_reloc_table->len, outf);

	// Pad up to the start of DOS code.
	// (Note that PKLITE does not record data between the end of the relocation
	// table, and the start of DOS code, so we can't reconstruct that.)
	dbuf_truncate(outf, d->guest_ei->start_of_dos_code);

	// Write the decompressed program code
	dbuf_copy(d->dcmpr_code, 0, d->dcmpr_code->len, outf);

	// "Overlay" segment
	if(d->host_ei->overlay_len>0) {
		if(outf->len == d->guest_ei->end_of_dos_code) {
			dbuf_copy(c->infile, d->host_ei->end_of_dos_code, d->host_ei->overlay_len, outf);
		}
		else {
			// We don't want to write the overlay to the wrong offset, but it's
			// not clear what to do here. This should not happen with pristine
			// files, but could happen if an overlay was added later.
			de_warn(c, "Overlay not copied to new file, due to inconsistent file "
				"structure");
		}
	}

	dbuf_close(outf);
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x114369
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x114369
movq -0x10(%rbp), %rax
cmpq $0x0, 0xc8(%rax)
je 0x114369
movq -0x10(%rbp), %rax
cmpq $0x0, 0xd8(%rax)
je 0x114369
movq -0x10(%rbp), %rax
cmpq $0x0, 0xd0(%rax)
jne 0x11436e
jmp 0x1144e3
movq -0x8(%rbp), %rdi
leaq 0xb02a6(%rip), %rsi     # 0x1c461f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
leaq 0x9aecd(%rip), %rsi     # 0x1af266
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x30(%rax), %rsi
callq 0x15ccf0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x30(%rax), %rsi
callq 0x168900
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x38(%rax), %rsi
callq 0x168900
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
cmpq $0x0, 0x50(%rax)
jle 0x1144cc
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
cmpq 0x48(%rcx), %rax
jne 0x1144b8
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x48(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x50(%rax), %rdx
movq -0x18(%rbp), %rcx
callq 0x166070
jmp 0x1144ca
movq -0x8(%rbp), %rdi
leaq 0xb017d(%rip), %rsi     # 0x1c4640
movb $0x0, %al
callq 0x15bc10
jmp 0x1144cc
movq -0x18(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)