// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/lzexe.c
// Function: do_write_dcmpr
static void do_write_dcmpr(deark *c, lctx *d)
{
	dbuf *outf = NULL;
	i64 final_file_size; // not including overlay
	i64 final_start_of_code;
	UI final_minmem, final_maxmem;

	de_dbg(c, "generating output file");
	de_dbg_indent(c, 1);

	outf = dbuf_create_output_file(c, "exe", NULL, 0);

#define FINAL_RELOC_POS 28
	final_start_of_code = de_pad_to_n(FINAL_RELOC_POS + d->guest_reloc_table->len,
		(i64)d->final_code_alignment);

	// Generate 28-byte header
	dbuf_writeu16le(outf, 0x5a4d); // 0  signature

	final_file_size = final_start_of_code + d->dcmpr_code->len;
	dbuf_writeu16le(outf, final_file_size%512); // 2  # of bytes in last page
	dbuf_writeu16le(outf, (final_file_size+511)/512); // 4  # of pages

	dbuf_writeu16le(outf, d->guest_reloc_table->len/4); // 6  # of reloc tbl entries
	dbuf_writeu16le(outf, final_start_of_code / 16); // 8  hdrsize/16

	// This logic is from unlzexe v0.7+ (A. Modra).
	final_minmem = d->host_minmem;
	final_maxmem = d->host_maxmem;
	if(d->host_maxmem!=0) {
		final_minmem -= (UI)d->ephdr.field5 + (((UI)d->ephdr.field6+15)/16) + 9;
		final_minmem &= 0xffff;
		if(d->host_maxmem != 0xffff) {
			final_maxmem -= (d->host_minmem-final_minmem);
			final_maxmem &= 0xffff;
		}
	}
	dbuf_writeu16le(outf, (i64)final_minmem); // 10  # of paragraphs required
	dbuf_writeu16le(outf, (i64)final_maxmem); // 12  # of paragraphs requested

	dbuf_writei16le(outf, d->ephdr.regSS); // 14
	dbuf_writeu16le(outf, d->ephdr.regSP); // 16
	dbuf_writeu16le(outf, 0); // 18  checksum
	dbuf_writeu16le(outf, d->ephdr.regIP); // 20
	dbuf_writei16le(outf, d->ephdr.regCS); // 22
	dbuf_writeu16le(outf, FINAL_RELOC_POS); // 24  reloc_tbl_pos
	dbuf_writeu16le(outf, 0); // 26  overlay indicator

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

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

	// Copy the overlay segment.
	// Normal LZEXE files never have such a thing, but some third-party utilities
	// construct such files.
	if(d->host_ei->overlay_len>0) {
		de_dbg(c, "overlay data at %"I64_FMT", len=%"I64_FMT, d->host_ei->end_of_dos_code,
			d->host_ei->overlay_len);
		dbuf_copy(c->infile, d->host_ei->end_of_dos_code, d->host_ei->overlay_len, outf);
	}

	dbuf_close(outf);
	de_dbg_indent(c, -1);
	if(!d->errflag) {
		de_stdwarn_execomp(c);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x9d27b(%rip), %rsi     # 0x12effa
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
leaq 0x919bb(%rip), %rsi     # 0x123755
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %r15
movq 0x80(%r14), %rax
movq 0x18(%rax), %rdi
addq $0x1c, %rdi
movslq 0xc(%r14), %rsi
callq 0xea80b
movq %rax, %r12
movl $0x5a4d, %esi           # imm = 0x5A4D
movq %r15, %rdi
callq 0xf1598
movq 0x88(%r14), %rax
movq 0x18(%rax), %rbp
movq %rbp, %rsi
addq %r12, %rsi
leaq 0x1ff(%rbp,%r12), %r13
movq %rsi, %rax
cmovsq %r13, %rax
andq $-0x200, %rax           # imm = 0xFE00
subq %rax, %rsi
movq %r15, %rdi
callq 0xf1598
leaq (%r12,%rbp), %rsi
addq $0x3fe, %rsi            # imm = 0x3FE
testq %r13, %r13
cmovnsq %r13, %rsi
sarq $0x9, %rsi
movq %r15, %rdi
callq 0xf1598
movq 0x80(%r14), %rax
movq 0x18(%rax), %rax
leaq 0x3(%rax), %rsi
testq %rax, %rax
cmovnsq %rax, %rsi
sarq $0x2, %rsi
movq %r15, %rdi
callq 0xf1598
leaq 0xf(%r12), %rsi
testq %r12, %r12
cmovnsq %r12, %rsi
sarq $0x4, %rsi
movq %r15, %rdi
callq 0xf1598
movl 0x20(%r14), %eax
movl 0x24(%r14), %ecx
testl %ecx, %ecx
je 0x91e9a
movl 0x68(%r14), %edx
addl $0xf, %edx
shrl $0x4, %edx
addl 0x60(%r14), %edx
subl %edx, %eax
addl $0xfff7, %eax           # imm = 0xFFF7
movl $0xffff, %esi           # imm = 0xFFFF
andl %esi, %eax
movl $0xffff, %r13d          # imm = 0xFFFF
cmpl %esi, %ecx
je 0x91e9d
subl %edx, %ecx
addl $0xfff7, %ecx           # imm = 0xFFF7
movzwl %cx, %r13d
jmp 0x91e9d
xorl %r13d, %r13d
movl %eax, %esi
movq %r15, %rdi
callq 0xf1598
movq %r15, %rdi
movq %r13, %rsi
callq 0xf1598
movq 0x38(%r14), %rsi
movq %r15, %rdi
callq 0xf15cc
movq 0x40(%r14), %rsi
movq %r15, %rdi
callq 0xf1598
movq %r15, %rdi
xorl %esi, %esi
callq 0xf1598
movq 0x50(%r14), %rsi
movq %r15, %rdi
callq 0xf1598
movq 0x48(%r14), %rsi
movq %r15, %rdi
callq 0xf15cc
movl $0x1c, %esi
movq %r15, %rdi
callq 0xf1598
movq %r15, %rdi
xorl %esi, %esi
callq 0xf1598
movl $0x1c, %esi
movq %r15, %rdi
callq 0xf152a
movq 0x80(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %r15, %rcx
callq 0xefabc
movq %r15, %rdi
movq %r12, %rsi
callq 0xf152a
movq 0x88(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %r15, %rcx
callq 0xefabc
movq 0x18(%r14), %rax
movq 0x50(%rax), %rcx
testq %rcx, %rcx
jle 0x91f7f
movq 0x48(%rax), %rdx
leaq 0x8bc1a(%rip), %rsi     # 0x11db77
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq 0x18(%r14), %rax
movq 0x48(%rax), %rsi
movq 0x50(%rax), %rdx
movq %r15, %rcx
callq 0xefabc
movq %r15, %rdi
callq 0xf0b13
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpl $0x0, 0x4(%r14)
je 0x91faa
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9b19