// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movsd %xmm0, 0x20(%rsp)
testq %rsi, %rsi
je 0x1adee2
movq %r8, %rbp
movq %rdi, %r12
movq %rcx, 0x38(%rsp)
movq %rsi, (%rsp)
movq %r9, 0x10(%rsp)
movq 0x90(%rsp), %r13
testq %rdx, %rdx
je 0x1ade76
movq (%rsp), %rbx
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %r12, %rsi
addq %rbx, %rsi
movq %r12, %rdi
callq 0x19e2b4
testq %rbx, %rbx
jle 0x1ade76
movq (%rsp), %r14
negq %r14
leaq 0x1c(%r12), %rbx
movl $0x1, %r15d
xorl %edi, %edi
movq %r14, %rax
addq %r15, %rax
je 0x1ade37
movl 0x8(%rbx), %eax
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
cmpl 0x8(%r12,%rcx), %eax
jne 0x1ade37
movq %rdi, %rax
jmp 0x1ade5f
movq %r15, %rcx
subq %rdi, %rcx
movq %r15, %rax
cmpq $0x2, %rcx
jl 0x1ade5f
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %r12, %rdi
addq %rax, %rdi
movq %rbx, %rsi
callq 0x19e55c
movq %r15, %rax
leaq (%r14,%r15), %rcx
incq %rcx
incq %r15
addq $0x1c, %rbx
movq %rax, %rdi
cmpq $0x1, %rcx
jne 0x1ade15
movq %rbp, %xmm0
movq 0x38(%rsp), %rdi
movq %rdi, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movq %r13, %xmm0
movq 0x10(%rsp), %rbx
movq %rbx, %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
movq (%rsp), %r15
jne 0x1adee2
movq 0x98(%rsp), %rdx
movq %rbp, %rsi
movsd 0x20(%rsp), %xmm0
movq %r12, %rcx
movq %r15, %r8
callq 0x1adc4f
testl %eax, %eax
je 0x1adef1
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r15, %r15
jle 0x1ae211
movq %rbp, 0x30(%rsp)
movq %r12, 0x8(%rsp)
movq 0xa0(%rsp), %rax
incq %rax
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rbp
addq %r13, %rbp
movq 0x8(%rsp), %rdx
movl 0xc(%rdx,%rbp), %esi
movl 0x18(%rdx,%rbp), %eax
movl %eax, %ecx
orl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, 0x18(%rdx,%rbp)
movabsq $-0x7ffffffe80000001, %r14 # imm = 0x800000017FFFFFFF
testl %esi, %esi
jns 0x1ae13b
movq 0x8(%rsp), %r14
addq %r14, %rbp
shldl $0x1, %eax, %esi
movl %esi, 0x2c(%rsp)
xorl $0x1, %esi
movq 0x30(%rsp), %rdi
movl %esi, 0x28(%rsp)
callq 0x1a4c50
movl 0x4(%rbp), %ecx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x20(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x98(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x40(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl $-0x80, %edx
movl %r13d, %edi
movq %r14, %rsi
callq 0x1a83b5
movabsq $-0x7ffffffe80000001, %r14 # imm = 0x800000017FFFFFFF
testl %eax, %eax
js 0x1ae13b
movq %r13, 0x48(%rsp)
movl %eax, %r15d
leaq (%r15,%r15,8), %rax
leaq (%rax,%rax,2), %rbx
movq 0x8(%rsp), %rax
addq %r15, %rax
addq %rax, %rbx
movq $-0x1, %r14
movl $0x1, %eax
movq %rax, 0x18(%rsp)
movl 0x8(%rbx), %ecx
subl $-0x80, %ecx
movl 0x4(%rbp), %eax
cmpl %eax, %ecx
jbe 0x1ae101
movl 0xc(%rbx), %r13d
movl 0x18(%rbx), %r12d
cmpq $0x0, 0xa8(%rsp)
je 0x1ae034
subl $-0x80, %eax
cmpl %eax, 0x4(%rbx)
setae %al
testl %r13d, %r13d
setns %cl
orb %al, %cl
je 0x1ae03d
jmp 0x1ae0ed
testl %r13d, %r13d
jns 0x1ae0ed
andb $0x7f, 0xf(%rbp)
andb $0x7f, 0xf(%rbx)
movq %rbp, %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6ea22
orb $-0x80, 0xf(%rbp)
orb $-0x80, 0xf(%rbx)
testq %rax, %rax
je 0x1ae0ed
shldl $0x1, %r12d, %r13d
cmpl %r13d, 0x2c(%rsp)
je 0x1ae0ed
xorl $0x1, %r13d
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x28(%rsp), %edx
movl %r13d, %ecx
movq 0x98(%rsp), %r8
movsd 0x20(%rsp), %xmm0
movq %rax, %r9
leaq 0x50(%rsp), %rax
pushq %rax
pushq $0x1
callq 0x1ab640
addq $0x10, %rsp
testq %rax, %rax
je 0x1ae0ed
movq 0x10(%rsp), %rax
movq (%rax,%r15,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 %r15, %r14
leaq -0x1(%r15), %rax
addq $-0x1c, %rbx
testq %r15, %r15
movq %rax, %r15
jg 0x1adff8
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 0x10(%rsp), %rbx
movq (%rsp), %r15
movq 0x48(%rsp), %r13
movq %r14, (%rbx,%r13,8)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
andq %rax, %r14
orq %r13, %r14
movq 0x90(%rsp), %r12
movq %r14, (%r12,%r13,8)
incq %r13
cmpq %r15, %r13
jne 0x1adf17
leaq (%r12,%r15,8), %rsi
movq %r12, %rdi
callq 0x19dd99
testq %r15, %r15
movq 0x8(%rsp), %r9
movl $0x80000000, %r10d      # imm = 0x80000000
jle 0x1adee2
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0xc0000000, %ecx       # imm = 0xC0000000
movl -0x8(%r12,%r15,8), %edx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
testb $0x40, 0x1b(%r9,%rsi)
je 0x1ae1ff
addq %r9, %rsi
cmpl $0x0, 0xc(%rsi)
jns 0x1ae1ff
movq (%rbx,%rdx,8), %rsi
testl %esi, %esi
js 0x1ae1ff
leaq (%rbx,%rdx,8), %r8
movq %rsi, %rdi
orq %r10, %rdi
movq %rdi, (%r8)
movq %rsi, %rdi
andq %rax, %rdi
cmpq %rax, %rdi
je 0x1ae1ff
leaq (%rdx,%rdx,8), %r8
leaq (%r8,%r8,2), %r8
addq %rdx, %r8
andl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
movl 0x18(%r9,%r8), %edx
andl %ecx, %edx
orl %esi, %edx
movl %edx, 0x18(%r9,%r8)
leaq (%rbx,%rdi,8), %r8
movq (%r8), %rsi
movq %rdi, %rdx
testl %esi, %esi
jns 0x1ae1be
leaq -0x1(%r15), %rdx
cmpq $0x1, %r15
movq %rdx, %r15
jg 0x1ae191
jmp 0x1adee2
leaq (,%r15,8), %rsi
addq %r13, %rsi
movq %r13, %rdi
callq 0x19dd99
jmp 0x1adee2