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

	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);

	// Copy/convert the central directory
	zipreloc_walk_central_dir(c, d, zipreloc_wcd_cb2);
	// 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);

	dbuf_close(d->outf);
	d->outf = NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
subq 0x68(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x8(%rbp), %rdi
leaq 0x8625b(%rip), %rsi     # 0x1cf20e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
leaq 0x8bb02(%rip), %rsi     # 0x1d4ac7
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x1685f0
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
subq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
callq 0x166070
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq 0x19f(%rip), %rdx       # 0x1491e0
callq 0x148cc0
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x80(%rcx), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
subq 0x80(%rcx), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x30(%rcx), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
subq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x18(%rbp)
movq $0x4, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdi
movl $0x4, %esi
callq 0x1685f0
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
addq $0x8, %rax
movq %rax, -0x18(%rbp)
movq $0x8, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rsi
movq -0x10(%rbp), %rax
addq 0x78(%rax), %rsi
callq 0x168c30
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
addq $0x14, %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdi
callq 0x1676a0
movq -0x10(%rbp), %rax
movq $0x0, 0x88(%rax)
addq $0x20, %rsp
popq %rbp
retq
nop