// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: quick_rechain_propagate_transitive_reduction(ul_idx_t const*, ug_opt_t const*, All_reads*, long, double, ul_ov_t*, int, long, int*, int*, long*, int*, long*, long*, long*, long*)
void quick_rechain_propagate_transitive_reduction(const ul_idx_t *uref, const ug_opt_t *uopt, All_reads *ridx, int64_t bw, double diff, 
ul_ov_t *a, int32_t a_n, int64_t ai, int32_t *rch, int32_t *f, int64_t *p, int32_t *c_n, int64_t *t, int64_t *mm_sc, 
int64_t *mm_idx, int64_t *mm_n)
{
	if((*mm_idx) < 0) return;
	int64_t mm_idx0 = (*mm_idx), j, k, is_c, sn;
	for (j = mm_idx0 + 1; j < a_n; j++) {
		t[j] = mm_idx0 - 1;
		if(p[j] < 0) continue;
		// if(a[ai].tn == 23989) {
		// 	fprintf(stderr, "+chain[i::%ld] (id::%u)%.*s\t%c\tj::%ld\t%.*s\tconnect::%u\n", ai, a[ai].tn,
		// 		(int)Get_NAME_LENGTH(R_INF, a[ai].tn), Get_NAME(R_INF, a[ai].tn), 
		// 		"+-"[a[ai].rev], j, (int)Get_NAME_LENGTH(R_INF, a[ai].tn), Get_NAME(R_INF, a[ai].tn), rch_connect(rch[j], ai));
		// }
		if((rch[j]>>2) != ai) {
			rch[j] = (ai<<2); rch[j] += 3;
			if((a[j].el) && (if_qchain_cnn(uref, uopt, ridx, bw, diff, &(a[ai]), &(a[j]), &is_c, NULL))) {
				rch[j] = (ai<<2); rch[j] += is_c;
			}
		}
		// if(a[ai].tn == 23989) {
		// 	fprintf(stderr, "-chain[i::%ld] (id::%u)%.*s\t%c\tj::%ld\t%.*s\tconnect::%u\n", ai, a[ai].tn,
		// 		(int)Get_NAME_LENGTH(R_INF, a[ai].tn), Get_NAME(R_INF, a[ai].tn), 
		// 		"+-"[a[ai].rev], j, (int)Get_NAME_LENGTH(R_INF, a[ai].tn), Get_NAME(R_INF, a[ai].tn), rch_connect(rch[j], ai));
		// }
		if(!rch_connect(rch[j], ai)) continue;
		for (k = p[j]; k >= 0 && k > mm_idx0; k = p[k]) {
			if(t[k] == mm_idx0) {
				k = mm_idx0; break;
			} else {
				k = mm_idx0-1; break;
			}
		}
		if (k != mm_idx0) continue;
		t[j] = mm_idx0;//a[j] could reach mm_idx0;
		if(p[j] != k) {
			if(!(if_qchain_cnn(uref, uopt, ridx, bw, diff, &(a[j]), &(a[k]), &is_c, NULL))) continue;
		}
		sn = c_n[j] + 1;
		if(sn >= (*mm_n)) {//must >=
			(*mm_n) = sn; (*mm_idx) = j;
		}
	}
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, 0x20(%rsp)
movsd %xmm0, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq 0xc8(%rsp), %rax
movq (%rax), %rbx
testq %rbx, %rbx
js 0x1b4b99
movslq %r9d, %r13
leaq 0x1(%rbx), %r14
cmpq %r13, %r14
jge 0x1b4b99
movq 0xb8(%rsp), %rdx
movq 0xa8(%rsp), %rsi
movq 0x98(%rsp), %r15
movq 0x90(%rsp), %rdi
leaq -0x1(%rbx), %r12
leal (,%rdi,4), %eax
movl %eax, 0x18(%rsp)
leal 0x3(,%rdi,4), %eax
movl %eax, 0x1c(%rsp)
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %r8
movq 0x20(%rsp), %rcx
addq %rcx, %r8
addq %rdi, %r8
movq %r8, 0x50(%rsp)
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rbx, %rax
leaq (%rcx,%rax), %rbp
addq $0x1c, %rbp
movq %r12, (%rdx,%r14,8)
cmpq $0x0, (%rsi,%r14,8)
js 0x1b4b89
movl (%r15,%r14,4), %eax
sarl $0x2, %eax
cltq
cmpq %rdi, %rax
je 0x1b4ac6
movl 0x1c(%rsp), %eax
movl %eax, (%r15,%r14,4)
testb $0x40, 0x1b(%rbp)
je 0x1b4ac6
leaq 0x48(%rsp), %rax
movq %rax, (%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
movsd 0x40(%rsp), %xmm0
movq 0x50(%rsp), %r8
movq %rbp, %r9
callq 0x1b4ba8
movq 0x90(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0xb8(%rsp), %rdx
testl %eax, %eax
je 0x1b4ac6
movl 0x18(%rsp), %ecx
movl %ecx, (%r15,%r14,4)
movl 0x48(%rsp), %eax
addl %ecx, %eax
movl %eax, (%r15,%r14,4)
movl (%r15,%r14,4), %eax
movl %eax, %ecx
notl %ecx
testb $0x3, %cl
je 0x1b4b89
sarl $0x2, %eax
cltq
cmpq %rdi, %rax
jne 0x1b4b89
movq (%rsi,%r14,8), %r9
cmpq %rbx, %r9
jle 0x1b4afa
cmpq %rbx, (%rdx,%r9,8)
movq %rbx, %r9
je 0x1b4afa
movq %r12, %r9
cmpq %rbx, %r9
jne 0x1b4b89
movq %rbx, (%rdx,%r14,8)
cmpq %r9, (%rsi,%r14,8)
je 0x1b4b5f
leaq (%r9,%r9,8), %rax
leaq (%rax,%rax,2), %rax
addq 0x20(%rsp), %r9
addq %rax, %r9
leaq 0x48(%rsp), %rax
movq %rax, (%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
movsd 0x40(%rsp), %xmm0
movq %rbp, %r8
callq 0x1b4ba8
movq 0x90(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0xb8(%rsp), %rdx
testl %eax, %eax
je 0x1b4b89
movq 0xb0(%rsp), %rax
movslq (%rax,%r14,4), %rax
incq %rax
movq 0xd0(%rsp), %rcx
cmpq %rax, (%rcx)
jg 0x1b4b89
movq %rax, (%rcx)
movq 0xc8(%rsp), %rax
movq %r14, (%rax)
incq %r14
addq $0x1c, %rbp
cmpq %r14, %r13
jne 0x1b4a42
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq