// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x296504
jmp 0x296af0
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x2965c3
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
imulq $0x1c, -0x10(%rbp), %rax
addq %rax, %rsi
callq 0x27b350
movq $0x1, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x10(%rbp), %rax
jg 0x2965c1
movq -0x40(%rbp), %rax
cmpq -0x10(%rbp), %rax
je 0x29657e
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x8(%rbp), %rcx
imulq $0x1c, -0x48(%rbp), %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
je 0x2965b1
movq -0x40(%rbp), %rax
subq -0x48(%rbp), %rax
cmpq $0x1, %rax
jle 0x2965a9
movq -0x8(%rbp), %rdi
imulq $0x1c, -0x48(%rbp), %rax
addq %rax, %rdi
movq -0x8(%rbp), %rsi
imulq $0x1c, -0x40(%rbp), %rax
addq %rax, %rsi
callq 0x27b890
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x2965b3
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x29654a
jmp 0x2965c3
cmpq $0x0, -0x20(%rbp)
je 0x296af0
cmpq $0x0, -0x28(%rbp)
je 0x296af0
cmpq $0x0, -0x30(%rbp)
je 0x296af0
cmpq $0x0, 0x10(%rbp)
je 0x296af0
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq 0x18(%rbp), %rdx
movsd -0x38(%rbp), %xmm0
movq -0x8(%rbp), %rcx
movq -0x10(%rbp), %r8
callq 0x2962e0
cmpl $0x0, %eax
jne 0x296aee
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x10(%rbp), %rax
jge 0x2969b2
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movl 0xc(%rax), %eax
shll %eax
movq -0xa0(%rbp), %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x94(%rbp)
movq -0xa0(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0xc0000000, %ecx       # imm = 0xC0000000
orl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, 0x18(%rax)
movq $0x1, -0x68(%rbp)
movq $0x1, -0x70(%rbp)
movq $-0x1, -0x78(%rbp)
movq -0xa0(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x296941
movq -0x28(%rbp), %rdi
movl -0x94(%rbp), %esi
xorl $0x1, %esi
callq 0x287de0
movq %rax, -0x58(%rbp)
movq -0xa0(%rbp), %rax
movl 0x4(%rax), %eax
addq -0x58(%rbp), %rax
cvtsi2sd %rax, %xmm0
mulsd -0x38(%rbp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq 0x18(%rbp), %rax
jge 0x2966f2
movq 0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xa0(%rbp), %rax
movl 0x4(%rax), %eax
addq -0x58(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq 0x20(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jle 0x296725
movq 0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
movl %eax, %edi
movq -0x8(%rbp), %rsi
movq -0x60(%rbp), %rax
addq $0x80, %rax
movl %eax, %edx
callq 0x28cf00
cltq
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jl 0x29693f
movq -0x8(%rbp), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movl 0xc(%rax), %eax
shll %eax
movq -0xa8(%rbp), %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x98(%rbp)
movq -0xa8(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x80, %eax
movq -0xa0(%rbp), %rcx
cmpl 0x4(%rcx), %eax
ja 0x2967ad
jmp 0x29693f
cmpq $0x0, 0x28(%rbp)
je 0x2967d7
movq -0xa8(%rbp), %rax
movl 0x4(%rax), %eax
movq -0xa0(%rbp), %rcx
movl 0x4(%rcx), %ecx
addl $0x80, %ecx
cmpl %ecx, %eax
jb 0x2967d7
jmp 0x29692e
movq -0xa8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
jne 0x2967f0
jmp 0x29692e
movq -0xa0(%rbp), %rax
movl 0xc(%rax), %ecx
shll %ecx
movl %ecx, 0xc(%rax)
movq -0xa0(%rbp), %rax
movl 0xc(%rax), %ecx
shrl %ecx
movl %ecx, 0xc(%rax)
movq -0xa8(%rbp), %rax
movl 0xc(%rax), %ecx
shll %ecx
movl %ecx, 0xc(%rax)
movq -0xa8(%rbp), %rax
movl 0xc(%rax), %ecx
shrl %ecx
movl %ecx, 0xc(%rax)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %rdx
movq %r9, %rcx
movq %r9, %r8
callq 0x71a90
movq %rax, -0x80(%rbp)
movq -0xa0(%rbp), %rax
movl 0xc(%rax), %ecx
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0xa8(%rbp), %rax
movl 0xc(%rax), %ecx
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
cmpq $0x0, -0x80(%rbp)
je 0x29692c
movl -0x94(%rbp), %eax
cmpl -0x98(%rbp), %eax
je 0x29692c
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x94(%rbp), %edx
xorl $0x1, %edx
movl -0x98(%rbp), %ecx
xorl $0x1, %ecx
movq 0x18(%rbp), %r8
movsd -0x38(%rbp), %xmm0
movq -0x80(%rbp), %r9
leaq -0x88(%rbp), %rax
movq $0x1, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x292000
cmpq $0x0, %rax
je 0x29692c
movq -0x68(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x292b40
movq %rax, %rcx
movq -0xb0(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x70(%rbp), %rax
jle 0x29692a
movq -0x90(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x29692c
jmp 0x29692e
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x29674e
jmp 0x296941
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x292ac0
movq %rax, %rdx
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shrq $0x20, %rdx
movq 0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rdx
movq 0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x29661e
movq 0x10(%rbp), %rdi
movq 0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x27a9b0
movq -0x10(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jl 0x296aec
movq 0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x296ad9
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x296ad9
jmp 0x296a30
xorl %eax, %eax
cmpq $0x0, -0x40(%rbp)
movb %al, -0xb1(%rbp)
jl 0x296a62
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rdx
movabsq $0x80000000, %rax       # imm = 0x80000000
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
sete %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x296a6e
jmp 0x296ad7
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movabsq $0x80000000, %rdx       # imm = 0x80000000
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x292ba0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jl 0x296ad2
movq -0x40(%rbp), %rax
movl %eax, %edx
movq -0x8(%rbp), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andl $0xc0000000, %ecx       # imm = 0xC0000000
orl %edx, %ecx
movl %ecx, 0x18(%rax)
jmp 0x296a30
jmp 0x296ad9
jmp 0x296adb
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2969d6
jmp 0x296aee
jmp 0x296af0
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)