// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %rdi, %rbx
testl %esi, %esi
jle 0x52ff6
movq (%rdx), %rdi
testb $0x1, 0x8(%rdi)
jne 0x53023
leaq 0x145ec(%rip), %rdx     # 0x675e9
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x2a0db
movq %rbx, %rdi
xorl %esi, %esi
callq 0x298b2
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x34(%rbp), %r14
movq %r14, %rsi
callq 0x297ed
leaq -0x30(%rbp), %rsi
movq %rax, (%rsi)
movq 0x60(%rbx), %rdi
movl (%r14), %edx
callq 0x47354
testq %rax, %rax
je 0x53180
movq %rax, %r15
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl $0x80, %esi
xorl %ecx, %ecx
callq 0x2a356
testq %rax, %rax
je 0x5318c
movq %rax, %r14
movq 0x60(%rbx), %r12
pushq $0x58
popq %rsi
movq %rax, %rdi
callq 0x27aea
movq %r12, %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x2ca19
testq %rax, %rax
je 0x530ca
movq %rax, %r13
movq %rax, %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x27aea
movq %r13, 0x18(%r14)
leaq -0x1d9c8(%rip), %rax    # 0x356e0
movq %rax, 0x28(%r14)
leaq 0x31fc(%rip), %rax      # 0x562af
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 0x530f7
leaq 0x60(%r14), %rsi
leaq 0x68(%r14), %rdx
movq %r12, %rdi
callq 0x5734a
testl %eax, %eax
je 0x531e2
movq 0x60(%rbx), %rdi
andq $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
pushq $0x1
popq %rcx
movq %r15, %rsi
movq %r12, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x474a5
testq %rax, %rax
je 0x53198
movq %rax, %r13
movq %r14, %r12
addq $0x60, %r12
movq 0x60(%rbx), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x68(%r14)
movq %rax, 0x60(%r14)
andl $0x0, 0x78(%r14)
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4769f
movl 0x70(%r14), %edx
testl %edx, %edx
je 0x531b4
movq 0x68(%r14), %rsi
movq %r14, %rdi
callq 0x55dc9
movl $0x2, 0x58(%r14)
movq 0x20(%r15), %rcx
testq %rcx, %rcx
je 0x53177
movq %r13, %rdi
movl %eax, %r15d
callq *%rcx
movl %r15d, %eax
testl %eax, %eax
jne 0x531ca
jmp 0x53218
leaq 0x14478(%rip), %rdx     # 0x675ff
jmp 0x52ffd
leaq 0x1311d(%rip), %rdx     # 0x662b0
jmp 0x52ffd
leaq 0x1448e(%rip), %rdx     # 0x6762d
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x3be68
jmp 0x53008
movl $0x2, 0x58(%r14)
movq 0x20(%r15), %rax
testq %rax, %rax
je 0x531ca
movq %r13, %rdi
callq *%rax
movq %r12, %rdi
callq 0x2a72f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a404
jmp 0x53008
movq 0x60(%r14), %rsi
movl 0x68(%r14), %edx
movq %r14, %rdi
callq 0x55dc9
testl %eax, %eax
je 0x53205
movq 0x60(%r14), %rdi
movq 0x68(%r14), %rsi
callq 0xc700
jmp 0x531d2
movl $0x1, 0x58(%r14)
leaq 0x3355c(%rip), %rax     # 0x86770
movq %rax, 0x70(%r14)
movq 0x10(%r14), %rax
movq %rax, 0x8(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x29a04
jmp 0x53012