// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/exepack.c
// Function: do_write_dcmpr
static void do_write_dcmpr(deark *c, lctx *d)
{
	dbuf *outf = NULL;
	i64 ihdr_minmem, ihdr_maxmem;
	i64 final_minmem;
	i64 cmprprog_mem_tot; // total memory consumed+reserved by the exepacked program
	struct exepack_output_structure ostr;

	de_zeromem(&ostr, sizeof(struct exepack_output_structure));

	decide_output_structure(c, d, &ostr);

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

	ihdr_minmem = de_getu16le(10);
	ihdr_maxmem = de_getu16le(12);

	cmprprog_mem_tot = (d->host_ei->end_of_dos_code - d->host_ei->start_of_dos_code) + ihdr_minmem*16;
	cmprprog_mem_tot = de_pad_to_n(cmprprog_mem_tot, 16);

	// Try to set minmem so that the total memory is the same, or at least does
	// not decrease.
	if(cmprprog_mem_tot >= d->dcmpr_code->len) {
		// This could be an overestimate, for small programs.
		final_minmem = de_pad_to_n(cmprprog_mem_tot - d->dcmpr_code->len, 16)/16;
	}
	else {
		final_minmem = 0;
	}

	// Generate 28-byte header
	dbuf_writeu16le(outf, 0x5a4d); // 0  signature
	dbuf_writeu16le(outf, ostr.final_file_size%512); // 2  # of bytes in last page
	dbuf_writeu16le(outf, (ostr.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, ostr.final_start_of_code / 16); // 8  hdrsize/16
	dbuf_writeu16le(outf, final_minmem);
	dbuf_writeu16le(outf, ihdr_maxmem);
	dbuf_writei16le(outf, d->ohdr.regSS); // 14  ss
	dbuf_writeu16le(outf, d->ohdr.regSP); // 16  sp
	dbuf_writeu16le(outf, 0); // 18  checksum
	dbuf_writeu16le(outf, d->ohdr.regIP); // 20  ip
	dbuf_writei16le(outf, d->ohdr.regCS); // 22  cs
	dbuf_writeu16le(outf, ostr.final_reloc_pos_field); // 24  reloc_tbl_pos
	dbuf_writeu16le(outf, 0); // 26  overlay indicator

	// Copy extra data between header and reloc table
	dbuf_copy(c->infile, 28, ostr.final_reloc_pos-28, outf);

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

	// Write cdata2
	if(ostr.cdata2_len_nopadding>0) {
		dbuf_copy(c->infile, ostr.cdata2_pos_in_host, ostr.cdata2_len_nopadding, outf);
	}

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

	// Copy overlay data
	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);

	if(d->host_ei->is_extended) {
		// I've seen rare NE and LX files in which the DOS stub is EXEPACK-compressed.
		// TODO? It might be possible to try to make them work, but I don't think
		// it's worth the trouble.
		de_warn(c, "This might be an extended EXE format. "
			"Only the DOS part will work after decompression.");
	}

	if(!d->errflag) {
		de_stdwarn_execomp(c);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movslq 0x8(%rsi), %r12
movq 0x10(%rsi), %rcx
movq 0x38(%rsi), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
sete %sil
movq 0x30(%rcx), %rdx
cmpq $0x1c, %rdx
setge %dil
orb %sil, %dil
movl $0x1c, %r15d
cmpb $0x1, %dil
jne 0x48298
cmpq 0x38(%rcx), %rdx
jle 0x4853e
xorl %edx, %edx
movl $0x1c, %eax
movq %rax, (%rsp)
movq 0x38(%r14), %rax
movq %r15, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
leaq (%rdx,%r15), %rdi
addq 0x18(%rax), %rdi
movq %r12, %rsi
callq 0xea80b
movq %rax, %r13
movq 0x40(%r14), %rax
movq 0x18(%rax), %r15
addq %r13, %r15
leaq 0xdb47f(%rip), %rsi     # 0x123755
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %rbp
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xef1ca
movq %rbx, %rcx
movq %rax, %rbx
movq %rcx, 0x20(%rsp)
movq 0x18(%rcx), %rdi
movl $0xc, %esi
callq 0xef1ca
movq %rax, 0x8(%rsp)
movq 0x10(%r14), %rax
shlq $0x4, %rbx
addq 0x48(%rax), %rbx
subq 0x38(%rax), %rbx
movl $0x10, %esi
movq %rbx, %rdi
callq 0xea80b
movq 0x40(%r14), %rcx
subq 0x18(%rcx), %rax
jl 0x48357
movl $0x10, %esi
movq %rax, %rdi
callq 0xea80b
leaq 0xf(%rax), %r12
testq %rax, %rax
cmovnsq %rax, %r12
sarq $0x4, %r12
movl $0x5a4d, %esi           # imm = 0x5A4D
movq %rbp, %rdi
callq 0xf1598
leaq 0x1ff(%r15), %rbx
testq %r15, %r15
movq %r15, %rax
cmovsq %rbx, %rax
andq $-0x200, %rax           # imm = 0xFE00
movq %r15, %rsi
subq %rax, %rsi
movq %rbp, %rdi
callq 0xf1598
addq $0x3fe, %r15            # imm = 0x3FE
testq %rbx, %rbx
cmovnsq %rbx, %r15
sarq $0x9, %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf1598
movq 0x38(%r14), %rax
movq 0x18(%rax), %rax
leaq 0x3(%rax), %rsi
testq %rax, %rax
cmovnsq %rax, %rsi
sarq $0x2, %rsi
movq %rbp, %rdi
callq 0xf1598
leaq 0xf(%r13), %rsi
testq %r13, %r13
cmovnsq %r13, %rsi
sarq $0x4, %rsi
movq %rbp, %rdi
callq 0xf1598
movq %rbp, %rdi
movq %r12, %rsi
callq 0xf1598
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0xf1598
movq 0x48(%r14), %rsi
movq %rbp, %rdi
callq 0xf15cc
movq 0x50(%r14), %rsi
movq %rbp, %rdi
callq 0xf1598
movq %rbp, %rdi
xorl %esi, %esi
callq 0xf1598
movq 0x60(%r14), %rsi
movq %rbp, %rdi
callq 0xf1598
movq 0x58(%r14), %rsi
movq %rbp, %rdi
callq 0xf15cc
movq %rbp, %rdi
movq (%rsp), %rsi
callq 0xf1598
movq %rbp, %rdi
xorl %esi, %esi
callq 0xf1598
movq 0x20(%rsp), %rbx
movq 0x18(%rbx), %rdi
movq 0x18(%rsp), %r15
leaq -0x1c(%r15), %rdx
movl $0x1c, %esi
movq %rbp, %rcx
callq 0xefabc
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf152a
movq 0x38(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %rbp, %rcx
callq 0xefabc
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
jle 0x48499
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movq %rbp, %rcx
callq 0xefabc
movq %rbp, %rdi
movq %r13, %rsi
callq 0xf152a
movq 0x40(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %rbp, %rcx
callq 0xefabc
movq 0x10(%r14), %rax
movq 0x50(%rax), %rcx
testq %rcx, %rcx
jle 0x484f0
movq 0x48(%rax), %rdx
leaq 0xd56a9(%rip), %rsi     # 0x11db77
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq 0x10(%r14), %rax
movq 0x48(%rax), %rsi
movq 0x50(%rax), %rdx
movq %rbp, %rcx
callq 0xefabc
movq %rbp, %rdi
callq 0xf0b13
movq 0x10(%r14), %rax
cmpb $0x0, 0x60(%rax)
je 0x48513
leaq 0xd568b(%rip), %rsi     # 0x11db94
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
cmpl $0x0, (%r14)
je 0x48528
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9b19
movq 0x10(%r14), %rcx
movq 0x30(%rcx), %rdx
movq 0x38(%rcx), %rcx
cmpq $0x1d, %rdx
movq %rdx, (%rsp)
cmovgeq %rdx, %r15
leaq 0xf(%rax), %rdx
testq %rax, %rax
cmovnsq %rax, %rdx
andq $-0x10, %rdx
subq %rdx, %rax
addq %r15, %rax
subq %rax, %rcx
xorl %edx, %edx
testq %rcx, %rcx
cmovgq %rcx, %rdx
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movq %rdx, %r13
callq 0xf1efb
movq 0x10(%r14), %rcx
movq 0x38(%rcx), %rcx
cmpq $0x201, %rcx            # imm = 0x201
jl 0x485a9
testl %eax, %eax
jne 0x485a9
movl $0x10, %r12d
movq %r13, %rdx
jmp 0x482a3
xorl %edx, %edx
testl %eax, %eax
cmovneq %rdx, %r13
movq %r13, %rdx
cmpq $0x201, %rcx            # imm = 0x201
movl $0x10, %eax
cmovgeq %rax, %r12
jmp 0x482a3
nopl (%rax)