// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x10(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jl 0xe831
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xe47e
movq -0x70(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
cmpl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jne 0xe483
jmp 0xe820
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x70(%rbp), %rcx
movl (%rcx), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
cmpl $-0x1, 0x14(%rax)
je 0xe4a3
jmp 0xe820
movq $-0x1, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
cmpq $0x0, -0x40(%rbp)
jl 0xe81e
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0xe4e9
jmp 0xe508
leaq 0x3730f6(%rip), %rdi    # 0x3815e6
leaq 0x372fcb(%rip), %rsi    # 0x3814c2
movl $0x58e, %edx            # imm = 0x58E
leaq 0x373106(%rip), %rcx    # 0x381609
callq 0x2130
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xe53c
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl %eax, %eax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
cmpl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jne 0xe581
movq $-0x1, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jge 0xe58d
jmp 0xe81e
movq -0x8(%rbp), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shll %eax
movq -0x8(%rbp), %rcx
imulq $0x1c, -0x48(%rbp), %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shll %eax
movq -0x8(%rbp), %rcx
imulq $0x1c, -0x40(%rbp), %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x78(%rbp)
movq -0x20(%rbp), %rax
movl -0x74(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movq -0x90(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0xe739
movq -0x90(%rbp), %rax
movq (%rax), %rax
movl -0x7c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x80(%rbp)
movq -0x90(%rbp), %rax
movq (%rax), %rax
movl -0x7c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x84(%rbp)
movl -0x80(%rbp), %eax
movl -0x74(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xe729
movl -0x84(%rbp), %eax
movl -0x78(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0xe729
movq -0x90(%rbp), %rax
movq (%rax), %rdi
movl -0x7c(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0x3b6c35(%rip), %rax    # 0x3c52d0
movq 0x18(%rax), %rax
movl -0x80(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %esi
leaq 0x3b6c21(%rip), %rax    # 0x3c52d0
movq 0x18(%rax), %rax
movl -0x84(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movl %eax, %ecx
leaq 0x3b612c(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movq -0x30(%rbp), %rax
movl %eax, %r8d
leaq -0xa8(%rbp), %r9
callq 0xe840
cltq
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jge 0xe6f6
jmp 0xe72b
movq -0xa8(%rbp), %rax
shrq $0x20, %rax
movl -0x74(%rbp), %ecx
cmpq %rcx, %rax
jne 0xe714
movl -0xa0(%rbp), %eax
cmpl -0x78(%rbp), %eax
je 0xe716
jmp 0xe72b
movq -0xa8(%rbp), %rax
movl %eax, %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0xe739
jmp 0xe72b
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xe60f
movl -0x7c(%rbp), %eax
movq -0x90(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0xe74a
jmp 0xe769
leaq 0x372f23(%rip), %rdi    # 0x381674
leaq 0x372d6a(%rip), %rsi    # 0x3814c2
movl $0x5b5, %edx            # imm = 0x5B5
leaq 0x372ea5(%rip), %rcx    # 0x381609
callq 0x2130
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xe788
jmp 0xe4c3
cmpq $0x0, -0x60(%rbp)
jl 0xe791
jmp 0xe7b0
leaq 0x372ee8(%rip), %rdi    # 0x381680
leaq 0x372d23(%rip), %rsi    # 0x3814c2
movl $0x5b7, %edx            # imm = 0x5B7
leaq 0x372e5e(%rip), %rcx    # 0x381609
callq 0x2130
movq -0x8(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x60(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movq -0x50(%rbp), %rax
subq -0x68(%rbp), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x60(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movq -0x60(%rbp), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x18(%rbp), %rdx
movq 0x30(%rdx), %rdx
imulq $0x24, -0x60(%rbp), %rsi
addq %rsi, %rdx
movl 0x14(%rdx), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x1c(%rax)
jmp 0xe4c3
jmp 0xe820
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xe43e
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)