// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
leaq 0x26422f(%rip), %rax    # 0x28b230
movq 0x50(%rax), %rax
movq 0x8(%rsi), %rcx
movq %rcx, 0x18(%rsp)
movslq 0x44(%rsi), %rcx
movq %rcx, 0x8(%rsp)
movq %rsi, 0x68(%rsp)
movl 0x40(%rsi), %ecx
movl %ecx, 0x2c(%rsp)
movl %eax, %r14d
shlq $0x2, %r14
movq %r14, %rdi
callq 0x2280
movq %rax, %r15
movq %rax, %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x20e0
movq %rbx, %r10
movq (%rbx), %rax
testq %rax, %rax
je 0x27082
movq 0x10(%r10), %rcx
addq $0x8, %rcx
pushq $0x1
popq %rdx
cmpl $0x0, -0x8(%rcx)
js 0x27075
leal -0x1(%rdx), %esi
movl (%rcx), %edi
shrl %edi
movl %esi, (%r15,%rdi,4)
movl -0x4(%rcx), %edi
shrl %edi
movl %esi, (%r15,%rdi,4)
movl %edx, %esi
addq $0x28, %rcx
incl %edx
cmpq %rsi, %rax
ja 0x2705b
xorl %ecx, %ecx
cmpq $0x0, (%r10)
movq %r10, 0x10(%rsp)
jne 0x2709d
movq $0x0, 0x30(%rsp)
jmp 0x274ed
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq $0x0, 0x30(%rsp)
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movq 0x10(%r10), %rsi
imulq $0x28, %rcx, %rax
cmpl $0x0, (%rsi,%rax)
movq %rcx, 0x80(%rsp)
js 0x274da
addq %rax, %rsi
movl 0x8(%rsi), %edx
movl %edx, %eax
shrl %eax
imulq $0x18, %rax, %rbp
leal (%rcx,%rcx), %eax
cmpl $0x0, 0xc(%r9,%rbp)
movq %rax, 0x40(%rsp)
je 0x272de
addq %r9, %rbp
xorq $0x1, %rdx
movq %rdx, 0x38(%rsp)
shlq $0x20, %rax
movq %rax, 0x78(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %rsi, 0x20(%rsp)
movq (%rbp), %rdi
cmpb $0x0, 0x20(%rdi,%r13)
je 0x27132
incq %r14
movl 0xc(%rbp), %eax
addq $0x28, %r13
cmpq %rax, %r14
jb 0x27112
jmp 0x272de
movl 0xc(%rdi,%r13), %eax
movl (%r15,%rax,4), %ebx
cmpq %r11, %rbx
je 0x2711e
movq 0x18(%r10), %r12
movq 0x20(%r12), %rcx
cmpl $0x0, (%rcx,%rbx,8)
js 0x2711e
movq (%rdi,%r13), %rcx
movl 0x8(%rdi,%r13), %edx
subl %ecx, %edx
cmpq %r8, %rdx
jl 0x2711e
movl 0x14(%rdi,%r13), %edx
subl 0x10(%rdi,%r13), %edx
cmpq %r8, %rdx
jl 0x2711e
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 0x2635c4(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movl 0x2c(%rsp), %ecx
leaq 0x48(%rsp), %r9
callq 0x19c0d3
testl %eax, %eax
js 0x271f1
movq 0x48(%rsp), %rdi
movq %rdi, %rax
shrq $0x20, %rax
cmpl 0x38(%rsp), %eax
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0x20(%rsp), %rsi
jne 0x2720b
movl 0x50(%rsp), %eax
movq 0x10(%r10), %rcx
imulq $0x28, %rbx, %rdx
cmpl 0x4(%rcx,%rdx), %eax
movq 0x8(%rsp), %r8
je 0x27215
pushq $-0x1
popq %rsi
jmp 0x27218
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
jmp 0x272d4
movq 0x8(%rsp), %r8
jmp 0x2711e
leal (%rbx,%rbx), %esi
cmpl 0x8(%rcx,%rdx), %eax
leal 0x1(%rbx,%rbx), %ebx
cmovnel %esi, %ebx
cmpl $-0x1, %ebx
je 0x272d4
movl 0x4(%r12), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
movl (%r12), %eax
cmpl %eax, %ecx
jne 0x27281
leal (%rax,%rax), %ecx
testl %eax, %eax
pushq $0x10
popq %rax
cmovel %eax, %ecx
movl %ecx, (%r12)
movq %rdi, 0x70(%rsp)
movq 0x8(%r12), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq 0x70(%rsp), %rdi
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r10
movq %rax, 0x8(%r12)
movq 0x8(%r12), %rax
movl 0x4(%r12), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%r12)
imulq $0x18, %rdx, %rcx
movups 0x48(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
movq 0x58(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movl %edi, %edx
orq 0x78(%rsp), %rdx
movq %rdx, (%rax,%rcx)
movl %ebx, 0x8(%rax,%rcx)
incq 0x30(%rsp)
movq 0x20(%rsp), %rsi
jmp 0x2711e
movl 0x4(%rsi), %ecx
movl %ecx, %eax
shrl %eax
imulq $0x18, %rax, %rbp
cmpl $0x0, 0xc(%r9,%rbp)
je 0x274da
addq %r9, %rbp
movq 0x40(%rsp), %rax
orl $0x1, %eax
xorq $0x1, %rcx
movq %rcx, 0x20(%rsp)
shlq $0x20, %rax
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
movq (%rbp), %rdi
cmpb $0x0, 0x20(%rdi,%r13)
je 0x27338
incq %r14
movl 0xc(%rbp), %eax
addq $0x28, %r13
cmpq %rax, %r14
jb 0x27318
jmp 0x274da
movl 0xc(%rdi,%r13), %eax
movl (%r15,%rax,4), %ebx
cmpq %r11, %rbx
je 0x27324
movq 0x18(%r10), %r12
movq 0x20(%r12), %rcx
cmpl $0x0, (%rcx,%rbx,8)
js 0x27324
movq (%rdi,%r13), %rcx
movl 0x8(%rdi,%r13), %edx
subl %ecx, %edx
cmpq %r8, %rdx
jl 0x27324
movl 0x14(%rdi,%r13), %edx
subl 0x10(%rdi,%r13), %edx
cmpq %r8, %rdx
jl 0x27324
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 0x2633be(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movl 0x2c(%rsp), %ecx
leaq 0x48(%rsp), %r9
callq 0x19c0d3
testl %eax, %eax
js 0x273f2
movq 0x48(%rsp), %rdi
movq %rdi, %rax
shrq $0x20, %rax
cmpl 0x20(%rsp), %eax
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
jne 0x2740c
movl 0x50(%rsp), %eax
movq 0x10(%r10), %rcx
imulq $0x28, %rbx, %rdx
cmpl 0x4(%rcx,%rdx), %eax
movq 0x8(%rsp), %r8
je 0x27416
pushq $-0x1
popq %rsi
jmp 0x27419
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
jmp 0x27324
movq 0x8(%rsp), %r8
jmp 0x27324
leal (%rbx,%rbx), %esi
cmpl 0x8(%rcx,%rdx), %eax
leal 0x1(%rbx,%rbx), %ebx
cmovnel %esi, %ebx
cmpl $-0x1, %ebx
je 0x27324
movl 0x4(%r12), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
movl (%r12), %eax
cmpl %eax, %ecx
jne 0x27482
leal (%rax,%rax), %ecx
testl %eax, %eax
pushq $0x10
popq %rax
cmovel %eax, %ecx
movl %ecx, (%r12)
movq %rdi, 0x38(%rsp)
movq 0x8(%r12), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq 0x38(%rsp), %rdi
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r10
movq %rax, 0x8(%r12)
movq 0x8(%r12), %rax
movl 0x4(%r12), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%r12)
imulq $0x18, %rdx, %rcx
movups 0x48(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
movq 0x58(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movl %edi, %edx
orq 0x40(%rsp), %rdx
movq %rdx, (%rax,%rcx)
movl %ebx, 0x8(%rax,%rcx)
incq 0x30(%rsp)
jmp 0x27324
movq 0x80(%rsp), %rcx
incl %ecx
cmpq %rcx, (%r10)
ja 0x270b6
movq 0x30(%rsp), %r14
testq %r14, %r14
je 0x2751f
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 0xa02fa
movq %r15, %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x1dabe9
movq 0x262aa3(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x210ff2(%rip), %rsi    # 0x238539
leaq 0x210fd6(%rip), %rdx    # 0x238524
movq %r14, %rcx
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0