// File: /workspace/github_c_makefile_star2_O3/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 0x60b09
movq (%rdx), %rdi
testb $0x1, 0x8(%rdi)
jne 0x60b5b
movq (%rbx), %rsi
movq 0x60(%rbx), %rdi
addq $0x8, %rsi
leaq 0x16aee(%rip), %rcx     # 0x77609
movl $0x2, %edx
callq 0x40afa
movq 0x8(%rbx), %rbx
movq %rbx, %rdi
callq 0x30465
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 0x30577
leaq -0x30(%rbp), %rsi
movq %rax, (%rsi)
movq 0x60(%rbx), %r14
movl (%r15), %edx
movq %r14, %rdi
callq 0x51bfb
testq %rax, %rax
je 0x60cd9
movq %rax, %r15
movq %rbx, %rdi
movl $0x80, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x312d8
movq 0x60(%rbx), %r12
testq %rax, %rax
je 0x60cef
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 0x33d86
testq %rax, %rax
je 0x60c1c
movq %rax, %r13
movl $0x200, %edx            # imm = 0x200
movq %rax, %rdi
xorl %esi, %esi
callq 0xc360
movq %r13, 0x18(%r14)
leaq -0x23211(%rip), %rax    # 0x3d9e9
movq %rax, 0x28(%r14)
leaq 0x3def(%rip), %rax      # 0x649f4
movq %rax, 0x20(%r14)
movl $0x40, 0x30(%r14)
movq %r12, (%r14)
movl $0xdead635a, 0x50(%r14) # imm = 0xDEAD635A
movq 0x60(%rbx), %rdi
movq -0x30(%rbp), %r12
cmpq 0x488(%rdi), %r15
jne 0x60c49
leaq 0x60(%r14), %rsi
leaq 0x68(%r14), %rdx
movq %r12, %rdi
callq 0x65d7e
testl %eax, %eax
je 0x60d51
movq 0x60(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
movl $0x1, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq $0x0
pushq $0x0
callq 0x51dad
addq $0x10, %rsp
testq %rax, %rax
je 0x60d05
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 0x52035
movl 0x70(%r14), %edx
testl %edx, %edx
je 0x60d23
movq 0x68(%r14), %rsi
movq %r14, %rdi
callq 0x6448b
movl $0x2, 0x58(%r14)
movq 0x20(%r15), %rcx
testq %rcx, %rcx
je 0x60cd0
movq %r13, %rdi
movl %eax, %r15d
callq *%rcx
movl %r15d, %eax
testl %eax, %eax
jne 0x60d39
jmp 0x60d87
movq (%rbx), %rsi
addq $0x8, %rsi
leaq 0x16938(%rip), %rcx     # 0x7761f
movq %r14, %rdi
jmp 0x60b1b
movq (%rbx), %rsi
addq $0x8, %rsi
leaq 0x155d3(%rip), %rcx     # 0x762d0
movq %r12, %rdi
jmp 0x60b1b
leaq 0x16941(%rip), %rdx     # 0x7764d
movq %rbx, %rdi
movl $0x1, %esi
movq %r12, %rcx
xorl %eax, %eax
callq 0x44ed3
jmp 0x60b25
movl $0x2, 0x58(%r14)
movq 0x20(%r15), %rax
testq %rax, %rax
je 0x60d39
movq %r13, %rdi
callq *%rax
movq %r12, %rdi
callq 0x317be
movq %rbx, %rdi
movq %r14, %rsi
callq 0x313b9
jmp 0x60b25
movq 0x60(%r14), %rsi
movl 0x68(%r14), %edx
movq %r14, %rdi
callq 0x6448b
testl %eax, %eax
je 0x60d74
movq 0x60(%r14), %rdi
movq 0x68(%r14), %rsi
callq 0xc6e0
jmp 0x60d41
movl $0x1, 0x58(%r14)
leaq 0x329dd(%rip), %rax     # 0x93760
movq %rax, 0x70(%r14)
movq 0x10(%r14), %rax
movq %rax, 0x8(%r14)
movq 0x8(%rbx), %rbx
movq %rbx, %rdi
callq 0x30465
movq %r14, (%rbx)
movl $0x100, %eax            # imm = 0x100
jmp 0x60b3d