// File: /workspace/github_c_makefile_star2_O1/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 0x23630
testl %eax, %eax
jle 0x38a4f
cmpb $0x2d, %bl
jne 0x388c9
movl 0x50(%r12), %edx
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x25145
movslq 0x5c(%r12), %rbx
testq %rbx, %rbx
jle 0x3890c
xorl %r14d, %r14d
xorl %ebp, %ebp
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 0x23630
xorl %ecx, %ecx
testl %eax, %eax
setg %cl
addl %ecx, %ebp
incq %r14
cmpq %r14, %rbx
jne 0x388d8
jmp 0x3890e
xorl %ebp, %ebp
testl %ebp, %ebp
je 0x38a4f
leaq 0x28(%r12), %r15
movl $0x88, %edi
callq 0x2dc95
movq %rax, %r14
leaq 0x28(%rax), %rdi
movq %r15, %rsi
callq 0x6680
movq 0x30(%r12), %rdi
callq 0x1f9f6
movq %rax, 0x30(%r14)
movl 0x38(%r12), %eax
movl %eax, 0x38(%r14)
movq 0x48(%r12), %rdi
callq 0x1f9f6
movq %rax, 0x48(%r14)
movq %r12, 0x20(%rsp)
movl 0x50(%r12), %eax
movl %eax, 0x50(%r14)
movl %ebp, 0x5c(%r14)
movslq %ebp, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x2db04
movq %rax, 0x60(%r14)
movq %r15, %rdi
callq 0x2db04
movq %rax, 0x68(%r14)
movq %r15, %rdi
callq 0x2db04
movq %r14, 0x10(%rsp)
movq %rax, 0x70(%r14)
testl %ebx, %ebx
jle 0x38a40
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %rbx, 0x18(%rsp)
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 0x23630
testl %eax, %eax
jle 0x38a2f
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
cmpq %r13, %rbx
jne 0x389ae
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x387f3
jmp 0x38a51
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq