// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil.c
// Function: fmtutil_find_zip_eocd
int fmtutil_find_zip_eocd(deark *c, dbuf *f, UI flags, i64 *foundpos)
{
	u32 bof_sig;
	u32 sig;
	int skip_sanity_check = 0;
	u8 *buf = NULL;
	int retval = 0;
	i64 buf_offset;
	i64 buf_size;
	i64 pos;
	i64 i;

	*foundpos = 0;
	if(f->len < 22) goto done;

	bof_sig = (u32)dbuf_getu32be(f, 0);
	if(bof_sig==CODE_PK36) {
		skip_sanity_check = 1;
	}

	// End-of-central-dir record usually starts 22 bytes from EOF. Try that first.
	pos = f->len - 22;
	sig = (u32)dbuf_getu32be(f, pos);
	if(sig==CODE_PK56 && (skip_sanity_check || is_sane_zip_eocd(f, pos))) {
		*foundpos = pos;
		retval = 1;
		goto done;
	}

	if(flags & 0x1) goto done;

	// Search for the signature.
	// The end-of-central-directory record could theoretically appear anywhere
	// in the file. We'll follow Info-Zip/UnZip's lead and search the last 66000
	// bytes.
#define MAX_ZIP_EOCD_SEARCH 66000
	buf_size = f->len;
	if(buf_size > MAX_ZIP_EOCD_SEARCH) buf_size = MAX_ZIP_EOCD_SEARCH;

	buf = de_malloc(c, buf_size);
	buf_offset = f->len - buf_size;
	dbuf_read(f, buf, buf_offset, buf_size);

	for(i=buf_size-22; i>=0; i--) {
		if(buf[i]=='P' && buf[i+1]=='K' && buf[i+2]==5 && buf[i+3]==6 &&
			(skip_sanity_check || is_sane_zip_eocd(f, buf_offset+i)))
		{
			*foundpos = buf_offset + i;
			retval = 1;
			goto done;
		}
	}

done:
	de_free(c, buf);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
cmpq $0x16, 0x18(%rax)
jge 0x1750e8
jmp 0x17526b
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165470
movl %eax, -0x24(%rbp)
cmpl $0x504b0306, -0x24(%rbp) # imm = 0x504B0306
jne 0x175108
movl $0x1, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
subq $0x16, %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x165470
movl %eax, -0x28(%rbp)
cmpl $0x504b0506, -0x28(%rbp) # imm = 0x504B0506
jne 0x175160
cmpl $0x0, -0x2c(%rbp)
jne 0x175149
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x175290
cmpl $0x0, %eax
je 0x175160
movq -0x58(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x3c(%rbp)
jmp 0x17526b
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x175170
jmp 0x17526b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x101d0, -0x50(%rbp)   # imm = 0x101D0
jle 0x17518e
movq $0x101d0, -0x50(%rbp)   # imm = 0x101D0
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x15b780
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
subq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x164d10
movq -0x50(%rbp), %rax
subq $0x16, %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jl 0x175269
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x50, %eax
jne 0x175256
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rcx
movzbl 0x1(%rax,%rcx), %eax
cmpl $0x4b, %eax
jne 0x175256
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rcx
movzbl 0x2(%rax,%rcx), %eax
cmpl $0x5, %eax
jne 0x175256
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rcx
movzbl 0x3(%rax,%rcx), %eax
cmpl $0x6, %eax
jne 0x175256
cmpl $0x0, -0x2c(%rbp)
jne 0x17523e
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq -0x60(%rbp), %rsi
callq 0x175290
cmpl $0x0, %eax
je 0x175256
movq -0x48(%rbp), %rcx
addq -0x60(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x3c(%rbp)
jmp 0x17526b
jmp 0x175258
movq -0x60(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1751d0
jmp 0x17526b
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
movl -0x3c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)