// File: chhylp123[P]hifiasm/inter.cpp
// Function: append_inexact_edges(ma_ug_t*, ug_opt_t const*, asg_t*)
void append_inexact_edges(ma_ug_t *ug, const ug_opt_t *uopt, asg_t *rg)
{
	uint32_t *idx = NULL, n_read = R_INF.total_reads, z, v, k, qn, tn, tu, ut_v, ut_w; 
	ma_utg_t *u = NULL; ma_hit_t_alloc *src = uopt->sources, *s = NULL;
	int32_t r; asg_arc_t t, *p = NULL;
	int64_t min_ovlp = uopt->min_ovlp, max_hang = uopt->max_hang, occ = 0;

	MALLOC(idx, n_read); memset(idx, -1, n_read*sizeof(*(idx)));
	for (z = 0; z < ug->u.n; z++) {
		u = &(ug->u.a[z]);
		if(u->circ) continue;
		idx[u->start>>1] = idx[u->end>>1] = z;
	}

	for (z = 0; z < ug->u.n; z++) {
		u = &(ug->u.a[z]);
		if(u->circ) continue;

		v = u->end^1; s = &(src[v>>1]); ut_v = (z<<1);
		for (k = 0; k < s->length; k++) {
			if(s->buffer[k].el) continue;///we just need inexact edges
			qn = Get_qn(s->buffer[k]); tn = Get_tn(s->buffer[k]); tu = idx[tn]; ut_w = (uint32_t)-1;
			if(tu == (uint32_t)-1 || ug->g->seq[tu].del) continue;
			if((Get_qe(s->buffer[k]) - Get_qs(s->buffer[k])) < min_ovlp) continue;
            if((Get_te(s->buffer[k]) - Get_ts(s->buffer[k])) < min_ovlp) continue;
			r = ma_hit2arc(&(s->buffer[k]), rg->seq[qn].len, rg->seq[tn].len, max_hang, asm_opt.max_hang_rate, min_ovlp, &t);
			if(r < 0 || (t.ul>>32) != v) continue;
			if(t.v == ug->u.a[tu].start) ut_w = tu<<1;
			if(t.v == ug->u.a[tu].end) ut_w = (tu<<1)+1;
			if(ut_w==(uint32_t)-1) continue;
			p = asg_arc_pushp(ug->g); memset(p, 0, sizeof(*p));
			*p = t; p->ul = ut_v; p->ul <<= 32; p->ul += ((uint32_t)(t.ul)); p->v = ut_w;
			occ++;
			// if((p->v>>1)>=ug->g->n_seq || (p->ul>>33)>=ug->g->n_seq) {
			// 	fprintf(stderr, "+ug->g->n_seq:%u, (p->ul>>33):%u, (p->v>>1):%u\n", 
			// 	(uint32_t)ug->g->n_seq, (uint32_t)(p->ul>>33), (uint32_t)(p->v>>1));
			// }
			// assert((p->v>>1)<ug->g->n_seq && (p->ul>>33)<ug->g->n_seq);
		}

		v = u->start^1; s = &(src[v>>1]); ut_v = (z<<1) + 1;
		for (k = 0; k < s->length; k++) {
			if(s->buffer[k].el) continue;///we just need inexact edges
			qn = Get_qn(s->buffer[k]); tn = Get_tn(s->buffer[k]); tu = idx[tn]; ut_w = (uint32_t)-1;
			if(tu == (uint32_t)-1 || ug->g->seq[tu].del) continue;
			if((Get_qe(s->buffer[k]) - Get_qs(s->buffer[k])) < min_ovlp) continue;
            if((Get_te(s->buffer[k]) - Get_ts(s->buffer[k])) < min_ovlp) continue;
			r = ma_hit2arc(&(s->buffer[k]), rg->seq[qn].len, rg->seq[tn].len, max_hang, asm_opt.max_hang_rate, min_ovlp, &t);
			if(r < 0 || (t.ul>>32) != v) continue;
			if(t.v == ug->u.a[tu].start) ut_w = tu<<1;
			if(t.v == ug->u.a[tu].end) ut_w = (tu<<1)+1;
			if(ut_w==(uint32_t)-1) continue;
			p = asg_arc_pushp(ug->g); memset(p, 0, sizeof(*p));
			*p = t; p->ul = ut_v; p->ul <<= 32; p->ul += ((uint32_t)(t.ul)); p->v = ut_w;
			occ++;
			// if((p->v>>1)>=ug->g->n_seq || (p->ul>>33)>=ug->g->n_seq) {
			// 	fprintf(stderr, "+ug->g->n_seq:%u, (p->ul>>33):%u, (p->v>>1):%u\n", 
			// 	(uint32_t)ug->g->n_seq, (uint32_t)(p->ul>>33), (uint32_t)(p->v>>1));
			// }
			// assert((p->v>>1)<ug->g->n_seq && (p->ul>>33)<ug->g->n_seq);
		}
	}
	if(occ) {
		free(ug->g->idx);
        ug->g->idx = 0;
        ug->g->is_srt = 0;
		asg_cleanup(ug->g); 
	}
	
	free(idx);
	///for debug
	debug_append_inexact_edges(ug, uopt);
	fprintf(stderr, "[M::%s] # inserted inexact edges: %ld\n", __func__, occ);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
leaq 0xcb95a(%rip), %rax     # 0x3c52d0
movq 0x50(%rax), %rax
movl %eax, -0x24(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x10(%rbp), %rax
movslq 0x44(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rax
movslq 0x40(%rax), %rax
movq %rax, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movl -0x24(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x2270
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, %edx
shlq $0x2, %rdx
movl $0xff, %esi
callq 0x2100
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x2f9a61
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2f9a2f
jmp 0x2f9a56
movl -0x28(%rbp), %edx
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
movl 0x4(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x2f99fd
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x2fa020
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2f9aa1
jmp 0x2fa012
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
xorl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x58(%rbp), %rax
movl -0x2c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl -0x28(%rbp), %eax
shll %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x2f9d57
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, 0x20(%rax)
je 0x2f9aff
jmp 0x2f9d49
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x34(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x3c(%rbp)
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
cmpl $-0x1, -0x3c(%rbp)
je 0x2f9b65
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2f9b6a
jmp 0x2f9d49
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movl -0x30(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
cmpq -0x90(%rbp), %rax
jge 0x2f9ba4
jmp 0x2f9d49
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movl -0x30(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
subl 0x10(%rcx), %eax
movl %eax, %eax
cmpq -0x90(%rbp), %rax
jge 0x2f9bdc
jmp 0x2f9d49
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl -0x30(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq -0x98(%rbp), %rax
movl %eax, %ecx
leaq 0xcabd3(%rip), %rax     # 0x3c47f8
movss 0x158(%rax), %xmm0
movq -0x90(%rbp), %rax
movl %eax, %r8d
leaq -0x80(%rbp), %r9
callq 0x287f40
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jl 0x2f9c59
movq -0x80(%rbp), %rax
shrq $0x20, %rax
movl -0x2c(%rbp), %ecx
cmpq %rcx, %rax
je 0x2f9c5e
jmp 0x2f9d49
movl -0x78(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jne 0x2f9c80
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x44(%rbp)
movl -0x78(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jne 0x2f9ca5
movl -0x3c(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
jne 0x2f9cb0
jmp 0x2f9d49
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x2f6e80
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movq -0x88(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, (%rax)
movq -0x78(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x40(%rbp), %eax
movl %eax, %ecx
movq -0x88(%rbp), %rax
movq %rcx, (%rax)
movq -0x88(%rbp), %rax
movq (%rax), %rcx
shlq $0x20, %rcx
movq %rcx, (%rax)
movq -0x80(%rbp), %rax
movl %eax, %eax
movl %eax, %ecx
movq -0x88(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl -0x44(%rbp), %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x2f9ad3
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
xorl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x58(%rbp), %rax
movl -0x2c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl -0x28(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x2fa010
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, 0x20(%rax)
je 0x2f9db8
jmp 0x2fa002
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x34(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x3c(%rbp)
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
cmpl $-0x1, -0x3c(%rbp)
je 0x2f9e1e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2f9e23
jmp 0x2fa002
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movl -0x30(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
subl %ecx, %eax
movl %eax, %eax
cmpq -0x90(%rbp), %rax
jge 0x2f9e5d
jmp 0x2fa002
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movl -0x30(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
subl 0x10(%rcx), %eax
movl %eax, %eax
cmpq -0x90(%rbp), %rax
jge 0x2f9e95
jmp 0x2fa002
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movl -0x30(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq -0x98(%rbp), %rax
movl %eax, %ecx
leaq 0xca91a(%rip), %rax     # 0x3c47f8
movss 0x158(%rax), %xmm0
movq -0x90(%rbp), %rax
movl %eax, %r8d
leaq -0x80(%rbp), %r9
callq 0x287f40
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jl 0x2f9f12
movq -0x80(%rbp), %rax
shrq $0x20, %rax
movl -0x2c(%rbp), %ecx
cmpq %rcx, %rax
je 0x2f9f17
jmp 0x2fa002
movl -0x78(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jne 0x2f9f39
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x44(%rbp)
movl -0x78(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jne 0x2f9f5e
movl -0x3c(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
jne 0x2f9f69
jmp 0x2fa002
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x2f6e80
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movq -0x88(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, (%rax)
movq -0x78(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x40(%rbp), %eax
movl %eax, %ecx
movq -0x88(%rbp), %rax
movq %rcx, (%rax)
movq -0x88(%rbp), %rax
movq (%rax), %rcx
shlq $0x20, %rcx
movq %rcx, (%rax)
movq -0x80(%rbp), %rax
movl %eax, %eax
movl %eax, %ecx
movq -0x88(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl -0x44(%rbp), %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x2f9d8c
jmp 0x2fa012
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x2f9a68
cmpq $0x0, -0xa0(%rbp)
je 0x2fa06f
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rdi
callq 0x2210
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq $0x0, 0x28(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0xcdd10
movq -0x20(%rbp), %rdi
callq 0x2210
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2f9710
movq 0xc9f54(%rip), %rax     # 0x3c3fe0
movq (%rax), %rdi
movq -0xa0(%rbp), %rcx
leaq 0x9b340(%rip), %rsi     # 0x3953dd
leaq 0x9b324(%rip), %rdx     # 0x3953c8
movb $0x0, %al
callq 0x22a0
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)