// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/zip.c
// Function: do_run_zip_combiner
static void do_run_zip_combiner(deark *c, de_module_params *mparams)
{
	struct zipcombine_ctx *d = NULL;
	dbuf *inf_last_segment;
	int eocd_found;
	int last_seg_xidx;
	dbuf *outf = NULL;

	d = de_malloc(c, sizeof(struct zipcombine_ctx));

	d->num_segments = 1;
	if(c->mp_data) {
		d->num_segments += c->mp_data->count;
	}
	de_dbg(c, "num segments: %d", d->num_segments);
	d->segments = de_mallocarray(c, d->num_segments, sizeof(struct zc_segment));

	last_seg_xidx = d->num_segments-1;
	inf_last_segment = de_mp_acquire_dbuf(c, last_seg_xidx);
	if(!inf_last_segment) {
		d->errflag = 1;
		goto done;
	}
	eocd_found = fmtutil_find_zip_eocd(c, inf_last_segment, 0x1, &d->eocd_pos.rel_pos);
	if(!eocd_found) {
		d->need_errmsg = 1;
		goto done;
	}

	d->eocd_pos.rel_seg_id = last_seg_xidx;
	de_dbg(c, "end-of-central-dir record found at %"I64_FMT", segment %d",
		d->eocd_pos.rel_pos, d->eocd_pos.rel_seg_id);
	simple_read_eocd(c, inf_last_segment, d->eocd_pos.rel_pos, &d->eocd);

	de_dbg_indent(c, 1);
	simple_dbg_eocd(c, &d->eocd);
	de_dbg_indent(c, -1);

	de_mp_release_dbuf(c, last_seg_xidx, &inf_last_segment);

	if(d->eocd.is_likely_zip64) {
		de_err(c, "Combining Zip64 is not supported");
		goto done;
	}

	if(d->eocd.this_seg_num != last_seg_xidx) {
		d->need_errmsg = 1;
		goto done;
	}

	zc_scan_and_read_to_membuf(c, d);
	if(d->errflag) goto done;

	zc_relpos_to_abspos(d, &d->eocd_pos);
	zc_format_advpos(d, &d->eocd_pos);
	de_dbg(c, "end-of-central-dir at %s", d->tmpsz);

	d->cdir_pos.rel_seg_id = (int)d->eocd.cdir_starting_seg_num;
	d->cdir_pos.rel_pos = d->eocd.cdir_offset;
	zc_relpos_to_abspos(d, &d->cdir_pos);
	if(d->errflag) goto done;
	zc_format_advpos(d, &d->cdir_pos);
	de_dbg(c, "central dir at %s", d->tmpsz);
	if(d->errflag) goto done;

	zc_modify_eocd(c, d);
	if(d->errflag) goto done;
	zc_modify_cdir(c, d);
	if(d->errflag) goto done;

	// Write the combined-and-modified file
	outf = dbuf_create_output_file(c, "zip", NULL, 0);
	dbuf_copy(d->f, 0, d->f->len, outf);

done:
	dbuf_close(outf);
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Failed to process multi-segment ZIP archive");
		}
		dbuf_close(d->f);
		de_free(c, d->segments);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movl $0xd8, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl $0x1, 0x4(%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x14763c
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movl (%rax), %ecx
movq -0x18(%rbp), %rax
addl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edx
leaq 0x8780d(%rip), %rsi     # 0x1cee5b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movslq 0x4(%rax), %rsi
movl $0x10, %edx
callq 0x15c450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
subl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x15f890
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1476a9
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
jmp 0x1478df
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rcx
addq $0x48, %rcx
addq $0x8, %rcx
movl $0x1, %edx
callq 0x1750a0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x1476dd
movq -0x18(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x1478df
movl -0x28(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdx
movq -0x18(%rbp), %rax
movl 0x48(%rax), %ecx
leaq 0x8776b(%rip), %rsi     # 0x1cee6c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdx
movq -0x18(%rbp), %rcx
addq $0x8, %rcx
callq 0x1480e0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x1481e0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %esi
leaq -0x20(%rbp), %rdx
callq 0x15f970
movq -0x18(%rbp), %rax
cmpb $0x0, 0x40(%rax)
je 0x147783
movq -0x8(%rbp), %rdi
leaq 0x87728(%rip), %rsi     # 0x1cee9f
movb $0x0, %al
callq 0x15bfb0
jmp 0x1478df
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x28(%rbp), %rcx
cmpq %rcx, %rax
je 0x1477a1
movq -0x18(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x1478df
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x148280
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
je 0x1477bc
jmp 0x1478df
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x48, %rsi
callq 0x1484c0
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x48, %rsi
callq 0x148530
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
addq $0x88, %rdx
leaq 0x876cc(%rip), %rsi     # 0x1ceec0
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x60, %rsi
callq 0x1484c0
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
je 0x14783b
jmp 0x1478df
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x60, %rsi
callq 0x148530
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
addq $0x88, %rdx
leaq 0x87677(%rip), %rsi     # 0x1ceed9
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
je 0x147874
jmp 0x1478df
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x148580
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
je 0x14788c
jmp 0x1478df
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x148620
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
je 0x1478a4
jmp 0x1478df
movq -0x8(%rbp), %rdi
leaq 0x8d218(%rip), %rsi     # 0x1d4ac7
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x30(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x30(%rbp), %rdi
callq 0x1676a0
cmpq $0x0, -0x18(%rbp)
je 0x147939
movq -0x18(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x14790b
movq -0x8(%rbp), %rdi
leaq 0x875e7(%rip), %rsi     # 0x1ceeeb
movb $0x0, %al
callq 0x15bfb0
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x80(%rax), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x30, %rsp
popq %rbp
retq
nop