// File: /workspace/github_c_makefile_star2_O3/icebert[P]pblat/lib/psl.c
// Function: pslTrimToQueryRange
struct psl *pslTrimToQueryRange(struct psl *oldPsl, int qMin, int qMax)
/* Return psl trimmed to fit inside qMin/qMax.  Note this does not
 * update the match/misMatch and related fields. */
{
int newSize;
int oldBlockCount = oldPsl->blockCount;
boolean qIsRc = (oldPsl->strand[0] == '-');
int newBlockCount = 0, completeBlockCount = 0;
int i;
struct psl *newPsl = NULL;
int qMn = qMin, qMx = qMax;   /* qMin/qMax adjusted for strand. */

/* Deal with case where we're completely trimmed out quickly. */
newSize = rangeIntersection(oldPsl->qStart, oldPsl->qEnd, qMin, qMax);
if (newSize <= 0)
    return NULL;

if (qIsRc)
    reverseIntRange(&qMn, &qMx, oldPsl->qSize);

/* Count how many blocks will survive trimming. */
oldBlockCount = oldPsl->blockCount;
for (i=0; i<oldBlockCount; ++i)
    {
    int s = oldPsl->qStarts[i];
    int e = s + oldPsl->blockSizes[i];
    int sz = e - s;
    int overlap;
    if ((overlap = rangeIntersection(s, e, qMn, qMx)) > 0)
        ++newBlockCount;
    if (overlap == sz)
        ++completeBlockCount;
    }

if (newBlockCount == 0)
    return NULL;

/* Allocate new psl and fill in what we already know. */
AllocVar(newPsl);
strcpy(newPsl->strand, oldPsl->strand);
newPsl->qName = cloneString(oldPsl->qName);
newPsl->qSize = oldPsl->qSize;
newPsl->tName = cloneString(oldPsl->tName);
newPsl->tSize = oldPsl->tSize;
newPsl->blockCount = newBlockCount;
AllocArray(newPsl->blockSizes, newBlockCount);
AllocArray(newPsl->qStarts, newBlockCount);
AllocArray(newPsl->tStarts, newBlockCount);

/* Fill in blockSizes, qStarts, tStarts with real data. */
newBlockCount = completeBlockCount = 0;
for (i=0; i<oldBlockCount; ++i)
    {
    int oldSz = oldPsl->blockSizes[i];
    int sz = oldSz;
    int qS = oldPsl->qStarts[i];
    int qE = qS + sz;
    int tS = oldPsl->tStarts[i];
    int tE = tS + sz;
    if (rangeIntersection(qS, qE, qMn, qMx) > 0)
        {
	int diff;
	if ((diff = (qMn - qS)) > 0)
	    {
	    tS += diff;
	    qS += diff;
	    sz -= diff;
	    }
	if ((diff = (qE - qMx)) > 0)
	    {
	    tE -= diff;
	    qE -= diff;
	    sz -= diff;
	    }
	newPsl->qStarts[newBlockCount] = qS;
	newPsl->tStarts[newBlockCount] = tS;
	newPsl->blockSizes[newBlockCount] = sz;
	++newBlockCount;
	if (sz == oldSz)
	    ++completeBlockCount;
	}
    }
pslRecalcBounds(newPsl);
return newPsl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ecx
movl %esi, %edx
movq %rdi, %r14
movb 0x28(%rdi), %bl
movl %esi, 0xc(%rsp)
movl %ecx, 0x8(%rsp)
movl 0x3c(%rdi), %edi
movl 0x40(%r14), %esi
callq 0x2394c
testl %eax, %eax
jle 0x38f64
cmpb $0x2d, %bl
jne 0x38de7
movl 0x38(%r14), %edx
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x25472
movslq 0x5c(%r14), %r12
xorl %r13d, %r13d
testq %r12, %r12
jle 0x38f64
leaq 0x28(%r14), %r15
xorl %ebx, %ebx
movq 0x60(%r14), %rax
movq 0x68(%r14), %rcx
movl (%rcx,%rbx,4), %edi
movl (%rax,%rbx,4), %esi
addl %edi, %esi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
callq 0x2394c
xorl %ecx, %ecx
testl %eax, %eax
setg %cl
addl %ecx, %r13d
incq %rbx
cmpq %rbx, %r12
jne 0x38dfd
testl %r13d, %r13d
je 0x38f64
movl $0x88, %edi
callq 0x2dff4
movq %rax, %rbx
leaq 0x28(%rax), %rdi
movq %r15, %rsi
callq 0x6680
movq 0x30(%r14), %rdi
callq 0x1feba
movq %rax, 0x30(%rbx)
movl 0x38(%r14), %eax
movl %eax, 0x38(%rbx)
movq 0x48(%r14), %rdi
callq 0x1feba
movq %rax, 0x48(%rbx)
movq %r14, 0x20(%rsp)
movl 0x50(%r14), %eax
movl %eax, 0x50(%rbx)
movl %r13d, 0x5c(%rbx)
movl %r13d, %r15d
shlq $0x2, %r15
movq %r15, %rdi
callq 0x2de63
movq %rax, 0x60(%rbx)
movq %r15, %rdi
callq 0x2de63
movq %rax, 0x68(%rbx)
movq %r15, %rdi
callq 0x2de63
movq %rbx, 0x10(%rsp)
movq %rax, 0x70(%rbx)
testl %r12d, %r12d
jle 0x38f55
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %r12, 0x18(%rsp)
movq 0x20(%rsp), %rdx
movq 0x60(%rdx), %rax
movq 0x68(%rdx), %rcx
movl (%rax,%r13,4), %r12d
movl (%rcx,%r13,4), %r15d
leal (%r15,%r12), %ebp
movq 0x70(%rdx), %rax
movl (%rax,%r13,4), %ebx
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movl %r15d, %edi
movl %ebp, %esi
callq 0x2394c
testl %eax, %eax
jle 0x38f44
movl 0xc(%rsp), %eax
movl %eax, %ecx
subl %r15d, %ecx
testl %ecx, %ecx
movl $0x0, %edx
cmovlel %edx, %ecx
cmovgl %eax, %r15d
addl %ecx, %ebx
subl 0x8(%rsp), %ebp
testl %ebp, %ebp
cmovlel %edx, %ebp
addl %ecx, %ebp
subl %ebp, %r12d
movq 0x10(%rsp), %rcx
movq 0x68(%rcx), %rax
movslq %r14d, %r14
movl %r15d, (%rax,%r14,4)
movq 0x70(%rcx), %rax
movl %ebx, (%rax,%r14,4)
movq 0x60(%rcx), %rax
movl %r12d, (%rax,%r14,4)
incl %r14d
incq %r13
movq 0x18(%rsp), %r12
cmpq %r13, %r12
jne 0x38ec3
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x38b23
jmp 0x38f66
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq