// File: /workspace/github_cpp_makefile_star2_O3/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 0x1b73e4
movq %r8, %rbx
movq %rcx, %r14
movq %rdx, 0x58(%rsp)
movq 0xc8(%rsp), %rbp
movsd %xmm0, 0x60(%rsp)
movq %rsi, 0x50(%rsp)
jle 0x1b73eb
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 0x1b732d
leaq (,%r13,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x19dd99
movl $0x1, %r15d
xorl %eax, %eax
cmpq %r13, %r15
je 0x1b7383
movq (%rbp,%rax,8), %rcx
xorq (%rbp,%r15,8), %rcx
shrq $0x20, %rcx
jne 0x1b7383
movq %rax, %rcx
jmp 0x1b73d3
movq %r15, %rdx
subq %rax, %rdx
movq %r15, %rcx
cmpq $0x2, %rdx
jl 0x1b73d3
cmpq %rax, %r15
jle 0x1b73b5
movq (%r14), %rcx
movq %rax, %rdx
movl (%rbp,%rdx,8), %esi
leaq (%rsi,%rsi,4), %rsi
movl 0x14(%rcx,%rsi,8), %esi
movl %esi, 0x4(%rbp,%rdx,8)
incq %rdx
cmpq %rdx, %r15
jne 0x1b739d
leaq (,%rax,8), %rdi
addq %rbp, %rdi
leaq (,%r15,8), %rsi
addq %rbp, %rsi
callq 0x19dd99
movq %r15, %rcx
leaq 0x1(%r15), %rdx
movq %rcx, %rax
cmpq %r13, %r15
movq %rdx, %r15
jne 0x1b7369
jmp 0x1b73fe
xorl %eax, %eax
jmp 0x1b754d
leaq (,%r13,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x19dd99
cmpq %r13, 0x10(%rbx)
jae 0x1b7458
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 0x1b74ab
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 0x1b7475
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 0x3ecb0
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 0x1b590b
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq