// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x2120
xorl %ecx, %ecx
xorl %r15d, %r15d
movq %rbx, 0x10(%rsp)
movq %r14, 0x8(%rsp)
cmpq %r14, %rcx
je 0x704f6
movq 0x28(%rbx), %rsi
movq (%rsi,%rcx,8), %r11
testl %r11d, %r11d
je 0x704ee
movq 0x8(%rbx), %r12
movq %r11, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %r8
movq 0x20(%rbx), %rdx
movl %ecx, %edi
shrl %edi
cmpl $0x0, (%rdx,%rdi,8)
movl %r11d, %r9d
js 0x704cd
leaq (%r12,%r8), %r10
imulq $0x18, %r9, %rdx
movq %r8, 0x18(%rsp)
addq %r12, %r8
addq $0x8, %r8
xorl %ebx, %ebx
cmpq %rbx, %rdx
je 0x70414
movl (%r8,%rbx), %edi
movb $0x1, (%rax,%rdi)
addq $0x18, %rbx
jmp 0x70401
decl %r11d
imulq $0x18, %r11, %rdi
movl (%r10,%rdi), %r11d
addl 0x4(%rsp), %r11d
movq %r12, 0x20(%rsp)
addq $0x8, %r12
xorl %r13d, %r13d
cmpq %r9, %r13
je 0x7048c
imulq $0x18, %r13, %rbp
movl 0x8(%r10,%rbp), %r8d
cmpb $0x1, (%rax,%r8)
jne 0x70487
addq %r10, %rbp
movq (%rsi,%r8,8), %rdi
movl %edi, %r8d
shrq $0x20, %rdi
imulq $0x18, %r8, %r14
imulq $0x18, %rdi, %rbx
addq %r12, %rbx
xorl %r8d, %r8d
cmpq %r8, %r14
je 0x70487
movl (%rbp), %edi
addl -0x8(%rbx,%r8), %edi
cmpl %r11d, %edi
ja 0x70487
movl (%rbx,%r8), %edi
cmpb $0x0, (%rax,%rdi)
je 0x70481
movb $0x2, (%rax,%rdi)
addq $0x18, %r8
jmp 0x70461
incq %r13
jmp 0x70430
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdi
addq %rdi, %rsi
addq $0xc, %rsi
xorl %edi, %edi
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
cmpq %rdi, %rdx
je 0x704ee
movl -0x4(%rsi,%rdi), %r8d
cmpb $0x2, (%rax,%r8)
jne 0x704c2
orb $-0x80, 0x3(%rsi,%rdi)
incl %r15d
movb $0x0, (%rax,%r8)
addq $0x18, %rdi
jmp 0x704a9
imulq $0x18, %r9, %rdx
leaq (%r12,%r8), %rsi
addq $0xc, %rsi
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x704eb
orb $-0x80, 0x3(%rsi,%rdi)
addq $0x18, %rdi
jmp 0x704db
addl %r11d, %r15d
incq %rcx
jmp 0x703ad
movq %rax, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq