// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movq %r9, (%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, 0x30(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x28(%rsp)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
pushq $-0x1
popq %r15
movq %rdx, 0x18(%rsp)
testq %rdx, %rdx
jle 0x1869a5
decq %rdx
movq %rdx, 0x20(%rsp)
imulq $0x1c, %rdx, %rax
movq 0x8(%rsp), %rcx
movl 0x10(%rcx,%rax), %r8d
movl 0x14(%rcx,%rax), %edx
movq %rdx, 0x40(%rsp)
movq 0xe8(%rsp), %rsi
movl %esi, 0x4(%rcx,%rax)
andl $0x0, 0x8(%rcx,%rax)
leaq (%rcx,%rax), %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rdi, 0x38(%rsp)
cmpq 0x40(%rsp), %r8
jae 0x186966
movq %rcx, 0x48(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq %r8, 0x50(%rsp)
imulq $0x28, %r8, %rax
movq 0x30(%rsp), %rdx
leaq (%rdx,%rax), %rsi
movq %rsi, 0x58(%rsp)
movslq (%rdx,%rax), %rax
imulq $0x1c, %rax, %rdx
leaq (%rcx,%rdx), %rax
movl (%rcx,%rdx), %r10d
movq (%rsp), %rcx
movq (%rcx), %rsi
imulq $0x1c, %r10, %rcx
movl 0x4(%rsi,%rcx), %r9d
movl 0x8(%rsi,%rcx), %edi
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl 0x10(%rsi,%rcx), %r8d
movl 0x14(%rsi,%rcx), %edx
xorl %ecx, %ecx
xorl %r12d, %r12d
testq %r10, %r10
js 0x1867d9
imulq $0x1c, %r10, %r13
movl 0x4(%rsi,%r13), %ebp
movl 0x8(%rsi,%r13), %ebx
movl %ebp, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpq %r11, %r9
cmovaeq %r11, %r9
movl 0x10(%rsi,%r13), %r11d
cmpq %r11, %r8
cmovaeq %r11, %r8
cmpq %rbx, %rdi
cmovbeq %rbx, %rdi
movl 0x14(%rsi,%r13), %r11d
cmpq %r11, %rdx
cmovbeq %r11, %rdx
testl %ebp, %ebp
js 0x1867b8
movq 0xe0(%rsp), %r11
movq %r10, (%r11,%rcx,8)
incq %rcx
jmp 0x1867bb
incq %r12
leaq (%rsi,%r13), %r10
addq $0x4, %r10
orl $0x80000000, %ebp       # imm = 0x80000000
movl %ebp, (%r10)
movl 0x8(%r10), %r10d
cmpq %r14, %r10
cmoveq %r15, %r10
jmp 0x186760
testq %rcx, %rcx
jle 0x186a3f
movl 0x4(%rax), %r10d
cmpq %r10, %r9
jne 0x186a01
movl 0x8(%rax), %r9d
cmpq %r9, %rdi
jne 0x186a01
movl 0x10(%rax), %edi
cmpq %rdi, %r8
jne 0x186a01
movl 0x14(%rax), %edi
cmpq %rdi, %rdx
jne 0x186a01
movl 0xc(%rax), %r8d
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq 0xe0(%rsp), %rdx
leaq 0x80(%rsp), %r13
movq %r13, %r9
callq 0x18654b
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x58(%rsp), %rbp
movslq (%rbp), %rcx
imulq $0x1c, %rcx, %rcx
movl 0xc(%rax,%rcx), %edx
movl 0x18(%rax,%rcx), %eax
shrdl $0x1f, %edx, %eax
movl %eax, 0x88(%rsp)
movq (%rbx), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl (%rax,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %r13, %rdi
xorl %esi, %esi
movq 0xe8(%rsp), %rdx
leaq 0x78(%rsp), %r8
leaq 0x70(%rsp), %r9
leaq 0x60(%rsp), %rax
pushq %rax
leaq 0x70(%rsp), %rax
pushq %rax
callq 0x184214
popq %rax
popq %rcx
movl 0x78(%rsp), %edx
movl %edx, 0x94(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0x68(%rsp), %edi
movl %edi, 0x8c(%rsp)
movl 0x60(%rsp), %esi
movl %esi, 0x90(%rsp)
movl 0x1c(%rbp), %r9d
movl 0x9c(%rsp), %r8d
movl %r9d, %ecx
subl %r8d, %ecx
jl 0x186a5e
movq %rbp, %r10
testq %r12, %r12
je 0x186900
movl %r8d, 0x1c(%r10)
movl %edx, 0x14(%r10)
movl %eax, 0x18(%r10)
movl %edi, 0xc(%r10)
movl %esi, 0x10(%r10)
jmp 0x186931
cmpl %r8d, %r9d
jne 0x186a20
cmpl %edx, 0x14(%r10)
jne 0x186a20
cmpl %eax, 0x18(%r10)
jne 0x186a20
cmpl %edi, 0xc(%r10)
jne 0x186a20
cmpl %esi, 0x10(%r10)
jne 0x186a20
movq 0x38(%rsp), %rdi
movl 0x4(%rdi), %esi
cmpl %edx, %esi
jle 0x186942
movl %edx, 0x4(%rdi)
movl %edx, %esi
movl 0x8(%rdi), %edx
cmpl %eax, %edx
movq 0x50(%rsp), %r8
jge 0x186953
movl %eax, 0x8(%rdi)
movl %eax, %edx
movslq %ecx, %rax
movq 0x48(%rsp), %rcx
addq %rax, %rcx
incq %r8
jmp 0x1866f8
movl (%rdi), %eax
subq %rcx, %rax
testq %rax, %rax
movl $0x0, %ecx
cmovleq %rcx, %rax
movl %eax, (%rdi)
cmpl %esi, %edx
movq 0x20(%rsp), %rdx
ja 0x1866b2
leaq 0x6e1d9(%rip), %rdi     # 0x1f4b66
leaq 0x6b18f(%rip), %rsi     # 0x1f1b23
leaq 0x6e140(%rip), %rcx     # 0x1f4adb
movl $0x3b2e, %edx           # imm = 0x3B2E
callq 0x2110
movq (%rsp), %rax
movq 0x8(%rax), %rcx
xorl %eax, %eax
testq %rcx, %rcx
cmovgq %rcx, %rax
pushq $0x4
popq %rcx
subq $0x1, %rax
jb 0x1869dc
movq (%rsp), %rdx
movq (%rdx), %rdx
movl (%rdx,%rcx), %esi
testl %esi, %esi
jns 0x1869d6
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl %esi, (%rdx,%rcx)
addq $0x1c, %rcx
jmp 0x1869b9
imulq $0x1c, 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdi
addq %rdi, %rsi
callq 0x15775d
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6dc84(%rip), %rdi     # 0x1f468c
leaq 0x6b114(%rip), %rsi     # 0x1f1b23
leaq 0x6e0c5(%rip), %rcx     # 0x1f4adb
movl $0x3af6, %edx           # imm = 0x3AF6
callq 0x2110
leaq 0x6de5d(%rip), %rdi     # 0x1f4884
leaq 0x6b0f5(%rip), %rsi     # 0x1f1b23
leaq 0x6e0a6(%rip), %rcx     # 0x1f4adb
movl $0x3b24, %edx           # imm = 0x3B24
callq 0x2110
leaq 0x6e08d(%rip), %rdi     # 0x1f4ad3
leaq 0x6b0d6(%rip), %rsi     # 0x1f1b23
leaq 0x6e087(%rip), %rcx     # 0x1f4adb
movl $0x3af4, %edx           # imm = 0x3AF4
callq 0x2110
leaq 0x6df64(%rip), %rdi     # 0x1f49c9
leaq 0x6b0b7(%rip), %rsi     # 0x1f1b23
leaq 0x6e068(%rip), %rcx     # 0x1f4adb
movl $0x3b0e, %edx           # imm = 0x3B0E
callq 0x2110