// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movq %r9, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq %rdx, 0x60(%rsp)
movq %rsi, 0x10(%rsp)
testq %rsi, %rsi
jle 0x35c1a
movq %rcx, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x10(%rsp), %rdx
leaq -0x1(%rdx), %rax
movq %rax, 0x10(%rsp)
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
movq %rdx, 0x70(%rsp)
addq %rdx, %rax
decq %rax
movl 0x18(%rdi,%rax), %esi
btl $0x1e, %esi
setae %dl
notl %esi
testl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
sete %sil
orb %dl, %sil
jne 0x359df
addq %rdi, %rax
movq 0x60(%rsp), %rdx
movq 0x30(%rdx), %rdx
movl (%rax), %eax
leaq (%rax,%rax,8), %rax
movq %rdx, 0x68(%rsp)
cmpl $-0x1, 0x14(%rdx,%rax,4)
je 0x359ec
cmpq $0x1, 0x70(%rsp)
jg 0x35984
jmp 0x35c1a
movq $-0x1, %rbx
movq 0x10(%rsp), %r10
movq $0x0, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
leaq (%r10,%r10,8), %rax
leaq (%rax,%rax,2), %rdx
addq %r10, %rdx
movl 0xc(%rdi,%rdx), %esi
testl %esi, %esi
jns 0x35c2c
addq %rdi, %rdx
movl 0x18(%rdx), %eax
btl $0x1e, %eax
jae 0x35a39
movl (%rdx), %ebx
movq 0x18(%rsp), %rdx
movq %rdx, 0x40(%rsp)
movq %rax, %r11
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andq %rdx, %r11
movq %r11, %r10
cmpq %rdx, %r11
movq $-0x1, %rdx
cmoveq %rdx, %r10
je 0x359df
shldl $0x1, %eax, %esi
leaq (%r10,%r10,8), %rax
leaq (%rax,%rax,2), %rdx
addq %r10, %rdx
movl 0x18(%rdi,%rdx), %r8d
movl %esi, %ebp
shrl %ebp
leaq (,%rbp,2), %rax
addq %rbp, %rax
movl 0xc(%rcx,%rax,8), %r9d
testq %r9, %r9
setne %r14b
movq %rdx, 0x78(%rsp)
je 0x35bb8
movq %r11, 0x48(%rsp)
movq %r10, 0x50(%rsp)
movq %rbx, 0x58(%rsp)
movl 0xc(%rdi,%rdx), %edx
movl %r8d, 0xc(%rsp)
shldl $0x1, %r8d, %edx
leaq (%rcx,%rax,8), %rax
xorl $0x1, %esi
movq %rsi, 0x80(%rsp)
movq (%rax), %r13
movl %edx, 0x24(%rsp)
shrl %edx
movl %edx, 0x2c(%rsp)
leaq 0x25575f(%rip), %rax    # 0x28b230
movq 0x18(%rax), %rax
movq %rax, 0x98(%rsp)
leaq 0x254c74(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movss %xmm0, 0x28(%rsp)
movq %r9, %r12
negq %r12
movl $0x1, %r15d
movq %r9, %rbx
movq (%r13), %rax
shrq $0x20, %rax
cmpl %eax, %ebp
jne 0x35b7b
movl 0xc(%r13), %ecx
cmpl 0x2c(%rsp), %ecx
jne 0x35b7b
movq 0x98(%rsp), %rdx
movl (%rdx,%rax,8), %esi
movl (%rdx,%rcx,8), %edx
movq %r13, %rdi
movq 0x88(%rsp), %rcx
movss 0x28(%rsp), %xmm0
movq 0x90(%rsp), %r8
leaq 0xa0(%rsp), %r9
callq 0x35c89
testl %eax, %eax
js 0x35b7b
movq 0xa0(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
cmpl 0x80(%rsp), %ecx
jne 0x35b7b
movl 0xa8(%rsp), %ecx
xorl 0x24(%rsp), %ecx
cmpl $0x1, %ecx
je 0x35c13
cmpq %rbx, %r15
setb %r14b
addq $0x28, %r13
leaq (%r12,%r15), %rax
incq %rax
incq %r15
cmpq $0x1, %rax
jne 0x35b01
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rbx
movq 0x50(%rsp), %r10
movq 0x48(%rsp), %r11
movl 0xc(%rsp), %r8d
testb $0x1, %r14b
je 0x35c4b
btl $0x1e, %r8d
movq 0x78(%rsp), %rax
jae 0x35c01
testq %rbx, %rbx
js 0x35c6a
addq %rdi, %rax
movl (%rax), %eax
leaq (%rbx,%rbx,8), %rsi
movq 0x68(%rsp), %r8
movl %eax, 0x14(%r8,%rsi,4)
movq 0x18(%rsp), %rdx
subl 0x40(%rsp), %edx
movl %edx, 0x18(%r8,%rsi,4)
leaq (%rax,%rax,8), %rax
movl %ebx, 0x1c(%r8,%rax,4)
cmpl $0x3fffffff, %r11d      # imm = 0x3FFFFFFF
jne 0x35a0a
jmp 0x359df
addq %rax, 0x18(%rsp)
jmp 0x35b9a
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1eebd4(%rip), %rdi    # 0x224807
leaq 0x1eeaa9(%rip), %rsi    # 0x2246e3
leaq 0x1eebe9(%rip), %rcx    # 0x22482a
movl $0x58e, %edx            # imm = 0x58E
callq 0x2110
leaq 0x1eec43(%rip), %rdi    # 0x224895
leaq 0x1eea8a(%rip), %rsi    # 0x2246e3
leaq 0x1eebca(%rip), %rcx    # 0x22482a
movl $0x5b5, %edx            # imm = 0x5B5
callq 0x2110
leaq 0x1eec30(%rip), %rdi    # 0x2248a1
leaq 0x1eea6b(%rip), %rsi    # 0x2246e3
leaq 0x1eebab(%rip), %rcx    # 0x22482a
movl $0x5b7, %edx            # imm = 0x5B7
callq 0x2110