// File: /workspace/github_cpp_makefile_star2_O3/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, 0x40(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x58(%rsp)
movq %rdi, 0x8(%rsp)
movq %rsi, 0x18(%rsp)
testq %rsi, %rsi
jle 0x3530a
movq 0x18(%rsp), %rcx
leaq -0x1(%rcx), %rax
movq %rax, 0x18(%rsp)
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
movq %rcx, 0x60(%rsp)
addq %rax, %rcx
decq %rcx
movq 0x8(%rsp), %rdx
movl 0x18(%rdx,%rcx), %edx
btl $0x1e, %edx
setae %sil
notl %edx
testl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
sete %dl
orb %sil, %dl
jne 0x34ff0
addq 0x8(%rsp), %rcx
movq 0x58(%rsp), %rdx
movq 0x30(%rdx), %rdx
movl (%rcx), %ecx
leaq (%rcx,%rcx,8), %rcx
movq %rdx, 0x68(%rsp)
cmpl $-0x1, 0x14(%rdx,%rcx,4)
je 0x34ffd
cmpq $0x1, 0x60(%rsp)
jg 0x34f8e
jmp 0x3530a
addq 0x18(%rsp), %rax
movq 0x8(%rsp), %rcx
movl 0xc(%rcx,%rax), %ecx
testl %ecx, %ecx
jns 0x352eb
movq %rcx, %rsi
addq 0x8(%rsp), %rax
movq $-0x1, %r8
xorl %edi, %edi
movl $0x0, 0x14(%rsp)
movl 0x18(%rax), %ecx
btl $0x1e, %ecx
jae 0x3503c
movl (%rax), %r8d
movl %edi, 0x14(%rsp)
movq %rcx, %rax
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andq %rdx, %rax
movq %rax, %r9
cmpq %rdx, %rax
movq $-0x1, %rdx
cmoveq %rdx, %r9
je 0x34ff0
movq %r9, 0x78(%rsp)
movq %r8, 0x80(%rsp)
movl %edi, 0x2c(%rsp)
shldl $0x1, %ecx, %esi
movq %rsi, 0x20(%rsp)
shrl %esi
movq %rsi, 0x90(%rsp)
leaq (%rsi,%rsi,2), %rdx
movq 0x38(%rsp), %rcx
movl 0xc(%rcx,%rdx,8), %ecx
testq %rcx, %rcx
je 0x3531c
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
movq 0x8(%rsp), %rax
leaq (%rax,%rsi), %rdi
movq %rdi, 0x70(%rsp)
movl 0xc(%rax,%rsi), %edi
movl 0x18(%rax,%rsi), %eax
movl %eax, 0x28(%rsp)
shldl $0x1, %eax, %edi
movq 0x38(%rsp), %rax
leaq (%rax,%rdx,8), %rax
movq 0x20(%rsp), %rdx
xorl $0x1, %edx
movq %rdx, 0x20(%rsp)
movq (%rax), %rax
movl %edi, %r13d
shrl %r13d
leaq 0x26114d(%rip), %rdx    # 0x296230
movq 0x18(%rdx), %rdx
movq %rdx, 0x50(%rsp)
leaq 0x260665(%rip), %rdx    # 0x295758
movss 0x158(%rdx), %xmm0
movl %edi, 0x34(%rsp)
andl $-0x2, %edi
movl %edi, 0x30(%rsp)
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rcx
movq %rcx, 0x88(%rsp)
xorl %r12d, %r12d
movq (%rax,%r12), %rbp
movq %rbp, %rsi
shrq $0x20, %rsi
cmpl %esi, 0x90(%rsp)
jne 0x351eb
cmpl %r13d, 0xc(%rax,%r12)
jne 0x351eb
movq 0x50(%rsp), %rcx
movl (%rcx,%r13,8), %r14d
movq 0x18(%rax,%r12), %rdx
testl %edx, %edx
js 0x35163
movl 0x10(%rax,%r12), %r9d
movl 0x14(%rax,%r12), %r11d
subl %r11d, %r14d
movl %r14d, %edi
movl %r9d, %r14d
jmp 0x35173
movl 0x10(%rax,%r12), %r9d
movl 0x14(%rax,%r12), %r11d
subl %r11d, %r14d
movl %r9d, %edi
movq 0x50(%rsp), %rcx
movl (%rcx,%rsi,8), %r15d
cmpl %ebp, %r14d
movl %ebp, %ecx
cmovll %r14d, %ecx
movl 0x8(%rax,%r12), %esi
movl %r15d, %r10d
subl %esi, %r10d
cmpl %edi, %r10d
movl %edi, %ebx
cmovll %r10d, %ebx
cmpl 0x48(%rsp), %ecx
jg 0x351eb
cmpl 0x48(%rsp), %ebx
jg 0x351eb
movl %esi, %r8d
subl %ebp, %r8d
xorps %xmm1, %xmm1
cvtsi2ss %r8, %xmm1
addl %ecx, %r8d
addl %ebx, %r8d
xorps %xmm2, %xmm2
cvtsi2ss %r8, %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0x351eb
subl %r9d, %r11d
xorps %xmm1, %xmm1
cvtsi2ss %r11, %xmm1
addl %ecx, %ebx
addl %r11d, %ebx
xorps %xmm2, %xmm2
cvtsi2ss %rbx, %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
jbe 0x35202
addq $0x28, %r12
cmpq %r12, 0x88(%rsp)
jne 0x35119
jmp 0x3531c
cmpl %edi, %r10d
setl %cl
setg %r10b
cmpl %r14d, %ebp
setg %r11b
setl %r9b
orb %r10b, %r11b
cmpb $0x1, %r11b
jne 0x351eb
orb %cl, %r9b
je 0x351eb
cmpl 0x40(%rsp), %r8d
jl 0x351eb
cmpl 0x40(%rsp), %ebx
jl 0x351eb
testl %edx, %edx
setns %cl
addl %esi, %edi
subl %edi, %r15d
xorl %edi, %edi
movl %ebp, %edx
subl %r14d, %edx
setle %r8b
setg %sil
cmovlel %r15d, %edx
testl %edx, %edx
js 0x351eb
movb %r8b, %dil
shrq $0x1f, %rbp
andl $-0x2, %ebp
orq %rdi, %rbp
cmpl 0x20(%rsp), %ebp
jne 0x351eb
xorb %sil, %cl
movzbl %cl, %ecx
orl 0x30(%rsp), %ecx
xorl 0x34(%rsp), %ecx
cmpl $0x1, %ecx
jne 0x351eb
movl 0x2c(%rsp), %edi
addl %edx, %edi
btl $0x1e, 0x28(%rsp)
movq 0x80(%rsp), %r8
movq 0x78(%rsp), %r9
jae 0x352c8
testq %r8, %r8
js 0x3533b
movq 0x70(%rsp), %rax
movl (%rax), %eax
leaq (%r8,%r8,8), %rcx
movq 0x68(%rsp), %rsi
movl %eax, 0x14(%rsi,%rcx,4)
movl %edi, %edx
subl 0x14(%rsp), %edx
movl %edx, 0x18(%rsi,%rcx,4)
leaq (%rax,%rax,8), %rax
movl %r8d, 0x1c(%rsi,%rax,4)
leaq (%r9,%r9,8), %rax
leaq (%rax,%rax,2), %rcx
addq %r9, %rcx
movq 0x8(%rsp), %rdx
leaq (%rdx,%rcx), %rax
movl 0xc(%rdx,%rcx), %ecx
movq %rcx, %rsi
testl %ecx, %ecx
js 0x3502c
leaq 0x1fb525(%rip), %rdi    # 0x230817
leaq 0x1fb3fa(%rip), %rsi    # 0x2306f3
leaq 0x1fb53a(%rip), %rcx    # 0x23083a
movl $0x58e, %edx            # imm = 0x58E
callq 0x2110
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1fb582(%rip), %rdi    # 0x2308a5
leaq 0x1fb3c9(%rip), %rsi    # 0x2306f3
leaq 0x1fb509(%rip), %rcx    # 0x23083a
movl $0x5b5, %edx            # imm = 0x5B5
callq 0x2110
leaq 0x1fb56f(%rip), %rdi    # 0x2308b1
leaq 0x1fb3aa(%rip), %rsi    # 0x2306f3
leaq 0x1fb4ea(%rip), %rcx    # 0x23083a
movl $0x5b7, %edx            # imm = 0x5B7
callq 0x2110