// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xe55ee(%rip), %rsi     # 0x1badc1
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
leaq 0xd9a73(%rip), %rsi     # 0x1af266
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq 0x18(%rax), %rdi
addq $0x1c, %rdi
movq -0x10(%rbp), %rax
movslq 0xc(%rax), %rsi
callq 0x15ce90
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl $0x5a4d, %esi           # imm = 0x5A4D
callq 0x168a10
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x88(%rcx), %rcx
addq 0x18(%rcx), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
movl $0x200, %ecx            # imm = 0x200
cqto
idivq %rcx
movq %rdx, %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rax
addq $0x1ff, %rax            # imm = 0x1FF
movl $0x200, %ecx            # imm = 0x200
cqto
idivq %rcx
movq %rax, %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq 0x18(%rax), %rax
movl $0x4, %ecx
cqto
idivq %rcx
movq %rax, %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rax
movl $0x10, %ecx
cqto
idivq %rcx
movq %rax, %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x24(%rax)
je 0xd593e
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
addl $0xf, %eax
shrl $0x4, %eax
addl %eax, %ecx
addl $0x9, %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0xffff, 0x24(%rax)     # imm = 0xFFFF
je 0xd593c
movq -0x10(%rbp), %rax
movl 0x20(%rax), %ecx
subl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movl %eax, -0x30(%rbp)
jmp 0xd593e
movq -0x18(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x168a10
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %eax
movl %eax, %esi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
callq 0x168a90
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x168a90
movq -0x18(%rbp), %rdi
movl $0x1c, %esi
callq 0x168a10
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168a10
movq -0x18(%rbp), %rdi
movl $0x1c, %esi
callq 0x168900
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x168900
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
cmpq $0x0, 0x50(%rax)
jle 0xd5a91
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x48(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x50(%rax), %rcx
leaq 0xd3dd7(%rip), %rsi     # 0x1a9838
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x48(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x50(%rax), %rdx
movq -0x18(%rbp), %rcx
callq 0x166070
movq -0x18(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0xd5abb
movq -0x8(%rbp), %rdi
callq 0x15bbe0
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)