// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
testl %esi, %esi
jle 0x650d6
movq %rdx, %r15
movq (%rdx), %rdi
testb $0x1, 0x8(%rdi)
jne 0x65124
movq (%rbx), %rsi
movq 0x60(%rbx), %rdi
addq $0x8, %rsi
leaq 0x14798(%rip), %rcx     # 0x79880
movl $0x2, %edx
callq 0x4153c
movq 0x8(%rbx), %rbx
movq %rbx, %rdi
callq 0x309f2
movq $0x0, (%rbx)
movl $0xfffffe90, %eax       # imm = 0xFFFFFE90
andl 0x8(%rbx), %eax
orl $0x8, %eax
movl %eax, 0x8(%rbx)
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %esi, %r12d
leaq -0x3c(%rbp), %rsi
callq 0x30b04
movq %rax, -0x38(%rbp)
cmpl $0x1, %r12d
jne 0x6514a
movl $0x1, -0x2c(%rbp)
leaq 0x1620d(%rip), %r13     # 0x7b355
jmp 0x6515a
movq 0x8(%r15), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x30b04
movq %rax, %r13
movq 0x60(%rbx), %rdi
movl -0x3c(%rbp), %edx
leaq -0x38(%rbp), %rsi
callq 0x53369
testq %rax, %rax
je 0x65197
movq %rax, %r14
movq %rbx, %rdi
movl $0x38, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x31892
testq %rax, %rax
je 0x651b6
cmpl $0x4, %r12d
jl 0x651d2
leaq 0x18(%r15), %rdx
jmp 0x651e6
movq -0x38(%rbp), %rcx
leaq 0x146fb(%rip), %rdx     # 0x7989d
movq %rbx, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x45d26
jmp 0x650f2
movq (%rbx), %rsi
movq 0x60(%rbx), %rdi
addq $0x8, %rsi
leaq 0x130fd(%rip), %rcx     # 0x782c5
movl $0x1, %edx
jmp 0x650ed
leaq 0x2f1f7(%rip), %rcx     # 0x943d0
movq %r15, %rdx
cmpq %rcx, %r14
je 0x651e6
xorl %r9d, %r9d
jmp 0x651e9
movq (%rdx), %r9
movq 0x60(%rbx), %rcx
movq %r14, -0x58(%rbp)
movq %r14, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movq %rcx, 0x10(%rax)
movl $0x0, 0x28(%rax)
movabsq $0xfeac1400000000, %rcx # imm = 0xFEAC1400000000
movq %rax, -0x60(%rbp)
movq %rcx, 0x30(%rax)
movslq -0x2c(%rbp), %rax
testq %rax, %rax
movq %r15, -0x50(%rbp)
jle 0x65288
movq %rax, %r14
addq %r13, %r14
movzbl (%r13), %ecx
andl $-0x21, %ecx
cmpl $0x51, %ecx
jle 0x65293
cmpl $0x58, %ecx
je 0x652ce
cmpl $0x57, %ecx
je 0x65303
cmpl $0x52, %ecx
jne 0x65357
incq %r13
movl $0x1, %r15d
cmpl $0x1, %eax
je 0x653b4
movzbl (%r13), %eax
cmpl $0x2b, %eax
je 0x6527d
cmpl $0x77, %eax
je 0x6527d
cmpl $0x57, %eax
jne 0x653b4
movl $0x4, %r15d
jmp 0x653b4
movl $0x1, %r15d
jmp 0x653b9
cmpl $0x41, %ecx
je 0x65335
cmpl $0x43, %ecx
jne 0x65357
incq %r13
movl $0xa, %r15d
cmpl $0x1, %eax
je 0x653b4
xorl %eax, %eax
cmpb $0x2b, (%r13)
sete %al
leal 0xa(,%rax,2), %r15d
jmp 0x653b4
incq %r13
movl $0x42, %r15d
cmpl $0x1, %eax
je 0x653b4
movzbl (%r13), %eax
cmpl $0x2b, %eax
je 0x652f8
cmpl $0x72, %eax
je 0x652f8
cmpl $0x52, %eax
jne 0x653b4
movl $0x44, %r15d
jmp 0x653b4
incq %r13
movl $0x1a, %r15d
cmpl $0x1, %eax
je 0x653b4
movzbl (%r13), %eax
cmpl $0x2b, %eax
je 0x6532d
cmpl $0x72, %eax
je 0x6532d
cmpl $0x52, %eax
jne 0x653b4
movl $0x1c, %r15d
jmp 0x653b4
incq %r13
movl $0x2a, %r15d
cmpl $0x1, %eax
je 0x653b4
xorl %eax, %eax
cmpb $0x2b, (%r13)
sete %al
leal 0x2a(,%rax,2), %r15d
jmp 0x653b4
movq (%rbx), %rsi
movq 0x60(%rbx), %rdi
addq $0x8, %rsi
leaq 0x1456a(%rip), %rcx     # 0x798d3
movl $0x3, %edx
movq %r9, %r15
callq 0x4153c
movq %r15, %r9
movl $0x1, %r15d
jmp 0x653b4
movzbl (%r13), %eax
andl $-0x21, %eax
cmpl $0x54, %eax
je 0x653a3
cmpl $0x42, %eax
jne 0x653b1
andl $0xfffffd7f, %r15d      # imm = 0xFFFFFD7F
orl $0x80, %r15d
jmp 0x653b1
andl $0xfffffd7f, %r15d      # imm = 0xFFFFFD7F
orl $0x200, %r15d           # imm = 0x200
incq %r13
cmpq %r14, %r13
jb 0x65381
movq 0x60(%rbx), %r13
movq -0x38(%rbp), %r14
xorl %r8d, %r8d
cmpl $0x3, %r12d
jl 0x653eb
movq -0x50(%rbp), %rax
movq %r14, -0x48(%rbp)
movq 0x10(%rax), %r14
movq %r14, %rdi
movq %r9, %r12
callq 0x446ef
movq %r12, %r9
movl (%r14), %r8d
movq -0x48(%rbp), %r14
movq %r13, %rdi
movq -0x58(%rbp), %rsi
movq %r14, %rdx
movl %r15d, %ecx
pushq $0x0
pushq $0x0
callq 0x534fd
addq $0x10, %rsp
movq -0x60(%rbp), %r15
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x65433
movq 0x8(%rbx), %rbx
movq %rbx, %rdi
callq 0x309f2
movq %r15, (%rbx)
movl $0xfffffe90, %eax       # imm = 0xFFFFFE90
andl 0x8(%rbx), %eax
orl $0x100, %eax            # imm = 0x100
jmp 0x65110
leaq 0x14208(%rip), %rdx     # 0x79642
movq %rbx, %rdi
movl $0x1, %esi
movq %r14, %rcx
xorl %eax, %eax
callq 0x45d26
movq 0x8(%rbx), %r14
movq %r14, %rdi
callq 0x309f2
movq $0x0, (%r14)
movl $0xfffffe90, %eax       # imm = 0xFFFFFE90
andl 0x8(%r14), %eax
orl $0x8, %eax
movl %eax, 0x8(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x31975
jmp 0x65113