// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/lib/chainBlock.c
// Function: chainBlocks
struct chain *chainBlocks(
	char *qName, int qSize, char qStrand,	/* Info on query sequence */
	char *tName, int tSize, 		/* Info on target. */
	struct cBlock **pBlockList, 		/* Unordered ungapped alignments. */
	ConnectCost connectCost, 		/* Calculate cost to connect nodes. */
	GapCost gapCost, 			/* Cost for non-overlapping nodes. */
	void *gapData, 				/* Passed through to connect/gapCosts */
	FILE *details)				/* NULL except for debugging */
/* Create list of chains from list of blocks.  The blockList will get
 * eaten up as the blocks are moved from the list to the chain. 
 * The list of chains returned is sorted by score. 
 *
 * The details FILE may be NULL, and is where additional information
 * about the chaining is put.
 *
 * Note that the connectCost needs to adjust for possibly partially 
 * overlapping blocks, and that these need to be taken out of the
 * resulting chains in general.  This can get fairly complex.  Also
 * the chains will need some cleanup at the end.  Use the chainConnect
 * module to help with this.  See hg/mouseStuff/axtChain for example usage. */
{
struct kdTree *tree;
struct kdLeaf *leafList = NULL, *leaf;
struct cBlock *block;
struct chain *chainList = NULL, *chain;
struct lm *lm;

/* Empty lists will be problematic later, so deal with them here. */
if (*pBlockList == NULL)
   return NULL;

/* Make a leaf for each block. */
lm = lmInit(0);  /* Memory for tree, branches and leaves. */
for (block = *pBlockList; block != NULL; block = block->next)
    {
    /* Watch out for 0-length blocks in input: */
    if (block->tStart == block->tEnd)
	continue;
    lmAllocVar(lm, leaf);
    leaf->cb = block;
    leaf->totalScore = block->score;
    slAddHead(&leafList, leaf);
    }

/* Figure out chains. */
slSort(&leafList, kdLeafCmpT);
tree = kdTreeMake(leafList, lm);
findBestPredecessors(tree, leafList, connectCost, gapCost, gapData);
slSort(&leafList, kdLeafCmpTotal);
chainList = peelChains(qName, qSize, qStrand, tName, tSize, leafList, details);

/* Rescore chains (since some truncated) */
for (chain = chainList; chain != NULL; chain = chain->next)
    chain->score = scoreBlocks(chain->blockList, connectCost, gapData);
slSort(&chainList,  chainCmpScore);

/* Clean up and go home. */
lmCleanup(&lm);
*pBlockList = NULL;
return chainList;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %r8d, 0x20(%rsp)
movq %rcx, 0x80(%rsp)
movl %edx, 0x34(%rsp)
movl %esi, 0x30(%rsp)
movq %rdi, 0x78(%rsp)
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x48(%rsp)
cmpq %rax, (%r9)
je 0x1f5bf
movq %r9, %rbx
xorl %edi, %edi
callq 0x2bea4
movq %rax, 0x28(%rsp)
movq %rbx, 0x70(%rsp)
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x1f1ff
movl 0x8(%rbx), %eax
cmpl 0xc(%rbx), %eax
je 0x1f1c0
movq 0x28(%rsp), %rdi
movl $0x28, %esi
callq 0x2bfa9
movq %rbx, 0x8(%rax)
xorps %xmm0, %xmm0
cvtsi2sdl 0x18(%rbx), %xmm0
movsd %xmm0, 0x18(%rax)
movq 0x18(%rsp), %rcx
movq %rcx, (%rax)
movq %rax, 0x18(%rsp)
jmp 0x1f1c0
leaq 0x3cb(%rip), %rsi       # 0x1f5d1
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x1fd04
movq (%rbx), %rbx
movq 0x28(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %rbx, %rdi
callq 0x1fbec
movl %eax, %r15d
leaq 0x50(%rsp), %rdi
callq 0x245d4
leaq 0x88(%rsp), %rdi
callq 0x245d4
movslq %r15d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r14
movq %r14, %rdi
callq 0x2db04
movq %rax, 0x40(%rsp)
movq %r14, %rdi
callq 0x2db04
movq %rax, 0x38(%rsp)
testq %rbx, %rbx
je 0x1f2b2
leaq 0x50(%rsp), %r14
leaq 0x88(%rsp), %rbp
movq 0x38(%rsp), %r13
movq 0x40(%rsp), %r12
movq %rbx, 0x10(%r13)
movq %rbx, 0x10(%r12)
movq %r14, %rdi
movq %r12, %rsi
callq 0x246d9
movq %rbp, %rdi
movq %r13, %rsi
callq 0x246d9
movq (%rbx), %rbx
addq $0x18, %r12
addq $0x18, %r13
testq %rbx, %rbx
jne 0x1f283
movq 0x100(%rsp), %rbx
movq 0xf0(%rsp), %rbp
leaq 0x347(%rip), %rsi       # 0x1f610
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x24872
leaq 0xa0(%rsp), %r12
movq %r14, (%r12)
leaq 0x88(%rsp), %rax
movq %rax, 0x8(%r12)
movl $0x8, %esi
movq 0x68(%rsp), %r13
movq %r13, %rdi
callq 0x2bfa9
movq %rax, %r14
movl %r15d, %edi
movq %r12, %rsi
xorl %edx, %edx
movq %r13, %rcx
callq 0x1f625
movq %rax, (%r14)
movq 0x40(%rsp), %rdi
callq 0x2dd41
movq 0x38(%rsp), %rdi
callq 0x2dd41
movq 0x18(%rsp), %r15
testq %r15, %r15
je 0x1f388
movq 0xf8(%rsp), %r12
movq (%r14), %r9
movupd 0xa20e0(%rip), %xmm0    # 0xc1428
movupd %xmm0, (%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x1f7a5
ucomisd 0x18(%r15), %xmm0
jbe 0x1f373
movsd %xmm0, 0x18(%r15)
movq %rax, 0x10(%r15)
movq (%r14), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1f913
movq (%r15), %r15
testq %r15, %r15
jne 0x1f33d
leaq 0x257(%rip), %rsi       # 0x1f5e6
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x1fd04
movq (%r14), %r12
movq $0x0, 0x50(%rsp)
testq %r12, %r12
je 0x1f515
movq 0x108(%rsp), %r14
movq %r12, %rax
movb $0x0, 0x20(%rax)
movq (%rax), %rax
testq %rax, %rax
jne 0x1f3bc
cmpb $0x0, 0x20(%r12)
jne 0x1f508
movl $0x50, %edi
callq 0x2dc95
movq %rax, %r13
movq 0x78(%rsp), %rdi
callq 0x1f9f6
movq %rax, 0x30(%r13)
movl 0x30(%rsp), %eax
movl %eax, 0x38(%r13)
movl 0x34(%rsp), %eax
movb %al, 0x3c(%r13)
movq 0x80(%rsp), %rdi
callq 0x1f9f6
movq %rax, 0x18(%r13)
movl 0x20(%rsp), %eax
movl %eax, 0x20(%r13)
movq 0x8(%r12), %rax
movl 0x14(%rax), %ecx
movl %ecx, 0x44(%r13)
movl 0xc(%rax), %eax
movl %eax, 0x28(%r13)
testq %r14, %r14
je 0x1f460
movsd 0x18(%r12), %xmm0
movsd %xmm0, 0x10(%r13)
movl $0xffffffff, 0x48(%r13) # imm = 0xFFFFFFFF
movq %r13, %rdi
movq %r14, %rsi
callq 0x1e5a8
movq $0x0, 0x10(%r13)
movl $0x0, 0x48(%r13)
movq 0x50(%rsp), %rax
movq %rax, (%r13)
movq %r13, 0x50(%rsp)
movq %r12, %r15
movb $0x1, 0x20(%r15)
movq 0x8(%r15), %rax
movq 0x8(%r13), %rcx
movq %rcx, (%rax)
movq %rax, 0x8(%r13)
movq 0x8(%r15), %rax
movl 0x10(%rax), %r8d
movl %r8d, 0x40(%r13)
movl 0x8(%rax), %ecx
movl %ecx, 0x24(%r13)
testq %r14, %r14
je 0x1f4be
movl 0x14(%rax), %r9d
movl 0x18(%rax), %edx
movsd 0x18(%r15), %xmm0
subl %r8d, %r9d
movq %r14, %rdi
leaq 0x70877(%rip), %rsi     # 0x8fd2e
movb $0x1, %al
callq 0x6a90
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x1f508
testq %r14, %r14
je 0x1f4f5
movq 0x8(%r15), %rcx
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movl 0x8(%rcx), %edx
movl 0x10(%rcx), %ecx
subl 0xc(%rax), %edx
subl 0x14(%rax), %ecx
movq %r14, %rdi
leaq 0x70850(%rip), %rsi     # 0x8fd3e
xorl %eax, %eax
callq 0x6a90
movq 0x10(%r15), %rax
movq 0x10(%rax), %r15
cmpb $0x0, 0x20(%r15)
je 0x1f471
movq (%r12), %r12
testq %r12, %r12
jne 0x1f3c8
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x1fcb3
movq (%r14), %r15
movq %r15, 0x48(%rsp)
testq %r15, %r15
je 0x1f592
movq 0x8(%r15), %r14
testq %r14, %r14
je 0x1f583
xorpd %xmm0, %xmm0
xorl %edi, %edi
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%r14), %xmm1
addsd %xmm0, %xmm1
testq %rdi, %rdi
je 0x1f56f
movq %r14, %rsi
movq %rbx, %rdx
movsd %xmm1, 0x20(%rsp)
callq *%rbp
movsd 0x20(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
subsd %xmm0, %xmm1
movq %r14, %rdi
movq (%r14), %rax
movapd %xmm1, %xmm0
movq %rax, %r14
testq %rax, %rax
jne 0x1f53e
jmp 0x1f587
xorpd %xmm1, %xmm1
movsd %xmm1, 0x10(%r15)
movq (%r15), %r15
jmp 0x1f52a
leaq -0x10be(%rip), %rsi     # 0x1e4db
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
callq 0x1fd04
leaq 0x28(%rsp), %rdi
callq 0x2bf4b
movq 0x70(%rsp), %rax
movq $0x0, (%rax)
movq (%rbx), %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq