// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, 0x60(%rsp)
movq %rdi, %r14
leaq 0xbbbe6(%rip), %rax     # 0x254230
movq 0x50(%rax), %rax
movq 0x8(%rsi), %rcx
movq %rcx, 0x58(%rsp)
movslq 0x44(%rsi), %rcx
movq %rcx, 0x8(%rsp)
movq %rsi, 0x68(%rsp)
movl 0x40(%rsi), %ecx
movl %ecx, 0x1c(%rsp)
movl %eax, %ebx
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x2280
movq %rax, %r15
movq %rax, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x20e0
movq %r14, %r9
movq (%r14), %rax
xorl %ecx, %ecx
movl %ecx, %esi
cmpq %rsi, %rax
jbe 0x1986c3
movq 0x10(%r9), %rdx
imulq $0x28, %rsi, %rsi
cmpl $0x0, (%rdx,%rsi)
js 0x1986bf
addq %rsi, %rdx
movl 0x8(%rdx), %esi
shrl %esi
movl %ecx, (%r15,%rsi,4)
movl 0x4(%rdx), %edx
shrl %edx
movl %ecx, (%r15,%rdx,4)
incl %ecx
jmp 0x198695
xorl %edx, %edx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq $0x0, 0x20(%rsp)
movq %r9, 0x10(%rsp)
movq 0x8(%rsp), %r8
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x198a11
movq 0x10(%r9), %rsi
imulq $0x28, %rdx, %rcx
cmpl $0x0, (%rsi,%rcx)
js 0x198a0a
addq %rcx, %rsi
movq %rsi, 0x30(%rsp)
movl 0x8(%rsi), %eax
movq %rax, %rcx
xorq $0x1, %rcx
movq %rcx, 0x28(%rsp)
shrl %eax
imulq $0x18, %rax, %r13
addq 0x58(%rsp), %r13
leal (%rdx,%rdx), %eax
movq %rax, 0x38(%rsp)
movq %rdx, 0x80(%rsp)
shlq $0x21, %rdx
movq %rdx, 0x78(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
movl 0xc(%r13), %eax
cmpq %rax, %r14
jae 0x198880
movq (%r13), %rdi
cmpb $0x0, 0x20(%rdi,%rbp)
je 0x19875c
incq %r14
addq $0x28, %rbp
jmp 0x19873b
movl 0xc(%rdi,%rbp), %eax
movl (%r15,%rax,4), %r12d
cmpq %r10, %r12
je 0x198753
movq 0x18(%r9), %rbx
movq 0x20(%rbx), %rcx
cmpl $0x0, (%rcx,%r12,8)
js 0x198753
movq (%rdi,%rbp), %rcx
movl 0x8(%rdi,%rbp), %edx
subl %ecx, %edx
cmpq %r8, %rdx
jl 0x198753
movl 0x14(%rdi,%rbp), %edx
subl 0x10(%rdi,%rbp), %edx
cmpq %r8, %rdx
jl 0x198753
addq %rbp, %rdi
shrq $0x20, %rcx
movq 0x60(%rsp), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rcx,8), %esi
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %esi
movl (%rdx,%rax,8), %edx
andl %ecx, %edx
leaq 0xbaf9e(%rip), %rax     # 0x253758
movss 0x158(%rax), %xmm0
movl 0x1c(%rsp), %ecx
leaq 0x40(%rsp), %r9
callq 0x15d560
testl %eax, %eax
js 0x19880c
movq 0x40(%rsp), %rdi
movq %rdi, %rax
shrq $0x20, %rax
cmpl 0x28(%rsp), %eax
movq 0x10(%rsp), %r9
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
jne 0x198876
movl 0x48(%rsp), %eax
movq 0x10(%r9), %rcx
imulq $0x28, %r12, %rdx
cmpl 0x4(%rcx,%rdx), %eax
je 0x198821
pushq $-0x1
popq %rsi
jmp 0x198825
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
jmp 0x198753
leal (%r12,%r12), %esi
cmpl 0x8(%rcx,%rdx), %eax
leal 0x1(%r12,%r12), %r12d
cmovnel %esi, %r12d
cmpl $-0x1, %r12d
je 0x198876
movq %rdi, 0x70(%rsp)
movq %rbx, %rdi
callq 0x197052
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r9
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rax)
movq 0x50(%rsp), %rcx
movq %rcx, 0x10(%rax)
movl 0x70(%rsp), %ecx
orq 0x78(%rsp), %rcx
movq %rcx, (%rax)
movl %r12d, 0x8(%rax)
incq 0x20(%rsp)
movq 0x8(%rsp), %r8
jmp 0x198753
movq 0x30(%rsp), %rax
movl 0x4(%rax), %eax
movq %rax, %rcx
xorq $0x1, %rcx
movq %rcx, 0x30(%rsp)
shrl %eax
imulq $0x18, %rax, %rbp
addq 0x58(%rsp), %rbp
movq 0x38(%rsp), %rax
orl $0x1, %eax
shlq $0x20, %rax
movq %rax, 0x38(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
movl 0xc(%rbp), %eax
cmpq %rax, %r14
jae 0x1989ff
movq (%rbp), %rdi
cmpb $0x0, 0x20(%rdi,%r13)
je 0x1988d7
incq %r14
addq $0x28, %r13
jmp 0x1988b6
movl 0xc(%rdi,%r13), %eax
movl (%r15,%rax,4), %r12d
cmpq %r10, %r12
je 0x1988ce
movq 0x18(%r9), %rbx
movq 0x20(%rbx), %rcx
cmpl $0x0, (%rcx,%r12,8)
js 0x1988ce
movq (%rdi,%r13), %rcx
movl 0x8(%rdi,%r13), %edx
subl %ecx, %edx
cmpq %r8, %rdx
jl 0x1988ce
movl 0x14(%rdi,%r13), %edx
subl 0x10(%rdi,%r13), %edx
cmpq %r8, %rdx
jl 0x1988ce
addq %r13, %rdi
shrq $0x20, %rcx
movq 0x60(%rsp), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rcx,8), %esi
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %esi
movl (%rdx,%rax,8), %edx
andl %ecx, %edx
leaq 0xbae1f(%rip), %rax     # 0x253758
movss 0x158(%rax), %xmm0
movl 0x1c(%rsp), %ecx
leaq 0x40(%rsp), %r9
callq 0x15d560
testl %eax, %eax
js 0x19898b
movq 0x40(%rsp), %rdi
movq %rdi, %rax
shrq $0x20, %rax
cmpl 0x30(%rsp), %eax
movq 0x10(%rsp), %r9
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
jne 0x1989f5
movl 0x48(%rsp), %eax
movq 0x10(%r9), %rcx
imulq $0x28, %r12, %rdx
cmpl 0x4(%rcx,%rdx), %eax
je 0x1989a0
pushq $-0x1
popq %rsi
jmp 0x1989a4
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
jmp 0x1988ce
leal (%r12,%r12), %esi
cmpl 0x8(%rcx,%rdx), %eax
leal 0x1(%r12,%r12), %r12d
cmovnel %esi, %r12d
cmpl $-0x1, %r12d
je 0x1989f5
movq %rdi, 0x28(%rsp)
movq %rbx, %rdi
callq 0x197052
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r9
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rax)
movq 0x50(%rsp), %rcx
movq %rcx, 0x10(%rax)
movl 0x28(%rsp), %ecx
orq 0x38(%rsp), %rcx
movq %rcx, (%rax)
movl %r12d, 0x8(%rax)
incq 0x20(%rsp)
movq 0x8(%rsp), %r8
jmp 0x1988ce
movq (%r9), %rax
movq 0x80(%rsp), %rdx
incl %edx
jmp 0x1986de
cmpq $0x0, 0x20(%rsp)
je 0x198a41
movq 0x10(%rsp), %rbx
movq 0x18(%rbx), %rax
movq 0x28(%rax), %rdi
callq 0x2220
movq 0x18(%rbx), %rax
andq $0x0, 0x28(%rax)
andb $0x7f, 0x7(%rax)
movq 0x18(%rbx), %rdi
callq 0x694eb
movq %r15, %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x19850d
movq 0xba581(%rip), %rax     # 0x252fe0
movq (%rax), %rdi
leaq 0x5dc68(%rip), %rsi     # 0x1f66d1
leaq 0x5dc4c(%rip), %rdx     # 0x1f66bc
movq 0x20(%rsp), %rcx
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0