// File: /workspace/github_c_makefile_star2_O1/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9Builtin_zip_open
static int jx9Builtin_zip_open(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const jx9_io_stream *pStream;
	SyArchive *pArchive;
	zip_raw_data *pRaw;
	const char *zFile;
	SyBlob *pContents;
	void *pHandle;
	int nLen;
	sxi32 rc;
	if( nArg < 1 || !jx9_value_is_string(apArg[0]) ){
		/* Missing/Invalid arguments, return FALSE */
		jx9_context_throw_error(pCtx, JX9_CTX_WARNING, "Expecting a file path");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Extract the file path */
	zFile = jx9_value_to_string(apArg[0], &nLen);
	/* Point to the target IO stream device */
	pStream = jx9VmGetStreamDevice(pCtx->pVm, &zFile, nLen);
	if( pStream == 0 ){
		jx9_context_throw_error(pCtx, JX9_CTX_WARNING, "No such stream device, JX9 is returning FALSE");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Create an in-memory archive */
	pArchive = (SyArchive *)jx9_context_alloc_chunk(pCtx, sizeof(SyArchive)+sizeof(zip_raw_data), TRUE, FALSE);
	if( pArchive == 0 ){
		jx9_context_throw_error(pCtx, JX9_CTX_WARNING, "JX9 is running out of memory");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	pRaw = (zip_raw_data *)&pArchive[1];
	/* Initialize the archive */
	SyArchiveInit(pArchive, &pCtx->pVm->sAllocator, 0, 0);
	/* Extract the default stream */
	if( pStream == pCtx->pVm->pDefStream /* file:// stream*/){
		const jx9_vfs *pVfs;
		/* Try to get a memory view of the whole file since ZIP files
		 * tends to be very big this days, this is a huge performance win.
		 */
		pVfs = jx9ExportBuiltinVfs();
		if( pVfs && pVfs->xMmap ){
			rc = pVfs->xMmap(zFile, &pRaw->raw.mmap.pMap, &pRaw->raw.mmap.nSize);
			if( rc == JX9_OK ){
				/* Nice, Extract the whole archive */
				rc = SyZipExtractFromBuf(pArchive, (const char *)pRaw->raw.mmap.pMap, (sxu32)pRaw->raw.mmap.nSize);
				if( rc != SXRET_OK ){
					if( pVfs->xUnmap ){
						pVfs->xUnmap(pRaw->raw.mmap.pMap, pRaw->raw.mmap.nSize);
					}
					/* Release the allocated chunk */
					jx9_context_free_chunk(pCtx, pArchive);
					/* Something goes wrong with this ZIP archive, return FALSE */
					jx9_result_bool(pCtx, 0);
					return JX9_OK;
				}
				/* Archive successfully opened */
				pRaw->iType = ZIP_RAW_DATA_MMAPED;
				pRaw->raw.mmap.pVfs = pVfs;
				goto success;
			}
		}
		/* FALL THROUGH */
	}
	/* Try to open the file in read-only mode */
	pHandle = jx9StreamOpenHandle(pCtx->pVm, pStream, zFile, JX9_IO_OPEN_RDONLY, FALSE, 0, FALSE, 0);
	if( pHandle == 0 ){
		jx9_context_throw_error_format(pCtx, JX9_CTX_ERR, "IO error while opening '%s'", zFile);
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	pContents = &pRaw->raw.sBlob;
	SyBlobInit(pContents, &pCtx->pVm->sAllocator);
	/* Read the whole file */
	jx9StreamReadWholeFile(pHandle, pStream, pContents);
	/* Assume an invalid ZIP file */
	rc = SXERR_INVALID;
	if( SyBlobLength(pContents) > 0 ){
		/* Extract archive entries */
		rc = SyZipExtractFromBuf(pArchive, (const char *)SyBlobData(pContents), SyBlobLength(pContents));
	}
	pRaw->iType = ZIP_RAW_DATA_MEMBUF;
	/* Close the stream */
	jx9StreamCloseHandle(pStream, pHandle);
	if( rc != SXRET_OK ){
		/* Release the working buffer */
		SyBlobRelease(pContents);
		/* Release the allocated chunk */
		jx9_context_free_chunk(pCtx, pArchive);
		/* Something goes wrong with this ZIP archive, return FALSE */
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
success:
	/* Reset the loop cursor */
	SyArchiveResetLoopCursor(pArchive);
	/* Return the in-memory archive as a resource handle */
	jx9_result_resource(pCtx, pArchive);
	return JX9_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
testl %esi, %esi
jle 0x6252d
movq (%rdx), %rdi
testb $0x1, 0x8(%rdi)
jne 0x6257f
movq (%rbx), %rsi
movq 0x60(%rbx), %rdi
addq $0x8, %rsi
leaq 0x170bf(%rip), %rcx     # 0x795fe
movl $0x2, %edx
callq 0x4153c
movq 0x8(%rbx), %rbx
movq %rbx, %rdi
callq 0x309f2
movq $0x0, (%rbx)
movl $0x8, %eax
movl $0xfffffe90, %ecx       # imm = 0xFFFFFE90
andl 0x8(%rbx), %ecx
orl %eax, %ecx
movl %ecx, 0x8(%rbx)
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x34(%rbp), %r15
movq %r15, %rsi
callq 0x30b04
leaq -0x30(%rbp), %rsi
movq %rax, (%rsi)
movq 0x60(%rbx), %r14
movl (%r15), %edx
movq %r14, %rdi
callq 0x53369
testq %rax, %rax
je 0x626f1
movq %rax, %r15
movq %rbx, %rdi
movl $0x80, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x31892
movq 0x60(%rbx), %r12
testq %rax, %rax
je 0x62707
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq $0x0, 0x50(%rax)
movq %r12, %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x343ed
testq %rax, %rax
je 0x62640
movq %rax, %r13
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0xc360
movq %r13, 0x18(%r14)
leaq -0x2426b(%rip), %rax    # 0x3e3b3
movq %rax, 0x28(%r14)
leaq 0x3f3d(%rip), %rax      # 0x66566
movq %rax, 0x20(%r14)
movl $0x40, 0x30(%r14)
movq %r12, (%r14)
movl $0xdead635a, 0x50(%r14) # imm = 0xDEAD635A
movq 0x60(%rbx), %rax
cmpq 0x488(%rax), %r15
je 0x6271d
movq 0x60(%rbx), %rdi
movq -0x30(%rbp), %r12
movq %r15, %rsi
movq %r12, %rdx
movl $0x1, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq $0x0
pushq $0x0
callq 0x534fd
addq $0x10, %rsp
testq %rax, %rax
je 0x62795
movq %rax, %r13
movq %r14, %r12
addq $0x60, %r12
movq 0x60(%rbx), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x68(%r14)
movq %rax, 0x60(%r14)
movl $0x0, 0x78(%r14)
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x53771
movl 0x70(%r14), %edx
testl %edx, %edx
je 0x627b3
movq 0x68(%r14), %rsi
movq %r14, %rdi
callq 0x65f4f
movl $0x2, 0x58(%r14)
movq 0x20(%r15), %rcx
testq %rcx, %rcx
je 0x626e4
movq %r13, %rdi
movl %eax, %r15d
callq *%rcx
movl %r15d, %eax
testl %eax, %eax
jne 0x627c9
jmp 0x6280b
movq (%rbx), %rsi
addq $0x8, %rsi
leaq 0x16f15(%rip), %rcx     # 0x79614
movq %r14, %rdi
jmp 0x6253f
movq (%rbx), %rsi
addq $0x8, %rsi
leaq 0x15bb0(%rip), %rcx     # 0x782c5
movq %r12, %rdi
jmp 0x6253f
movq -0x30(%rbp), %rdi
leaq 0x60(%r14), %rsi
leaq 0x68(%r14), %rdx
callq 0x67942
xorl %ecx, %ecx
testl %eax, %eax
jne 0x627f9
movq 0x60(%r14), %rsi
movl 0x68(%r14), %edx
movq %r14, %rdi
callq 0x65f4f
testl %eax, %eax
je 0x627e1
movq 0x60(%r14), %rdi
movq 0x68(%r14), %rsi
callq 0xc6e0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31975
movq 0x8(%rbx), %r12
movq %r12, %rdi
callq 0x309f2
movq $0x0, (%r12)
movl $0xfffffe90, %eax       # imm = 0xFFFFFE90
andl 0x8(%r12), %eax
orl $0x8, %eax
movl %eax, 0x8(%r12)
movl $0x1, %ecx
jmp 0x627f9
leaq 0x16ea6(%rip), %rdx     # 0x79642
movq %rbx, %rdi
movl $0x1, %esi
movq %r12, %rcx
xorl %eax, %eax
callq 0x45d26
jmp 0x62549
movl $0x2, 0x58(%r14)
movq 0x20(%r15), %rax
testq %rax, %rax
je 0x627c9
movq %r13, %rdi
callq *%rax
movq %r12, %rdi
callq 0x31d8f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31975
jmp 0x62549
movl $0x1, 0x58(%r14)
leaq 0x32f70(%rip), %rax     # 0x95760
movq %rax, 0x70(%r14)
movl $0x2, %ecx
cmpl $0x2, %ecx
je 0x6280b
testl %ecx, %ecx
je 0x62651
jmp 0x6256e
movq 0x10(%r14), %rax
movq %rax, 0x8(%r14)
movq 0x8(%rbx), %rbx
movq %rbx, %rdi
callq 0x309f2
movq %r14, (%rbx)
movl $0x100, %eax            # imm = 0x100
jmp 0x62561