// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/diet.c
// Function: write_exe_file
static void write_exe_file(deark *c, lctx *d)
{
	struct exe_dcmpr_ctx *ectx = NULL;
	dbuf *outf = NULL;
	dbuf *hdr_for_dcmpr_file = NULL;
	dbuf *guest_reloc_table = NULL;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	if(!d->dcmpr_code) goto done;

	ectx = de_malloc(c, sizeof(struct exe_dcmpr_ctx));

	de_dbg(c, "[writing EXE]");
	de_dbg_indent(c, 1);
	hdr_for_dcmpr_file = dbuf_create_membuf(c, 28, 0);
	acquire_new_exe_header(c, d, ectx, hdr_for_dcmpr_file);
	if(d->errflag) goto done;

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

	// 28-byte MZ header
	dbuf_copy(hdr_for_dcmpr_file, 0, 28, outf);

	// Copy the custom data up to the relocation table.
	// (If there's no relocation table, this will be everything up to the
	// code image).
	dbuf_copy(d->dcmpr_code, ectx->mz_pos+28, ectx->cdata1_size, outf);

	if(ectx->guest_ei.num_relocs!=0) {
		// Relocation table
		guest_reloc_table = dbuf_create_membuf(c, 4*ectx->guest_ei.num_relocs, 0);
		decode_reloc_tbl(c, d, ectx, d->dcmpr_code, ectx->encoded_reloc_tbl_pos,
			ectx->guest_ei.num_relocs, guest_reloc_table);
		dbuf_copy(guest_reloc_table, 0, 4*ectx->guest_ei.num_relocs, outf);

		// Custom data following the relocation table
		dbuf_copy(d->dcmpr_code,
			ectx->encoded_reloc_tbl_pos + ectx->encoded_reloc_tbl_size,
			ectx->cdata2_size, outf);
	}

	// Code image and (internal, compressed) overlay
	dbuf_copy(d->dcmpr_code, 0, ectx->mz_pos, outf);

	// Copy external overlay. Pristine DIET-compressed files never have such a
	// thing, but some other workflows (e.g. ARJ v2.00 SFX) create such files.
	if(d->host_ei->overlay_len>0) {
		if(ectx->guest_ei.overlay_len>0) {
			de_warn(c, "Ignoring overlay at %"I64_FMT" -- file already "
				"has an overlay", d->host_ei->end_of_dos_code);
		}
		else {
			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);
		}
	}

done:
	dbuf_close(guest_reloc_table);
	if(outf) {
		dbuf_close(outf);
		de_stdwarn_execomp(c);
	}
	dbuf_close(hdr_for_dcmpr_file);
	if(ectx) {
		de_free(c, ectx);
	}
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x15b580
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
jne 0x6a8ed
jmp 0x6aafd
movq -0x8(%rbp), %rdi
movl $0x118, %esi            # imm = 0x118
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x13f975(%rip), %rsi    # 0x1aa27f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x1c, %esi
xorl %edx, %edx
callq 0x168020
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x6ae10
movq -0x10(%rbp), %rax
cmpb $0x0, 0x28(%rax)
je 0x6a957
jmp 0x6aafd
movq -0x8(%rbp), %rdi
leaq 0x144904(%rip), %rsi    # 0x1af266
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x20(%rbp)
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
movl $0x1c, %edx
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
addq $0x1c, %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x20(%rbp), %rcx
callq 0x166070
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x6aa4c
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rsi
shlq $0x2, %rsi
xorl %edx, %edx
callq 0x168020
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %r8
movq -0x18(%rbp), %rax
movq 0x30(%rax), %r9
movq -0x30(%rbp), %rax
movq %rax, (%rsp)
callq 0x6b130
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdx
shlq $0x2, %rdx
movq -0x20(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
addq 0x10(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x20(%rbp), %rcx
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movq -0x20(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, 0x50(%rax)
jle 0x6aafb
movq -0x18(%rbp), %rax
cmpq $0x0, 0x78(%rax)
jle 0x6aaa6
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x48(%rax), %rdx
leaq 0x13f7f0(%rip), %rsi    # 0x1aa28d
movb $0x0, %al
callq 0x15bc10
jmp 0x6aaf9
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x48(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x50(%rax), %rcx
leaq 0x13ed6f(%rip), %rsi    # 0x1a9838
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x48(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x50(%rax), %rdx
movq -0x20(%rbp), %rcx
callq 0x166070
jmp 0x6aafb
jmp 0x6aafd
movq -0x30(%rbp), %rdi
callq 0x1676a0
cmpq $0x0, -0x20(%rbp)
je 0x6ab1f
movq -0x20(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
callq 0x15bbe0
movq -0x28(%rbp), %rdi
callq 0x1676a0
cmpq $0x0, -0x18(%rbp)
je 0x6ab3c
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x15b5a0
addq $0x40, %rsp
popq %rbp
retq
nop