// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
testq %rsi, %rsi
je 0x1a4fa6
movq %r9, %r12
movq %rsi, %rbp
movq %rdi, %r13
movq %rcx, 0x28(%rsp)
movq %r8, 0x10(%rsp)
testq %rdx, %rdx
je 0x1a4c35
leaq (,%rbp,8), %rax
addq %rbp, %rax
leaq (%rax,%rax,2), %rsi
addq %r13, %rsi
addq %rbp, %rsi
movq %r13, %rdi
callq 0x195511
testq %rbp, %rbp
jle 0x1a4c35
movq %rbp, %r14
negq %r14
leaq 0x1c(%r13), %rbx
movl $0x1, %r15d
xorl %edi, %edi
movq %r14, %rax
addq %r15, %rax
je 0x1a4bf6
movl 0x8(%rbx), %eax
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
cmpl 0x8(%r13,%rcx), %eax
jne 0x1a4bf6
movq %rdi, %rax
jmp 0x1a4c1e
movq %r15, %rcx
subq %rdi, %rcx
movq %r15, %rax
cmpq $0x2, %rcx
jl 0x1a4c1e
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %r13, %rdi
addq %rax, %rdi
movq %rbx, %rsi
callq 0x1957bd
movq %r15, %rax
leaq (%r14,%r15), %rcx
incq %rcx
incq %r15
addq $0x1c, %rbx
movq %rax, %rdi
cmpq $0x1, %rcx
jne 0x1a4bd4
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
sete %al
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
sete %cl
orb %al, %cl
testq %r12, %r12
sete %al
orb %cl, %al
movq 0x90(%rsp), %rbx
testq %rbx, %rbx
sete %cl
orb %al, %cl
jne 0x1a4fa6
movq 0x98(%rsp), %rdx
movsd 0x18(%rsp), %xmm0
movq %r13, %rcx
movq %rbp, %r8
callq 0x1a4a1a
testl %eax, %eax
jne 0x1a4fa6
movq %r13, 0x8(%rsp)
movl $0x80000000, %r14d      # imm = 0x80000000
testq %rbp, %rbp
jle 0x1a4f08
movq 0xa0(%rsp), %rax
incq %rax
movq %rax, 0x38(%rsp)
xorl %ecx, %ecx
movq %r12, 0x30(%rsp)
movq %rbp, 0x40(%rsp)
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rbp
addq %rcx, %rbp
movq 0x8(%rsp), %rbx
movl 0xc(%rbx,%rbp), %edx
movl 0x18(%rbx,%rbp), %eax
movq %rcx, %r14
movl %eax, %ecx
orl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, 0x18(%rbx,%rbp)
movq $-0x1, %r15
movl $0x1, %ecx
movq %rcx, (%rsp)
testl %edx, %edx
movq %r14, 0x48(%rsp)
js 0x1a4d69
movq (%rsp), %rdx
movl %edx, %eax
movl $0x80000000, %r14d      # imm = 0x80000000
orl %r14d, %eax
movl %edx, %ecx
negl %ecx
testq %rdx, %rdx
cmovnsq %rax, %rcx
shlq $0x20, %rcx
testq %r15, %r15
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovsq %rax, %r15
orq %rcx, %r15
movq 0x30(%rsp), %r12
movq 0x48(%rsp), %rcx
movq %r15, (%r12,%rcx,8)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
andq %rax, %r15
orq %rcx, %r15
movq 0x90(%rsp), %rbx
movq %r15, (%rbx,%rcx,8)
incq %rcx
movq 0x40(%rsp), %rbp
cmpq %rbp, %rcx
jne 0x1a4cbc
jmp 0x1a4f08
movl %edx, %ecx
addq %rbx, %rbp
shldl $0x1, %eax, %ecx
movl %ecx, 0x24(%rsp)
movl %ecx, %esi
xorl $0x1, %esi
movq 0x10(%rsp), %rdi
movl %esi, 0x20(%rsp)
callq 0x19c00f
movl 0x4(%rbp), %ecx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x18(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x98(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x38(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl $-0x80, %edx
movl %r14d, %edi
movq %rbx, %rsi
callq 0x19f679
testl %eax, %eax
js 0x1a4cfc
movl %eax, %r14d
leaq (%r14,%r14,8), %rax
leaq (%rax,%rax,2), %rbx
movq 0x8(%rsp), %rax
addq %r14, %rax
addq %rax, %rbx
movl $0x1, %eax
movq %rax, (%rsp)
movq $-0x1, %r15
movl 0x8(%rbx), %ecx
subl $-0x80, %ecx
movl 0x4(%rbp), %eax
cmpl %eax, %ecx
jbe 0x1a4cfc
movl 0xc(%rbx), %r13d
movl 0x18(%rbx), %r12d
cmpq $0x0, 0xa8(%rsp)
je 0x1a4e38
subl $-0x80, %eax
cmpl %eax, 0x4(%rbx)
setae %al
testl %r13d, %r13d
setns %cl
orb %al, %cl
je 0x1a4e41
jmp 0x1a4eef
testl %r13d, %r13d
jns 0x1a4eef
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 0x6f050
orb $-0x80, 0xf(%rbp)
orb $-0x80, 0xf(%rbx)
testq %rax, %rax
je 0x1a4eef
shldl $0x1, %r12d, %r13d
cmpl %r13d, 0x24(%rsp)
je 0x1a4eef
xorl $0x1, %r13d
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl 0x20(%rsp), %edx
movl %r13d, %ecx
movq 0x98(%rsp), %r8
movsd 0x18(%rsp), %xmm0
movq %rax, %r9
leaq 0x50(%rsp), %rax
pushq %rax
pushq $0x1
callq 0x1a24f3
addq $0x10, %rsp
testq %rax, %rax
je 0x1a4eef
movq 0x30(%rsp), %rax
movq (%rax,%r14,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 (%rsp), %rdx
cmpq %rdx, %rcx
cmovgeq %rax, %rdx
movq %rdx, (%rsp)
cmovgeq %r14, %r15
leaq -0x1(%r14), %rax
addq $-0x1c, %rbx
testq %r14, %r14
movq %rax, %r14
jg 0x1a4dfc
jmp 0x1a4cfc
leaq (%rbx,%rbp,8), %rsi
movq %rbx, %rdi
callq 0x194fef
testq %rbp, %rbp
movq 0x8(%rsp), %r9
jle 0x1a4fa6
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0xc0000000, %ecx       # imm = 0xC0000000
movl -0x8(%rbx,%rbp,8), %edx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
testb $0x40, 0x1b(%r9,%rsi)
je 0x1a4f99
addq %r9, %rsi
cmpl $0x0, 0xc(%rsi)
jns 0x1a4f99
movq (%r12,%rdx,8), %rsi
testl %esi, %esi
js 0x1a4f99
leaq (%r12,%rdx,8), %r8
movq %rsi, %rdi
orq %r14, %rdi
movq %rdi, (%r8)
movq %rsi, %rdi
andq %rax, %rdi
cmpq %rax, %rdi
je 0x1a4f99
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 (%r12,%rdi,8), %r8
movq (%r8), %rsi
movq %rdi, %rdx
testl %esi, %esi
jns 0x1a4f58
leaq -0x1(%rbp), %rdx
cmpq $0x1, %rbp
movq %rdx, %rbp
jg 0x1a4f2c
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq