// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: ctg_chain_graph(void*, ul_idx_t const*, ma_ug_t const*, vec_mg_lchain_t*, vec_mg_lchain_t*, vec_mg_path_dst_t*, vec_sp_node_t*, vec_mg_pathv_t*, long, ug_opt_t const*, long, double, unsigned long*, st_mt_t*, Chain_Data*, long, long, long, unsigned long)
int64_t ctg_chain_graph(void *km, const ul_idx_t *uref, const ma_ug_t *ug, vec_mg_lchain_t *lc, 
vec_mg_lchain_t *sw, vec_mg_path_dst_t *dst, vec_sp_node_t *out, vec_mg_pathv_t *path,
int64_t qlen, const ug_opt_t *uopt, int64_t bw, double ng_diff_thre, uint64_t *srt, 
st_mt_t *bf, Chain_Data* dp, int64_t max_skip, int64_t max_iter, int64_t max_dis, uint64_t id)
{
	bf->n = 0;
    if(lc->n == 0) return 0;
	int64_t i, j, lc_n = lc->n, x, m_idx; mg_lchain_t *r;


	for (i = 0; i < lc_n; i++) {
		r = &lc->a[i]; r->dist_pre = -1; 
		srt[i] = r->qe; srt[i] <<= 32; srt[i] |= (uint64_t)i; 
	}
	radix_sort_gfa64(srt, srt+lc_n); 
	for (i = 1, j = 0; i <= lc_n; i++) {
		if (i == lc_n || (srt[i]>>32) != (srt[j]>>32)) {
			if(i - j > 1) {
				for (x = j; x < i; x++) {
					srt[x] <<= 32; srt[x] >>= 32; srt[x] |= ((uint64_t)lc->a[(uint32_t)srt[x]].qs)<<32;
				}
				radix_sort_gfa64(srt+j, srt+i);
			}
			j = i;
		}
	}


	kv_resize(mg_lchain_t, *sw, (uint64_t)lc_n); sw->n = lc_n;
	for (i = 0; i < lc_n; i++) sw->a[i] = lc->a[(uint32_t)srt[i]];
	memcpy(lc->a, sw->a, lc_n *sizeof((*(lc->a))));


	resize_Chain_Data(dp, lc_n, NULL);
	int32_t *p; int64_t *f, *t; 
	t = dp->tmp; p = dp->score; f = dp->pre;

	m_idx = gl_chain_linear(uref, ug, lc, sw, qlen, uopt, bw, ng_diff_thre, bf, 
		max_skip, max_iter, max_dis, p, f, t, lc->n, id);

	// if(m_idx >= 0 && primary_chain_check(bf->a, bf->n, lc->a)) return m_idx;
	// else m_idx = -1;
	// bf->n = 0;
	return m_idx;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq 0xe0(%rsp), %rax
andq $0x0, (%rax)
movq 0x8(%rcx), %r13
testq %r13, %r13
je 0x16da44
movq %rcx, %r14
movq %rdx, 0x60(%rsp)
movq 0xd8(%rsp), %rbp
xorl %eax, %eax
testq %r13, %r13
movl $0x0, %r15d
cmovgq %r13, %r15
pushq $0x4
popq %rcx
cmpq %rax, %r15
je 0x16d865
movq (%r14), %rdx
orl $-0x1, (%rdx,%rcx,8)
movl -0x8(%rdx,%rcx,8), %edx
shlq $0x20, %rdx
orq %rax, %rdx
movq %rdx, (%rbp,%rax,8)
incq %rax
addq $0x5, %rcx
jmp 0x16d840
movq %rsi, 0x58(%rsp)
movq %r8, 0x68(%rsp)
movsd %xmm0, 0x70(%rsp)
leaq (,%r13,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x156b0d
leaq 0x1(%r15), %r12
pushq $0x1
popq %rbx
xorl %eax, %eax
cmpq %r12, %rbx
je 0x16d905
cmpq %r13, %rbx
je 0x16d8b0
movq (%rbp,%rax,8), %rcx
xorq (%rbp,%rbx,8), %rcx
shrq $0x20, %rcx
jne 0x16d8b0
movq %rax, %rcx
jmp 0x16d8fd
movq %rbx, %rdx
subq %rax, %rdx
movq %rbx, %rcx
cmpq $0x2, %rdx
jl 0x16d8fd
movq %rax, %rcx
cmpq %rbx, %rcx
jge 0x16d8df
movl (%rbp,%rcx,8), %edx
movq (%r14), %rsi
imulq $0x28, %rdx, %rdx
movl 0x14(%rsi,%rdx), %edx
movl %edx, 0x4(%rbp,%rcx,8)
incq %rcx
jmp 0x16d8c2
leaq (,%rax,8), %rdi
addq %rbp, %rdi
leaq (,%rbx,8), %rsi
addq %rbp, %rsi
callq 0x156b0d
movq %rbx, %rcx
incq %rbx
movq %rcx, %rax
jmp 0x16d891
movq 0x68(%rsp), %rbx
cmpq %r13, 0x10(%rbx)
jae 0x16d95b
leaq -0x1(%r13), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
incq %rcx
movq %rcx, 0x10(%rbx)
movq (%rbx), %rdi
imulq $0x28, %rcx, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %r13, 0x8(%rbx)
xorl %eax, %eax
xorl %ecx, %ecx
movq (%r14), %rdi
cmpq %rcx, %r15
je 0x16d99b
movl (%rbp,%rcx,8), %edx
imulq $0x28, %rdx, %rdx
movq (%rbx), %rsi
movq 0x20(%rdi,%rdx), %r8
movq %r8, 0x20(%rsi,%rax)
movups (%rdi,%rdx), %xmm0
movups 0x10(%rdi,%rdx), %xmm1
movups %xmm1, 0x10(%rsi,%rax)
movups %xmm0, (%rsi,%rax)
incq %rcx
addq $0x28, %rax
jmp 0x16d963
movq (%rbx), %rsi
imulq $0x28, %r13, %rdx
callq 0x2160
movq 0xe8(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x1383e
movq 0x28(%r15), %rax
movups (%r15), %xmm0
movq 0x8(%r14), %rcx
movq %rcx, 0x40(%rsp)
movq %rax, 0x38(%rsp)
movups %xmm0, 0x28(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, (%rsp)
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq 0xc0(%rsp), %r8
movq 0xc8(%rsp), %r9
movsd 0x70(%rsp), %xmm0
callq 0x16c201
jmp 0x16da46
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq