// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: select_max_gchain(void*, ul_idx_t const*, long, st_mt_t*, vec_mg_lchain_t*, kv_ul_ov_t*, asg_t const*, st_mt_t*, vec_sp_node_t*, vec_mg_pathv_t*, vec_mg_lchain_t*)
uint32_t select_max_gchain(void *km, const ul_idx_t *uref, int64_t ulid, st_mt_t *idx, vec_mg_lchain_t *e, kv_ul_ov_t *raw_idx, 
const asg_t *g, st_mt_t *dst_done, vec_sp_node_t *out, vec_mg_pathv_t *res, vec_mg_lchain_t *gchains)
{
	gchains->n = 0;
    if(idx->n <= 0) return 0;
    int64_t a_n, idx_n = idx->n, i, k, n_mchain = 0, min_sc, max_sc; uint64_t om, ok, ovlp; 
    ul_ov_t *m = NULL, *p = NULL, kp; mg_lchain_t *a = e->a, *g_item; int64_t raw_idx_n = raw_idx->n;
    ul_ov_t *gb = NULL; int64_t gb_n = 0;
    for (i = a_n = 0; i < idx_n; ++i) {
        kv_pushp(ul_ov_t, *raw_idx, &p);
        p->qn = ((int64_t)(idx->a[i]>>32));//score
        p->ts = a_n; p->te = a_n + ((uint32_t)idx->a[i]);
        p->qs = a[p->ts].qs; p->qe = a[p->te-1].qe; p->tn = 1;//(tn = 1) -> normal; (t = 0) -> duplicated chain
        a_n += ((uint32_t)idx->a[i]);
    }
    gb = raw_idx->a + raw_idx_n; gb_n = raw_idx->n - raw_idx_n;
    radix_sort_ul_ov_srt_qn(gb, gb + gb_n);//sort by scores
	for (k = 0, n_mchain = gb_n>>1; k < n_mchain; k++) {
		kp = gb[k]; gb[k] = gb[gb_n-k-1]; gb[gb_n-k-1] = kp;
	}
	
	for (k = 0; k < gb_n; k++) {//filter too close chains
		m = &(gb[k]); om = m->qe - m->qs; ///current chain
		// fprintf(stderr, "k::%ld[M::%s::sc->%u] q::[%u, %u), set::%u\n", k, __func__, m->qn, m->qs, m->qe, m->tn);
		if(m->tn == 0) continue;
		for (i = k-1; i >= 0; i--) {
			p = &(gb[i]);
			ovlp = ((MIN(m->qe, p->qe) > MAX(m->qs, p->qs))? (MIN(m->qe, p->qe) - MAX(m->qs, p->qs)):0);
			if(ovlp == 0) continue;
			min_sc = MIN(p->qn, m->qn); max_sc = MAX(p->qn, m->qn);
			ok = p->qe - p->qs; ok = MAX(ok, om);
			if(min_sc < (max_sc*0.98)) break;
			if((ovlp > GC_OFFSET_POS) && (min_sc > (max_sc*0.98)) && (ovlp > (ok*0.8))) {
				// fprintf(stderr, "k::%ld[M::%s::i->%ld] min_sc::%ld, max_sc::%ld\n", 
				// k, __func__, i, min_sc, max_sc);
				m->tn = p->tn = 0;
			}
		}

		for (i = k+1; i < gb_n; i++) {
			p = &(gb[i]);
			ovlp = ((MIN(m->qe, p->qe) > MAX(m->qs, p->qs))? (MIN(m->qe, p->qe) - MAX(m->qs, p->qs)):0);
			if(ovlp == 0) continue;
			min_sc = MIN(p->qn, m->qn); max_sc = MAX(p->qn, m->qn);
			ok = p->qe - p->qs; ok = MAX(ok, om);
			if(min_sc < (max_sc*0.98)) break;
			if((ovlp > GC_OFFSET_POS) && (min_sc > (max_sc*0.98)) && (ovlp > (ok*0.8))) {
				// fprintf(stderr, "k::%ld[M::%s::i->%ld] min_sc::%ld, max_sc::%ld\n", 
				// k, __func__, i, min_sc, max_sc);
				m->tn = p->tn = 0;
			}
		}
	}

	for (k = i = 0; k < gb_n; k++) {
		m = &(gb[k]); if(m->tn == 0) continue;
		gb[i++] = gb[k];
	}
	// fprintf(stderr, "[M::%s::] gb_n0::%ld, gb_n::%ld\n", __func__, gb_n, i);
	gb_n = i;

	for (k = n_mchain = 0; k < gb_n; k++) {
		m = &(gb[k]); om = m->qe - m->qs;
		for (i = 0; i < n_mchain; i++) {
			p = &(gb[i]);
			ovlp = ((MIN(m->qe, p->qe) > MAX(m->qs, p->qs))? (MIN(m->qe, p->qe) - MAX(m->qs, p->qs)):0);
			if(ovlp == 0) continue;
			ok = p->qe - p->qs;
			if((ovlp > ok*0.1) || (ovlp > om*0.1)) break;
		}
		if(i < n_mchain) continue;
		gb[n_mchain++] = gb[k];
	}
	gb_n = n_mchain;

	if(gb_n) {
		gchains->n = 0;
		for (k = 0; k < gb_n; k++) {
			kv_pushp(mg_lchain_t, *gchains, &g_item);
			g_item->v = (uint32_t)-1; 
			g_item->qs = gb[k].qs; g_item->qe = gb[k].qe; 
			g_item->rs = gb[k].ts; g_item->re = gb[k].te; 
			g_item->cnt = g_item->off = 0;
			gen_gchain_track(km, a + g_item->rs, g_item->re - g_item->rs, g, dst_done, out, res);
			kv_resize(mg_lchain_t, *gchains, gchains->n + res->n); ///a = gchains->a + gchains->n;
			for (i = 0, g_item = &(gchains->a[gchains->n-1]); i < ((int64_t)res->n); i++) {
				if(res->a[i].v == (uint32_t)-1) {
					gchains->a[i+gchains->n] = a[res->a[i].pre + g_item->rs]; 
					gchains->a[i+gchains->n].dist_pre = res->a[i].d;
					// if(ulid == 14714) {
					// 	fprintf(stderr, "+[M::%s::]\tutg%.6dl(%c)\tq::[%d, %d)\tt::[%d, %d)\n", __func__, 
					// 		(int32_t)(gchains->a[i+gchains->n].v>>1)+1, "+-"[gchains->a[i+gchains->n].v&1],
					// 		gchains->a[i+gchains->n].qs, gchains->a[i+gchains->n].qe,
					// 		gchains->a[i+gchains->n].rs, gchains->a[i+gchains->n].re);
					// }
				} else {
					gchains->a[i+gchains->n].v = res->a[i].v; 
					gchains->a[i+gchains->n].off = -1; 
					gchains->a[i+gchains->n].dist_pre = res->a[i].d;
					///the nodes detected by the graph chaining should be fully covered
					gchains->a[i+gchains->n].rs = 0;
					gchains->a[i+gchains->n].re = uref->ug->g->seq[res->a[i].v>>1].len;
					// fprintf(stderr, "aaaaaaa, ulid->%ld\n", ulid);
					// fprintf(stderr, "-[M::%s::]\tutg%.6dl(%c)\n", __func__, 
					// (int32_t)(gchains->a[i+gchains->n].v>>1)+1, "+-"[gchains->a[i+gchains->n].v&1]);
					// if(ulid == 14714) {
					// 	fprintf(stderr, "-[M::%s::]\tutg%.6dl(%c)\tq::[%d, %d)\tt::[%d, %d)\n", __func__, 
					// 		(int32_t)(gchains->a[i+gchains->n].v>>1)+1, "+-"[gchains->a[i+gchains->n].v&1],
					// 		gchains->a[i+gchains->n].qs, gchains->a[i+gchains->n].qe,
					// 		gchains->a[i+gchains->n].rs, gchains->a[i+gchains->n].re);
					// }
				}
			}
			g_item->cnt = res->n;
			gchains->n += res->n;
			// fprintf(stderr, "sbsbsbsb, ulid->%ld\n", ulid);
			// debug_gchain(km, g, gchains->a + gchains->n - res->n, res->n, dst_done, out);
		}
	}

	raw_idx->n = raw_idx_n;
    return n_mchain;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq 0xf0(%rsp), %rax
andq $0x0, 0x8(%rax)
movq %rsi, 0x68(%rsp)
movq %rdi, 0x58(%rsp)
movq (%rcx), %rdi
testq %rdi, %rdi
je 0x16efdb
movq %rcx, %r14
movq 0xe8(%rsp), %r15
movq (%r8), %rbx
xorl %r12d, %r12d
testq %rdi, %rdi
cmovleq %r12, %rdi
movq 0x8(%r9), %r13
movq %r13, 0x50(%rsp)
xorl %ebp, %ebp
movq %r9, 0x20(%rsp)
cmpq %r12, %rdi
je 0x16e9cb
movq (%r9), %rax
cmpq 0x10(%r9), %r13
jne 0x16e973
leaq (,%r13,2), %rcx
testq %r13, %r13
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%r9)
imulq $0x1c, %rcx, %rsi
movq %rdi, %r13
movq %rax, %rdi
callq 0x2370
movq %r13, %rdi
movq 0x20(%rsp), %r9
movq %rax, (%r9)
movq 0x8(%r9), %r13
imulq $0x1c, %r13, %rcx
incq %r13
movq %r13, 0x8(%r9)
movq 0x10(%r14), %rdx
movq (%rdx,%r12,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
movl %esi, (%rax,%rcx)
movl %ebp, 0x10(%rax,%rcx)
addq %rbp, %rdx
movl %edx, 0x14(%rax,%rcx)
movl %ebp, %esi
imulq $0x28, %rsi, %rsi
movl 0x14(%rbx,%rsi), %esi
movl %esi, 0x4(%rax,%rcx)
leal -0x1(%rdx), %esi
imulq $0x28, %rsi, %rsi
movl 0x18(%rbx,%rsi), %esi
movl %esi, 0x8(%rax,%rcx)
movl $0x1, 0xc(%rax,%rcx)
incq %r12
movq %rdx, %rbp
jmp 0x16e92d
movq (%r9), %rbp
movq 0x50(%rsp), %rax
imulq $0x1c, %rax, %rcx
movq %rcx, 0x40(%rsp)
leaq (%rcx,%rbp), %r14
imulq $0x1c, %r13, %r12
subq %rax, %r13
leaq (%r12,%rbp), %rsi
movq %r14, %rdi
callq 0x15775d
movq %r13, %rcx
sarq %rcx
xorl %eax, %eax
testq %rcx, %rcx
cmovgq %rcx, %rax
movq %rbp, 0x48(%rsp)
leaq (%r12,%rbp), %rcx
addq $-0x1c, %rcx
movq %r14, 0x28(%rsp)
subq $0x1, %rax
jb 0x16ea58
movups (%r14), %xmm0
movups 0xc(%r14), %xmm1
movups %xmm1, 0x7c(%rsp)
movaps %xmm0, 0x70(%rsp)
movups (%rcx), %xmm0
movups 0xc(%rcx), %xmm1
movups %xmm1, 0xc(%r14)
movups %xmm0, (%r14)
movups 0x7c(%rsp), %xmm0
movups %xmm0, 0xc(%rcx)
movaps 0x70(%rsp), %xmm0
movups %xmm0, (%rcx)
addq $-0x1c, %rcx
addq $0x1c, %r14
jmp 0x16ea14
xorl %edx, %edx
testq %r13, %r13
movl $0x0, %edi
cmovgq %r13, %rdi
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
leaq -0x10(%rcx,%rax), %rsi
movq %rsi, 0x18(%rsp)
leaq 0x28(%rcx,%rax), %rax
movq %rax, 0x38(%rsp)
pushq $0x1
popq %rax
movq %rax, 0x30(%rsp)
movsd 0x82f9c(%rip), %xmm0    # 0x1f1a30
movsd 0x74a64(%rip), %xmm1    # 0x1e3500
movq %rdi, 0x60(%rsp)
cmpq %rdi, %rdx
je 0x16ec33
movq %rdx, 0x10(%rsp)
imulq $0x1c, %rdx, %rdx
movq 0x28(%rsp), %rax
cmpl $0x0, 0xc(%rax,%rdx)
je 0x16ec10
movq 0x28(%rsp), %rax
leaq (%rax,%rdx), %r8
movl 0x4(%rax,%rdx), %r9d
movl 0x8(%rax,%rdx), %r10d
movl %r10d, %r11d
subl %r9d, %r11d
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rbp
testq %rbp, %rbp
jle 0x16eb77
movl -0x8(%rdi), %r14d
movl -0x4(%rdi), %esi
cmpl %esi, %r10d
movl %esi, %edx
cmovbl %r10d, %edx
cmpl %r14d, %r9d
movl %r14d, %r12d
cmoval %r9d, %r12d
subl %r12d, %edx
jbe 0x16eb6b
movl -0xc(%rdi), %r12d
movl (%r8), %ecx
cmpl %ecx, %r12d
movl %ecx, %eax
cmovbl %r12d, %eax
cmoval %r12d, %ecx
subl %r14d, %esi
cmpl %r11d, %esi
cmovbel %r11d, %esi
cvtsi2sd %rax, %xmm2
cvtsi2sd %rcx, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
ja 0x16eb77
cmpl $0x9, %edx
jb 0x16eb6b
ucomisd %xmm3, %xmm2
jbe 0x16eb6b
movl %edx, %eax
cvtsi2sd %rax, %xmm2
movl %esi, %eax
cvtsi2sd %rax, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x16eb6b
andl $0x0, (%rdi)
andl $0x0, 0xc(%r8)
addq $-0x1c, %rdi
decq %rbp
jmp 0x16eae6
movq 0x38(%rsp), %rbp
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
jge 0x16ec10
movl -0x8(%rbp), %r12d
movl -0x4(%rbp), %esi
cmpl %esi, %r10d
movl %esi, %edx
cmovbl %r10d, %edx
cmpl %r12d, %r9d
movl %r12d, %eax
cmoval %r9d, %eax
subl %eax, %edx
jbe 0x16ec04
movl -0xc(%rbp), %eax
movl (%r8), %ecx
cmpl %ecx, %eax
movl %ecx, %r14d
cmovbl %eax, %r14d
cmoval %eax, %ecx
subl %r12d, %esi
cmpl %r11d, %esi
cmovbel %r11d, %esi
cvtsi2sd %r14, %xmm2
cvtsi2sd %rcx, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
ja 0x16ec10
cmpl $0x9, %edx
jb 0x16ec04
ucomisd %xmm3, %xmm2
jbe 0x16ec04
movl %edx, %eax
cvtsi2sd %rax, %xmm2
movl %esi, %eax
cvtsi2sd %rax, %xmm3
mulsd %xmm1, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x16ec04
andl $0x0, (%rbp)
andl $0x0, 0xc(%r8)
incq %rdi
addq $0x1c, %rbp
jmp 0x16eb81
movq 0x10(%rsp), %rdx
incq %rdx
addq $0x1c, 0x18(%rsp)
incq 0x30(%rsp)
addq $0x1c, 0x38(%rsp)
movq 0x60(%rsp), %rdi
jmp 0x16eaa1
xorl %ecx, %ecx
movq 0x28(%rsp), %r13
movq %r13, %rdx
movq 0x20(%rsp), %rsi
subq $0x1, %rdi
jb 0x16ec72
cmpl $0x0, 0xc(%rdx)
je 0x16ec6c
imulq $0x1c, %rcx, %rax
incq %rcx
movupd (%rdx), %xmm0
movupd 0xc(%rdx), %xmm1
movupd %xmm1, 0xc(%r13,%rax)
movupd %xmm0, (%r13,%rax)
addq $0x1c, %rdx
jmp 0x16ec42
xorl %eax, %eax
testq %rcx, %rcx
cmovleq %rax, %rcx
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rdi
addq %rdi, %rdx
addq $0x8, %rdx
movsd 0x73f5c(%rip), %xmm0    # 0x1e2bf0
movq $0x0, 0x10(%rsp)
cmpq %rcx, %rax
je 0x16ed59
imulq $0x1c, %rax, %r8
leaq (%r8,%r13), %rsi
movl 0x4(%r13,%r8), %edi
movl 0x8(%r13,%r8), %r8d
movl %r8d, %r9d
subl %edi, %r9d
cvtsi2sd %r9, %xmm1
mulsd %xmm0, %xmm1
movq 0x10(%rsp), %r10
testq %r10, %r10
movl $0x0, %r9d
cmovgq %r10, %r9
movq %rdx, %r10
subq $0x1, %r9
jb 0x16ed24
movl -0x4(%r10), %ebp
movl (%r10), %r11d
cmpl %r11d, %r8d
movl %r11d, %r12d
cmovbl %r8d, %r12d
cmpl %ebp, %edi
movl %ebp, %r14d
cmoval %edi, %r14d
subl %r14d, %r12d
jbe 0x16ed1e
subl %ebp, %r11d
cvtsi2sd %r12, %xmm2
cvtsi2sd %r11, %xmm3
mulsd %xmm0, %xmm3
ucomisd %xmm3, %xmm2
ja 0x16ed4c
ucomisd %xmm1, %xmm2
ja 0x16ed4c
addq $0x1c, %r10
jmp 0x16ecdc
movq 0x10(%rsp), %r8
imulq $0x1c, %r8, %rdi
incq %r8
movq %r8, 0x10(%rsp)
movupd (%rsi), %xmm1
movupd 0xc(%rsi), %xmm2
movupd %xmm2, 0xc(%r13,%rdi)
movupd %xmm1, (%r13,%rdi)
incq %rax
movq 0x20(%rsp), %rsi
jmp 0x16ec9d
cmpq $0x0, 0x10(%rsp)
movq 0xf0(%rsp), %r14
je 0x16efcb
andq $0x0, 0x8(%r14)
xorl %ecx, %ecx
movq 0x10(%rsp), %rax
testq %rax, %rax
movl $0x0, %edx
cmovgq %rax, %rdx
movq %rdx, 0x18(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
xorl %edi, %edi
cmpq 0x18(%rsp), %rdi
je 0x16efcb
movq (%r14), %rax
cmpq 0x10(%r14), %rcx
jne 0x16edd6
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r14)
imulq $0x28, %rdx, %rsi
movq %rdi, %r12
movq %rax, %rdi
callq 0x2370
movq %r12, %rdi
movq %rax, (%r14)
movq 0x8(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r14)
imulq $0x28, %rcx, %rcx
orl $-0x1, 0x8(%rax,%rcx)
movq %rdi, %r12
imulq $0x1c, %rdi, %rdx
movq 0x28(%rsp), %rsi
movq 0x4(%rsi,%rdx), %xmm0
movq 0x10(%rsi,%rdx), %rdx
movq %rdx, %xmm1
pshufd $0x55, %xmm1, %xmm2     # xmm2 = xmm1[1,1,1,1]
psubd %xmm1, %xmm2
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqu %xmm1, 0xc(%rax,%rcx)
andl $0x0, (%rax,%rcx)
andl $0x80000000, 0x4(%rax,%rcx) # imm = 0x80000000
movslq %edx, %rax
imulq $0x28, %rax, %rsi
addq %rbx, %rsi
movd %xmm2, %eax
movslq %eax, %rdx
movq %r15, (%rsp)
movq 0x58(%rsp), %rdi
movq 0xd0(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0xe0(%rsp), %r9
callq 0x16efef
movq (%r14), %rax
movq 0x8(%r14), %rdx
movq 0x8(%r15), %rcx
addq %rdx, %rcx
cmpq %rcx, 0x10(%r14)
jae 0x16eebb
decq %rcx
movq %rcx, %rdx
shrq %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x2, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x4, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x8, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x10, %rdx
orq %rcx, %rdx
incq %rdx
movq %rdx, 0x10(%r14)
imulq $0x28, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r14)
movq 0x8(%r14), %rdx
imulq $0x28, %rdx, %rcx
addq %rax, %rcx
pushq $0x4
popq %rsi
pushq $0x20
popq %rdi
xorl %r8d, %r8d
movq 0x8(%r15), %r9
cmpq %r9, %r8
jge 0x16ef90
movq (%r15), %r10
movl -0x4(%r10,%rsi), %r9d
cmpq %r13, %r9
je 0x16ef26
imulq $0x28, %rdx, %r11
addq %rax, %r11
movl %r9d, -0x18(%rdi,%r11)
orl $-0x1, -0x20(%rdi,%r11)
movl (%r10,%rsi), %r10d
movl %r10d, (%rdi,%r11)
andl $0x0, -0x14(%rdi,%r11)
movq 0x68(%rsp), %r10
movq (%r10), %r10
movq 0x18(%r10), %r10
movq 0x20(%r10), %r10
shrl %r9d
movl (%r10,%r9,8), %r9d
andl %ebp, %r9d
movl %r9d, -0x10(%rdi,%r11)
jmp 0x16ef80
movslq 0x4(%r10,%rsi), %r9
movslq -0x1c(%rcx), %r10
addq %r9, %r10
imulq $0x28, %r10, %r9
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq 0x20(%rbx,%r9), %rdx
movq %rdx, (%rdi,%rax)
movdqu (%rbx,%r9), %xmm0
movdqu 0x10(%rbx,%r9), %xmm1
movdqu %xmm1, -0x10(%rdi,%rax)
movdqu %xmm0, -0x20(%rdi,%rax)
movq (%r15), %rax
movl (%rax,%rsi), %r9d
movq 0xf0(%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %rdx
imulq $0x28, %rdx, %r10
addq %rax, %r10
movl %r9d, (%rdi,%r10)
incq %r8
addq $0x28, %rdi
addq $0xc, %rsi
jmp 0x16eecb
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl -0x24(%rcx), %eax
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %eax
orl %r9d, %eax
movl %eax, -0x24(%rcx)
movq 0xf0(%rsp), %r14
movq 0x8(%r14), %rcx
addq 0x8(%r15), %rcx
movq %rcx, 0x8(%r14)
movq %r12, %rdi
incq %rdi
movq 0x20(%rsp), %rsi
jmp 0x16ed97
movq 0x50(%rsp), %rax
movq %rax, 0x8(%rsi)
movq 0x10(%rsp), %rax
jmp 0x16efdd
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq