// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %rdi, %rbx
leaq 0x38(%rsp), %rsi
callq 0xe8a1c
cmpq $0x0, 0x58(%r14)
je 0x4c33d
movl $0x118, %esi            # imm = 0x118
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r12
leaq 0xd254c(%rip), %rsi     # 0x11e6e2
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movl $0x1c, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r15
cmpb $0x0, 0x28(%r14)
jne 0x4c343
movl (%r14), %eax
addl $-0x7, %eax
cmpl $0x3, %eax
ja 0x4c5ed
movl %eax, %eax
leaq 0xd26b6(%rip), %rcx     # 0x11e898
movq 0x18(%rbx), %rdi
movq 0x50(%r14), %rdx
movq 0x40(%rdx), %rsi
addq (%rcx,%rax,8), %rsi
callq 0xeebc8
movq %rax, %r13
shlq $0x4, %r13
leaq 0xd2522(%rip), %rsi     # 0x11e727
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe8617
cmpl $0x7, (%r14)
jne 0x4c37d
leaq 0x3c(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
leaq 0xd2568(%rip), %rsi     # 0x11e798
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x30(%r14), %rbp
addq 0x40(%r14), %rbp
leaq 0xd256b(%rip), %rsi     # 0x11e7c1
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeed0d
movl $0xd1edfeca, %ecx       # imm = 0xD1EDFECA
cmpq %rcx, %rax
jne 0x4c3eb
movq %r12, 0x20(%rsp)
movq 0x50(%r14), %rax
movq 0x48(%rax), %rdi
subq %rbp, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
callq 0xe9c3b
movq %rax, %r12
movq 0x18(%rbx), %rdi
leaq 0xd252f(%rip), %rsi     # 0x11e7d4
leaq 0x10(%rsp), %r9
movl $0x4, %edx
movq %rbp, %rcx
movq %rax, %r8
callq 0xf149c
movl %eax, 0x30(%rsp)
testl %eax, %eax
je 0x4c3f7
movq %r15, 0x18(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x4(%rax), %r15
movq %r15, %rcx
subq %rbp, %rcx
movq %r14, %r12
movq 0x50(%r14), %rax
movq 0x48(%rax), %r8
subq %r15, %r8
leaq 0xd24ea(%rip), %rsi     # 0x11e7de
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebc8
leaq 0xd24e9(%rip), %rsi     # 0x11e7fd
movq %rbx, %rdi
movq %rax, 0x28(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rsi
addq $0x7, %rsi
callq 0xeebc8
jmp 0x4c432
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %edi, %edi
callq 0xf052b
movq %r15, %rdi
callq 0xf052b
testq %r12, %r12
je 0x4c362
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe8be2
movl 0x38(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x2b(%r14)
je 0x4c5ed
testb $0x20, 0x2d(%r14)
je 0x4c5ed
movq 0x38(%r14), %rdx
leaq 0xf(%rdx), %rax
testq %rdx, %rdx
cmovnsq %rdx, %rax
andq $-0x10, %rax
subq %rax, %rdx
addq %r13, %rdx
movq %rdx, (%r12)
leaq 0xd2395(%rip), %rsi     # 0x11e74c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x58(%r14), %rdi
movq (%r12), %rsi
callq 0xeeb67
cmpq $0x5a4d, %rax           # imm = 0x5A4D
je 0x4c52f
cmpq $0x4d5a, %rax           # imm = 0x4D5A
je 0x4c52f
jmp 0x4c5ed
movw $0x101, 0x28(%r14)      # imm = 0x101
jmp 0x4c518
movq %r12, %r8
movq %r14, %r12
movq 0x18(%rbx), %rdi
leaq 0xd23d1(%rip), %rsi     # 0x11e7d9
leaq 0x10(%rsp), %r9
movl $0x4, %edx
movq %rbp, %rcx
callq 0xf149c
testl %eax, %eax
je 0x4c845
movq %r15, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
xorl %eax, %eax
leaq 0xd23eb(%rip), %rsi     # 0x11e824
xorl %r14d, %r14d
movq %rbx, %rdi
movq %rax, 0x8(%rsp)
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x58(%r12), %rdi
leaq (%r14,%r13), %rbp
movq %rbp, %rsi
callq 0xeead3
movzbl %al, %r15d
cmpb $0x5a, %r15b
je 0x4c46f
cmpl $0x4d, %r15d
jne 0x4c4cc
movq 0x58(%r12), %rdi
leaq (%r14,%r13), %rsi
incq %rsi
callq 0xeead3
movzbl %al, %eax
addl %r15d, %eax
cmpl $0xa7, %eax
jne 0x4c4cc
movq 0x58(%r12), %rdi
leaq (%r14,%r13), %rsi
addq $0x6, %rsi
callq 0xeebc8
cmpq 0x8(%rsp), %rax
jne 0x4c4cc
cmpl $0x0, 0x30(%rsp)
je 0x4c4f0
movq 0x58(%r12), %rdi
leaq (%r14,%r13), %rsi
addq $0x18, %rsi
callq 0xeebc8
movq 0x28(%rsp), %rcx
subq %rax, %rcx
cmpq %r14, %rcx
je 0x4c4f0
incq %r14
cmpq $0x10, %r14
jne 0x4c44e
movq %r12, %r14
movw $0x101, 0x28(%r12)      # imm = 0x101
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r12
jmp 0x4c518
movq 0x20(%rsp), %r13
movq %rbp, (%r13)
leaq 0xd233c(%rip), %rsi     # 0x11e83c
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rsp), %r15
movq %r12, %r14
movq %r13, %r12
movl 0x3c(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
cmpb $0x0, 0x28(%r14)
jne 0x4c5f4
movq 0x58(%r14), %rdi
movq (%r12), %rsi
movl $0x1c, %edx
movq %r15, %rcx
callq 0xef481
movl $0x3, %esi
movq %r15, %rdi
callq 0xeead3
andb $0x1, %al
movzbl %al, %edx
movl $0x3, %esi
movq %r15, %rdi
callq 0xf0e8b
movq %r12, %rdx
addq $0x28, %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x104f96
movq 0x60(%r12), %rax
movq (%r12), %rsi
movq 0x30(%r12), %rcx
leaq (%rsi,%rax), %rdx
subq 0x70(%r12), %rdx
xorl %edi, %edi
testq %rdx, %rdx
cmovgq %rdx, %rdi
movq %rdi, 0x78(%r12)
movq 0x58(%r12), %rdx
addq %rdx, %rsi
movq %rsi, 0x8(%r12)
testq %rcx, %rcx
je 0x4c5d5
leaq (%rdx,%rcx,4), %rcx
subq %rcx, %rax
leaq -0x1c(%rdx), %rcx
movq %rcx, 0x18(%r12)
movq %rax, 0x20(%r12)
cmpq $0x1c, %rdx
jl 0x4c5ed
testq %rax, %rax
js 0x4c5ed
cmpb $0x0, 0x28(%r14)
jne 0x4c5f4
jmp 0x4c620
leaq -0x1c(%rax), %rcx
movq %rcx, 0x18(%r12)
movq $0x0, 0x20(%r12)
cmpq $0x1c, %rax
jge 0x4c5cc
movw $0x101, 0x28(%r14)      # imm = 0x101
cmpb $0x0, 0x29(%r14)
je 0x4c343
leaq 0xd216d(%rip), %rsi     # 0x11e773
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movb $0x0, 0x29(%r14)
cmpb $0x0, 0x28(%r14)
jne 0x4c343
leaq 0xd730e(%rip), %rsi     # 0x123935
movq $0x0, 0x8(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %rax, %r13
movl $0x1c, %edx
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xef481
movq 0x58(%r14), %rdi
movq (%r12), %rsi
movq 0x18(%r12), %rdx
addq $0x1c, %rsi
movq %r13, %rcx
callq 0xef481
movq 0x30(%r12), %rsi
testq %rsi, %rsi
je 0x4c7b2
movq %r13, 0x30(%rsp)
shlq $0x2, %rsi
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x8(%rsp)
movq 0x58(%r14), %r13
movq 0x8(%r12), %rsi
movq 0x30(%r12), %rcx
movq %rsi, 0x10(%rsp)
movq %rcx, %rdx
movq %rsi, %rax
testq %rcx, %rcx
jle 0x4c76a
movq %rbx, 0x40(%rsp)
movq %rsi, 0x48(%rsp)
movq %r14, 0x28(%rsp)
movq %r15, 0x18(%rsp)
movq %r12, 0x20(%rsp)
leaq 0x10(%rsp), %rbx
movq %rcx, 0x50(%rsp)
movq %rcx, %r12
xorl %r14d, %r14d
movq %r13, %rdi
movq %rbx, %rsi
callq 0xeebeb
movq %rax, %r15
testw %r15w, %r15w
js 0x4c704
movq %r13, %rdi
movq %rbx, %rsi
callq 0xeebeb
movq %rax, %r14
movl %r15d, %ebp
jmp 0x4c71e
movl %r15d, %eax
andl $0x7fff, %eax           # imm = 0x7FFF
cmpl $0xc000, %r15d          # imm = 0xC000
cmovael %r15d, %eax
addl %r14d, %eax
movzwl %ax, %r14d
movl %r14d, %esi
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0xf0f95
movl %ebp, %esi
movq %r15, %rdi
callq 0xf0f95
decq %r12
leaq 0x10(%rsp), %rbx
jne 0x4c6dd
movq 0x10(%rsp), %rax
movq 0x20(%rsp), %r12
movq 0x30(%r12), %rdx
movq 0x18(%rsp), %r15
movq 0x28(%rsp), %r14
movq 0x50(%rsp), %rcx
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rbx
shlq $0x2, %rcx
subq %rsi, %rax
testb $0x20, 0x2d(%r14)
cmoveq %rcx, %rax
movq %rax, 0x10(%r12)
shlq $0x2, %rdx
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movq 0x30(%rsp), %r13
movq %r13, %rcx
callq 0xef481
movq 0x58(%r14), %rdi
movq 0x10(%r12), %rsi
movq 0x20(%r12), %rdx
addq 0x8(%r12), %rsi
movq %r13, %rcx
callq 0xef481
movq 0x58(%r14), %rdi
movq (%r12), %rdx
xorl %esi, %esi
movq %r13, %rcx
callq 0xef481
movq 0x50(%r14), %rax
movq 0x50(%rax), %rcx
testq %rcx, %rcx
jle 0x4c819
cmpq $0x0, 0x78(%r12)
movq 0x48(%rax), %rdx
jle 0x4c7f0
leaq 0xd1f0c(%rip), %rsi     # 0x11e6f0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
jmp 0x4c819
leaq 0xd1490(%rip), %rsi     # 0x11dc87
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq 0x50(%r14), %rax
movq 0x48(%rax), %rsi
movq 0x50(%rax), %rdx
movq %r13, %rcx
callq 0xef481
movq 0x8(%rsp), %rdi
callq 0xf052b
testq %r13, %r13
je 0x4c838
movq %r13, %rdi
callq 0xf052b
movq %rbx, %rdi
callq 0xe8f9c
movq %r15, %rdi
callq 0xf052b
jmp 0x4c357
movq %r12, %r14
movw $0x101, 0x28(%r12)      # imm = 0x101
jmp 0x4c4e9