// File: /workspace/github_c_makefile_star2_O0/ToniWestbrook[P]paladin/bwamem.c
// Function: mem_chain
mem_chain_v mem_chain(const mem_opt_t *opt, const bwt_t *bwt, const bntseq_t *bns, int len, const uint8_t *seq, void *buf)
{
	int i, b, e, l_rep;
	int64_t l_pac = bns->l_pac;
	mem_chain_v chain;
	kbtree_t(chn) *tree;
	smem_aux_t *aux;

	kv_init(chain);
	if (len < opt->min_seed_len) return chain; // if the query is shorter than the seed length, no match
	tree = kb_init(chn, KB_DEFAULT_SIZE);

	aux = buf? (smem_aux_t*)buf : smem_aux_init();

	mem_collect_intv(opt, bwt, len, seq, aux);

	for (i = 0, b = e = l_rep = 0; i < aux->mem.n; ++i) { // compute frac_rep
		bwtintv_t *p = &aux->mem.a[i];
		int sb = (p->info>>32), se = (uint32_t)p->info;
		if (p->x[2] <= opt->max_occ) continue;
		if (sb > e) l_rep += e - b, b = sb, e = se;
		else e = e > se? e : se;
	}
	l_rep += e - b;
	for (i = 0; i < aux->mem.n; ++i) {
		bwtintv_t *p = &aux->mem.a[i];
		int step, count, slen = (uint32_t)p->info - (p->info>>32); // seed length
		int64_t k;
		step = p->x[2] > opt->max_occ? p->x[2] / opt->max_occ : 1;
		for (k = count = 0; k < p->x[2] && count < opt->max_occ; k += step, ++count) {
			mem_chain_t tmp, *lower, *upper;
			mem_seed_t s;
			int rid, to_add = 0;
			s.rbeg = tmp.pos = bwt_sa(bwt, p->x[0] + k); // this is the base coordinate in the forward-reverse reference
			s.qbeg = p->info>>32;
			s.score= s.len = slen;
			rid = bns_intv2rid(bns, s.rbeg, s.rbeg + s.len);
			if (rid < 0) continue; // bridging multiple reference sequences or the forward-reverse boundary; TODO: split the seed; don't discard it!!!
			if (kb_size(tree)) {
				kb_intervalp(chn, tree, &tmp, &lower, &upper); // find the closest chain
				if (!lower || !test_and_merge(opt, l_pac, lower, &s, rid)) to_add = 1;
			} else to_add = 1;
			if (to_add) { // add the seed as a new chain
				tmp.n = 1; tmp.m = 4;
				tmp.seeds = calloc(tmp.m, sizeof(mem_seed_t));
				tmp.seeds[0] = s;
				tmp.rid = rid;
				tmp.is_alt = !!bns->anns[rid].is_alt;
				kb_putp(chn, tree, &tmp);
			}
		}
	}
	if (buf == 0) smem_aux_destroy(aux);

	kv_resize(mem_chain_t, chain, kb_size(tree));

	#define traverse_func(p_) (chain.a[chain.n++] = *(p_))
	__kb_traverse(mem_chain_t, tree, traverse_func);
	#undef traverse_func

	for (i = 0; i < chain.n; ++i) chain.a[i].frac_rep = (float)l_rep / len;
	logMessage(__func__, LOG_LEVEL_DEBUG, "* fraction of repetitive seeds: %.3f\n", (float)l_rep / len);

	kb_destroy(chn, tree);
	return chain;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x120(%rbp)
movq %rdi, %rax
movq %rax, -0x118(%rbp)
movq 0x10(%rbp), %rax
movq %rsi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movq %r9, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x0, 0x8(%rdi)
movq $0x0, (%rdi)
movq $0x0, 0x10(%rdi)
movl -0x1c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x78(%rcx), %eax
jge 0x1cf27
jmp 0x1d86a
movl $0x200, %edi            # imm = 0x200
callq 0x1c950
movq %rax, -0x48(%rbp)
cmpq $0x0, 0x10(%rbp)
je 0x1cf49
movq 0x10(%rbp), %rax
movq %rax, -0x128(%rbp)
jmp 0x1cf55
callq 0x2cb30
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x28(%rbp), %rcx
movq -0x50(%rbp), %r8
callq 0x1d880
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movslq -0x2c(%rbp), %rax
movq -0x50(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1d047
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x8(%rbp), %rcx
movslq 0x8c(%rcx), %rcx
cmpq %rcx, %rax
ja 0x1cff0
jmp 0x1d039
movl -0x5c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x1d012
movl -0x34(%rbp), %eax
subl -0x30(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x1d037
movl -0x34(%rbp), %eax
cmpl -0x60(%rbp), %eax
jle 0x1d025
movl -0x34(%rbp), %eax
movl %eax, -0x12c(%rbp)
jmp 0x1d02e
movl -0x60(%rbp), %eax
movl %eax, -0x12c(%rbp)
movl -0x12c(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x1d039
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1cf94
movl -0x34(%rbp), %eax
subl -0x30(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movl $0x0, -0x2c(%rbp)
movslq -0x2c(%rbp), %rax
movq -0x50(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1d324
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %eax
movq -0x68(%rbp), %rcx
movq 0x18(%rcx), %rcx
shrq $0x20, %rcx
subq %rcx, %rax
movl %eax, -0x74(%rbp)
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x8(%rbp), %rcx
movslq 0x8c(%rcx), %rcx
cmpq %rcx, %rax
jbe 0x1d0d7
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x8(%rbp), %rcx
movslq 0x8c(%rcx), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, -0x138(%rbp)
jmp 0x1d0e5
movl $0x1, %eax
movq %rax, -0x138(%rbp)
jmp 0x1d0e5
movq -0x138(%rbp), %rax
movl %eax, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rcx
movq -0x68(%rbp), %rdx
xorl %eax, %eax
cmpq 0x10(%rdx), %rcx
movb %al, -0x139(%rbp)
jae 0x1d12a
movl -0x70(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8c(%rcx), %eax
setl %al
movb %al, -0x139(%rbp)
movb -0x139(%rbp), %al
testb $0x1, %al
jne 0x1d139
jmp 0x1d314
movl $0x0, -0xd8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rax
movq (%rax), %rsi
addq -0x80(%rbp), %rsi
callq 0x13ff0
movq %rax, -0x90(%rbp)
movq %rax, -0xd0(%rbp)
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
movl %eax, -0xc8(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0xc4(%rbp)
movl %eax, -0xc0(%rbp)
movq -0x18(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movq -0xd0(%rbp), %rdx
movslq -0xc4(%rbp), %rax
addq %rax, %rdx
callq 0x17a90
movl %eax, -0xd4(%rbp)
cmpl $0x0, -0xd4(%rbp)
jge 0x1d1bb
jmp 0x1d2fa
movq -0x48(%rbp), %rax
cmpl $0x0, 0x20(%rax)
je 0x1d220
movq -0x48(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
leaq -0xb8(%rbp), %rcx
callq 0x1de10
cmpq $0x0, -0xb0(%rbp)
je 0x1d214
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movl -0xd4(%rbp), %r8d
leaq -0xd0(%rbp), %rcx
callq 0x1df30
cmpl $0x0, %eax
jne 0x1d21e
movl $0x1, -0xd8(%rbp)
jmp 0x1d22a
movl $0x1, -0xd8(%rbp)
cmpl $0x0, -0xd8(%rbp)
je 0x1d2f8
movl $0x1, -0xa8(%rbp)
movl $0x4, -0xa4(%rbp)
movslq -0xa4(%rbp), %rdi
movl $0x18, %esi
leaq 0x1d45a(%rip), %rdx     # 0x3a6b8
movl $0x174, %ecx            # imm = 0x174
leaq 0x1d4de(%rip), %r8      # 0x3a748
callq 0x31bf0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq -0xd0(%rbp), %rcx
movq %rcx, (%rax)
movq -0xc8(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0xc0(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0xd4(%rbp), %eax
movl %eax, -0x9c(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0xd4(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x14(%rax)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %ecx
movl -0x98(%rbp), %eax
andl $0x1, %ecx
shll $0x1f, %ecx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
orl %ecx, %eax
movl %eax, -0x98(%rbp)
movq -0x48(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x1e1a0
jmp 0x1d2fa
movslq -0x6c(%rbp), %rax
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x1d0fe
jmp 0x1d316
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1d05a
cmpq $0x0, 0x10(%rbp)
jne 0x1d334
movq -0x50(%rbp), %rdi
callq 0x1e290
movq -0x120(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq 0x20(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rax), %rdi
imulq $0x28, 0x8(%rax), %rsi
leaq 0x1d361(%rip), %rdx     # 0x3a6b8
movl $0x17e, %ecx            # imm = 0x17E
leaq 0x1d3e5(%rip), %r8      # 0x3a748
callq 0x31d40
movq %rax, %rcx
movq -0x120(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x8, -0xdc(%rbp)
movslq -0xdc(%rbp), %rdi
movl $0x10, %esi
leaq 0x1d325(%rip), %rdx     # 0x3a6b8
movl $0x181, %ecx            # imm = 0x181
leaq 0x1d3a9(%rip), %r8      # 0x3a748
callq 0x31bf0
movq %rax, -0xe8(%rbp)
movq %rax, -0xf0(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rcx
movq -0xf0(%rbp), %rax
movq %rcx, (%rax)
movq -0xf0(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0x1d3d3
movq -0xf0(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, (%rcx)
movb %al, -0x13a(%rbp)
je 0x1d40b
movq -0xf0(%rbp), %rax
movl 0x8(%rax), %eax
movq -0xf0(%rbp), %rcx
movq (%rcx), %rcx
movl (%rcx), %ecx
sarl %ecx
cmpl %ecx, %eax
setle %al
movb %al, -0x13a(%rbp)
movb -0x13a(%rbp), %al
testb $0x1, %al
jne 0x1d41a
jmp 0x1d529
movq -0xf0(%rbp), %rax
movq -0xe8(%rbp), %rcx
subq %rcx, %rax
sarq $0x4, %rax
movl -0xdc(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jne 0x1d4a3
movl -0xdc(%rbp), %eax
shll %eax
movl %eax, -0xdc(%rbp)
movq -0xe8(%rbp), %rdi
movslq -0xdc(%rbp), %rsi
shlq $0x4, %rsi
leaq 0x1d251(%rip), %rdx     # 0x3a6b8
movl $0x181, %ecx            # imm = 0x181
leaq 0x1d2d5(%rip), %r8      # 0x3a748
callq 0x31d40
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movl -0xdc(%rbp), %ecx
sarl %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
addq $-0x10, %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0xf0(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
shll $0x1f, %eax
sarl $0x1f, %eax
cmpl $0x0, %eax
je 0x1d4f5
movq -0xf0(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
movslq 0xc(%rcx), %rcx
addq %rcx, %rax
movq -0xf0(%rbp), %rcx
movslq 0x8(%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x148(%rbp)
jmp 0x1d500
xorl %eax, %eax
movq %rax, -0x148(%rbp)
jmp 0x1d500
movq -0x148(%rbp), %rcx
movq -0xf0(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0xf0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0xf0(%rbp)
jmp 0x1d3d3
movq -0xf0(%rbp), %rax
addq $-0x10, %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
cmpq -0xe8(%rbp), %rax
jb 0x1d5d3
movq -0xf0(%rbp), %rax
cmpq $0x0, (%rax)
je 0x1d5c1
movq -0xf0(%rbp), %rax
movl 0x8(%rax), %eax
movq -0xf0(%rbp), %rcx
movq (%rcx), %rcx
movl (%rcx), %ecx
sarl %ecx
cmpl %ecx, %eax
jge 0x1d5c1
movq -0x120(%rbp), %rcx
movq 0x10(%rcx), %rdi
movq (%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
imulq $0x28, %rax, %rax
addq %rax, %rdi
movq -0xf0(%rbp), %rax
movq (%rax), %rsi
addq $0x4, %rsi
movq -0xf0(%rbp), %rax
movslq 0x8(%rax), %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movl $0x28, %edx
callq 0x3250
movq -0xf0(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x1d5d5
jmp 0x1d5da
jmp 0x1d3d1
movq -0xe8(%rbp), %rdi
callq 0x3380
movl $0x0, -0x2c(%rbp)
movq -0x120(%rbp), %rcx
movslq -0x2c(%rbp), %rax
cmpq (%rcx), %rax
jae 0x1d631
movq -0x120(%rbp), %rax
cvtsi2ssl -0x38(%rbp), %xmm0
cvtsi2ssl -0x1c(%rbp), %xmm1
divss %xmm1, %xmm0
movq 0x10(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x14(%rax)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1d5ed
cvtsi2ssl -0x38(%rbp), %xmm0
cvtsi2ssl -0x1c(%rbp), %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x1d0fe(%rip), %rdi     # 0x3a748
movl $0x4, %esi
leaq 0x1d0fc(%rip), %rdx     # 0x3a752
movb $0x1, %al
callq 0x12510
movl $0x8, -0xf8(%rbp)
movq $0x0, -0x110(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x1d853
movslq -0xf8(%rbp), %rdi
movl $0x8, %esi
leaq 0x1d028(%rip), %rdx     # 0x3a6b8
movl $0x187, %ecx            # imm = 0x187
leaq 0x1d0ac(%rip), %r8      # 0x3a748
callq 0x31bf0
movq %rax, -0x110(%rbp)
movq %rax, -0x108(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rcx
movq -0x108(%rbp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, -0x108(%rbp)
movq %rcx, (%rax)
movq -0x108(%rbp), %rax
cmpq -0x110(%rbp), %rax
je 0x1d851
movq -0x108(%rbp), %rax
movq %rax, %rcx
addq $-0x8, %rcx
movq %rcx, -0x108(%rbp)
movq -0x8(%rax), %rax
movq %rax, -0x100(%rbp)
cmpq $0x0, -0x100(%rbp)
je 0x1d720
movq -0x100(%rbp), %rax
movl (%rax), %eax
shll $0x1f, %eax
sarl $0x1f, %eax
cmpl $0x0, %eax
jne 0x1d72e
movq -0x100(%rbp), %rdi
callq 0x3380
jmp 0x1d6ce
movl $0x0, -0xf4(%rbp)
movl -0xf4(%rbp), %eax
movq -0x100(%rbp), %rcx
movl (%rcx), %ecx
sarl %ecx
cmpl %ecx, %eax
jg 0x1d840
movq -0x100(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq 0xc(%rcx), %rcx
addq %rcx, %rax
movslq -0xf4(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x1d82a
movq -0x108(%rbp), %rax
movq -0x110(%rbp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movslq -0xf8(%rbp), %rcx
cmpq %rcx, %rax
jne 0x1d7f5
movl -0xf8(%rbp), %eax
shll %eax
movl %eax, -0xf8(%rbp)
movq -0x110(%rbp), %rdi
movslq -0xf8(%rbp), %rsi
shlq $0x3, %rsi
leaq 0x1cefb(%rip), %rdx     # 0x3a6b8
movl $0x187, %ecx            # imm = 0x187
leaq 0x1cf7f(%rip), %r8      # 0x3a748
callq 0x31d40
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movl -0xf8(%rbp), %ecx
sarl %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x108(%rbp)
movq -0x100(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq 0xc(%rcx), %rcx
addq %rcx, %rax
movslq -0xf4(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x108(%rbp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, -0x108(%rbp)
movq %rcx, (%rax)
jmp 0x1d82c
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x1d738
movq -0x100(%rbp), %rdi
callq 0x3380
jmp 0x1d6ce
jmp 0x1d853
movq -0x48(%rbp), %rdi
callq 0x3380
movq -0x110(%rbp), %rdi
callq 0x3380
jmp 0x1d86a
movq -0x118(%rbp), %rax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw (%rax,%rax)