// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
cmpl $0x1, -0x14(%rbp)
jl 0xa48aa
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x57480
cmpl $0x0, %eax
jne 0xa48d6
movq -0x10(%rbp), %rdi
movl $0x2, %esi
leaq 0x21228(%rip), %rdx     # 0xc5ae2
callq 0x580b0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x57070
movl $0x0, -0x4(%rbp)
jmp 0xa4bd7
movq -0x20(%rbp), %rax
movq (%rax), %rdi
leaq -0x54(%rbp), %rsi
callq 0x56e20
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movl -0x54(%rbp), %edx
leaq -0x40(%rbp), %rsi
callq 0x8e570
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xa4935
movq -0x10(%rbp), %rdi
movl $0x2, %esi
leaq 0x211df(%rip), %rdx     # 0xc5af8
callq 0x580b0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x57070
movl $0x0, -0x4(%rbp)
jmp 0xa4bd7
movq -0x10(%rbp), %rdi
movl $0x80, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x58640
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0xa4981
movq -0x10(%rbp), %rdi
movl $0x2, %esi
leaq 0x1fe4c(%rip), %rdx     # 0xc47b1
callq 0x580b0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x57070
movl $0x0, -0x4(%rbp)
jmp 0xa4bd7
movq -0x30(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0xa9c90
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x60(%rcx), %rcx
cmpq 0x488(%rcx), %rax
jne 0xa4aa8
callq 0x5a4c0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0xa4aa6
movq -0x60(%rbp), %rax
cmpq $0x0, 0x108(%rax)
je 0xa4aa6
movq -0x60(%rbp), %rax
movq 0x108(%rax), %rax
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x8, %rsi
movq -0x38(%rbp), %rdx
addq $0x8, %rdx
addq $0x8, %rdx
callq *%rax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jne 0xa4aa4
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
callq 0xa9d80
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
je 0xa4a89
movq -0x60(%rbp), %rax
cmpq $0x0, 0x110(%rax)
je 0xa4a65
movq -0x60(%rbp), %rax
movq 0x110(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x38(%rbp), %rcx
movq 0x10(%rcx), %rsi
callq *%rax
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x58780
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x57070
movl $0x0, -0x4(%rbp)
jmp 0xa4bd7
movq -0x38(%rbp), %rax
movl $0x1, (%rax)
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0xa4bba
jmp 0xa4aa6
jmp 0xa4aa8
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x40(%rbp), %rdx
movl $0x1, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x8e7e0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xa4b17
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rcx
movl $0x1, %esi
leaq 0x2102d(%rip), %rdx     # 0xc5b26
movb $0x0, %al
callq 0x79580
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x57070
movl $0x0, -0x4(%rbp)
jmp 0xa4bd7
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rsi
callq 0x58bb0
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x8e9f0
movl $0xfffffff7, -0x58(%rbp) # imm = 0xFFFFFFF7
movq -0x48(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jbe 0xa4b71
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x48(%rbp), %rax
movl 0x10(%rax), %edx
callq 0xa9d80
movl %eax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movl $0x2, (%rax)
movq -0x28(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x8ea80
cmpl $0x0, -0x58(%rbp)
je 0xa4bb8
movq -0x48(%rbp), %rdi
callq 0x58c00
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x58780
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x57070
movl $0x0, -0x4(%rbp)
jmp 0xa4bd7
jmp 0xa4bba
movq -0x30(%rbp), %rdi
callq 0xa9f00
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x57340
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq