// File: /workspace/github_c_makefile_star2_O1/JimKent[P]linearSat/lib/linefile.c
// Function: lineFileNext
boolean lineFileNext(struct lineFile *lf, char **retStart, int *retSize)
/* Fetch next line from file. */
{
char *buf = lf->buf;
int bytesInBuf = lf->bytesInBuf;
int endIx = lf->lineEnd;
boolean gotLf = FALSE;
int newStart;

if (lf->reuse)
    {
    lf->reuse = FALSE;
    if (retSize != NULL)
	*retSize = lf->lineEnd - lf->lineStart;
    *retStart = buf + lf->lineStart;
    if (lf->metaOutput && *retStart[0] == '#')
        metaDataAdd(lf, *retStart);
    return TRUE;
    }

if (lf->nextCallBack)
    return lf->nextCallBack(lf, retStart, retSize);


#ifdef USE_TABIX
if (lf->tabix != NULL && lf->tabixIter != NULL)
    {
    // Just use line-oriented ti_read:
    int lineSize = 0;
    const char *line = ti_read(lf->tabix, lf->tabixIter, &lineSize);
    if (line == NULL)
	return FALSE;
    lf->bufOffsetInFile = -1;
    lf->bytesInBuf = lineSize;
    lf->lineIx = -1;
    lf->lineStart = 0;
    lf->lineEnd = lineSize;
    if (lineSize > lf->bufSize)
	// shouldn't be!  but just in case:
	lineFileExpandBuf(lf, lineSize * 2);
    safecpy(lf->buf, lf->bufSize, line);
    *retStart = lf->buf;
    if (retSize != NULL)
	*retSize = lineSize;
    return TRUE;
    }
#endif // USE_TABIX

determineNlType(lf, buf+endIx, bytesInBuf);

/* Find next end of line in buffer. */
switch(lf->nlType)
    {
    case nlt_unix:
    case nlt_dos:
	for (endIx = lf->lineEnd; endIx < bytesInBuf; ++endIx)
	    {
	    if (buf[endIx] == '\n')
		{
		gotLf = TRUE;
		endIx += 1;
		break;
		}
	    }
	break;
    case nlt_mac:
	for (endIx = lf->lineEnd; endIx < bytesInBuf; ++endIx)
	    {
	    if (buf[endIx] == '\r')
		{
		gotLf = TRUE;
		endIx += 1;
		break;
		}
	    }
	break;
    case nlt_undet:
	break;
    }

/* If not in buffer read in a new buffer's worth. */
while (!gotLf)
    {
    int oldEnd = lf->lineEnd;
    int sizeLeft = bytesInBuf - oldEnd;
    int bufSize = lf->bufSize;
    int readSize = bufSize - sizeLeft;

    if (oldEnd > 0 && sizeLeft > 0)
	{
	memmove(buf, buf+oldEnd, sizeLeft);
	}
    lf->bufOffsetInFile += oldEnd;
    if (lf->fd >= 0)
	readSize = lineFileLongNetRead(lf->fd, buf+sizeLeft, readSize);
#ifdef USE_TABIX
    else if (lf->tabix != NULL && readSize > 0)
	{
	readSize = ti_bgzf_read(lf->tabix->fp, buf+sizeLeft, readSize);
	if (readSize < 1)
	    return FALSE;
	}
#endif // USE_TABIX
    else
        readSize = 0;

    if ((readSize == 0) && (endIx > oldEnd))
	{
	endIx = sizeLeft;
	buf[endIx] = 0;
	lf->bytesInBuf = newStart = lf->lineStart = 0;
	lf->lineEnd = endIx;
	++lf->lineIx;
	if (retSize != NULL)
	    *retSize = endIx - newStart;
	*retStart = buf + newStart;
        if (*retStart[0] == '#')
            metaDataAdd(lf, *retStart);
	return TRUE;
	}
    else if (readSize <= 0)
	{
	lf->bytesInBuf = lf->lineStart = lf->lineEnd = 0;
	return FALSE;
	}
    bytesInBuf = lf->bytesInBuf = readSize + sizeLeft;
    lf->lineEnd = 0;

    determineNlType(lf, buf+endIx, bytesInBuf);

    /* Look for next end of line.  */
    switch(lf->nlType)
	{
    	case nlt_unix:
	case nlt_dos:
	    for (endIx = sizeLeft; endIx <bytesInBuf; ++endIx)
		{
		if (buf[endIx] == '\n')
		    {
		    endIx += 1;
		    gotLf = TRUE;
		    break;
		    }
		}
	    break;
	case nlt_mac:
	    for (endIx = sizeLeft; endIx <bytesInBuf; ++endIx)
		{
		if (buf[endIx] == '\r')
		    {
		    endIx += 1;
		    gotLf = TRUE;
		    break;
		    }
		}
	    break;
	case nlt_undet:
	    break;
	}
    if (!gotLf && bytesInBuf == lf->bufSize)
        {
	if (bufSize >= 512*1024*1024)
	    {
	    errAbort("Line too long (more than %d chars) line %d of %s",
		lf->bufSize, lf->lineIx+1, lf->fileName);
	    }
	else
	    {
	    lineFileExpandBuf(lf, bufSize*2);
	    buf = lf->buf;
	    }
	}
    }

if (lf->zTerm)
    {
    buf[endIx-1] = 0;
    if ((lf->nlType == nlt_dos) && (buf[endIx-2]=='\r'))
	{
	buf[endIx-2] = 0;
	}
    }

lf->lineStart = newStart = lf->lineEnd;
lf->lineEnd = endIx;
++lf->lineIx;
if (retSize != NULL)
    *retSize = endIx - newStart;
*retStart = buf + newStart;
if (*retStart[0] == '#')
    metaDataAdd(lf, *retStart);
return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq 0x40(%rdi), %r13
movl 0x30(%rdi), %ecx
cmpb $0x0, 0x3c(%rdi)
je 0xd2d6
movb $0x0, 0x3c(%rbx)
testq %rdx, %rdx
je 0xd2a0
subl 0x2c(%rbx), %ecx
movl %ecx, (%rdx)
movslq 0x2c(%rbx), %rax
addq %rax, %r13
movq %r13, (%rsi)
movl $0x1, %r14d
cmpq $0x0, 0x50(%rbx)
je 0xd60f
cmpb $0x23, (%r13)
jne 0xd60f
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd621
jmp 0xd60f
movq %rdx, 0x18(%rsp)
movq 0x90(%rbx), %rax
testq %rax, %rax
je 0xd2ff
movq %rbx, %rdi
movq 0x18(%rsp), %rdx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movq %rsi, 0x30(%rsp)
movl 0x20(%rbx), %r15d
movq %rcx, %rbp
movslq %ecx, %rsi
addq %r13, %rsi
movq %rbx, %rdi
movl %r15d, %edx
callq 0xd68a
movl 0x38(%rbx), %eax
movl $0x0, 0xc(%rsp)
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0xd353
movl 0x30(%rbx), %ebp
movl %ebp, %eax
subl %r15d, %eax
jge 0xd39b
movslq %ebp, %rdx
addq %r13, %rdx
xorl %ecx, %ecx
cmpb $0xa, (%rdx)
je 0xd38b
decl %ecx
incq %rdx
cmpl %ecx, %eax
jne 0xd341
subl %ecx, %ebp
jmp 0xd381
cmpl $0x3, %eax
jne 0xd39b
movl 0x30(%rbx), %ebp
cmpl %r15d, %ebp
jge 0xd39b
movslq %r15d, %rax
movslq %ebp, %rcx
leaq (%rcx,%r13), %rdx
subq %rax, %rcx
xorl %eax, %eax
cmpb $0xd, (%rdx)
je 0xd38f
decq %rax
incq %rdx
cmpq %rax, %rcx
jne 0xd36f
subl %eax, %ebp
movl $0x0, 0xc(%rsp)
jmp 0xd39b
subl %ecx, %ebp
jmp 0xd391
subl %eax, %ebp
incl %ebp
movl $0x1, 0xc(%rsp)
movq %rbx, 0x38(%rsp)
cmpl $0x0, 0xc(%rsp)
jne 0xd5af
movq %rbp, 0x40(%rsp)
movl %r14d, 0x10(%rsp)
movslq 0x30(%rbx), %r14
movl %r15d, 0x14(%rsp)
movl %r15d, %ebp
subl %r14d, %ebp
movl 0x14(%rbx), %eax
testq %r14, %r14
jle 0xd3e4
testl %ebp, %ebp
jle 0xd3e4
movl %eax, %r15d
leaq (%r14,%r13), %rsi
movl %ebp, %edx
movq %r13, %rdi
callq 0x35d0
movl %r15d, %eax
movq %r13, 0x20(%rsp)
addq %r14, 0x18(%rbx)
movl 0x10(%rbx), %r12d
xorl %r15d, %r15d
testl %r12d, %r12d
movl %eax, 0x2c(%rsp)
js 0xd433
movl %eax, %ebx
subl %ebp, %ebx
testl %ebx, %ebx
jle 0xd433
movslq %ebp, %r13
addq 0x20(%rsp), %r13
xorl %r15d, %r15d
movl %ebx, %edx
movl %r12d, %edi
movq %r13, %rsi
callq 0x33d0
testl %eax, %eax
jle 0xd433
addl %eax, %r15d
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, %r13
subl %eax, %ebx
jg 0xd410
testl %r15d, %r15d
movq 0x40(%rsp), %rax
jne 0xd49c
cmpl %r14d, %eax
jle 0xd49c
movslq %ebp, %rax
movq 0x20(%rsp), %r13
movb $0x0, (%r13,%rax)
movq 0x38(%rsp), %rbx
incl 0x28(%rbx)
xorl %eax, %eax
movl %eax, 0x2c(%rbx)
movl %eax, 0x20(%rbx)
movl %ebp, 0x30(%rbx)
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
movq 0x30(%rsp), %rax
je 0xd474
movl %ebp, (%rcx)
movq %r13, (%rax)
movl $0x1, %r14d
cmpb $0x23, (%r13)
movl 0x14(%rsp), %r15d
jne 0xd494
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd621
xorl %r12d, %r12d
jmp 0xd5a4
testl %r15d, %r15d
movq 0x38(%rsp), %rbx
movq 0x20(%rsp), %r13
je 0xd4fa
leal (%r15,%rbp), %edx
movl %edx, 0x20(%rbx)
movl $0x0, 0x30(%rbx)
movslq %eax, %rsi
addq %r13, %rsi
movq %rbx, %rdi
movl %edx, %r14d
callq 0xd68a
movl 0x38(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0xd51b
testl %r15d, %r15d
jle 0xd54a
movslq %ebp, %rbp
movl %r14d, %r15d
movslq %r14d, %rax
movl 0x10(%rsp), %r14d
cmpb $0xa, (%r13,%rbp)
je 0xd554
incq %rbp
cmpq %rax, %rbp
jl 0xd4e8
jmp 0xd55e
movq $0x0, 0x2c(%rbx)
movl $0x0, 0x20(%rbx)
xorl %r12d, %r12d
movl %eax, %ebp
xorl %r14d, %r14d
movl 0x14(%rsp), %r15d
jmp 0xd5a4
cmpl $0x3, %eax
jne 0xd545
testl %r15d, %r15d
jle 0xd54a
movslq %ebp, %rbp
movl %r14d, %r15d
movslq %r14d, %rax
movl 0x10(%rsp), %r14d
cmpb $0xd, (%r13,%rbp)
je 0xd554
incq %rbp
cmpq %rax, %rbp
jl 0xd533
jmp 0xd55e
movq 0x40(%rsp), %rbp
movl %r14d, %r15d
movl 0x10(%rsp), %r14d
jmp 0xd55e
incl %ebp
movl $0x1, 0xc(%rsp)
movb $0x1, %r12b
cmpl $0x0, 0xc(%rsp)
jne 0xd5a4
movl 0x14(%rbx), %esi
cmpl %esi, %r15d
jne 0xd5a4
movl 0x2c(%rsp), %eax
cmpl $0x20000000, %eax       # imm = 0x20000000
jl 0xd594
movl 0x28(%rbx), %edx
incl %edx
movq 0x8(%rbx), %rcx
leaq 0xc916(%rip), %rdi      # 0x19ea1
xorl %eax, %eax
callq 0xb8d7
jmp 0xd5a4
addl %eax, %eax
movq %rbx, %rdi
movl %eax, %esi
callq 0xd034
movq 0x40(%rbx), %r13
testb %r12b, %r12b
jne 0xd3a0
jmp 0xd60f
cmpb $0x0, 0x34(%rbx)
je 0xd5d2
movslq %ebp, %rax
movb $0x0, -0x1(%rax,%r13)
cmpl $0x2, 0x38(%rbx)
jne 0xd5d2
cmpb $0xd, -0x2(%rax,%r13)
jne 0xd5d2
movb $0x0, -0x2(%r13,%rax)
movslq 0x30(%rbx), %rax
movl %eax, 0x2c(%rbx)
movl %ebp, 0x30(%rbx)
incl 0x28(%rbx)
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
je 0xd5ed
subl %eax, %ebp
movl %ebp, (%rcx)
leaq (%rax,%r13), %rsi
movq 0x30(%rsp), %rcx
movq %rsi, (%rcx)
movl $0x1, %r14d
cmpb $0x23, (%r13,%rax)
jne 0xd60f
movq %rbx, %rdi
jmp 0xd2cc
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq