// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: StreamReadLine
static jx9_int64 StreamReadLine(io_private *pDev, const char **pzData, jx9_int64 nMaxLen)
{
	const jx9_io_stream *pStream = pDev->pStream;
	char zBuf[8192];
	jx9_int64 n;
	sxi32 rc;
	n = 0;
	if( pDev->nOfft >= SyBlobLength(&pDev->sBuffer) ){
		/* Reset the working buffer so that we avoid excessive memory allocation */
		SyBlobReset(&pDev->sBuffer);
		pDev->nOfft = 0;
	}
	if( SyBlobLength(&pDev->sBuffer) - pDev->nOfft > 0 ){
		/* Check if there is a line */
		rc = GetLine(pDev, &n, pzData);
		if( rc == SXRET_OK ){
			/* Got line, update the cursor  */
			pDev->nOfft += (sxu32)n;
			return n;
		}
	}
	/* Perform the read operation until a new line is extracted or length
	 * limit is reached.
	 */
	for(;;){
		n = pStream->xRead(pDev->pHandle, zBuf, (nMaxLen > 0 && nMaxLen < sizeof(zBuf)) ? nMaxLen : sizeof(zBuf));
		if( n < 1 ){
			/* EOF or IO error */
			break;
		}
		/* Append the data just read */
		SyBlobAppend(&pDev->sBuffer, zBuf, (sxu32)n);
		/* Try to extract a line */
		rc = GetLine(pDev, &n, pzData);
		if( rc == SXRET_OK ){
			/* Got one, return immediately */
			pDev->nOfft += (sxu32)n;
			return n;
		}
		if( nMaxLen > 0 && (SyBlobLength(&pDev->sBuffer) - pDev->nOfft >= nMaxLen) ){
			/* Read limit reached, return the available data */
			*pzData = (const char *)SyBlobDataAt(&pDev->sBuffer, pDev->nOfft);
			n = SyBlobLength(&pDev->sBuffer) - pDev->nOfft;
			/* Reset the working buffer */
			SyBlobReset(&pDev->sBuffer);
			pDev->nOfft = 0;
			return n;
		}
	}
	if( SyBlobLength(&pDev->sBuffer) - pDev->nOfft > 0 ){
		/* Read limit reached, return the available data */
		*pzData = (const char *)SyBlobDataAt(&pDev->sBuffer, pDev->nOfft);
		n = SyBlobLength(&pDev->sBuffer) - pDev->nOfft;
		/* Reset the working buffer */
		SyBlobReset(&pDev->sBuffer);
		pDev->nOfft = 0;
	}
	return n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2028, %rsp           # imm = 0x2028
movq %rdx, %r12
movq %rsi, -0x38(%rbp)
movq %rdi, %rbx
movq (%rdi), %rax
movq %rax, -0x48(%rbp)
andq $0x0, -0x30(%rbp)
movl 0x20(%rdi), %eax
movl 0x30(%rdi), %ecx
leaq 0x10(%rdi), %rdx
movq %rdx, -0x40(%rbp)
cmpl %eax, %ecx
jb 0x563d3
movq -0x40(%rbp), %rdi
callq 0x31b1e
andl $0x0, 0x30(%rbx)
movl 0x20(%rbx), %eax
xorl %ecx, %ecx
cmpl %ecx, %eax
je 0x563ef
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
movq -0x38(%rbp), %rdx
callq 0x564c6
testl %eax, %eax
je 0x56477
leaq -0x1(%r12), %rax
cmpq $0x1fff, %rax           # imm = 0x1FFF
movl $0x2000, %r14d          # imm = 0x2000
cmovbq %r12, %r14
leaq -0x2050(%rbp), %r15
movq 0x8(%rbx), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq -0x48(%rbp), %rax
callq *0x30(%rax)
movq %rax, %r13
movq %rax, -0x30(%rbp)
testq %rax, %rax
jle 0x56483
movq -0x40(%rbp), %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x2e250
movq %rbx, %rdi
leaq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x564c6
testl %eax, %eax
je 0x56477
testq %r12, %r12
jle 0x5640b
movl 0x20(%rbx), %r13d
movl 0x30(%rbx), %eax
subl %eax, %r13d
cmpq %r12, %r13
jb 0x5640b
addq 0x18(%rbx), %rax
movq -0x38(%rbp), %rcx
movq %rax, (%rcx)
movq -0x40(%rbp), %rdi
callq 0x31b1e
xorl %eax, %eax
jmp 0x564ae
movq -0x30(%rbp), %r13
movl 0x30(%rbx), %eax
addl %r13d, %eax
jmp 0x564ae
movl 0x20(%rbx), %r14d
movl 0x30(%rbx), %eax
cmpl %eax, %r14d
je 0x564b1
movq %rax, %rcx
addq 0x18(%rbx), %rcx
movq -0x38(%rbp), %rdx
movq %rcx, (%rdx)
subl %eax, %r14d
movq -0x40(%rbp), %rdi
callq 0x31b1e
xorl %eax, %eax
movq %r14, %r13
movl %eax, 0x30(%rbx)
movq %r13, %rax
addq $0x2028, %rsp           # imm = 0x2028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq