// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_trans(asg_t*, int)
int asg_arc_del_trans(asg_t *g, int fuzz)
{
    double startTime = Get_T();

	uint8_t *mark;
	///n_vtx = number of seq * 2
	///the reason is that each read has two direction (query->target, target->query)
	uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0;
	///at first, all nodes should be set to vacant
	mark = (uint8_t*)calloc(n_vtx, 1);

	/**v is the id+direction of a node, 
     * the high 31-bit is the id, 
     * and the lowest 1-bit is the direction
     * (0 means query-to-target, 1 means target-to-query)**/
	for (v = 0; v < n_vtx; ++v) {
		///nv is the number of overlaps with v(qn+direction)
		uint32_t L, i, nv = asg_arc_n(g, v);
		///av is the array of v
		asg_arc_t *av = asg_arc_a(g, v);
        ///that means in this direction, read v is not overlapped with any other reads
		if (nv == 0) continue; // no hits
		
        ///if the read itself has been removed
		if (g->seq[v>>1].del) 
        {
			for (i = 0; i < nv; ++i) av[i].del = 1, ++n_reduced;
			continue;
		}



        /**
	********************************query-to-target overlap****************************
	case 1: u = 0, rev = 0                           in the view of target: direction is 1 
	query: CCCCCCCCTAATTAAAAT                        target: TAATTAAAATGGGGGG (use ex-target as query)
	               ||||||||||         <--->                  ||||||||||
	       target: TAATTAAAATGGGGGG           query: CCCCCCCCTAATTAAAAT (use ex-query as target)

	case 2: u = 0, rev = 1                           in the view of target: direction is 0
	query: CCCCCCCCTAATTAAAAT					     target: CCCCCCATTTTAATTA  (use ex-target as query)
                   ||||||||||        <--->                         ||||||||||
	       target: TAATTAAAATGGGGGG                         query: ATTTTAATTAGGGGGGGG  (use ex-query as target)
	********************************query-to-target overlap****************************

	********************************target-to-query overlap****************************
	case 3: u = 1, rev = 0                           in the view of target: direction is 0
			 query: AAATAATATCCCCCCGCG                target: GGGCCGGCAAATAATAT (use ex-target as query)
					|||||||||          <--->                          |||||||||
	target: GGGCCGGCAAATAATAT                                  query: AAATAATATCCCCCCGCG (use ex-query as target)

	case 4: u = 1, rev = 1                          in the view of target: direction is 1
	         query: AAATAATATCCCCCCGCG                        target: ATATTATTTGCCGGCCC (use ex-target as query)
                    |||||||||             <--->                       |||||||||
	target: GGGCCGGCAAATAATAT                          query: CGCGGGGGATATTATTT (use ex-query as target)
	********************************target-to-query overlap****************************

	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
			uint32_t w = av[i].v;
			
			uint32_t j, nw = asg_arc_n(g, w);
			asg_arc_t *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;
		}
		#if 0
		for (i = 0; i < nv; ++i) {
			uint32_t w = av[i].v;
			uint32_t j, nw = asg_arc_n(g, w);
			asg_arc_t *aw = asg_arc_a(g, w);
			for (j = 0; j < nw && (j == 0 || asg_arc_len(aw[j]) < fuzz); ++j)
				if (mark[aw[j].v]) mark[aw[j].v] = 2;
		}
		#endif
        //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);

    if(VERBOSE >= 1)
    {
	    fprintf(stderr, "[M::%s] transitively reduced %d arcs\n", __func__, n_reduced);
    }

	if (n_reduced) {
		asg_cleanup(g);
		asg_symm(g);
	}

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }
	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
callq 0x300fc
movl 0x14(%rbx), %r15d
addl %r15d, %r15d
movl $0x1, %esi
movq %r15, %rdi
callq 0x2120
testl %r15d, %r15d
je 0xa8e20
movl %r15d, %r9d
xorl %edx, %edx
xorl %ebp, %ebp
movq %rbx, 0x18(%rsp)
movq %r9, 0x10(%rsp)
movq 0x28(%rbx), %rdi
movq (%rdi,%rdx,8), %r11
testl %r11d, %r11d
je 0xa8e12
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 0xa8def
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 0xa8d1f
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 0xa8da6
movq (%rdi,%r9,8), %r14
testl %r14d, %r14d
je 0xa8da6
leaq (%r10,%rcx,8), %rcx
movq %r14, %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 0xa8da6
movl (%rbx), %r9d
cmpb $0x0, (%rax,%r9)
je 0xa8d9d
movb $0x2, (%rax,%r9)
addq $0x18, %rbx
decl %r14d
jne 0xa8d82
incq %r13
cmpq %r11, %r13
jne 0xa8d4d
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 0xa8dda
orb $-0x80, 0x3(%rdi,%rcx)
incl %ebp
movl -0x4(%rdi,%rcx), %r8d
movb $0x0, (%rax,%r8)
addq $0x18, %rcx
cmpq %rcx, %rsi
jne 0xa8dc7
jmp 0xa8e12
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 0xa8e01
addl %r11d, %ebp
incq %rdx
cmpq %r9, %rdx
jne 0xa8cd7
jmp 0xa8e22
xorl %ebp, %ebp
movq %rax, %rdi
callq 0x2220
testl %ebp, %ebp
je 0xa8e4a
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq