// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq 0xd0(%rsp), %rax
movq $0x0, (%rax)
movq 0x8(%rcx), %r13
testq %r13, %r13
je 0x1ae2fb
movq %r8, %rbx
movq %rcx, %r14
movq %rsi, 0x50(%rsp)
movq %rdx, 0x58(%rsp)
movsd %xmm0, 0x60(%rsp)
movq 0xc8(%rsp), %rbp
jle 0x1ae0fc
movq (%r14), %rax
addq $0x20, %rax
xorl %ecx, %ecx
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl -0x8(%rax), %edx
shlq $0x20, %rdx
orq %rcx, %rdx
movq %rdx, (%rbp,%rcx,8)
incq %rcx
addq $0x28, %rax
cmpq %rcx, %r13
jne 0x1ae0db
leaq (,%r13,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x194fef
testq %r13, %r13
jle 0x1ae1aa
movl $0x1, %r15d
xorl %eax, %eax
cmpq %r13, %r15
je 0x1ae13a
movq (%rbp,%rax,8), %rcx
xorq (%rbp,%r15,8), %rcx
shrq $0x20, %rcx
jne 0x1ae13a
movq %rax, %rcx
jmp 0x1ae197
movq %r15, %rdx
subq %rax, %rdx
movq %r15, %rcx
cmpq $0x2, %rdx
jl 0x1ae197
cmpq %rax, %r15
jle 0x1ae179
movq (%r14), %rcx
movq %rax, %rdx
movl (%rbp,%rdx,8), %esi
movq %rsi, (%rbp,%rdx,8)
leaq (%rsi,%rsi,4), %rdi
movl 0x14(%rcx,%rdi,8), %edi
shlq $0x20, %rdi
orq %rsi, %rdi
movq %rdi, (%rbp,%rdx,8)
incq %rdx
cmpq %rdx, %r15
jne 0x1ae154
leaq (,%rax,8), %rdi
addq %rbp, %rdi
leaq (,%r15,8), %rsi
addq %rbp, %rsi
callq 0x194fef
movq %r15, %rcx
leaq 0x1(%r15), %rdx
movq %rcx, %rax
cmpq %r13, %r15
movq %rdx, %r15
jne 0x1ae120
cmpq %r13, 0x10(%rbx)
jae 0x1ae204
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
leaq 0x1(%rcx), %rax
movq %rax, 0x10(%rbx)
movq (%rbx), %rdi
leaq 0x8(,%rcx,8), %rax
leaq (%rax,%rax,4), %rsi
callq 0x2370
movq %rax, (%rbx)
movq 0xd8(%rsp), %r15
movq 0xc0(%rsp), %r12
movq %r13, 0x8(%rbx)
testq %r13, %r13
jle 0x1ae257
xorl %eax, %eax
xorl %ecx, %ecx
movq (%r14), %rdx
movl (%rbp,%rcx,8), %esi
leaq (%rsi,%rsi,4), %rsi
movq (%rbx), %rdi
movq 0x20(%rdx,%rsi,8), %r8
movq %r8, 0x20(%rdi,%rax)
movups (%rdx,%rsi,8), %xmm0
movups 0x10(%rdx,%rsi,8), %xmm1
movups %xmm1, 0x10(%rdi,%rax)
movups %xmm0, (%rdi,%rax)
incq %rcx
addq $0x28, %rax
cmpq %rcx, %r13
jne 0x1ae221
movq (%r14), %rdi
movq (%rbx), %rsi
leaq (,%r13,8), %rax
leaq (%rax,%rax,4), %rdx
callq 0x2160
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x3f828
movq 0x28(%r15), %rax
movups (%r15), %xmm0
movq 0x8(%r14), %rcx
movq %rcx, 0x40(%rsp)
movq %rax, 0x38(%rsp)
movups %xmm0, 0x28(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r12, (%rsp)
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq 0xb0(%rsp), %r8
movq 0xb8(%rsp), %r9
movsd 0x60(%rsp), %xmm0
callq 0x1ac67f
jmp 0x1ae2fd
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq