// File: /workspace/github_c_makefile_star2_O3/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, %rbp
movb 0x29(%rdi), %bl
movl %esi, 0xc(%rsp)
movl %ecx, 0x8(%rsp)
movl 0x54(%rdi), %edi
movl 0x58(%rbp), %esi
callq 0x2394c
testl %eax, %eax
jle 0x38d87
cmpb $0x2d, %bl
jne 0x38c0c
movl 0x50(%rbp), %edx
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x25472
movslq 0x5c(%rbp), %r12
xorl %r13d, %r13d
testq %r12, %r12
jle 0x38d87
leaq 0x28(%rbp), %r15
xorl %ebx, %ebx
movq 0x60(%rbp), %rax
movq 0x70(%rbp), %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 0x38c22
testl %r13d, %r13d
je 0x38d87
movl $0x88, %edi
callq 0x2dff4
movq %rax, %rbx
leaq 0x28(%rax), %rdi
movq %r15, %rsi
callq 0x6680
movq 0x30(%rbp), %rdi
callq 0x1feba
movq %rax, 0x30(%rbx)
movl 0x38(%rbp), %eax
movl %eax, 0x38(%rbx)
movq 0x48(%rbp), %rdi
callq 0x1feba
movq %rax, 0x48(%rbx)
movl 0x50(%rbp), %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 0x38d78
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %rbp, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq 0x60(%rbp), %rax
movq 0x68(%rbp), %rcx
movl (%rax,%r13,4), %r12d
movq 0x70(%rbp), %rax
movl (%rax,%r13,4), %r15d
leal (%r15,%r12), %ebp
movl (%rcx,%r13,4), %ebx
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movl %r15d, %edi
movl %ebp, %esi
callq 0x2394c
testl %eax, %eax
jle 0x38d62
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 %ebx, (%rax,%r14,4)
movq 0x70(%rcx), %rax
movl %r15d, (%rax,%r14,4)
movq 0x60(%rcx), %rax
movl %r12d, (%rax,%r14,4)
incl %r14d
incq %r13
movq 0x18(%rsp), %r12
cmpq %r13, %r12
movq 0x20(%rsp), %rbp
jne 0x38ce6
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x38b23
jmp 0x38d89
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq