// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: dedup_second_chain_adv(ul_idx_t const*, ul_ov_t*, long, mg_lchain_t*, kv_ul_ov_t*, kv_ul_ov_t*, unsigned long*, long, long)
void dedup_second_chain_adv(const ul_idx_t *uref, ul_ov_t *gb, int64_t gb_n, mg_lchain_t *chain_a,
kv_ul_ov_t *raw_idx, kv_ul_ov_t *raw_chn, uint64_t *b, int64_t qlen, int64_t ulid)
{
	int64_t k, i, z, ss, ee, b_n, n_s; uint64_t qs, qe, ts, te; uint32_t mk = 0x80000000/**, pi**/; mg_lchain_t nn;
	int64_t iqs, iqe, its, ite, tsc;

	for (z = gb_n - 1; z >= 0; z--) {///start from the best chain
		ss = gb[z].ts; ee = gb[z].te; tsc = 0; gb[z].qs = qlen; gb[z].qe = 0;
		for (k = ss; k < ee; k++) {
			i = raw_idx->a[chain_a[k].off].qn; b_n = 0;
			qs = ((raw_chn->a[i].qs<<1)>>1); qe = raw_chn->a[i].qe;
			ts = raw_chn->a[i].ts; te = raw_chn->a[i].te;
			for (n_s = 0;i>=0;) {
				if(((raw_chn->a[i].qs<<1)>>1) < qs) qs = ((raw_chn->a[i].qs<<1)>>1);
				if(raw_chn->a[i].ts < ts) ts = raw_chn->a[i].ts;
				if(raw_chn->a[i].qe > qe) qe = raw_chn->a[i].qe;
				if(raw_chn->a[i].te > te) te = raw_chn->a[i].te;

				if(!(raw_chn->a[i].qs&mk)) b[b_n++] = i;
				else n_s++;

				raw_chn->a[i].qs |= mk;
				if(raw_chn->a[i].tn == (uint32_t)-1) i = -1;
				else i = raw_chn->a[i].tn;
			}
			assert(b_n > 0);
			assert(raw_idx->a[chain_a[k].off].qs == qs && raw_idx->a[chain_a[k].off].qe == qe && 
					raw_idx->a[chain_a[k].off].ts == ts && raw_idx->a[chain_a[k].off].te == te);
			// if(!(raw_idx->a[chain_a[k].off].qs == qs && raw_idx->a[chain_a[k].off].qe == qe && 
			// 		raw_idx->a[chain_a[k].off].ts == ts && raw_idx->a[chain_a[k].off].te == te)) {
			// 	fprintf(stderr, "\n[M::%s::ulid->%ld******] raw_idx_offset:%d, qs:%lu, qe:%lu, ts:%lu, te:%lu, raw_idx->qs:%u, raw_idx->qe:%u, raw_idx->ts:%u, raw_idx->te:%u\n", 
			// 	__func__, ulid, chain_a[k].off, qs, qe, ts, te, 
			// 	raw_idx->a[chain_a[k].off].qs, raw_idx->a[chain_a[k].off].qe, 
			// 	raw_idx->a[chain_a[k].off].ts, raw_idx->a[chain_a[k].off].te); 

			// 	for (i = raw_idx->a[chain_a[k].off].qn;i>=0;) {
			// 		if(((raw_chn->a[i].qs<<1)>>1) < qs) qs = ((raw_chn->a[i].qs<<1)>>1);
			// 		if(raw_chn->a[i].ts < ts) ts = raw_chn->a[i].ts;
			// 		if(raw_chn->a[i].qe > qe) qe = raw_chn->a[i].qe;
			// 		if(raw_chn->a[i].te > te) te = raw_chn->a[i].te;
			// 		fprintf(stderr, "[M::%s->pieces (%ld)] qs->%u, qe->%u, ts->%u, te->%u\n", __func__, i, 
            // 				((raw_chn->a[i].qs<<1)>>1), raw_chn->a[i].qe, raw_chn->a[i].ts, raw_chn->a[i].te);
			// 		if(raw_chn->a[i].tn == (uint32_t)-1) i = -1;
			// 		else i = raw_chn->a[i].tn;
			// 	}
			// }
			
			update_exist_chain_adv(uref, raw_chn->a, b, b_n, raw_idx->a[chain_a[k].off].tn, &nn);
			nn.v = (raw_idx->a[chain_a[k].off].tn<<1)|raw_idx->a[chain_a[k].off].rev;
			extend_end_coord(&nn, NULL, qlen, uref->ug->g->seq[raw_idx->a[chain_a[k].off].tn].len, &iqs, &iqe, &its, &ite);
			nn.qs = iqs; nn.qe = iqe; nn.rs = its; nn.re = ite;
			assert(chain_a[k].score >= nn.score);
			tsc += (chain_a[k].score - nn.score);

			if(n_s) {
				// raw_idx->a[chain_a[k].off].qn = b[b_n-1];
				// for (i = 0, pi = (uint32_t)-1; i < b_n; i++) {
				// 	raw_chn->a[b[i]].tn = pi; pi = b[i];
				// }
				///don't update chain_a[k] as it will be used for taceback in the next step
				chain_a[k].score = nn.score; 
				chain_a[k].qs = nn.qs; chain_a[k].qe = nn.qe; 
				chain_a[k].rs = nn.rs; chain_a[k].re = nn.re; 
				// raw_idx->a[chain_a[k].off].sec = nn.score; 
				// raw_idx->a[chain_a[k].off].qs = nn.qs; 
				// raw_idx->a[chain_a[k].off].qe = nn.qe; 
				// raw_idx->a[chain_a[k].off].ts = nn.rs; 
				// raw_idx->a[chain_a[k].off].te = nn.re;
			} else {
				// if(!(chain_a[k].score == nn.score && chain_a[k].qs == nn.qs && chain_a[k].qe == nn.qe && chain_a[k].rs == nn.rs && chain_a[k].re == nn.re)) {
				// 	fprintf(stderr, "++++[M::%s::k->%ld] chain_a[k].score->%d, nn.score->%d, chain_a[k].qs->%d, nn.qs->%d, chain_a[k].qe->%d, nn.qe->%d, chain_a[k].rs->%d, nn.rs->%d, chain_a[k].re->%d, nn.re->%d\n", __func__, 
				// 	k, chain_a[k].score, nn.score, chain_a[k].qs, nn.qs, chain_a[k].qe, nn.qe, chain_a[k].rs, nn.rs, chain_a[k].re, nn.re);
				// }
				assert(chain_a[k].score == nn.score && chain_a[k].qs == nn.qs && chain_a[k].qe == nn.qe && chain_a[k].rs == nn.rs && chain_a[k].re == nn.re);
			}

			if((int32_t)gb[z].qs > nn.qs) gb[z].qs = nn.qs;///update qs and qe
			if((int32_t)gb[z].qe < nn.qe) gb[z].qe = nn.qe;
		}
		tsc = (int64_t)(gb[z].qn) - tsc; if(tsc < 0) tsc = 0; gb[z].qn = tsc;///update score
		// if(gb[z].qe <= gb[z].qs) {
		// 	fprintf(stderr, "++++[M::%s::] gb[%ld].qe->%u, gb[%ld].qs->%u\n", __func__, z, gb[z].qe, z, gb[z].qs);
		// }
		assert(gb[z].qe > gb[z].qs);
		// fprintf(stderr, "[M::%s::z->%ld] score->%u, qs->%u, qe->%u, occ->%u\n", 
		// __func__, z, gb[z].qn, gb[z].qs, gb[z].qe, gb[z].te-gb[z].ts); 
	}

	for (i = 0; i < (int64_t)raw_chn->n; i++){
		if(raw_chn->a[i].qs&mk) raw_chn->a[i].qs -= mk;
	}
	radix_sort_ul_ov_srt_qn(gb, gb + gb_n);//sort by scores
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdi, 0x60(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, (%rsp)
movq 0xd0(%rsp), %rbx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %rdx, 0x40(%rsp)
movq %rsi, 0x48(%rsp)
movq %r9, 0x58(%rsp)
testq %rdx, %rdx
jle 0x1d2c7e
decq %rdx
leaq (%rdx,%rdx,8), %rax
leaq (%rax,%rax,2), %r8
addq %rdx, %r8
movl 0x10(%rsi,%r8), %edi
movl 0x14(%rsi,%r8), %ecx
movq (%rsp), %rax
movl %eax, 0x4(%rsi,%r8)
movl $0x0, 0x8(%rsi,%r8)
movq %rcx, 0x70(%rsp)
cmpl %ecx, %edi
jae 0x1d2c5f
movq %rdx, 0x50(%rsp)
addq %rsi, %r8
movq 0x8(%rsp), %rax
movq (%rax), %rax
xorl %edx, %edx
movq %r8, 0x78(%rsp)
movq %rdx, 0x80(%rsp)
movq %rdi, 0x88(%rsp)
leaq (%rdi,%rdi,4), %rcx
movq 0x68(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, 0x10(%rsp)
movslq (%rcx), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rcx, %rsi
leaq (%rax,%rsi), %rcx
movq %rcx, 0x90(%rsp)
movl (%rax,%rsi), %r10d
movq (%r9), %rsi
leaq (%r10,%r10,8), %rax
leaq (%rax,%rax,2), %rax
addq %r10, %rax
movl 0x4(%rsi,%rax), %r12d
movl 0x8(%rsi,%rax), %r14d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl 0x10(%rsi,%rax), %edx
movl 0x14(%rsi,%rax), %eax
xorl %r15d, %r15d
xorl %ecx, %ecx
movq %r12, %rbp
movq %r14, %rdi
movq %rdx, %r8
leaq (%r10,%r10,8), %rdx
leaq (%rdx,%rdx,2), %r11
addq %r10, %r11
movl 0x4(%rsi,%r11), %r9d
movl 0x8(%rsi,%r11), %r14d
movl %r9d, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
cmpq %r12, %rbp
cmovbq %rbp, %r12
movl 0x10(%rsi,%r11), %edx
cmpq %rdx, %r8
cmovbq %r8, %rdx
cmpq %r14, %rdi
cmovaq %rdi, %r14
movq %rax, %rdi
movl 0x14(%rsi,%r11), %eax
cmpq %rax, %rdi
cmovaq %rdi, %rax
testl %r9d, %r9d
js 0x1d2a35
movq %r10, (%rbx,%rcx,8)
incq %rcx
jmp 0x1d2a38
incq %r15
leaq (%rsi,%r11), %rdi
addq $0x4, %rdi
orl $0x80000000, %r9d       # imm = 0x80000000
movl %r9d, (%rdi)
movl 0x8(%rdi), %r10d
cmpq %r13, %r10
jne 0x1d29d6
testq %rcx, %rcx
jle 0x1d2d0f
movq 0x90(%rsp), %r8
movl 0x4(%r8), %edi
cmpq %rdi, %r12
jne 0x1d2cd1
movl 0x8(%r8), %edi
cmpq %rdi, %r14
jne 0x1d2cd1
movl 0x10(%r8), %edi
cmpq %rdi, %rdx
jne 0x1d2cd1
movl 0x14(%r8), %edx
cmpq %rdx, %rax
jne 0x1d2cd1
movl 0xc(%r8), %r8d
movq 0x60(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
leaq 0x18(%rsp), %r9
callq 0x1d277c
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rsp), %rcx
movslq (%rcx), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl 0xc(%rax,%rdx), %ecx
movl 0x18(%rax,%rdx), %edi
shrdl $0x1f, %ecx, %edi
movl %edi, 0x20(%rsp)
movq (%r14), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movslq 0x2c(%rsp), %r8
movslq 0x24(%rsp), %rdx
movslq 0x28(%rsp), %rsi
movq %rcx, %r10
subq %rsi, %r10
movq %rcx, %r11
subq %rdx, %r11
testb $0x1, %dil
cmoveq %rdx, %r10
cmoveq %rsi, %r11
movslq 0x30(%rsp), %rdx
movl %r10d, %r9d
subl %r8d, %r9d
movq (%rsp), %rbp
movq %rbp, %r14
subq %rdx, %r14
movq %rcx, %r12
subq %r11, %r12
addl %r14d, %r11d
movl %r8d, %esi
subl %r10d, %esi
cmpq %r12, %r14
leal (%r12,%rdx), %edx
cmovgl %ecx, %r11d
cmovlel %ebp, %edx
cmpq %r8, %r10
movl $0x0, %r8d
cmovll %r8d, %r9d
movl $0x0, %r14d
cmovgel %r8d, %esi
movl %ecx, %r8d
subl %r9d, %r8d
testb $0x1, %dil
cmovel %r11d, %r8d
subl %r11d, %ecx
movq 0x10(%rsp), %r11
testb $0x1, %dil
movl %esi, 0x2c(%rsp)
movl %edx, 0x30(%rsp)
cmovel %r9d, %ecx
movl %ecx, 0x24(%rsp)
movl %r8d, 0x28(%rsp)
movl 0x1c(%r11), %r10d
movl 0x34(%rsp), %r9d
movl %r10d, %edi
subl %r9d, %edi
jl 0x1d2d2e
testq %r15, %r15
je 0x1d2bbf
movl %r9d, 0x1c(%r11)
movl %esi, 0x14(%r11)
movl %edx, 0x18(%r11)
movl %ecx, 0xc(%r11)
movl %r8d, 0x10(%r11)
jmp 0x1d2bf0
cmpl %r9d, %r10d
jne 0x1d2cf0
cmpl %esi, 0x14(%r11)
jne 0x1d2cf0
cmpl %edx, 0x18(%r11)
jne 0x1d2cf0
cmpl %ecx, 0xc(%r11)
jne 0x1d2cf0
cmpl %r8d, 0x10(%r11)
jne 0x1d2cf0
movq 0x78(%rsp), %r8
movl 0x4(%r8), %ecx
cmpl %esi, %ecx
jle 0x1d2c03
movl %esi, 0x4(%r8)
movl %esi, %ecx
movl 0x8(%r8), %esi
cmpl %edx, %esi
movq 0x58(%rsp), %r9
jge 0x1d2c16
movl %edx, 0x8(%r8)
movl %edx, %esi
movslq %edi, %rdx
movq 0x80(%rsp), %rdi
addq %rdx, %rdi
movq %rdi, %rdx
movq 0x88(%rsp), %rdi
incq %rdi
cmpq 0x70(%rsp), %rdi
jne 0x1d296a
movl (%r8), %eax
subq %rdx, %rax
testq %rax, %rax
cmovleq %r14, %rax
movl %eax, (%r8)
cmpl %ecx, %esi
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
ja 0x1d2913
leaq 0x70038(%rip), %rdi     # 0x242c9e
leaq 0x6cfee(%rip), %rsi     # 0x23fc5b
leaq 0x6ff9f(%rip), %rcx     # 0x242c13
movl $0x3b2e, %edx           # imm = 0x3B2E
callq 0x2110
movq %rsi, %rdi
movq 0x8(%r9), %rax
testq %rax, %rax
jle 0x1d2ca8
movq (%r9), %rcx
addq $0x4, %rcx
movl (%rcx), %edx
testl %edx, %edx
jns 0x1d2c9f
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %edx, (%rcx)
addq $0x1c, %rcx
decq %rax
jne 0x1d2c91
movq 0x40(%rsp), %rsi
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdi, %rsi
addq %rax, %rsi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x19ead4
leaq 0x6faec(%rip), %rdi     # 0x2427c4
leaq 0x6cf7c(%rip), %rsi     # 0x23fc5b
leaq 0x6ff2d(%rip), %rcx     # 0x242c13
movl $0x3af6, %edx           # imm = 0x3AF6
callq 0x2110
leaq 0x6fcc5(%rip), %rdi     # 0x2429bc
leaq 0x6cf5d(%rip), %rsi     # 0x23fc5b
leaq 0x6ff0e(%rip), %rcx     # 0x242c13
movl $0x3b24, %edx           # imm = 0x3B24
callq 0x2110
leaq 0x6fef5(%rip), %rdi     # 0x242c0b
leaq 0x6cf3e(%rip), %rsi     # 0x23fc5b
leaq 0x6feef(%rip), %rcx     # 0x242c13
movl $0x3af4, %edx           # imm = 0x3AF4
callq 0x2110
leaq 0x6fdcc(%rip), %rdi     # 0x242b01
leaq 0x6cf1f(%rip), %rsi     # 0x23fc5b
leaq 0x6fed0(%rip), %rcx     # 0x242c13
movl $0x3b0e, %edx           # imm = 0x3B0E
callq 0x2110