// File: /workspace/github_cpp_makefile_star2_O3/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, 0x4(%rsp)
movq %rdi, %rbx
movl 0x14(%rdi), %r14d
addl %r14d, %r14d
movl $0x1, %esi
movq %r14, %rdi
callq 0x2120
testl %r14d, %r14d
je 0xaa08b
xorl %ecx, %ecx
xorl %ebp, %ebp
movq %rbx, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq 0x28(%rbx), %rsi
movq (%rsi,%rcx,8), %r11
testl %r11d, %r11d
je 0xaa07d
movq 0x8(%rbx), %r15
movq %r11, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %r8
movq 0x20(%rbx), %rdx
movl %ecx, %edi
shrl %edi
cmpl $0x0, (%rdx,%rdi,8)
movl %r11d, %r9d
js 0xaa05a
movq %r8, 0x18(%rsp)
leaq (%r15,%r8,8), %r10
leaq (,%r9,8), %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %r8d, %r8d
movl 0x8(%r10,%r8), %edi
movb $0x1, (%rax,%rdi)
addq $0x18, %r8
cmpq %r8, %rdx
jne 0xa9f7b
decl %r11d
leaq (%r11,%r11,2), %rdi
movl (%r10,%rdi,8), %r11d
addl 0x4(%rsp), %r11d
movq %r15, 0x20(%rsp)
leaq 0x8(%r15), %r12
xorl %r13d, %r13d
leaq (,%r13,2), %r8
addq %r13, %r8
movl 0x8(%r10,%r8,8), %edi
cmpb $0x1, (%rax,%rdi)
jne 0xaa011
movq (%rsi,%rdi,8), %rbx
movl %ebx, %edi
testq %rdi, %rdi
je 0xaa011
leaq (%r10,%r8,8), %rdi
movq %rbx, %r8
shrq $0x20, %r8
movl (%rdi), %r14d
movl %ebx, %edi
leaq (%r8,%r8,2), %r8
leaq (%r12,%r8,8), %r15
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rbx
xorl %r8d, %r8d
movl -0x8(%r15,%r8), %edi
addl %r14d, %edi
cmpl %r11d, %edi
ja 0xaa011
movl (%r15,%r8), %edi
cmpb $0x0, (%rax,%rdi)
je 0xaa008
movb $0x2, (%rax,%rdi)
addq $0x18, %r8
cmpq %r8, %rbx
jne 0xa9fed
incq %r13
cmpq %r9, %r13
jne 0xa9fa9
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdi
leaq (%rsi,%rdi,8), %rsi
addq $0xc, %rsi
xorl %edi, %edi
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
movl -0x4(%rsi,%rdi), %r8d
cmpb $0x2, (%rax,%r8)
jne 0xaa04a
orb $-0x80, 0x3(%rsi,%rdi)
incl %ebp
movb $0x0, (%rax,%r8)
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0xaa037
jmp 0xaa07d
leaq (%r15,%r8,8), %rdx
addq $0xc, %rdx
shlq $0x3, %r9
leaq (%r9,%r9,2), %rsi
xorl %edi, %edi
orb $-0x80, 0x3(%rdx,%rdi)
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0xaa06c
addl %r11d, %ebp
incq %rcx
cmpq %r14, %rcx
jne 0xa9f2e
jmp 0xaa08d
xorl %ebp, %ebp
movq %rax, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq