// File: /workspace/github_c_makefile_star2_O3/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), %r13
movq 0x10(%rsi), %rcx
movq 0x38(%rsi), %rax
movq 0x18(%rax), %rax
movq 0x30(%rcx), %rdx
testq %rax, %rax
je 0x48273
movl $0x1c, %r15d
cmpq $0x1c, %rdx
jl 0x48282
movq 0x38(%rcx), %rcx
cmpq %rcx, %rdx
jg 0x48282
jmp 0x48290
movq 0x38(%rcx), %rcx
cmpq %rcx, %rdx
jle 0x48290
movl $0x1c, %r15d
xorl %r12d, %r12d
movl $0x1c, %eax
movq %rax, (%rsp)
jmp 0x48308
cmpq $0x1d, %rdx
movl $0x1c, %r15d
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 %r12d, %r12d
testq %rcx, %rcx
cmovgq %rcx, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xf1856
movq 0x10(%r14), %rcx
movq 0x38(%rcx), %rcx
cmpq $0x201, %rcx            # imm = 0x201
jl 0x482f0
testl %eax, %eax
jne 0x482f0
movl $0x10, %r13d
jmp 0x48308
xorl %edx, %edx
testl %eax, %eax
cmovneq %rdx, %r12
cmpq $0x201, %rcx            # imm = 0x201
movl $0x10, %eax
cmovgeq %rax, %r13
movq 0x38(%r14), %rax
movq %r15, 0x18(%rsp)
movq %r12, 0x10(%rsp)
leaq (%r12,%r15), %rdi
addq 0x18(%rax), %rdi
movq %r13, %rsi
callq 0xe9cb4
movq %rax, %r13
movq 0x40(%r14), %rax
movq 0x18(%rax), %r15
addq %r13, %r15
leaq 0xdb5fa(%rip), %rsi     # 0x123935
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %rax, %rbp
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xeebc8
movq %rbx, %rcx
movq %rax, %rbx
movq %rcx, 0x20(%rsp)
movq 0x18(%rcx), %rdi
movl $0xc, %esi
callq 0xeebc8
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 0xe9cb4
movq 0x40(%r14), %rcx
subq 0x18(%rcx), %rax
jl 0x483bc
movl $0x10, %esi
movq %rax, %rdi
callq 0xe9cb4
leaq 0xf(%rax), %r12
testq %rax, %rax
cmovnsq %rax, %r12
sarq $0x4, %r12
movl $0x5a4d, %esi           # imm = 0x5A4D
movq %rbp, %rdi
callq 0xf0f95
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 0xf0f95
addq $0x3fe, %r15            # imm = 0x3FE
testq %rbx, %rbx
cmovnsq %rbx, %r15
sarq $0x9, %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf0f95
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 0xf0f95
leaq 0xf(%r13), %rsi
testq %r13, %r13
cmovnsq %r13, %rsi
sarq $0x4, %rsi
movq %rbp, %rdi
callq 0xf0f95
movq %rbp, %rdi
movq %r12, %rsi
callq 0xf0f95
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0xf0f95
movq 0x48(%r14), %rsi
movq %rbp, %rdi
callq 0xf0fc9
movq 0x50(%r14), %rsi
movq %rbp, %rdi
callq 0xf0f95
movq %rbp, %rdi
xorl %esi, %esi
callq 0xf0f95
movq 0x60(%r14), %rsi
movq %rbp, %rdi
callq 0xf0f95
movq 0x58(%r14), %rsi
movq %rbp, %rdi
callq 0xf0fc9
movq %rbp, %rdi
movq (%rsp), %rsi
callq 0xf0f95
movq %rbp, %rdi
xorl %esi, %esi
callq 0xf0f95
movq 0x20(%rsp), %rbx
movq 0x18(%rbx), %rdi
movq 0x18(%rsp), %r15
leaq -0x1c(%r15), %rdx
movl $0x1c, %esi
movq %rbp, %rcx
callq 0xef481
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf0f27
movq 0x38(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %rbp, %rcx
callq 0xef481
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
je 0x484fe
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movq %rbp, %rcx
callq 0xef481
movq %rbp, %rdi
movq %r13, %rsi
callq 0xf0f27
movq 0x40(%r14), %rdi
movq 0x18(%rdi), %rdx
xorl %esi, %esi
movq %rbp, %rcx
callq 0xef481
movq 0x10(%r14), %rax
movq 0x50(%rax), %rcx
testq %rcx, %rcx
jle 0x48555
movq 0x48(%rax), %rdx
leaq 0xd5754(%rip), %rsi     # 0x11dc87
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq 0x10(%r14), %rax
movq 0x48(%rax), %rsi
movq 0x50(%rax), %rdx
movq %rbp, %rcx
callq 0xef481
movq %rbp, %rdi
callq 0xf052b
movq 0x10(%r14), %rax
cmpb $0x0, 0x60(%rax)
je 0x48578
leaq 0xd5736(%rip), %rsi     # 0x11dca4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
cmpl $0x0, (%r14)
je 0x4858d
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 0xe8f9c
nop