// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: assgin_primary_chains(ul_ov_t*, long, long, ul_idx_t const*, ug_opt_t const*, unsigned long*, unsigned long*, long, double, long, long)
void assgin_primary_chains(ul_ov_t *a, int64_t a_n, int64_t is_srt, const ul_idx_t *uref, const ug_opt_t *uopt, 
uint64_t *track, uint64_t *srt, int64_t bw, double diff_ec_ul, int64_t qlen, int64_t is_ungap)
{
	if(a_n == 0) return;
	int64_t i, j, k, mm_ovlp, x, csc, mm_sc, mm_idx, qo, share, sc; 
	uint32_t li_v, lj_v; ul_ov_t *li = NULL, *lj = NULL;
	if(is_srt) {
		radix_sort_ul_ov_srt_qe(a, a + a_n);
		for (i = 1, j = 0; i <= a_n; i++) {
			if (i == a_n || a[i].qe != a[j].qe) {
				if(i - j > 1) {
					radix_sort_ul_ov_srt_qs(a+j, a+i);
				}
				j = i;
			}
		}
	}

	if(uref && uopt && track && srt) {
		if(quick_primary_assgin(uref, uopt, bw, diff_ec_ul, a, a_n) == 0) {
			for (i = 0; i < a_n; ++i) {
				li = &(a[i]); li_v = (li->tn<<1)|li->rev; li->sec = SEC_MODE; 
				mm_sc = csc = 1; mm_idx = -1; 
				if(li->tn&((uint32_t)(0x80000000))) {
					mm_ovlp = max_ovlp_src(uopt, li_v^1);
					x = (li->qs + mm_ovlp)*diff_ec_ul; 
					if(x < bw) x = bw; 
					x += li->qs + mm_ovlp;
					if (x > qlen+1) x = qlen+1;
					x = find_ul_ov_max(i, a, x+G_CHAIN_INDEL);
					
					for (j = x; j >= 0; --j) {
						lj = &(a[j]); lj_v = (lj->tn<<1)|lj->rev;
						if(lj->qe+G_CHAIN_INDEL <= li->qs) break;//even this pair has a overlap, its length will be very small; just ignore
						if(is_ungap && (lj->qs >= li->qs+G_CHAIN_INDEL)) continue; // lj is contained in li on the query coordinate; 128 for indel offset
						if(!(lj->tn&((uint32_t)(0x80000000)))) continue;
						li->tn <<= 1; li->tn >>= 1; lj->tn <<= 1; lj->tn >>= 1;
						qo = infer_rovlp(li, lj, NULL, NULL, NULL, NULL);
						li->tn |= ((uint32_t)(0x80000000)); lj->tn |= ((uint32_t)(0x80000000));
						if(qo && li_v != lj_v && get_ecov_adv(uref, uopt, li_v^1, lj_v^1, bw, diff_ec_ul, qo, 1, &share)) {
							sc = csc + pop_sc(track[j]);
							if(sc > mm_sc) mm_sc = sc, mm_idx = j;
						}
					}
				}
				track[i] = push_sc_pre(mm_sc, mm_idx);
				srt[i] = track[i]>>32; srt[i] <<= 32; srt[i] |= i;
				// fprintf(stderr, "[M::] i->%ld; mm_idx->%ld\n", i, mm_idx);
			}

			radix_sort_gfa64(srt, srt+a_n); 
			for (k = a_n-1; k >= 0; --k) {
				i = (uint32_t)srt[k];
				// if(i < 0 || i >= a_n) fprintf(stderr, "sbsbsbsbsbsb, k->%ld, i->%ld, a_n->%ld\n", k, i, a_n);
				if(a[i].el && (a[i].tn&((uint32_t)(0x80000000)))) {
					for (; i >= 0 && (track[i]&((uint64_t)0x80000000)) == 0;) {
						track[i] |= ((uint64_t)0x80000000); j = i;
						i = pop_pre(track[i]); 
						if(i >= 0) {
							// if(i == j) fprintf(stderr, "sb\n");
							a[j].sec = i; 
						}
					}
				}
			}
		}
	}	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm0, 0x8(%rsp)
testq %rsi, %rsi
je 0x16548f
movq %r9, %r15
movq %rsi, %r13
movq %rdi, %rbp
movq %rcx, 0x38(%rsp)
movq %r8, 0x10(%rsp)
testq %rdx, %rdx
je 0x16541f
pushq $0x1
popq %rbx
imulq $0x1c, %r13, %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x156f5c
xorl %eax, %eax
testq %r13, %r13
movl $0x0, %r14d
cmovgq %r13, %r14
negq %r14
leaq 0x1c(%rbp), %r12
leaq (%r14,%rbx), %rcx
cmpq $0x1, %rcx
je 0x16541f
cmpq %rbx, %r13
je 0x1653f2
movl 0x8(%r12), %ecx
imulq $0x1c, %rax, %rdx
cmpl 0x8(%rbp,%rdx), %ecx
jne 0x1653f2
movq %rax, %rcx
jmp 0x165413
movq %rbx, %rdx
subq %rax, %rdx
movq %rbx, %rcx
cmpq $0x2, %rdx
jl 0x165413
imulq $0x1c, %rax, %rdi
addq %rbp, %rdi
movq %r12, %rsi
callq 0x1571fa
movq %rbx, %rcx
incq %rbx
addq $0x1c, %r12
movq %rcx, %rax
jmp 0x1653cf
movq 0x10(%rsp), %r12
movq %r12, %xmm0
movq 0x38(%rsp), %rdi
movq %rdi, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movq 0xa0(%rsp), %rbx
movq %rbx, %xmm0
movq %r15, %xmm2
punpcklqdq %xmm0, %xmm2    # xmm2 = xmm2[0],xmm0[0]
pxor %xmm0, %xmm0
pcmpeqd %xmm0, %xmm2
pcmpeqd %xmm0, %xmm1
movdqa %xmm1, %xmm0
shufps $0xdd, %xmm2, %xmm0     # xmm0 = xmm0[1,3],xmm2[1,3]
shufps $0x88, %xmm2, %xmm1     # xmm1 = xmm1[0,2],xmm2[0,2]
andps %xmm0, %xmm1
movmskps %xmm1, %eax
testl %eax, %eax
jne 0x16548f
movq 0xa8(%rsp), %rdx
movq %r12, %rsi
movsd 0x8(%rsp), %xmm0
movq %rbp, %rcx
movq %r13, %r8
callq 0x16522d
testl %eax, %eax
je 0x16549e
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb0(%rsp), %rax
incq %rax
movq %rax, 0x48(%rsp)
xorl %esi, %esi
testq %r13, %r13
movl $0x0, %edi
cmovgq %r13, %rdi
movabsq $-0x7ffffffe80000001, %r8 # imm = 0x800000017FFFFFFF
movabsq $-0x100000000, %r9      # imm = 0xFFFFFFFF00000000
movq %r15, 0x28(%rsp)
movq %r13, 0x50(%rsp)
movq %rbp, 0x30(%rsp)
movq %rdi, 0x40(%rsp)
cmpq %rdi, %rsi
je 0x16571a
movq %rbp, %rdx
imulq $0x1c, %rsi, %rbp
movl 0xc(%rdx,%rbp), %r10d
movl 0x18(%rdx,%rbp), %eax
movl %eax, %ecx
orl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, 0x18(%rdx,%rbp)
movq %r8, %r14
testl %r10d, %r10d
jns 0x1656ff
movq 0x30(%rsp), %rbx
addq %rbx, %rbp
shldl $0x1, %eax, %r10d
movl %r10d, 0x24(%rsp)
xorl $0x1, %r10d
movq %rsi, %r14
movq %r12, %rdi
movl %r10d, 0x20(%rsp)
movl %r10d, %esi
callq 0x15d49a
movl 0x4(%rbp), %ecx
addq %rax, %rcx
cvtsi2sd %rcx, %xmm0
mulsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0xa8(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x48(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl $-0x80, %edx
movq %r14, 0x58(%rsp)
movl %r14d, %edi
movq %rbx, %rsi
callq 0x160717
movslq %eax, %r12
imulq $0x1c, %r12, %r13
addq %rbx, %r13
pushq $0x1
popq %rax
movq %rax, 0x18(%rsp)
pushq $-0x1
popq %r14
testq %r12, %r12
js 0x16569e
movl 0x8(%r13), %ecx
subl $-0x80, %ecx
movl 0x4(%rbp), %eax
cmpl %eax, %ecx
jbe 0x16569e
movl 0xc(%r13), %ebx
movl 0x18(%r13), %r15d
cmpq $0x0, 0xb8(%rsp)
je 0x1655e1
subl $-0x80, %eax
cmpl %eax, 0x4(%r13)
setae %al
testl %ebx, %ebx
setns %cl
orb %al, %cl
je 0x1655e9
jmp 0x165692
testl %ebx, %ebx
jns 0x165692
andb $0x7f, 0xf(%rbp)
andb $0x7f, 0xf(%r13)
movq %rbp, %rdi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x3d261
orb $-0x80, 0xf(%rbp)
orb $-0x80, 0xf(%r13)
testq %rax, %rax
je 0x165692
shldl $0x1, %r15d, %ebx
cmpl %ebx, 0x24(%rsp)
je 0x165692
xorl $0x1, %ebx
movq 0x38(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl 0x20(%rsp), %edx
movl %ebx, %ecx
movq 0xa8(%rsp), %r8
movsd 0x8(%rsp), %xmm0
movq %rax, %r9
leaq 0x60(%rsp), %rax
pushq %rax
pushq $0x1
callq 0x1631f5
popq %rcx
popq %rdx
testq %rax, %rax
je 0x165692
movq 0x28(%rsp), %rax
movq (%rax,%r12,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq -0x80000000(%rcx), %rdx
negq %rcx
testq %rax, %rax
cmovsq %rdx, %rcx
leaq 0x1(%rcx), %rax
movq 0x18(%rsp), %rdx
cmpq %rdx, %rcx
cmovgeq %rax, %rdx
movq %rdx, 0x18(%rsp)
cmovgeq %r12, %r14
decq %r12
addq $-0x1c, %r13
jmp 0x16559b
movq 0x18(%rsp), %rdx
movl %edx, %eax
movl $0x80000000, %ecx       # imm = 0x80000000
orl %ecx, %eax
movl %edx, %ecx
negl %ecx
testq %rdx, %rdx
cmovnsq %rax, %rcx
shlq $0x20, %rcx
testq %r14, %r14
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovsq %rax, %r14
orq %rcx, %r14
movq 0x28(%rsp), %r15
movq 0x50(%rsp), %r13
movq 0xa0(%rsp), %rbx
movq 0x10(%rsp), %r12
movq 0x58(%rsp), %rsi
movq 0x40(%rsp), %rdi
movabsq $-0x7ffffffe80000001, %r8 # imm = 0x800000017FFFFFFF
movabsq $-0x100000000, %r9      # imm = 0xFFFFFFFF00000000
movq %r14, (%r15,%rsi,8)
andq %r9, %r14
orq %rsi, %r14
movq %r14, (%rbx,%rsi,8)
incq %rsi
movq 0x30(%rsp), %rbp
jmp 0x1654e4
leaq (%rbx,%r13,8), %rsi
movq %rbx, %rdi
callq 0x156b0d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0xc0000000, %ecx       # imm = 0xC0000000
movl $0x80000000, %r9d       # imm = 0x80000000
testq %r13, %r13
jle 0x16548f
movl -0x8(%rbx,%r13,8), %edx
decq %r13
imulq $0x1c, %rdx, %rsi
testb $0x40, 0x1b(%rbp,%rsi)
je 0x165736
addq %rbp, %rsi
cmpl $0x0, 0xc(%rsi)
jns 0x165736
jmp 0x16577a
imulq $0x1c, %rdx, %rdx
andl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
movl 0x18(%rbp,%rdx), %r8d
andl %ecx, %r8d
orl %esi, %r8d
movl %r8d, 0x18(%rbp,%rdx)
movq %rdi, %rdx
movq (%r15,%rdx,8), %rsi
testl %esi, %esi
js 0x165736
movq %rsi, %rdi
orq %r9, %rdi
movq %rdi, (%r15,%rdx,8)
movq %rsi, %rdi
andq %rax, %rdi
cmpq %rax, %rdi
jne 0x16575d
jmp 0x165736