// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Process_Read.cpp
// Function: determine_chain_distance(ul_ov_t*, long, ul_vec_t*, ma_hit_t_alloc*, long, long, long)
void determine_chain_distance(ul_ov_t *o, int64_t on, ul_vec_t *p, ma_hit_t_alloc *src, int64_t max_hang, int64_t min_ovlp, int64_t rid)
{
	int64_t k, i, m, l = 0, r, last_i, last_dis; ul_ov_t *z = NULL; 
	uint32_t li_v, lj_v, t, qn, tn; ma_hit_t_alloc *x = NULL; asg_arc_t te;

	for (k = on-1; k >= 0; --k) {
		z = &(o[k]);
		if((!z->el) || (z->sec == SEC_MODE)) continue;
		///if z->el = 1, z->qn must work
		if(p->bb.a[z->qn].pidx != (uint32_t)-1) continue;
		last_i = -1; last_dis = 0;
		for (i = k, l = 0; i >= 0;) {
			assert((o[i].tn&((uint32_t)(0x80000000))));
			if(o[i].el) {
				last_i = o[i].qn; last_dis = l;
			}
			m = i; i = o[i].sec; if(o[m].sec == SEC_MODE) i = -1;
			// i = ((o[i].sec == SEC_MODE)?-1:o[i].sec); 
			if(i < 0) break;
			// if(i >= on || i < 0) fprintf(stderr, "m->%ld, i->%ld, rid->%ld, on->%ld, o[m].sec->%u\n", m, i, rid, on, o[m].sec);
			li_v = (o[m].tn<<1)|o[m].rev; li_v ^= 1;
			lj_v = (o[i].tn<<1)|o[i].rev; lj_v ^= 1;

			x = &(src[li_v>>1]);
			for (t = 0; t < x->length; t++) {
				qn = Get_qn(x->buffer[t]);
        		tn = Get_tn(x->buffer[t]);
				if(qn == (li_v>>1) && tn == (lj_v>>1)) {
					r = ma_hit2arc(&(x->buffer[t]), Get_READ_LENGTH(R_INF, qn), Get_READ_LENGTH(R_INF, tn),
						max_hang, asm_opt.max_hang_rate, min_ovlp, &te);
            		if(r < 0) continue;
            		if((te.ul>>32) != li_v || te.v != lj_v) continue;
					l += (uint32_t)te.ul;
					break;
				}
			}
			// if(t>=x->length) {
			// 	fprintf(stderr, "m->%ld(id::%u), i->%ld(id::%u), rid->%ld, on->%ld\n", 
			// 	m, (o[m].tn<<1)>>1, i, (o[i].tn<<1)>>1, rid, on);
			// 	// fprintf(stderr, "[i::%ld]id::%u\t\tq::[%u, %u)\tt::[%u, %u)\n", 
			// 	// 	i, o[i].tn, o[i].qs, o[i].qe, o[i].ts, o[i].te);
			// 	// fprintf(stderr, "[m::%ld]id::%u\t\tq::[%u, %u)\tt::[%u, %u)\n", 
			// 	// 	m, o[m].tn, o[m].qs, o[m].qe, o[m].ts, o[m].te);
			// 	fprintf(stderr, "[i::%ld]id::%u\t%.*s\t%c\tq::[%u, %u)\tt::[%u, %u)\n", 
			// 		i, (o[i].tn<<1)>>1, (int)Get_NAME_LENGTH(R_INF, ((o[i].tn<<1)>>1)), Get_NAME(R_INF, ((o[i].tn<<1)>>1)), 
			// 		"+-"[o[i].rev], o[i].qs, o[i].qe, o[i].ts, o[i].te);
			// 	fprintf(stderr, "[m::%ld]id::%u\t%.*s\t%c\tq::[%u, %u)\tt::[%u, %u)\n", 
			// 		m, (o[m].tn<<1)>>1, (int)Get_NAME_LENGTH(R_INF, ((o[m].tn<<1)>>1)), Get_NAME(R_INF, ((o[m].tn<<1)>>1)), 
			// 		"+-"[o[m].rev], o[m].qs, o[m].qe, o[m].ts, o[m].te);
			// 	exit(1);
			// }
			assert(t<x->length);
			if(!(o[i].el)) continue;
			assert(last_i>=0);
			p->bb.a[last_i].pidx = o[i].qn;
			p->bb.a[last_i].pdis = l - last_dis;///TODO: enable pdis
			p->bb.a[p->bb.a[last_i].pidx].aidx = last_i;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x38(%rsp)
movq 0x8(%rsp), %rax
testq %rax, %rax
jle 0x8fef
decq %rax
movq %rax, 0x8(%rsp)
imulq $0x1c, %rax, %rax
movl 0x18(%rdi,%rax), %ecx
btl $0x1e, %ecx
setae %dl
notl %ecx
testl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
sete %cl
orb %dl, %cl
jne 0x8df3
addq %rdi, %rax
movq 0x28(%rsp), %rcx
movq 0x30(%rcx), %rcx
movl (%rax), %eax
imulq $0x24, %rax, %rax
cmpl $-0x1, 0x14(%rcx,%rax)
jne 0x8df3
movq %rcx, 0x30(%rsp)
xorl %r8d, %r8d
pushq $-0x1
popq %r9
movq $0x0, 0x20(%rsp)
movq 0x8(%rsp), %r10
testq %r10, %r10
js 0x8df3
imulq $0x1c, %r10, %rcx
movl 0xc(%rdi,%rcx), %r15d
testl %r15d, %r15d
jns 0x9020
addq %rdi, %rcx
movl 0x18(%rcx), %eax
btl $0x1e, %eax
jae 0x8e85
movl (%rcx), %r9d
movq %r8, 0x20(%rsp)
movq %rax, %rcx
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andq %rdx, %rcx
movq %rcx, %rsi
cmpq %rdx, %rcx
pushq $-0x1
popq %rdx
cmoveq %rdx, %rsi
je 0x8df3
movq %rsi, 0x50(%rsp)
movq %r9, 0x58(%rsp)
movq %r8, 0x60(%rsp)
shldl $0x1, %eax, %r15d
movl %r15d, %eax
xorl $0x1, %eax
movq %rax, 0x68(%rsp)
imulq $0x1c, %rcx, %rax
leaq (%rdi,%rax), %rcx
movq %rcx, 0x48(%rsp)
movl 0xc(%rdi,%rax), %edx
movl 0x18(%rdi,%rax), %eax
movl %eax, 0x14(%rsp)
shldl $0x1, %eax, %edx
shrl %r15d
imulq $0x18, %r15, %rax
movq 0x40(%rsp), %rcx
leaq (%rcx,%rax), %r12
movl 0xc(%rcx,%rax), %eax
movl %edx, 0x18(%rsp)
movl %edx, %r13d
shrl %r13d
leaq 0x24b32c(%rip), %rcx    # 0x254230
movq 0x18(%rcx), %rbx
leaq 0x24a849(%rip), %rcx    # 0x253758
movss 0x158(%rcx), %xmm0
movss %xmm0, 0x1c(%rsp)
imulq $0x28, %rax, %r14
xorl %ebp, %ebp
cmpq %rbp, %r14
je 0x9001
movq (%r12), %rdi
movq (%rdi,%rbp), %rax
shrq $0x20, %rax
cmpl %eax, %r15d
jne 0x8f94
cmpl %r13d, 0xc(%rdi,%rbp)
jne 0x8f94
addq %rbp, %rdi
movl (%rbx,%rax,8), %esi
movl (%rbx,%r13,8), %edx
movq 0x70(%rsp), %rcx
movss 0x1c(%rsp), %xmm0
movq 0x78(%rsp), %r8
leaq 0x80(%rsp), %r9
callq 0x905e
testl %eax, %eax
js 0x8f94
movq 0x80(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
cmpl 0x68(%rsp), %ecx
jne 0x8f94
movl 0x88(%rsp), %ecx
xorl 0x18(%rsp), %ecx
cmpl $0x1, %ecx
je 0x8f9a
addq $0x28, %rbp
jmp 0x8f23
movq 0x60(%rsp), %r8
addq %rax, %r8
btl $0x1e, 0x14(%rsp)
movq 0x38(%rsp), %rdi
movq 0x58(%rsp), %r9
movq 0x50(%rsp), %r10
jae 0x8e5a
testq %r9, %r9
js 0x903f
movq 0x48(%rsp), %rax
movl (%rax), %eax
imulq $0x24, %r9, %rcx
movq 0x30(%rsp), %rsi
movl %eax, 0x14(%rsi,%rcx)
movl %r8d, %edx
subl 0x20(%rsp), %edx
movl %edx, 0x18(%rsi,%rcx)
imulq $0x24, %rax, %rax
movl %r9d, 0x1c(%rsi,%rax)
jmp 0x8e5a
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d989d(%rip), %rdi    # 0x1e28a5
leaq 0x1d96e4(%rip), %rsi    # 0x1e26f3
leaq 0x1d9824(%rip), %rcx    # 0x1e283a
movl $0x5b5, %edx            # imm = 0x5B5
callq 0x2110
leaq 0x1d97f0(%rip), %rdi    # 0x1e2817
leaq 0x1d96c5(%rip), %rsi    # 0x1e26f3
leaq 0x1d9805(%rip), %rcx    # 0x1e283a
movl $0x58e, %edx            # imm = 0x58E
callq 0x2110
leaq 0x1d986b(%rip), %rdi    # 0x1e28b1
leaq 0x1d96a6(%rip), %rsi    # 0x1e26f3
leaq 0x1d97e6(%rip), %rcx    # 0x1e283a
movl $0x5b7, %edx            # imm = 0x5B7
callq 0x2110