// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/deark-zip.c
// Function: zipw_finalize
static void zipw_finalize(deark *c, struct zipw_ctx *zzz)
{
	i64 cdir_start;
	i64 zip64_eocd_pos;
	int opt_zip64;
	int need_zip64 = 0;
	int use_zip64 = 0;

	cdir_start = zzz->outf->len;

	if((zzz->membercount > 0xffff) || (cdir_start > 0xffffffffLL) ||
		(zzz->cdir->len > 0xffffffffLL))
	{
		need_zip64 = 1;
	}

	opt_zip64 = de_get_ext_option_bool(c, "archive:zip64", 0);
	if(opt_zip64>0) {
		use_zip64 = 1; // Zip64 always
	}
	else if(need_zip64) {
		use_zip64 = 1; // Zip64 auto
		de_info(c, "Note: Writing a ZIP file that uses Zip64 extensions. Not all unzip "
			"programs will correctly support it.");
	}

	// Write the central directory
	dbuf_copy(zzz->cdir, 0, zzz->cdir->len, zzz->outf);

	zip64_eocd_pos = zzz->outf->len;

	if(use_zip64) {
		// Write 56-byte zip64 EOCD record
		dbuf_writeu32le(zzz->outf, CODE_PK66);
		dbuf_writeu64le(zzz->outf, 56-12); // recsize
		dbuf_writeu16le(zzz->outf, ZIPENC_VER_MADE_BY);

		// version-needed: 4.5 = minimum for Zip64. This is a formality, because
		// anything that doesn't support Zip64 won't ever see this field.
		// Unfortunately, the original EOCD record does not have a version-needed
		// field, so (I guess) there is no good way to tell old unzip programs that
		// they cannot fully support this ZIP file.
		dbuf_writeu16le(zzz->outf, 45);

		dbuf_writeu32le(zzz->outf, 0); // this disk num
		dbuf_writeu32le(zzz->outf, 0); // central dir disk
		dbuf_writeu64le(zzz->outf, (u64)zzz->membercount); // num files this disk
		dbuf_writeu64le(zzz->outf, (u64)zzz->membercount); // num files total
		dbuf_writeu64le(zzz->outf, (u64)zzz->cdir->len); // central dir size
		dbuf_writeu64le(zzz->outf, (u64)cdir_start); // central dir offset

		// Write 20-byte EOCD locator
		dbuf_writeu32le(zzz->outf, CODE_PK67);
		dbuf_writeu32le(zzz->outf, 0); // central dir disk
		dbuf_writeu64le(zzz->outf, (u64)zip64_eocd_pos);
		dbuf_writeu32le(zzz->outf, 1); // number of disks
	}

	// Write 22-byte EOCD record
	dbuf_writeu32le(zzz->outf, CODE_PK56);
	dbuf_writeu16le(zzz->outf, 0); // this disk num
	dbuf_writeu16le(zzz->outf, 0); // central dir disk

	if(zzz->membercount > 0xffff) {
		dbuf_writeu16le(zzz->outf, 0xffff);
		dbuf_writeu16le(zzz->outf, 0xffff);
	}
	else {
		dbuf_writeu16le(zzz->outf, zzz->membercount); // num files this disk
		dbuf_writeu16le(zzz->outf, zzz->membercount); // num files total
	}

	if(zzz->cdir->len > 0xffffffffLL) {
		dbuf_writeu32le(zzz->outf, 0xffffffffLL);
	}
	else {
		dbuf_writeu32le(zzz->outf, zzz->cdir->len);
	}

	if(cdir_start > 0xffffffffLL) {
		dbuf_writeu32le(zzz->outf, 0xffffffffLL);
	}
	else {
		dbuf_writeu32le(zzz->outf, cdir_start);
	}

	dbuf_writeu16le(zzz->outf, 0); // ZIP comment length
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpq $0xffff, 0x18(%rax)     # imm = 0xFFFF
jg 0x1634ca
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x18(%rbp)
jg 0x1634ca
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, 0x18(%rax)
jle 0x1634d1
movl $0x1, -0x28(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x716e8(%rip), %rsi     # 0x1d4bc4
xorl %edx, %edx
callq 0x15cbc0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jle 0x1634f5
movl $0x1, -0x2c(%rbp)
jmp 0x163516
cmpl $0x0, -0x28(%rbp)
je 0x163514
movl $0x1, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x716c5(%rip), %rsi     # 0x1d4bd2
movb $0x0, %al
callq 0x15c0c0
jmp 0x163516
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x20(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x163659
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0x6064b50, %esi        # imm = 0x6064B50
callq 0x168c30
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0x2c, %esi
callq 0x168d60
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0x33f, %esi            # imm = 0x33F
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0x2d, %esi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168c30
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168c30
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x168d60
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x168d60
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rsi
callq 0x168d60
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x168d60
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0x7064b50, %esi        # imm = 0x7064B50
callq 0x168c30
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168c30
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x168d60
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0x1, %esi
callq 0x168c30
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0x6054b50, %esi        # imm = 0x6054B50
callq 0x168c30
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168a10
movq -0x10(%rbp), %rax
cmpq $0xffff, 0x18(%rax)     # imm = 0xFFFF
jle 0x1636c1
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x168a10
jmp 0x1636eb
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x168a10
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, 0x18(%rax)
jle 0x163712
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x168c30
jmp 0x16372b
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rsi
callq 0x168c30
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x18(%rbp)
jle 0x16374a
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x168c30
jmp 0x16375b
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x168c30
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168a10
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)