// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
movq %rcx, 0x78(%rsp)
movl %edx, 0x34(%rsp)
andq $0x0, 0x20(%rsp)
movl %esi, 0x30(%rsp)
movq %rdi, 0x70(%rsp)
cmpq $0x0, (%r9)
je 0x1beba
movq %r9, %rbp
movq 0x100(%rsp), %r14
movq 0xf0(%rsp), %r15
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x26f00
movq %rax, 0x38(%rsp)
movq %rax, 0x80(%rsp)
pushq $0x28
popq %rbx
movq %rbp, %r13
movq (%r13), %r13
testq %r13, %r13
je 0x1bb2c
movl 0x8(%r13), %eax
cmpl 0xc(%r13), %eax
je 0x1baf0
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x26ff4
movq %r13, 0x8(%rax)
cvtsi2sdl 0x18(%r13), %xmm0
movsd %xmm0, 0x18(%rax)
movq %r12, (%rax)
movq %rax, 0x20(%rsp)
movq %rax, %r12
jmp 0x1baf0
movq %rbp, 0x68(%rsp)
leaq 0x396(%rip), %rsi       # 0x1bece
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x1c5ab
movq (%rbx), %r13
movq %r13, %rdi
callq 0x1c4aa
movl %eax, %ebp
leaq 0x48(%rsp), %rdi
callq 0x20294
leaq 0x88(%rsp), %rdi
callq 0x20294
movl %ebp, 0x2c(%rsp)
movslq %ebp, %rax
imulq $0x18, %rax, %r12
movq %r12, %rdi
callq 0x28a1f
movq %rax, 0x40(%rsp)
movq %r12, %rdi
callq 0x28a1f
movq %rax, %rbp
movq %rax, 0x60(%rsp)
movq 0x40(%rsp), %r12
testq %r13, %r13
je 0x1bbcf
movq %r13, 0x10(%rbp)
movq %r13, 0x10(%r12)
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x2038f
leaq 0x88(%rsp), %rdi
movq %rbp, %rsi
callq 0x2038f
movq (%r13), %r13
addq $0x18, %r12
addq $0x18, %rbp
jmp 0x1bb96
leaq 0x335(%rip), %rsi       # 0x1bf0b
leaq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x20520
leaq 0xa0(%rsp), %r12
movq %r13, (%r12)
leaq 0x88(%rsp), %rax
movq %rax, 0x8(%r12)
pushq $0x8
popq %rsi
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
callq 0x26ff4
movq %rax, %r13
movl 0x2c(%rsp), %edi
movq %r12, %rsi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x1bf20
movq %rax, (%r13)
movq 0x40(%rsp), %rdi
callq 0x28c2d
movq 0x60(%rsp), %rdi
callq 0x28c2d
movq 0xf8(%rsp), %r12
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x1bc8d
movq (%r13), %r9
movupd 0x997f4(%rip), %xmm0    # 0xb5448
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x1c0b0
ucomisd 0x18(%rbx), %xmm0
jbe 0x1bc7d
movsd %xmm0, 0x18(%rbx)
movq %rax, 0x10(%rbx)
movq (%r13), %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x1c208
jmp 0x1bc40
leaq 0x24f(%rip), %rsi       # 0x1bee3
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x1c5ab
movq (%rbx), %rbp
andq $0x0, 0x48(%rsp)
movq %rbp, %rax
testq %rax, %rax
je 0x1bcbb
movb $0x0, 0x20(%rax)
movq (%rax), %rax
jmp 0x1bcad
xorl %ebx, %ebx
testq %rbp, %rbp
je 0x1be1c
cmpb $0x0, 0x20(%rbp)
jne 0x1be13
pushq $0x50
popq %rdi
callq 0x28bb0
movq %rax, %r13
movq 0x70(%rsp), %rdi
callq 0x1c306
movq %rax, 0x30(%r13)
movl 0x30(%rsp), %eax
movl %eax, 0x38(%r13)
movl 0x34(%rsp), %eax
movb %al, 0x3c(%r13)
movq 0x78(%rsp), %rdi
callq 0x1c306
movq 0x108(%rsp), %rdi
movq %rax, 0x18(%r13)
movl 0x18(%rsp), %eax
movl %eax, 0x20(%r13)
movq 0x8(%rbp), %rax
movl 0x14(%rax), %ecx
movl %ecx, 0x44(%r13)
movl 0xc(%rax), %eax
movl %eax, 0x28(%r13)
testq %rdi, %rdi
je 0x1bd60
movsd 0x18(%rbp), %xmm0
movsd %xmm0, 0x10(%r13)
orl $-0x1, 0x48(%r13)
movq %r13, %rdi
movq 0x108(%rsp), %rsi
callq 0x1af29
movq 0x108(%rsp), %rdi
andq $0x0, 0x10(%r13)
andl $0x0, 0x48(%r13)
movq %rbx, (%r13)
movq %r13, 0x48(%rsp)
movq %rbp, %r12
movb $0x1, 0x20(%r12)
movq 0x8(%r12), %rax
movq 0x8(%r13), %rcx
movq %rcx, (%rax)
movq %rax, 0x8(%r13)
movl 0x10(%rax), %r8d
movl %r8d, 0x40(%r13)
movl 0x8(%rax), %ecx
movl %ecx, 0x24(%r13)
testq %rdi, %rdi
je 0x1bdf6
movl 0x14(%rax), %r9d
movl 0x18(%rax), %edx
movsd 0x18(%r12), %xmm0
subl %r8d, %r9d
movq %rdi, %rbx
leaq 0x62f6d(%rip), %rsi     # 0x7ed1e
movb $0x1, %al
callq 0x6a90
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x1be10
movq 0x8(%r12), %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 %rbx, %rdi
leaq 0x62f49(%rip), %rsi     # 0x7ed2e
xorl %eax, %eax
callq 0x6a90
movq 0x10(%r12), %rax
movq %rbx, %rdi
jmp 0x1be00
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x1be10
movq 0x10(%rax), %r12
cmpb $0x0, 0x20(%r12)
je 0x1bd6c
movq %r13, %rbx
movq (%rbp), %rbp
jmp 0x1bcbd
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
callq 0x1c567
movq (%rbx), %r12
movq %r12, (%rbx)
movq 0x68(%rsp), %r13
testq %r12, %r12
je 0x1be8f
leaq 0x8(%r12), %rbx
xorpd %xmm1, %xmm1
xorl %eax, %eax
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x1be82
movq %rax, %rdi
cvtsi2sdl 0x18(%rbx), %xmm0
addsd %xmm0, %xmm1
movq %rbx, %rax
testq %rdi, %rdi
je 0x1be44
movq %rbx, %rsi
movq %r14, %rdx
movsd %xmm1, 0x18(%rsp)
callq *%r15
movsd 0x18(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
subsd %xmm0, %xmm1
movq %rbx, %rax
jmp 0x1be44
movsd %xmm1, 0x10(%r12)
movq (%r12), %r12
jmp 0x1be34
leaq -0x1036(%rip), %rsi     # 0x1ae60
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
callq 0x1c5ab
leaq 0x80(%rsp), %rdi
callq 0x26fa2
andq $0x0, (%r13)
movq (%rbx), %rax
jmp 0x1bebc
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq