// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rdi, %rbx
movq 0x40(%rdi), %r14
movl 0x30(%rdi), %r13d
cmpb $0x0, 0x3c(%rdi)
je 0xd0a1
movb $0x0, 0x3c(%rbx)
testq %rdx, %rdx
je 0xd072
subl 0x2c(%rbx), %r13d
movl %r13d, (%rdx)
movslq 0x2c(%rbx), %rax
addq %rax, %r14
movq %r14, (%rsi)
movl $0x1, %ebp
cmpq $0x0, 0x50(%rbx)
je 0xd31e
cmpb $0x23, (%r14)
jne 0xd31e
movq %rbx, %rdi
movq %r14, %rsi
jmp 0xd319
movq 0x90(%rbx), %rax
testq %rax, %rax
je 0xd0c0
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movq %rdx, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movl 0x20(%rbx), %ebp
movslq %r13d, %rsi
addq %r14, %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0xd3e1
movl 0x38(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0xd109
movl 0x30(%rbx), %r13d
cmpl %ebp, %r13d
jge 0xd133
movslq %r13d, %r12
cmpb $0xa, (%r14,%r12)
je 0xd2b8
incq %r12
cmpl %r12d, %ebp
jne 0xd0f4
jmp 0xd130
cmpl $0x3, %eax
jne 0xd133
movl 0x30(%rbx), %r13d
cmpl %ebp, %r13d
jge 0xd133
movslq %ebp, %rax
movslq %r13d, %r12
cmpb $0xd, (%r14,%r12)
je 0xd2b8
incq %r12
cmpq %r12, %rax
jne 0xd11d
movl %ebp, %r13d
movq %r14, 0x28(%rsp)
movslq 0x30(%rbx), %r15
movl %ebp, %ecx
subl %r15d, %ecx
movl 0x14(%rbx), %eax
testq %r15, %r15
movl %eax, (%rsp)
movq %rcx, 0x10(%rsp)
jle 0xd16b
testl %ecx, %ecx
jle 0xd16b
leaq (%r14,%r15), %rsi
movl %ecx, %edx
movq %r14, %rdi
callq 0x35d0
movq 0x10(%rsp), %rcx
movl (%rsp), %eax
movq %r15, 0x8(%rsp)
addq %r15, 0x18(%rbx)
movl 0x10(%rbx), %ebp
movslq %ecx, %r12
leaq (%r14,%r12), %rdx
xorl %r15d, %r15d
testl %ebp, %ebp
movl %r13d, 0x4(%rsp)
js 0xd1d5
movl %eax, %r14d
subl %ecx, %r14d
testl %r14d, %r14d
jle 0xd1d5
xorl %r15d, %r15d
movq %rdx, 0x30(%rsp)
movq %rdx, %r13
movl %r14d, %edx
movl %ebp, %edi
movq %r13, %rsi
callq 0x33d0
testl %eax, %eax
jle 0xd1c6
addl %eax, %r15d
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, %r13
subl %eax, %r14d
jg 0xd1a0
jmp 0xd1ee
movl 0x4(%rsp), %r13d
movq 0x10(%rsp), %rcx
movq 0x30(%rsp), %rdx
testl %r15d, %r15d
jne 0xd1e5
cmpl 0x8(%rsp), %r13d
jg 0xd32f
testl %r15d, %r15d
je 0xd365
movq 0x10(%rsp), %rbp
leal (%r15,%rbp), %edx
movl %edx, 0x20(%rbx)
movl $0x0, 0x30(%rbx)
movl 0x4(%rsp), %r13d
movslq %r13d, %rsi
movq 0x28(%rsp), %r14
addq %r14, %rsi
movq %rbx, %rdi
movl %edx, 0x8(%rsp)
callq 0xd3e1
movl 0x38(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0xd248
movl %ebp, %r13d
testl %r15d, %r15d
movl 0x8(%rsp), %ebp
jle 0xd273
movslq %ebp, %rax
cmpb $0xa, (%r14,%r12)
je 0xd2b8
incq %r12
cmpq %rax, %r12
jl 0xd237
jmp 0xd270
cmpl $0x3, %eax
movl 0x8(%rsp), %ebp
jne 0xd273
movq 0x10(%rsp), %rax
movl %eax, %r13d
testl %r15d, %r15d
jle 0xd273
movslq %ebp, %rax
cmpb $0xd, (%r14,%r12)
je 0xd2b8
incq %r12
cmpq %rax, %r12
jl 0xd261
movl %r12d, %r13d
cmpl 0x14(%rbx), %ebp
jne 0xd138
movl (%rsp), %esi
cmpl $0x20000000, %esi       # imm = 0x20000000
jl 0xd2a5
movl 0x28(%rbx), %edx
incl %edx
movq 0x8(%rbx), %rcx
leaq 0xcc1a(%rip), %rdi      # 0x19eb1
movl %ebp, %esi
xorl %eax, %eax
callq 0xb6c8
jmp 0xd138
addl %esi, %esi
movq %rbx, %rdi
callq 0xce03
movq 0x40(%rbx), %r14
jmp 0xd133
leal 0x1(%r12), %eax
cmpb $0x0, 0x34(%rbx)
movq 0x18(%rsp), %rdx
je 0xd2e8
movslq %r12d, %rcx
movb $0x0, (%r14,%rcx)
cmpl $0x2, 0x38(%rbx)
jne 0xd2e8
decl %r12d
movslq %r12d, %rcx
cmpb $0xd, (%r14,%rcx)
jne 0xd2e8
movb $0x0, (%r14,%rcx)
movslq 0x30(%rbx), %rcx
movl %ecx, 0x2c(%rbx)
movl %eax, 0x30(%rbx)
incl 0x28(%rbx)
testq %rdx, %rdx
je 0xd2fe
subl %ecx, %eax
movl %eax, (%rdx)
leaq (%r14,%rcx), %rsi
movq 0x20(%rsp), %rax
movq %rsi, (%rax)
movl $0x1, %ebp
cmpb $0x23, (%r14,%rcx)
jne 0xd31e
movq %rbx, %rdi
callq 0xd378
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, (%rdx)
xorl %eax, %eax
movl %eax, 0x2c(%rbx)
movl %eax, 0x20(%rbx)
movl %ecx, 0x30(%rbx)
incl 0x28(%rbx)
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0xd34c
movl %ecx, (%rax)
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rax
movq %rsi, (%rax)
movl $0x1, %ebp
cmpb $0x23, (%rsi)
je 0xd316
jmp 0xd31e
movq $0x0, 0x2c(%rbx)
movl $0x0, 0x20(%rbx)
xorl %ebp, %ebp
jmp 0xd31e