// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/lib/psl.c
// Function: pslTrimToTargetRange
struct psl *pslTrimToTargetRange(struct psl *oldPsl, int tMin, int tMax)
/* Return psl trimmed to fit inside tMin/tMax.  Note this does not
 * update the match/misMatch and related fields. */
{
int newSize;
int oldBlockCount = oldPsl->blockCount;
boolean tIsRc = (oldPsl->strand[1] == '-');
int newBlockCount = 0, completeBlockCount = 0;
int i;
struct psl *newPsl = NULL;
int tMn = tMin, tMx = tMax;   /* tMin/tMax adjusted for strand. */

/* Deal with case where we're completely trimmed out quickly. */
newSize = rangeIntersection(oldPsl->tStart, oldPsl->tEnd, tMin, tMax);
if (newSize <= 0)
    return NULL;

if (tIsRc)
    reverseIntRange(&tMn, &tMx, oldPsl->tSize);

/* Count how many blocks will survive trimming. */
oldBlockCount = oldPsl->blockCount;
for (i=0; i<oldBlockCount; ++i)
    {
    int s = oldPsl->tStarts[i];
    int e = s + oldPsl->blockSizes[i];
    int sz = e - s;
    int overlap;
    if ((overlap = rangeIntersection(s, e, tMn, tMx)) > 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 tS = oldPsl->tStarts[i];
    int tE = tS + sz;
    int qS = oldPsl->qStarts[i];
    int qE = qS + sz;
    if (rangeIntersection(tS, tE, tMn, tMx) > 0)
        {
	int diff;
	if ((diff = (tMn - tS)) > 0)
	    {
	    tS += diff;
	    qS += diff;
	    sz -= diff;
	    }
	if ((diff = (tE - tMx)) > 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, %r12
movb 0x29(%rdi), %bl
movl %esi, 0xc(%rsp)
movl %ecx, 0x8(%rsp)
movl 0x54(%rdi), %edi
movl 0x58(%r12), %esi
callq 0x1f506
testl %eax, %eax
jle 0x330f8
cmpb $0x2d, %bl
jne 0x32f85
movl 0x50(%r12), %edx
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x20d61
leaq 0x28(%r12), %r15
movl 0x5c(%r12), %ebx
xorl %r14d, %r14d
testl %ebx, %ebx
cmovlel %r14d, %ebx
xorl %ebp, %ebp
cmpq %r14, %rbx
je 0x32fce
movq 0x60(%r12), %rax
movq 0x70(%r12), %rcx
movl (%rcx,%r14,4), %edi
movl (%rax,%r14,4), %esi
addl %edi, %esi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
callq 0x1f506
xorl %ecx, %ecx
testl %eax, %eax
setg %cl
addl %ecx, %ebp
incq %r14
jmp 0x32f9a
testl %ebp, %ebp
je 0x330f8
movl $0x88, %edi
callq 0x28bb0
movq %rax, %r14
leaq 0x28(%rax), %rdi
movq %r15, %rsi
callq 0x6680
movq 0x30(%r12), %rdi
callq 0x1c306
movq %rax, 0x30(%r14)
movl 0x38(%r12), %eax
movl %eax, 0x38(%r14)
movq 0x48(%r12), %rdi
callq 0x1c306
movq %rax, 0x48(%r14)
movq %r12, 0x20(%rsp)
movl 0x50(%r12), %eax
movl %eax, 0x50(%r14)
movl %ebp, 0x5c(%r14)
movl %ebp, %r15d
shlq $0x2, %r15
movq %r15, %rdi
callq 0x28a1f
movq %rax, 0x60(%r14)
movq %r15, %rdi
callq 0x28a1f
movq %rax, 0x68(%r14)
movq %r15, %rdi
callq 0x28a1f
movq %r14, 0x10(%rsp)
movq %rax, 0x70(%r14)
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %rbx, 0x18(%rsp)
cmpq %r13, %rbx
je 0x330fc
movq 0x20(%rsp), %rdx
movq 0x60(%rdx), %rax
movq 0x68(%rdx), %rcx
movl (%rax,%r13,4), %r14d
movq 0x70(%rdx), %rax
movl (%rax,%r13,4), %r15d
leal (%r15,%r14), %ebp
movl (%rcx,%r13,4), %ebx
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movl %r15d, %edi
movl %ebp, %esi
callq 0x1f506
testl %eax, %eax
jle 0x330eb
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, %r14d
movq 0x10(%rsp), %rcx
movq 0x68(%rcx), %rax
movslq %r12d, %r12
movl %ebx, (%rax,%r12,4)
movq 0x70(%rcx), %rax
movl %r15d, (%rax,%r12,4)
movq 0x60(%rcx), %rax
movl %r14d, (%rax,%r12,4)
incl %r12d
incq %r13
movq 0x18(%rsp), %rbx
jmp 0x33061
xorl %ebx, %ebx
jmp 0x33109
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x32e98
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq