// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/zip.c
// Function: do_end_of_central_dir
static int do_end_of_central_dir(deark *c, lctx *d)
{
	i64 pos;
	i64 alt_cdir_offset;
	u8 have_alt_cdir_offset = 0;
	int retval = 0;
	char tmpsz[64];

	pos = d->eocd_pos;
	de_dbg(c, "end-of-central-dir record at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	d->eocd.this_seg_num = de_getu16le(pos+4);
	de_dbg(c, "this segment num: %"I64_FMT, d->eocd.this_seg_num);
	d->eocd.cdir_starting_seg_num = de_getu16le(pos+6);
	de_dbg(c, "segment num with central dir start: %"I64_FMT,
		d->eocd.cdir_starting_seg_num);

	d->eocd.cdir_num_entries_this_seg = de_getu16le(pos+8);
	de_dbg(c, "central dir num entries on this segment: %s",
		format_u16_with_zip64_override(d, d->eocd.cdir_num_entries_this_seg, tmpsz, sizeof(tmpsz)));
	if(d->is_zip64 && (d->eocd.cdir_num_entries_this_seg==0xffff)) {
		d->eocd.cdir_num_entries_this_seg = d->eocd64.cdir_num_entries_this_seg;
	}

	d->eocd.cdir_num_entries_total = de_getu16le(pos+10);
	if(d->is_resof) {
		d->eocd.cdir_byte_size = - de_geti32le(pos+12);
	}
	else {
		d->eocd.cdir_byte_size = de_getu32le(pos+12);
	}

	d->eocd.cdir_offset = de_getu32le(pos+16);
	de_dbg(c, "central dir num entries: %s",
		format_u16_with_zip64_override(d, d->eocd.cdir_num_entries_total, tmpsz, sizeof(tmpsz)));
	if(d->is_zip64 && (d->eocd.cdir_num_entries_total==0xffff)) {
		d->eocd.cdir_num_entries_total = d->eocd64.cdir_num_entries_total;
	}

	de_dbg(c, "central dir size: %"I64_FMT, d->eocd.cdir_byte_size);
	if(d->is_zip64 && (d->eocd.cdir_byte_size==0xffffffffLL)) {
		d->eocd.cdir_byte_size = d->eocd64.cdir_byte_size;
	}

	de_dbg(c, "central dir offset: %s, segment: %"I64_FMT,
		format_u32_with_zip64_override(d, d->eocd.cdir_offset, tmpsz, sizeof(tmpsz)),
		d->eocd.cdir_starting_seg_num);
	if(d->is_zip64 && (d->eocd.cdir_offset==0xffffffffLL)) {
		d->eocd.cdir_offset = d->eocd64.cdir_offset;
	}

	d->eocd.archive_comment_len = de_getu16le(pos+20);
	de_dbg(c, "comment length: %d", (int)d->eocd.archive_comment_len);

	if((d->eocd.cdir_starting_seg_num==d->eocd.this_seg_num) &&
		(d->eocd.cdir_offset + d->eocd.cdir_byte_size > d->eocd_pos))
	{
		// If the central dir pos is wrong, we expect it to be too small, not
		// too large. This is probably not a ZIP file (EOCD sig. false positive).
		// TODO?: Maybe the signature-search function should be more discriminating.
		de_err(c, "Invalid EOCD record. This might not be a ZIP file.");
		goto done;
	}

	if(d->eocd.archive_comment_len>0) {
		// The comment for the whole .ZIP file presumably has to use
		// cp437 encoding. There's no flag that could indicate otherwise.
		do_comment(c, d, pos+22, d->eocd.archive_comment_len, 0,
			"ZIP file comment", "comment.txt");
	}

	if(d->eocd.cdir_starting_seg_num!=d->eocd.this_seg_num ||
		(d->is_zip64 && d->zip64_eocd_segnum!=d->eocd.this_seg_num))
	{
		de_err(c, "This looks like part of a multi-segment ZIP archive.");
		print_multisegment_note(c, d);
		goto done;
	}

	if(d->eocd.this_seg_num!=0) {
		de_warn(c, "This file might be part of a multi-segment ZIP archive.");
		print_multisegment_note(c, d);
	}

	if(d->eocd.cdir_num_entries_this_seg!=d->eocd.cdir_num_entries_total) {
		de_warn(c, "This ZIP file might not be supported correctly "
			"(number-of-entries-this-seg=%d, number-of-entries-total=%d)",
			(int)d->eocd.cdir_num_entries_this_seg, (int)d->eocd.cdir_num_entries_total);
	}

	alt_cdir_offset =
		(d->is_zip64 ? d->zip64_eocd_pos : d->eocd_pos) -
		d->eocd.cdir_byte_size;
	if(alt_cdir_offset>=0 && alt_cdir_offset!=d->eocd.cdir_offset) {
		have_alt_cdir_offset = 1;
	}

	if(have_alt_cdir_offset) {
		u32 sig;

		de_warn(c, "Inconsistent central directory offset. Reported to be %"I64_FMT", "
			"but based on its reported size, it should be %"I64_FMT".",
			d->eocd.cdir_offset, alt_cdir_offset);

		sig = (u32)de_getu32be(alt_cdir_offset);
		if(sig==CODE_PK12) {
			d->offset_correction = alt_cdir_offset - d->eocd.cdir_offset;
			de_dbg(c, "likely central dir found at %"I64_FMT, alt_cdir_offset);
			d->eocd.cdir_offset = alt_cdir_offset;
		}
	}

	retval = 1;

done:
	de_dbg_indent(c, -1);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x21(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x8298d(%rip), %rsi     # 0x1d01d9
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x81683(%rip), %rsi     # 0x1cef17
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x6, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x829a6(%rip), %rsi     # 0x1d0274
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq -0x70(%rbp), %rdx
movl $0x40, %ecx
callq 0x14de50
movq -0x80(%rbp), %rdi
movq %rax, %rdx
leaq 0x82977(%rip), %rsi     # 0x1d029c
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0xac(%rax), %eax
cmpl $0x0, %eax
je 0x14d95a
movq -0x10(%rbp), %rax
cmpq $0xffff, 0x18(%rax)     # imm = 0xFFFF
jne 0x14d95a
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0xa, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0xad(%rax)
je 0x14d9ab
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0xc, %rsi
callq 0x165600
xorl %ecx, %ecx
subq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
jmp 0x14d9cb
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0xc, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x10, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
leaq -0x70(%rbp), %rdx
movl $0x40, %ecx
callq 0x14de50
movq -0x88(%rbp), %rdi
movq %rax, %rdx
leaq 0x828a7(%rip), %rsi     # 0x1d02c8
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0xac(%rax), %eax
cmpl $0x0, %eax
je 0x14da56
movq -0x10(%rbp), %rax
cmpq $0xffff, 0x20(%rax)     # imm = 0xFFFF
jne 0x14da56
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x8150e(%rip), %rsi     # 0x1cef77
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0xac(%rax), %eax
cmpl $0x0, %eax
je 0x14da9f
movq -0x10(%rbp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, 0x28(%rax)
jne 0x14da9f
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
leaq -0x70(%rbp), %rdx
movl $0x40, %ecx
callq 0x14a500
movq -0x90(%rbp), %rdi
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
leaq 0x82807(%rip), %rsi     # 0x1d02e4
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0xac(%rax), %eax
cmpl $0x0, %eax
je 0x14db13
movq -0x10(%rbp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, 0x30(%rax)
jne 0x14db13
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x14, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %edx
leaq 0x75107(%rip), %rsi     # 0x1c2c4f
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x14db95
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x28(%rcx), %rax
movq -0x10(%rbp), %rcx
cmpq 0x88(%rcx), %rax
jle 0x14db95
movq -0x8(%rbp), %rdi
leaq 0x82780(%rip), %rsi     # 0x1d0309
movb $0x0, %al
callq 0x15bfb0
jmp 0x14dd74
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jle 0x14dbd2
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
addq $0x16, %rdx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
xorl %r8d, %r8d
leaq 0x8277a(%rip), %r9      # 0x1d033c
leaq 0x56687(%rip), %rax     # 0x1a4250
movq %rax, (%rsp)
callq 0x14a830
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x14dc09
movq -0x10(%rbp), %rax
movzbl 0xac(%rax), %eax
cmpl $0x0, %eax
je 0x14dc2d
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
je 0x14dc2d
movq -0x8(%rbp), %rdi
leaq 0x82739(%rip), %rsi     # 0x1d034d
movb $0x0, %al
callq 0x15bfb0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x14d200
jmp 0x14dd74
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x14dc57
movq -0x8(%rbp), %rdi
leaq 0x8273f(%rip), %rsi     # 0x1d0382
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x14d200
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
je 0x14dc8f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %ecx
leaq 0x82732(%rip), %rsi     # 0x1d03ba
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movzbl 0xac(%rax), %eax
cmpl $0x0, %eax
je 0x14dcb3
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq %rax, -0x98(%rbp)
jmp 0x14dcc5
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq -0x10(%rbp), %rcx
subq 0x28(%rcx), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jl 0x14dcf1
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x30(%rcx), %rax
je 0x14dcf1
movb $0x1, -0x21(%rbp)
cmpb $0x0, -0x21(%rbp)
je 0x14dd6d
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdx
movq -0x20(%rbp), %rcx
leaq 0x82717(%rip), %rsi     # 0x1d0425
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x165470
movl %eax, -0x74(%rbp)
cmpl $0x504b0102, -0x74(%rbp) # imm = 0x504B0102
jne 0x14dd6b
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
subq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x8273a(%rip), %rsi     # 0x1d0492
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0x14dd6d
movl $0x1, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x28(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nop