// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rdi, %rbx
movq 0x40(%rdi), %r14
movl 0x30(%rdi), %r12d
cmpb $0x0, 0x3c(%rdi)
je 0xbd4f
movb $0x0, 0x3c(%rbx)
testq %rdx, %rdx
je 0xbd19
subl 0x2c(%rbx), %r12d
movl %r12d, (%rdx)
movslq 0x2c(%rbx), %rax
addq %rax, %r14
movq %r14, (%rsi)
pushq $0x1
popq %rbp
cmpq $0x0, 0x50(%rbx)
je 0xbd3e
cmpb $0x23, (%r14)
jne 0xbd3e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbf9d
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x90(%rbx), %rax
testq %rax, %rax
je 0xbd6e
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movq %rdx, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movl 0x20(%rbx), %eax
movl %eax, %ebp
movslq %eax, %r15
movslq %r12d, %rsi
addq %r14, %rsi
movq %rbx, %rdi
movl %r15d, %edx
callq 0xc003
movl 0x38(%rbx), %eax
movb $0x1, %r13b
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0xbdb6
movslq 0x30(%rbx), %r12
movl %ebp, %esi
cmpq %r15, %r12
jge 0xbdf1
cmpb $0xa, (%r14,%r12)
je 0xbdd6
incq %r12
jmp 0xbda5
cmpl $0x3, %eax
jne 0xbdd2
movslq 0x30(%rbx), %r12
movl %ebp, %esi
cmpq %r15, %r12
jge 0xbdf1
cmpb $0xd, (%r14,%r12)
je 0xbdd6
incq %r12
jmp 0xbdc1
movl %ebp, %esi
jmp 0xbdf1
incl %r12d
xorl %r13d, %r13d
jmp 0xbdf1
addl %esi, %esi
movq %rbx, %rdi
callq 0xbac3
movl %ebp, %esi
movq 0x40(%rbx), %r14
movb $0x1, %r13b
movslq %r12d, %r15
testb $0x1, %r13b
je 0xbef9
movslq 0x30(%rbx), %rbp
movl %esi, %r13d
subl %ebp, %r13d
movl 0x14(%rbx), %edx
testq %rbp, %rbp
movl %edx, 0x4(%rsp)
jle 0xbe2c
testl %r13d, %r13d
jle 0xbe2c
leaq (%r14,%rbp), %rsi
movl %r13d, %edx
movq %r14, %rdi
callq 0x35d0
movl 0x4(%rsp), %edx
addq %rbp, 0x18(%rbx)
movl 0x10(%rbx), %edi
testl %edi, %edi
js 0xbe47
subl %r13d, %edx
movslq %r13d, %rsi
addq %r14, %rsi
callq 0xbca6
jmp 0xbe49
xorl %eax, %eax
testl %eax, %eax
jne 0xbe56
cmpl %ebp, %r12d
jg 0xbf59
testl %eax, %eax
je 0xbf8d
addl %r13d, %eax
movl %eax, 0x20(%rbx)
andl $0x0, 0x30(%rbx)
addq %r14, %r15
movq %rbx, %rdi
movl %eax, %ebp
movq %r15, %rsi
movl %eax, %edx
callq 0xc003
movl %ebp, %esi
movl 0x38(%rbx), %ecx
leal -0x1(%rcx), %edx
movslq %ebp, %rax
cmpl $0x2, %edx
jae 0xbea2
movslq %r13d, %r12
cmpq %rax, %r12
jge 0xbebf
cmpb $0xa, (%r14,%r12)
je 0xbdd6
incq %r12
jmp 0xbe8d
cmpl $0x3, %ecx
jne 0xbebf
movslq %r13d, %r12
cmpq %rax, %r12
jge 0xbebf
cmpb $0xd, (%r14,%r12)
je 0xbdd6
incq %r12
jmp 0xbeaa
movb $0x1, %r13b
cmpl 0x14(%rbx), %esi
jne 0xbdf1
movl 0x4(%rsp), %esi
cmpl $0x20000000, %esi       # imm = 0x20000000
jl 0xbdde
movl 0x28(%rbx), %edx
incl %edx
movq 0x8(%rbx), %rcx
leaq 0xbfc6(%rip), %rdi      # 0x17eb1
movl %ebp, %esi
xorl %eax, %eax
callq 0xa61f
jmp 0xbdd2
cmpb $0x0, 0x34(%rbx)
je 0xbf19
movb $0x0, -0x1(%r15,%r14)
cmpl $0x2, 0x38(%rbx)
jne 0xbf19
cmpb $0xd, -0x2(%r15,%r14)
jne 0xbf19
movb $0x0, -0x2(%r14,%r15)
movslq 0x30(%rbx), %rax
movl %eax, 0x2c(%rbx)
movl %r12d, 0x30(%rbx)
incl 0x28(%rbx)
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0xbf37
subl %eax, %r12d
movl %r12d, (%rcx)
leaq (%r14,%rax), %rsi
movq 0x10(%rsp), %rcx
movq %rsi, (%rcx)
pushq $0x1
popq %rbp
cmpb $0x23, (%r14,%rax)
jne 0xbd3e
movq %rbx, %rdi
jmp 0xbd39
movslq %r13d, %rax
movb $0x0, (%r14,%rax)
andl $0x0, 0x2c(%rbx)
andl $0x0, 0x20(%rbx)
movl %r13d, 0x30(%rbx)
incl 0x28(%rbx)
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0xbf7d
movl %r13d, (%rax)
movq 0x10(%rsp), %rax
movq %r14, (%rax)
pushq $0x1
popq %rbp
jmp 0xbd2d
andq $0x0, 0x2c(%rbx)
andl $0x0, 0x20(%rbx)
xorl %ebp, %ebp
jmp 0xbd3e