// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9Builtin_fread
static int jx9Builtin_fread(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const jx9_io_stream *pStream;
	io_private *pDev;
	jx9_int64 nRead;
	void *pBuf;
	int nLen;
	if( nArg < 1 || !jx9_value_is_resource(apArg[0]) ){
		/* Missing/Invalid arguments, return FALSE */
		jx9_context_throw_error(pCtx, JX9_CTX_WARNING, "Expecting an IO handle");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Extract our private data */
	pDev = (io_private *)jx9_value_to_resource(apArg[0]);
	/* Make sure we are dealing with a valid io_private instance */
	if( IO_PRIVATE_INVALID(pDev) ){
		/*Expecting an IO handle */
		jx9_context_throw_error(pCtx, JX9_CTX_WARNING, "Expecting an IO handle");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Point to the target IO stream device */
	pStream = pDev->pStream;
	if( pStream == 0  ){
		jx9_context_throw_error_format(pCtx, JX9_CTX_WARNING, 
			"IO routine(%s) not implemented in the underlying stream(%s) device, JX9 is returning FALSE", 
			jx9_function_name(pCtx), pStream ? pStream->zName : "null_stream"
			);
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
        nLen = 4096;
	if( nArg > 1 ){
 	  nLen = jx9_value_to_int(apArg[1]);
	  if( nLen < 1 ){
		/* Invalid length, set a default length */
		nLen = 4096;
	  }
        }
	/* Allocate enough buffer */
	pBuf = jx9_context_alloc_chunk(pCtx, (unsigned int)nLen, FALSE, FALSE);
	if( pBuf == 0 ){
		jx9_context_throw_error(pCtx, JX9_CTX_ERR, "JX9 is running out of memory");			
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Perform the requested operation */
	nRead = StreamRead(pDev, pBuf, (jx9_int64)nLen);
	if( nRead < 1 ){
		/* Nothing read, return FALSE */
		jx9_result_bool(pCtx, 0);
	}else{
		/* Make a copy of the data just read */
		jx9_result_string(pCtx, (const char *)pBuf, (int)nRead);
	}
	/* Release the buffer */
	jx9_context_free_chunk(pCtx, pBuf);
	return JX9_OK;	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
testl %esi, %esi
jle 0x53e37
movq (%rdx), %rax
testb $0x1, 0x9(%rax)
je 0x53e37
movq (%rax), %r14
testq %r14, %r14
je 0x53e37
cmpl $0xfeac14, 0x34(%r14)   # imm = 0xFEAC14
jne 0x53e37
cmpq $0x0, (%r14)
je 0x53e5e
movl $0x1000, %r12d          # imm = 0x1000
cmpl $0x1, %esi
je 0x53dfe
movq 0x8(%rdx), %rdi
callq 0x29772
testl %eax, %eax
cmovgl %eax, %r12d
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2a356
testq %rax, %rax
je 0x53e82
movq %rax, %r15
movslq %r12d, %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x562e8
movq %rbx, %rdi
testq %rax, %rax
jle 0x53e8d
movq %r15, %rsi
movl %eax, %edx
callq 0x29903
jmp 0x53e94
leaq 0x1386e(%rip), %rdx     # 0x676ac
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x2a0db
movq %rbx, %rdi
xorl %esi, %esi
callq 0x298b2
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rax
movq 0x8(%rax), %rcx
leaq 0x13857(%rip), %rdx     # 0x676c3
leaq 0x138ab(%rip), %r8      # 0x6771e
pushq $0x2
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3be68
jmp 0x53e49
leaq 0x12427(%rip), %rdx     # 0x662b0
pushq $0x1
jmp 0x53e40
xorl %esi, %esi
callq 0x298b2
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2a404
jmp 0x53e53