// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r15
movq %rdi, %r12
leaq 0x48(%rsp), %rsi
callq 0xe958c
cmpq $0x0, 0x58(%r15)
je 0x4c2e9
movl $0x118, %esi            # imm = 0x118
movq %r12, %rdi
callq 0xe969b
movq %rax, %rbx
leaq 0xd2332(%rip), %rsi     # 0x11e5db
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
movq %r12, %rdi
movl $0x1, %esi
callq 0xe9585
movl $0x1c, %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r14
cmpb $0x0, 0x28(%r15)
je 0x4c343
cmpb $0x0, 0x28(%r15)
je 0x4c51f
xorl %r13d, %r13d
jmp 0x4c2f1
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %rbp, %rdi
callq 0xf0b13
testq %r13, %r13
je 0x4c310
movq %r13, %rdi
callq 0xf0b13
movq %r12, %rdi
callq 0xe9b19
movq %r14, %rdi
callq 0xf0b13
testq %rbx, %rbx
je 0x4c328
movq %r12, %rdi
movq %rbx, %rsi
callq 0xe9752
movl 0x48(%rsp), %esi
movq %r12, %rdi
callq 0xe9595
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%r15), %eax
addl $-0x7, %eax
cmpl $0x3, %eax
ja 0x4c982
movl %eax, %eax
leaq 0xd2435(%rip), %rcx     # 0x11e790
movq 0x18(%r12), %rdi
movq 0x50(%r15), %rdx
movq 0x40(%rdx), %rsi
addq (%rcx,%rax,8), %rsi
callq 0xef1ca
movq %rax, %r13
shlq $0x4, %r13
leaq 0xd22a1(%rip), %rsi     # 0x11e620
movq %r12, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9226
cmpl $0x7, (%r15)
jne 0x4c6dd
movq %r14, 0x40(%rsp)
movq %rbx, 0x18(%rsp)
leaq 0x4c(%rsp), %rsi
movq %r12, %rdi
callq 0xe958c
leaq 0xd22dd(%rip), %rsi     # 0x11e691
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
movq %r12, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x30(%r15), %rbp
addq 0x40(%r15), %rbp
leaq 0xd22e0(%rip), %rsi     # 0x11e6ba
movq %r12, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r12), %rdi
movq %rbp, %rsi
callq 0xef32a
movl $0xd1edfeca, %ecx       # imm = 0xD1EDFECA
cmpq %rcx, %rax
movq %r12, 0x38(%rsp)
jne 0x4c8b3
movq %r15, 0x10(%rsp)
movq 0x50(%r15), %rax
movq 0x48(%rax), %rdi
subq %rbp, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0xea792
movq %rax, %r14
movq 0x18(%r12), %rdi
leaq 0xd229d(%rip), %rsi     # 0x11e6cd
leaq 0x20(%rsp), %rbx
movl $0x4, %edx
movq %rbp, %rcx
movq %rax, %r8
movq %rbx, %r9
callq 0xf1a9c
movq (%rbx), %rcx
addq $0x4, %rcx
xorl %edx, %edx
xorl %r15d, %r15d
testl %eax, %eax
setne %bl
cmovneq %rcx, %rdx
jne 0x4c498
movq 0x18(%r12), %rdi
movq %rdx, 0x8(%rsp)
leaq 0xd2262(%rip), %rsi     # 0x11e6d2
leaq 0x20(%rsp), %r9
movl $0x4, %edx
movq %rbp, %rcx
movq %r14, %r8
callq 0xf1a9c
movq 0x8(%rsp), %rdx
testl %eax, %eax
movb %bl, %r15b
movl $0x2, %ebx
cmovel %r15d, %ebx
testb %bl, %bl
movq 0x10(%rsp), %r15
je 0x4c8b3
movq $0x0, 0x8(%rsp)
cmpb $0x1, %bl
movl %ebx, 0x30(%rsp)
jne 0x4c778
movq %rdx, %rcx
subq %rbp, %rcx
movq 0x50(%r15), %rax
movq 0x48(%rax), %r8
subq %rdx, %r8
movq %rdx, %rbx
leaq 0xd2201(%rip), %rsi     # 0x11e6d7
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r12), %rdi
movq %rbx, %rsi
callq 0xef1ca
leaq 0xd2202(%rip), %rsi     # 0x11e6f6
movq %r12, %rdi
movq %rax, 0x28(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r12), %rdi
addq $0x3, %rbx
movq %rbx, %rsi
callq 0xef1ca
movq %rax, %rbp
jmp 0x4c783
leaq 0xd722f(%rip), %rsi     # 0x123755
xorl %ebp, %ebp
movq %r12, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %r13
movl $0x1c, %edx
movq %r14, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xefabc
movq 0x58(%r15), %rdi
movq (%rbx), %rsi
movq 0x18(%rbx), %rdx
addq $0x1c, %rsi
movq %r13, %rcx
callq 0xefabc
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x4c696
movq %r13, 0x28(%rsp)
movq %r14, 0x40(%rsp)
shlq $0x2, %rsi
xorl %r13d, %r13d
movq %r12, 0x38(%rsp)
movq %r12, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq 0x58(%r15), %r15
movq 0x8(%rbx), %rcx
movq %rbx, 0x18(%rsp)
movq 0x30(%rbx), %rax
movq %rcx, 0x50(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, 0x30(%rsp)
testq %rax, %rax
jle 0x4c62b
leaq 0x20(%rsp), %rbp
movq 0x30(%rsp), %r14
xorl %r12d, %r12d
movq %r15, %rdi
movq %rbp, %rsi
callq 0xef1ed
movq %rax, %rbx
testw %bx, %bx
js 0x4c5ef
movq %r15, %rdi
movq %rbp, %rsi
callq 0xef1ed
movq %rax, %r12
movl %ebx, %r13d
jmp 0x4c606
movl %ebx, %eax
andl $0x7fff, %eax           # imm = 0x7FFF
cmpl $0xc000, %ebx           # imm = 0xC000
cmovael %ebx, %eax
addl %r12d, %eax
movzwl %ax, %r12d
movl %r12d, %esi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xf1598
movl %r13d, %esi
movq %rbx, %rdi
callq 0xf1598
decq %r14
leaq 0x20(%rsp), %rbp
jne 0x4c5c9
movq 0x20(%rsp), %rax
subq 0x50(%rsp), %rax
movq 0x30(%rsp), %rcx
shlq $0x2, %rcx
movq 0x10(%rsp), %r15
testb $0x20, 0x2d(%r15)
cmovneq %rax, %rcx
movq 0x18(%rsp), %rbx
movq %rcx, 0x10(%rbx)
movq 0x30(%rbx), %rdx
shlq $0x2, %rdx
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
movq 0x28(%rsp), %r13
movq %r13, %rcx
callq 0xefabc
movq 0x58(%r15), %rdi
movq 0x10(%rbx), %rsi
movq 0x20(%rbx), %rdx
addq 0x8(%rbx), %rsi
movq %r13, %rcx
callq 0xefabc
movq 0x38(%rsp), %r12
movq 0x40(%rsp), %r14
movq 0x58(%r15), %rdi
movq (%rbx), %rdx
xorl %esi, %esi
movq %r13, %rcx
callq 0xefabc
movq 0x50(%r15), %rax
movq 0x50(%rax), %rcx
testq %rcx, %rcx
jle 0x4c2f3
cmpq $0x0, 0x78(%rbx)
movq 0x48(%rax), %rdx
jle 0x4c749
leaq 0xd1f1b(%rip), %rsi     # 0x11e5e9
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0x4c2f3
cmpb $0x0, 0x2b(%r15)
je 0x4c982
testb $0x20, 0x2d(%r15)
je 0x4c982
movq 0x38(%r15), %rdx
leaq 0xf(%rdx), %rax
testq %rdx, %rdx
cmovnsq %rdx, %rax
andq $-0x10, %rax
subq %rax, %rdx
addq %r13, %rdx
movq %rdx, (%rbx)
leaq 0xd1f2f(%rip), %rsi     # 0x11e645
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x58(%r15), %rdi
movq (%rbx), %rsi
callq 0xef169
cmpq $0x5a4d, %rax           # imm = 0x5A4D
je 0x4c8e0
cmpq $0x4d5a, %rax           # imm = 0x4D5A
je 0x4c8e0
jmp 0x4c982
leaq 0xd1427(%rip), %rsi     # 0x11db77
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r12), %rdi
movq 0x50(%r15), %rax
movq 0x48(%rax), %rsi
movq 0x50(%rax), %rdx
movq %r13, %rcx
callq 0xefabc
jmp 0x4c2f3
movq $0x0, 0x28(%rsp)
xorl %ebp, %ebp
leaq 0xd1f93(%rip), %rsi     # 0x11e71d
movq %r12, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe9226
xorl %r12d, %r12d
movq 0x58(%r15), %rdi
leaq (%r12,%r13), %r14
movq %r14, %rsi
callq 0xef0d5
movzbl %al, %ebx
movl $0x5, %ecx
cmpb $0x5a, %bl
je 0x4c7c0
cmpl $0x4d, %ebx
jne 0x4c858
movq %rbp, %r15
movq 0x10(%rsp), %rbp
movq 0x58(%rbp), %rdi
leaq (%r12,%r13), %rsi
incq %rsi
callq 0xef0d5
movzbl %al, %eax
addl %ebx, %eax
cmpl $0xa7, %eax
jne 0x4c84b
movq 0x58(%rbp), %rdi
movq %r13, %rbx
leaq (%r12,%r13), %rsi
addq $0x6, %rsi
callq 0xef1ca
movq %r15, %rbp
cmpq %r15, %rax
jne 0x4c850
cmpb $0x1, 0x30(%rsp)
movq %rbx, %r13
jne 0x4c82d
movq 0x10(%rsp), %rax
movq 0x58(%rax), %rdi
leaq (%r12,%r13), %rsi
addq $0x18, %rsi
callq 0xef1ca
movq 0x28(%rsp), %rcx
subq %rax, %rcx
cmpq %r12, %rcx
jne 0x4c853
movq 0x8(%rsp), %rcx
leal 0x1(%rcx), %eax
testl %ecx, %ecx
jle 0x4c871
movq 0x10(%rsp), %rcx
movw $0x101, 0x28(%rcx)      # imm = 0x101
movl $0x2, %ecx
jmp 0x4c87e
movq %r15, %rbp
jmp 0x4c853
movq %rbx, %r13
movl $0x5, %ecx
cmpl $0x5, %ecx
jne 0x4c885
incq %r12
cmpq $0x10, %r12
movq 0x10(%rsp), %r15
jne 0x4c79a
jmp 0x4c88f
movq 0x18(%rsp), %rcx
movq %r14, (%rcx)
movl $0x3, %ecx
movq %rax, 0x8(%rsp)
jmp 0x4c858
cmpl $0x2, %ecx
movq 0x10(%rsp), %r15
je 0x4c8ba
cmpl $0x1, 0x8(%rsp)
jne 0x4c8b3
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0xd1e90(%rip), %rsi     # 0x11e735
movq 0x38(%rsp), %rdi
xorl %eax, %eax
callq 0xe9226
jmp 0x4c8ba
movw $0x101, 0x28(%r15)      # imm = 0x101
movl 0x4c(%rsp), %esi
movq 0x38(%rsp), %r12
movq %r12, %rdi
callq 0xe9595
cmpb $0x0, 0x28(%r15)
movq 0x18(%rsp), %rbx
movq 0x40(%rsp), %r14
jne 0x4c989
movq 0x58(%r15), %rdi
movq (%rbx), %rsi
movl $0x1c, %edx
movq %r14, %rcx
callq 0xefabc
movl $0x3, %esi
movq %r14, %rdi
callq 0xef0d5
andb $0x1, %al
movzbl %al, %edx
movl $0x3, %esi
movq %r14, %rdi
callq 0xf148e
movq %rbx, %rdx
addq $0x28, %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x1053ee
movq 0x60(%rbx), %rax
movq (%rbx), %rsi
movq 0x30(%rbx), %rcx
leaq (%rsi,%rax), %rdx
subq 0x70(%rbx), %rdx
xorl %edi, %edi
testq %rdx, %rdx
cmovgq %rdx, %rdi
movq %rdi, 0x78(%rbx)
movq 0x58(%rbx), %rdx
addq %rdx, %rsi
movq %rsi, 0x8(%rbx)
testq %rcx, %rcx
je 0x4c966
leaq -0x1c(%rdx), %rsi
movq %rsi, 0x18(%rbx)
leaq (%rdx,%rcx,4), %rcx
subq %rcx, %rax
jmp 0x4c970
addq $-0x1c, %rax
movq %rax, 0x18(%rbx)
xorl %eax, %eax
movq %rax, 0x20(%rbx)
cmpq $0x0, 0x18(%rbx)
js 0x4c982
cmpq $0x0, 0x20(%rbx)
jns 0x4c989
movw $0x101, 0x28(%r15)      # imm = 0x101
cmpb $0x0, 0x28(%r15)
je 0x4c2d9
cmpb $0x0, 0x29(%r15)
je 0x4c2d9
leaq 0xd1cc6(%rip), %rsi     # 0x11e66c
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9df7
movb $0x0, 0x29(%r15)
jmp 0x4c2d9