// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x19140
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0x647d0
movq -0x8(%rbp), %rdi
leaq 0x14ada2(%rip), %rsi    # 0x1af266
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xa, %esi
callq 0x165280
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xc, %esi
callq 0x165280
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x48(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
subq 0x38(%rcx), %rax
movq -0x20(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movl $0x10, %esi
callq 0x15ce90
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x40(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x64574
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
subq 0x18(%rax), %rdi
movl $0x10, %esi
callq 0x15ce90
movl $0x10, %ecx
cqto
idivq %rcx
movq %rax, -0x30(%rbp)
jmp 0x6457c
movq $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movl $0x5a4d, %esi           # imm = 0x5A4D
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rax
movl $0x200, %ecx            # imm = 0x200
cqto
idivq %rcx
movq %rdx, %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x40(%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 0x38(%rax), %rax
movq 0x18(%rax), %rax
movl $0x4, %ecx
cqto
idivq %rcx
movq %rax, %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x58(%rbp), %rax
movl $0x10, %ecx
cqto
idivq %rcx
movq %rax, %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x168a90
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%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 0x60(%rax), %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rsi
callq 0x168a90
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x168a10
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168a10
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x50(%rbp), %rdx
subq $0x1c, %rdx
movq -0x18(%rbp), %rcx
movl $0x1c, %esi
callq 0x166070
movq -0x18(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x168900
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
cmpq $0x0, -0x60(%rbp)
jle 0x646f1
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x166070
movq -0x18(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x168900
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x50(%rax)
jle 0x64781
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x48(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x50(%rax), %rcx
leaq 0x1450e7(%rip), %rsi    # 0x1a9838
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x48(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x50(%rax), %rdx
movq -0x18(%rbp), %rcx
callq 0x166070
movq -0x18(%rbp), %rdi
callq 0x1676a0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
cmpb $0x0, 0x60(%rax)
je 0x647aa
movq -0x8(%rbp), %rdi
leaq 0x1450b2(%rip), %rsi    # 0x1a9855
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x647bc
movq -0x8(%rbp), %rdi
callq 0x15bbe0
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)