// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_trans_ul(asg_t*, int)
int asg_arc_del_trans_ul(asg_t *g, int fuzz)
{
	uint32_t v, n_vtx = g->n_seq<<1, n_reduced = 0, L, i, nv;
    uint8_t *mark; asg_arc_t *av; CALLOC(mark, n_vtx);
    uint32_t w, j, nw; asg_arc_t *aw;

	for (v = 0; v < n_vtx; ++v) {
		nv = asg_arc_n(g, v); av = asg_arc_a(g, v);
		if (nv == 0) continue; // no hits		
		if (g->seq[v>>1].del)  {
			for (i = 0; i < nv; ++i) av[i].del = 1, ++n_reduced;
			continue;
		}
        /**
        p->ul: |____________31__________|__________1___________|______________32_____________|
                            qns            direction of overlap       length of this node (not overlap length)
                                            (in the view of query)
        p->v : |___________31___________|__________1___________|
                            tns             reverse direction of overlap
                                        (in the view of target)
        p->ol: overlap length
        **/


		//all outnode of v should be set to "not reduce"
		for (i = 0; i < nv; ++i) mark[av[i].v] = 1;

		///length of node (not overlap length)
		///av[nv-1] is longest out-dege
		/**
		 * v---------------
		 *   w1---------------
		 *      w2--------------
		 *         w3--------------
		 *            w4--------------
		 *               w5-------------
		 * for v, the longest out-edge is v->w5
		 **/
		L = asg_arc_len(av[nv-1]) + fuzz;


		for (i = 0; i < nv; ++i) {
			//w is an out-node of v
			w = av[i].v;
			nw = asg_arc_n(g, w); aw = asg_arc_a(g, w);
			///if w has already been reduced
			if (mark[av[i].v] != 1) continue;

			for (j = 0; j < nw && asg_arc_len(aw[j]) + asg_arc_len(av[i]) <= L; ++j)
				if (mark[aw[j].v]) mark[aw[j].v] = 2;
		}

        // for (i = 0; i < nv; ++i) {
        //     if((av[i].del) || (mark[av[i].v] != 1)) continue;
        //     w = av[i].v;
		// 	nw = asg_arc_n(g, w); aw = asg_arc_a(g, w);
        //     for (j = 0; j < nw && asg_arc_len(aw[j]) + asg_arc_len(av[i]) <= L; ++j) {
        //         if(v == 20996 && w == 21011) {
        //             fprintf(stderr, "(0):v->%u, ou->%u\n", aw[j].v, aw[j].ou);
        //         }
                
        //         if (mark[aw[j].v] == 2) {
        //             if((((uint32_t)av[i].ou) + ((uint32_t)aw[j].ou)) <= OU_MASK) {
        //                 av[i].ou = av[i].ou + aw[j].ou;
        //             } else {
        //                 av[i].ou = OU_MASK;
        //             }
        //         }
        //     }
        // }
        //remove edges
		for (i = 0; i < nv; ++i) {
			if (mark[av[i].v] == 2) {
                av[i].del = 1, ++n_reduced;
            }
			mark[av[i].v] = 0;
		}
	}
	free(mark);
    asg_cleanup(g);
    asg_symm(g);
    // prt_specfic_sge(g, 10498, 10505, __func__);
    // normalize_gou(g);
    
	return n_reduced;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, 0xc(%rsp)
movq %rdi, %rbx
movl 0x14(%rdi), %r14d
addl %r14d, %r14d
movl $0x1, %esi
movq %r14, %rdi
callq 0x2120
testl %r14d, %r14d
je 0xa8fe7
movl %r14d, %r9d
xorl %edx, %edx
xorl %r14d, %r14d
movq %rbx, 0x18(%rsp)
movq %r9, 0x10(%rsp)
movq 0x28(%rbx), %rdi
movq (%rdi,%rdx,8), %r11
testl %r11d, %r11d
je 0xa8fd9
movq 0x8(%rbx), %r8
movq %r11, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r10
movq 0x20(%rbx), %rcx
movl %edx, %esi
shrl %esi
cmpl $0x0, (%rcx,%rsi,8)
movl %r11d, %esi
js 0xa8fb6
movq %r10, 0x20(%rsp)
leaq (%r8,%r10,8), %r10
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %ecx, %ecx
movl 0x8(%r10,%rcx), %r9d
movb $0x1, (%rax,%r9)
addq $0x18, %rcx
cmpq %rcx, %rsi
jne 0xa8ee7
leal -0x1(%r11), %ecx
leaq (%rcx,%rcx,2), %rcx
movl (%r10,%rcx,8), %r15d
addl 0xc(%rsp), %r15d
movl %r11d, %r11d
leaq 0x8(%r8), %r12
xorl %r13d, %r13d
leaq (,%r13,2), %rcx
addq %r13, %rcx
movl 0x8(%r10,%rcx,8), %r9d
cmpb $0x1, (%rax,%r9)
jne 0xa8f6c
movq (%rdi,%r9,8), %rbp
testl %ebp, %ebp
je 0xa8f6c
leaq (%r10,%rcx,8), %rcx
movq %rbp, %r9
shrq $0x20, %r9
movl (%rcx), %ecx
leaq (%r9,%r9,2), %r9
leaq (%r12,%r9,8), %rbx
movl -0x8(%rbx), %r9d
addl %ecx, %r9d
cmpl %r15d, %r9d
ja 0xa8f6c
movl (%rbx), %r9d
cmpb $0x0, (%rax,%r9)
je 0xa8f64
movb $0x2, (%rax,%r9)
addq $0x18, %rbx
decl %ebp
jne 0xa8f49
incq %r13
cmpq %r11, %r13
jne 0xa8f15
movq 0x20(%rsp), %rcx
leaq (%r8,%rcx,8), %rdi
addq $0xc, %rdi
xorl %ecx, %ecx
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r9
movl -0x4(%rdi,%rcx), %r8d
cmpb $0x2, (%rax,%r8)
jne 0xa8fa1
orb $-0x80, 0x3(%rdi,%rcx)
incl %r14d
movl -0x4(%rdi,%rcx), %r8d
movb $0x0, (%rax,%r8)
addq $0x18, %rcx
cmpq %rcx, %rsi
jne 0xa8f8d
jmp 0xa8fd9
leaq (%r8,%r10,8), %rcx
addq $0xc, %rcx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
orb $-0x80, 0x3(%rcx,%rdi)
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0xa8fc8
addl %r11d, %r14d
incq %rdx
cmpq %r9, %rdx
jne 0xa8e9f
jmp 0xa8fea
xorl %r14d, %r14d
movq %rax, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq