// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/zip.c
// Function: zip_relocator_main
static void zip_relocator_main(deark *c, struct zipreloc_ctx *d)
{
	i64 cpstart, cplen;
	struct zip_wcd_ctx *wcdctx = NULL;

	d->offset_diff = d->relocpos - d->min_ldir_offset;

	de_dbg(c, "[writing new file]");
	d->outf = dbuf_create_output_file(c, "zip", NULL, 0);

	// Write any extra space requested at the start of the file
	dbuf_write_zeroes(d->outf, d->relocpos);

	// Copy the main part of the ZIP file
	cpstart = d->min_ldir_offset;
	cplen = d->cdir_offset_actual - cpstart;
	dbuf_copy(c->infile, cpstart, cplen, d->outf);

	wcdctx = de_malloc(c, sizeof(struct zip_wcd_ctx));
	wcdctx->userdata = (void*)d;
	wcdctx->cbfn = zipreloc_wcd_convert;
	wcdctx->max_entries = d->eocd.cdir_num_entries_this_seg;
	wcdctx->inf = c->infile;
	wcdctx->inf_startpos = d->cdir_offset_actual;
	wcdctx->inf_endpos = c->infile->len;

	// Copy/convert the central directory
	zip_wcd_run(c, wcdctx);
	if(wcdctx->errflag) {
		d->errflag = 1;
		d->need_errmsg = wcdctx->need_errmsg;
	}

	if(d->errflag) {
		goto done;
	}

	// Copy any unused bytes at the end of the central dir
	cpstart = d->cdir_offset_actual + d->central_dir_nbytes_converted;
	cplen =  d->eocd.cdir_byte_size - d->central_dir_nbytes_converted;
	dbuf_copy(c->infile, cpstart, cplen, d->outf);

	// Copy anything between the central dir and the EOCD record
	cpstart = d->cdir_offset_actual+d->eocd.cdir_byte_size;
	cplen = d->eocd_pos - cpstart;
	dbuf_copy(c->infile, cpstart, cplen, d->outf);

	// Copy/convert the EOCD record & archive comment

	// First 4 bytes
	cpstart = d->eocd_pos;
	cplen = 4;
	dbuf_copy(c->infile, cpstart, cplen, d->outf);

	// Next 4 bytes are segment ID numbers, that we force to zero.
	dbuf_write_zeroes(d->outf, 4);

	// Next 8 bytes (num entries cdir this segment, thru cdir size)
	cpstart = d->eocd_pos+8;
	cplen = 8;
	dbuf_copy(c->infile, cpstart, cplen, d->outf);

	// Adjusted central dir offset
	dbuf_writeu32le(d->outf, d->cdir_offset_actual + d->offset_diff);

	// Last 2 bytes of EOCD record, plus archive comment
	cpstart = d->eocd_pos + 20;
	cplen = 2 + d->eocd.archive_comment_len;
	dbuf_copy(c->infile, cpstart, cplen, d->outf);

done:
	dbuf_close(d->outf);
	d->outf = NULL;
	de_free(c, wcdctx);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rsi), %rax
subq 0x68(%rsi), %rax
movq %rax, 0x78(%rsi)
leaq 0x6653f(%rip), %rsi     # 0x143704
xorl %eax, %eax
callq 0xe8617
leaq 0x6bd94(%rip), %rsi     # 0x148f67
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %rax, 0x88(%r14)
movq 0x8(%r14), %rsi
movq %rax, %rdi
callq 0xf0d35
movq 0x60(%r14), %rdx
movq 0x68(%r14), %rsi
subq %rsi, %rdx
movq 0x18(%rbx), %rdi
movq 0x88(%r14), %rcx
callq 0xef481
movl $0x78, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
movq %r14, 0x8(%rax)
leaq 0x136(%rip), %rax       # 0xdd35e
movq %rax, (%r15)
movq 0x20(%r14), %rax
movq %rax, 0x30(%r15)
movq 0x18(%rbx), %rax
movq %rax, 0x18(%r15)
movq 0x60(%r14), %rcx
movq %rcx, 0x20(%r15)
movq 0x18(%rax), %rax
movq %rax, 0x28(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xdcd15
cmpb $0x0, 0x39(%r15)
je 0xdd26e
movb $0x1, (%r14)
movb 0x3a(%r15), %al
movb %al, 0x1(%r14)
jmp 0xdd337
cmpb $0x0, (%r14)
jne 0xdd337
movq 0x30(%r14), %rdx
movq 0x80(%r14), %rax
movq 0x60(%r14), %rsi
addq %rax, %rsi
subq %rax, %rdx
movq 0x18(%rbx), %rdi
movq 0x88(%r14), %rcx
callq 0xef481
movq 0x30(%r14), %rsi
addq 0x60(%r14), %rsi
movq 0x50(%r14), %rdx
subq %rsi, %rdx
movq 0x18(%rbx), %rdi
movq 0x88(%r14), %rcx
callq 0xef481
movq 0x18(%rbx), %rdi
movq 0x50(%r14), %rsi
movq 0x88(%r14), %rcx
movl $0x4, %edx
callq 0xef481
movq 0x88(%r14), %rdi
movl $0x4, %esi
callq 0xf0d35
movq 0x50(%r14), %rsi
movq 0x88(%r14), %rcx
addq $0x8, %rsi
movq 0x18(%rbx), %rdi
movl $0x8, %edx
callq 0xef481
movq 0x78(%r14), %rsi
movq 0x88(%r14), %rdi
addq 0x60(%r14), %rsi
callq 0xf1042
movq 0x40(%r14), %rdx
movq 0x50(%r14), %rsi
addq $0x14, %rsi
addq $0x2, %rdx
movq 0x18(%rbx), %rdi
movq 0x88(%r14), %rcx
callq 0xef481
movq 0x88(%r14), %rdi
callq 0xf052b
movq $0x0, 0x88(%r14)
movq %rbx, %rdi
movq %r15, %rsi
popq %rbx
popq %r14
popq %r15
jmp 0xe8be2