// File: /workspace/github_cpp_makefile_star2_O1/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 %rcx, 0x60(%rsp)
movq %rsi, (%rsp)
movq %rdi, 0x58(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xd0(%rsp), %rbx
xorl %r11d, %r11d
movq $-0x1, %r15
movq %rdx, 0x48(%rsp)
movq %r9, 0x8(%rsp)
movq %r8, 0x18(%rsp)
testq %rdx, %rdx
jle 0x1c8daf
decq %rdx
leaq (%rdx,%rdx,8), %rax
leaq (%rax,%rax,2), %rax
movq %rdx, 0x50(%rsp)
addq %rdx, %rax
movq (%rsp), %rcx
leaq (%rcx,%rax), %rdi
movl 0x10(%rcx,%rax), %esi
movl 0x14(%rcx,%rax), %r10d
movq 0x10(%rsp), %rdx
movl %edx, 0x4(%rcx,%rax)
movl $0x0, 0x8(%rcx,%rax)
movq %r10, 0x70(%rsp)
xorl %ecx, %ecx
cmpl %r10d, %esi
jae 0x1c8d71
movq %rdi, 0x68(%rsp)
movq %rcx, 0x78(%rsp)
movq (%r8), %rcx
movq %rsi, 0x80(%rsp)
leaq (%rsi,%rsi,4), %rax
movq 0x60(%rsp), %rdx
leaq (%rdx,%rax,8), %rax
movq %rax, 0x88(%rsp)
movslq (%rax), %rax
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
leaq (%rcx,%rdx), %rax
movq %rax, 0x90(%rsp)
movl (%rcx,%rdx), %r10d
movq (%r9), %rsi
leaq (%r10,%r10,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r10, %rcx
movl 0x4(%rsi,%rcx), %r12d
movl 0x8(%rsi,%rcx), %eax
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl 0x10(%rsi,%rcx), %r13d
movl 0x14(%rsi,%rcx), %edx
xorl %r14d, %r14d
xorl %ecx, %ecx
movq %r12, %rbp
movq %rax, %rdi
movq %r13, %r8
leaq (%r10,%r10,8), %rax
leaq (%rax,%rax,2), %r11
addq %r10, %r11
movl 0x4(%rsi,%r11), %r9d
movl 0x8(%rsi,%r11), %eax
movl %r9d, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
cmpq %r12, %rbp
cmovbq %rbp, %r12
movl 0x10(%rsi,%r11), %r13d
cmpq %r13, %r8
cmovbq %r8, %r13
cmpq %rax, %rdi
cmovaq %rdi, %rax
movq %rdx, %rdi
movl 0x14(%rsi,%r11), %edx
cmpq %rdx, %rdi
cmovaq %rdi, %rdx
testl %r9d, %r9d
js 0x1c8b6a
movq %r10, (%rbx,%rcx,8)
incq %rcx
jmp 0x1c8b6d
incq %r14
leaq (%rsi,%r11), %rdi
addq $0x4, %rdi
orl $0x80000000, %r9d       # imm = 0x80000000
movl %r9d, (%rdi)
movl 0x8(%rdi), %r10d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmpq %rdi, %r10
cmoveq %r15, %r10
jne 0x1c8b0b
testq %rcx, %rcx
jle 0x1c8e41
movq 0x90(%rsp), %r8
movl 0x4(%r8), %edi
cmpq %rdi, %r12
jne 0x1c8e03
movl 0x8(%r8), %edi
cmpq %rdi, %rax
jne 0x1c8e03
movl 0x10(%r8), %eax
cmpq %rax, %r13
jne 0x1c8e03
movl 0x14(%r8), %eax
cmpq %rax, %rdx
jne 0x1c8e03
movl 0xc(%r8), %r8d
movq 0x58(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rdx
leaq 0x20(%rsp), %r9
callq 0x1c88a7
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x88(%rsp), %r8
movslq (%r8), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl 0xc(%rax,%rdx), %ecx
movl 0x18(%rax,%rdx), %esi
shrdl $0x1f, %ecx, %esi
movl %esi, 0x28(%rsp)
movq (%r12), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl 0xc(%rax,%rdx), %eax
movl (%rcx,%rax,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movslq 0x34(%rsp), %rdi
movslq 0x2c(%rsp), %rcx
movslq 0x30(%rsp), %rdx
movq %rax, %r9
subq %rdx, %r9
movq %rax, %r10
subq %rcx, %r10
testb $0x1, %sil
cmoveq %rcx, %r9
cmoveq %rdx, %r10
movslq 0x38(%rsp), %rcx
movl %r9d, %ebp
subl %edi, %ebp
movq 0x10(%rsp), %r13
movq %r13, %r11
subq %rcx, %r11
movq %rax, %r12
subq %r10, %r12
addl %r11d, %r10d
addl %r12d, %ecx
movl %edi, %edx
subl %r9d, %edx
cmpq %r12, %r11
cmovgl %eax, %r10d
cmovlel %r13d, %ecx
cmpq %rdi, %r9
movl $0x0, %edi
cmovll %edi, %ebp
movl $0x0, %r11d
cmovgel %edi, %edx
movl %eax, %edi
subl %ebp, %edi
testb $0x1, %sil
cmovel %r10d, %edi
subl %r10d, %eax
testb $0x1, %sil
movl %edx, 0x34(%rsp)
movl %ecx, 0x38(%rsp)
cmovel %ebp, %eax
movl %eax, 0x2c(%rsp)
movl %edi, 0x30(%rsp)
movl 0x1c(%r8), %r9d
movl 0x3c(%rsp), %r10d
movl %r9d, %esi
subl %r10d, %esi
jl 0x1c8e60
testq %r14, %r14
je 0x1c8d00
movl %r10d, 0x1c(%r8)
movl %edx, 0x14(%r8)
movl %ecx, 0x18(%r8)
movl %eax, 0xc(%r8)
movl %edi, 0x10(%r8)
movq 0x8(%rsp), %r9
jmp 0x1c8d36
cmpl %r10d, %r9d
movq 0x8(%rsp), %r9
jne 0x1c8e22
cmpl %edx, 0x14(%r8)
jne 0x1c8e22
cmpl %ecx, 0x18(%r8)
jne 0x1c8e22
cmpl %eax, 0xc(%r8)
jne 0x1c8e22
cmpl %edi, 0x10(%r8)
jne 0x1c8e22
movq 0x68(%rsp), %rdi
cmpl %edx, 0x4(%rdi)
jle 0x1c8d43
movl %edx, 0x4(%rdi)
cmpl %ecx, 0x8(%rdi)
movq 0x18(%rsp), %r8
jge 0x1c8d50
movl %ecx, 0x8(%rdi)
movslq %esi, %rax
movq 0x78(%rsp), %rcx
addq %rax, %rcx
movq 0x80(%rsp), %rsi
incq %rsi
cmpq 0x70(%rsp), %rsi
jne 0x1c8a9c
movl (%rdi), %eax
subq %rcx, %rax
testq %rax, %rax
cmovleq %r11, %rax
movl 0x8(%rdi), %ecx
movl %eax, (%rdi)
cmpl 0x4(%rdi), %ecx
movq 0x50(%rsp), %rdx
ja 0x1c8a49
leaq 0x6dc37(%rip), %rdi     # 0x2369ce
leaq 0x6abed(%rip), %rsi     # 0x23398b
leaq 0x6db9e(%rip), %rcx     # 0x236943
movl $0x3b2e, %edx           # imm = 0x3B2E
callq 0x2110
movq 0x8(%r9), %rax
testq %rax, %rax
jle 0x1c8dd6
movq (%r9), %rcx
addq $0x4, %rcx
movl (%rcx), %edx
testl %edx, %edx
jns 0x1c8dcd
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %edx, (%rcx)
addq $0x1c, %rcx
decq %rax
jne 0x1c8dbf
movq 0x48(%rsp), %rsi
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %rax
movq (%rsp), %rdi
addq %rdi, %rsi
addq %rax, %rsi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x195d3d
leaq 0x6d6ea(%rip), %rdi     # 0x2364f4
leaq 0x6ab7a(%rip), %rsi     # 0x23398b
leaq 0x6db2b(%rip), %rcx     # 0x236943
movl $0x3af6, %edx           # imm = 0x3AF6
callq 0x2110
leaq 0x6d8c3(%rip), %rdi     # 0x2366ec
leaq 0x6ab5b(%rip), %rsi     # 0x23398b
leaq 0x6db0c(%rip), %rcx     # 0x236943
movl $0x3b24, %edx           # imm = 0x3B24
callq 0x2110
leaq 0x6daf3(%rip), %rdi     # 0x23693b
leaq 0x6ab3c(%rip), %rsi     # 0x23398b
leaq 0x6daed(%rip), %rcx     # 0x236943
movl $0x3af4, %edx           # imm = 0x3AF4
callq 0x2110
leaq 0x6d9ca(%rip), %rdi     # 0x236831
leaq 0x6ab1d(%rip), %rsi     # 0x23398b
leaq 0x6dace(%rip), %rcx     # 0x236943
movl $0x3b0e, %edx           # imm = 0x3B0E
callq 0x2110