// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9Builtin_fopen
static int jx9Builtin_fopen(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const jx9_io_stream *pStream;
	const char *zUri, *zMode;
	jx9_value *pResource;
	io_private *pDev;
	int iLen, imLen;
	int iOpenFlags;
	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 or URL");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Extract the URI and the desired access mode */
	zUri  = jx9_value_to_string(apArg[0], &iLen);
	if( nArg > 1 ){
		zMode = jx9_value_to_string(apArg[1], &imLen);
	}else{
		/* Set a default read-only mode */
		zMode = "r";
		imLen = (int)sizeof(char);
	}
	/* Try to extract a stream */
	pStream = jx9VmGetStreamDevice(pCtx->pVm, &zUri, iLen);
	if( pStream == 0 ){
		jx9_context_throw_error_format(pCtx, JX9_CTX_WARNING, 
			"No stream device is associated with the given URI(%s)", zUri);
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Allocate a new IO private instance */
	pDev = (io_private *)jx9_context_alloc_chunk(pCtx, sizeof(io_private), TRUE, FALSE);
	if( pDev == 0 ){
		jx9_context_throw_error(pCtx, JX9_CTX_ERR, "JX9 is running out of memory");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	pResource = 0;
	if( nArg > 3 ){
		pResource = apArg[3];
	}else if( is_jx9_stream(pStream) ){
		/* TICKET 1433-80: The jx9:// stream need a jx9_value to access the underlying
		 * virtual machine.
		 */
		pResource = apArg[0];
	}
	/* Initialize the structure */
	InitIOPrivate(pCtx->pVm, pStream, pDev);
	/* Convert open mode to JX9 flags */
	iOpenFlags = StrModeToFlags(pCtx, zMode, imLen);
	/* Try to get a handle */
	pDev->pHandle = jx9StreamOpenHandle(pCtx->pVm, pStream, zUri, iOpenFlags, 
		nArg > 2 ? jx9_value_to_bool(apArg[2]) : FALSE, pResource, FALSE, 0);
	if( pDev->pHandle == 0 ){
		jx9_context_throw_error_format(pCtx, JX9_CTX_ERR, "IO error while opening '%s'", zUri);
		jx9_result_bool(pCtx, 0);
		jx9_context_free_chunk(pCtx, pDev);
		return JX9_OK;
	}
	/* All done, return the io_private instance as a resource */
	jx9_result_resource(pCtx, pDev);
	return JX9_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
cmpl $0x1, -0x14(%rbp)
jl 0xa877d
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x57480
cmpl $0x0, %eax
jne 0xa87a9
movq -0x10(%rbp), %rdi
movl $0x2, %esi
leaq 0x1d5d7(%rip), %rdx     # 0xc5d64
callq 0x580b0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x57070
movl $0x0, -0x4(%rbp)
jmp 0xa89bf
movq -0x20(%rbp), %rax
movq (%rax), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x56e20
movq %rax, -0x30(%rbp)
cmpl $0x1, -0x14(%rbp)
jle 0xa87da
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x50(%rbp), %rsi
callq 0x56e20
movq %rax, -0x38(%rbp)
jmp 0xa87ec
leaq 0x1f474(%rip), %rax     # 0xc7c55
movq %rax, -0x38(%rbp)
movl $0x1, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movl -0x4c(%rbp), %edx
leaq -0x30(%rbp), %rsi
callq 0x8e570
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xa883d
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rcx
movl $0x2, %esi
leaq 0x1d562(%rip), %rdx     # 0xc5d81
movb $0x0, %al
callq 0x79580
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x57070
movl $0x0, -0x4(%rbp)
jmp 0xa89bf
movq -0x10(%rbp), %rdi
movl $0x38, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x58640
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xa8889
movq -0x10(%rbp), %rdi
movl $0x1, %esi
leaq 0x1bf44(%rip), %rdx     # 0xc47b1
callq 0x580b0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x57070
movl $0x0, -0x4(%rbp)
jmp 0xa89bf
movq $0x0, -0x40(%rbp)
cmpl $0x3, -0x14(%rbp)
jle 0xa88a5
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0xa88c0
movq -0x28(%rbp), %rdi
callq 0xab0c0
cmpl $0x0, %eax
je 0xa88be
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0xa88c0
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x90730
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x50(%rbp), %edx
callq 0xab0e0
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x58(%rbp)
cmpl $0x2, -0x14(%rbp)
jle 0xa8922
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x56cf0
movl %eax, -0x74(%rbp)
jmp 0xa8929
xorl %eax, %eax
movl %eax, -0x74(%rbp)
jmp 0xa8929
movl -0x58(%rbp), %ecx
movq -0x60(%rbp), %rdx
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdi
movl -0x74(%rbp), %r8d
movq -0x40(%rbp), %r9
xorl %eax, %eax
xorl %eax, %eax
movl $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x8e7e0
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xa89ab
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rcx
movl $0x1, %esi
leaq 0x1d1a3(%rip), %rdx     # 0xc5b26
movb $0x0, %al
callq 0x79580
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x57070
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x58780
movl $0x0, -0x4(%rbp)
jmp 0xa89bf
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x57340
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)