// File: /workspace/github_c_makefile_star2_O1/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 $0x2018, %rsp           # imm = 0x2018
movq %rdx, %r15
movq %rsi, -0x30(%rbp)
movq %rdi, %rbx
movl 0x30(%rdi), %eax
cmpl 0x20(%rdi), %eax
jb 0x66694
movl $0x0, 0x20(%rbx)
movl 0x28(%rbx), %eax
testb $0x4, %al
je 0x6668d
movq $0x0, 0x18(%rbx)
movl $0x0, 0x24(%rbx)
andl $-0x5, %eax
movl %eax, 0x28(%rbx)
movl $0x0, 0x30(%rbx)
movq (%rbx), %rax
movq %rax, -0x40(%rbp)
movl 0x20(%rbx), %ecx
movl 0x30(%rbx), %eax
cmpl %eax, %ecx
je 0x666eb
movq 0x18(%rbx), %rdx
addq %rax, %rdx
subl %eax, %ecx
addq %rdx, %rcx
xorl %r12d, %r12d
movb (%rdx,%r12), %sil
cmpb $0xa, %sil
je 0x666d2
leaq (%rdx,%r12), %rdi
incq %rdi
incq %r12
cmpq %rcx, %rdi
jb 0x666b4
xorl %r12d, %r12d
jmp 0x666dc
incq %r12
movq -0x30(%rbp), %rcx
movq %rdx, (%rcx)
cmpb $0xa, %sil
jne 0x666eb
leal (%r12,%rax), %ecx
jmp 0x66815
leaq 0x10(%rbx), %rax
movq %rax, -0x38(%rbp)
leaq -0x1(%r15), %rax
cmpq $0x1fff, %rax           # imm = 0x1FFF
movl $0x2000, %r14d          # imm = 0x2000
cmovbq %r15, %r14
leaq -0x2040(%rbp), %r13
movq 0x8(%rbx), %rdi
movq %r13, %rsi
movq %r14, %rdx
movq -0x40(%rbp), %rax
callq *0x30(%rax)
movq %rax, %r12
testq %rax, %rax
jle 0x667cd
movq -0x38(%rbp), %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x360ae
movl 0x20(%rbx), %eax
movl 0x30(%rbx), %ecx
subl %ecx, %eax
je 0x66766
movq 0x18(%rbx), %rdx
addq %rcx, %rdx
leaq (%rdx,%rax), %rdi
xorl %esi, %esi
cmpb $0xa, (%rdx,%rsi)
je 0x6676a
leaq (%rdx,%rsi), %r8
incq %r8
incq %rsi
cmpq %rdi, %r8
jb 0x66751
xorl %edx, %edx
jmp 0x66779
incq %rsi
movq -0x30(%rbp), %rdi
movq %rdx, (%rdi)
movb $0x1, %dl
movq %rsi, %r12
testb %dl, %dl
jne 0x66812
testq %r15, %r15
setle %dl
cmpq %r15, %rax
setl %sil
orb %dl, %sil
jne 0x6670e
addq 0x18(%rbx), %rcx
movq -0x30(%rbp), %rdx
movq %rcx, (%rdx)
movl $0x0, 0x20(%rbx)
movl 0x28(%rbx), %edx
xorl %ecx, %ecx
testb $0x4, %dl
je 0x667c8
movq $0x0, 0x18(%rbx)
movl $0x0, 0x24(%rbx)
andl $-0x5, %edx
movl %edx, 0x28(%rbx)
movq %rax, %r12
jmp 0x66815
movl 0x20(%rbx), %eax
movl 0x30(%rbx), %ecx
cmpl %ecx, %eax
je 0x66818
movq %rcx, %rdx
addq 0x18(%rbx), %rdx
movq -0x30(%rbp), %rsi
movq %rdx, (%rsi)
movl %eax, %r12d
subl %ecx, %r12d
movl $0x0, 0x20(%rbx)
movl 0x28(%rbx), %eax
xorl %ecx, %ecx
testb $0x4, %al
je 0x66815
movq $0x0, 0x18(%rbx)
movl $0x0, 0x24(%rbx)
andl $-0x5, %eax
movl %eax, 0x28(%rbx)
jmp 0x66815
addl %r12d, %ecx
movl %ecx, 0x30(%rbx)
movq %r12, %rax
addq $0x2018, %rsp           # imm = 0x2018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq