// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movl %r8d, 0x18(%rsp)
movq %rcx, 0x68(%rsp)
movl %edx, 0x2c(%rsp)
movl %esi, 0x28(%rsp)
movq %rdi, 0x60(%rsp)
movq $0x0, 0x10(%rsp)
cmpq $0x0, (%r9)
je 0x1fa7a
movq %r9, %rbx
xorl %edi, %edi
callq 0x2c220
movq %rax, %r14
movq %rax, 0x70(%rsp)
movq %rbx, 0x58(%rsp)
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x1f6ca
xorl %r15d, %r15d
movl 0x8(%rbx), %eax
cmpl 0xc(%rbx), %eax
je 0x1f6c2
movl $0x28, %esi
movq %r14, %rdi
callq 0x2c325
movq %rbx, 0x8(%rax)
xorps %xmm0, %xmm0
cvtsi2sdl 0x18(%rbx), %xmm0
movsd %xmm0, 0x18(%rax)
movq %r15, (%rax)
movq %rax, 0x10(%rsp)
movq %rax, %r15
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x1f691
leaq 0x3bd(%rip), %rsi       # 0x1fa8e
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x201c4
movq (%rbx), %rbx
movq %rbx, %rdi
callq 0x200ac
movl %eax, %ebp
leaq 0x40(%rsp), %rdi
callq 0x248d4
leaq 0x78(%rsp), %rdi
callq 0x248d4
movl %ebp, 0x24(%rsp)
movslq %ebp, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r15
movq %r15, %rdi
callq 0x2de63
movq %rax, 0x38(%rsp)
movq %r15, %rdi
callq 0x2de63
movq %rax, 0x30(%rsp)
testq %rbx, %rbx
je 0x1f76f
leaq 0x40(%rsp), %r12
leaq 0x78(%rsp), %rbp
movq 0x30(%rsp), %r13
movq 0x38(%rsp), %r15
movq %rbx, 0x10(%r13)
movq %rbx, 0x10(%r15)
movq %r12, %rdi
movq %r15, %rsi
callq 0x249d9
movq %rbp, %rdi
movq %r13, %rsi
callq 0x249d9
movq (%rbx), %rbx
addq $0x18, %r15
addq $0x18, %r13
testq %rbx, %rbx
jne 0x1f741
movq 0xf0(%rsp), %rbx
movq 0xe0(%rsp), %rbp
leaq 0x347(%rip), %rsi       # 0x1facd
leaq 0x40(%rsp), %r12
movq %r12, %rdi
callq 0x24b72
leaq 0x90(%rsp), %r15
movq %r12, (%r15)
leaq 0x78(%rsp), %rax
movq %rax, 0x8(%r15)
movl $0x8, %esi
movq %r14, %rdi
callq 0x2c325
movq %rax, %r12
movl 0x24(%rsp), %edi
movq %r15, %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x1fae2
movq %rax, (%r12)
movq 0x38(%rsp), %rdi
callq 0x2e0a0
movq 0x30(%rsp), %rdi
callq 0x2e0a0
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x1f83f
movq 0xe8(%rsp), %r15
movq (%r12), %r9
movupd 0xa1c2a(%rip), %xmm0    # 0xc1428
movupd %xmm0, (%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x1fc71
ucomisd 0x18(%r14), %xmm0
jbe 0x1f829
movsd %xmm0, 0x18(%r14)
movq %rax, 0x10(%r14)
movq (%r12), %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1fdd4
movq (%r14), %r14
testq %r14, %r14
jne 0x1f7f2
leaq 0x25d(%rip), %rsi       # 0x1faa3
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x201c4
movq (%r14), %r15
movq $0x0, 0x40(%rsp)
testq %r15, %r15
je 0x1f9d0
movq 0xf8(%rsp), %r14
movq %r15, %rax
movb $0x0, 0x20(%rax)
movq (%rax), %rax
testq %rax, %rax
jne 0x1f873
xorl %r12d, %r12d
cmpb $0x0, 0x20(%r15)
jne 0x1f9c4
movl $0x50, %edi
callq 0x2dff4
movq %rax, %r13
movq 0x60(%rsp), %rdi
callq 0x1feba
movq %rax, 0x30(%r13)
movl 0x28(%rsp), %eax
movl %eax, 0x38(%r13)
movl 0x2c(%rsp), %eax
movb %al, 0x3c(%r13)
movq 0x68(%rsp), %rdi
callq 0x1feba
movq %rax, 0x18(%r13)
movl 0x18(%rsp), %eax
movl %eax, 0x20(%r13)
movq 0x8(%r15), %rax
movl 0x14(%rax), %ecx
movl %ecx, 0x44(%r13)
movl 0xc(%rax), %eax
movl %eax, 0x28(%r13)
testq %r14, %r14
je 0x1f914
movsd 0x18(%r15), %xmm0
movsd %xmm0, 0x10(%r13)
movl $0xffffffff, 0x48(%r13) # imm = 0xFFFFFFFF
movq %r13, %rdi
movq %r14, %rsi
callq 0x1e9cc
movq $0x0, 0x10(%r13)
movl $0x0, 0x48(%r13)
movq %r12, (%r13)
movq %r13, 0x40(%rsp)
movq %r15, %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 %r14, %r14
je 0x1f9a7
movl 0x14(%rax), %r9d
movl 0x18(%rax), %edx
movsd 0x18(%r12), %xmm0
subl %r8d, %r9d
movq %r14, %rdi
leaq 0x70489(%rip), %rsi     # 0x8fdee
movb $0x1, %al
callq 0x6a90
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x1f9c1
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 %r14, %rdi
leaq 0x70465(%rip), %rsi     # 0x8fdfe
xorl %eax, %eax
callq 0x6a90
movq 0x10(%r12), %rax
jmp 0x1f9b1
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x1f9c1
movq 0x10(%rax), %r12
cmpb $0x0, 0x20(%r12)
je 0x1f920
movq %r13, %r12
movq (%r15), %r15
testq %r15, %r15
jne 0x1f882
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x20173
movq (%r14), %r15
movq %r15, (%r14)
testq %r15, %r15
je 0x1fa4b
movq 0x8(%r15), %r14
testq %r14, %r14
je 0x1fa3c
xorpd %xmm0, %xmm0
xorl %edi, %edi
xorps %xmm1, %xmm1
cvtsi2sdl 0x18(%r14), %xmm1
addsd %xmm0, %xmm1
testq %rdi, %rdi
je 0x1fa28
movq %r14, %rsi
movq %rbx, %rdx
movsd %xmm1, 0x18(%rsp)
callq *%rbp
movsd 0x18(%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 0x1f9f7
jmp 0x1fa40
xorpd %xmm1, %xmm1
movsd %xmm1, 0x10(%r15)
movq (%r15), %r15
jmp 0x1f9e3
leaq -0x1153(%rip), %rsi     # 0x1e8ff
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x201c4
leaq 0x70(%rsp), %rdi
callq 0x2c2c7
movq 0x58(%rsp), %rax
movq $0x0, (%rax)
movq (%rbx), %rax
jmp 0x1fa7c
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq