// File: /workspace/github_cpp_makefile_star2_O2/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, 0x4(%rsp)
movq %rdi, %rbx
callq 0x251c
movl 0x14(%rbx), %r15d
addl %r15d, %r15d
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x2120
xorl %ecx, %ecx
xorl %r14d, %r14d
movq %rbx, 0x10(%rsp)
movq %r15, 0x8(%rsp)
cmpq %r15, %rcx
je 0x70348
movq 0x28(%rbx), %rsi
movq (%rsi,%rcx,8), %r11
testl %r11d, %r11d
je 0x70340
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 0x7031f
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 0x70266
movl (%r8,%rbx), %edi
movb $0x1, (%rax,%rdi)
addq $0x18, %rbx
jmp 0x70253
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 0x702de
imulq $0x18, %r13, %rbp
movl 0x8(%r10,%rbp), %r8d
cmpb $0x1, (%rax,%r8)
jne 0x702d9
addq %r10, %rbp
movq (%rsi,%r8,8), %rdi
movl %edi, %r8d
shrq $0x20, %rdi
imulq $0x18, %r8, %r15
imulq $0x18, %rdi, %rbx
addq %r12, %rbx
xorl %r8d, %r8d
cmpq %r8, %r15
je 0x702d9
movl (%rbp), %edi
addl -0x8(%rbx,%r8), %edi
cmpl %r11d, %edi
ja 0x702d9
movl (%rbx,%r8), %edi
cmpb $0x0, (%rax,%rdi)
je 0x702d3
movb $0x2, (%rax,%rdi)
addq $0x18, %r8
jmp 0x702b3
incq %r13
jmp 0x70282
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdi
addq %rdi, %rsi
addq $0xc, %rsi
xorl %edi, %edi
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r15
cmpq %rdi, %rdx
je 0x70340
movl -0x4(%rsi,%rdi), %r8d
cmpb $0x2, (%rax,%r8)
jne 0x70314
orb $-0x80, 0x3(%rsi,%rdi)
incl %r14d
movb $0x0, (%rax,%r8)
addq $0x18, %rdi
jmp 0x702fb
imulq $0x18, %r9, %rdx
leaq (%r12,%r8), %rsi
addq $0xc, %rsi
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x7033d
orb $-0x80, 0x3(%rsi,%rdi)
addq $0x18, %rdi
jmp 0x7032d
addl %r11d, %r14d
incq %rcx
jmp 0x701ff
movq %rax, %rdi
callq 0x2220
testl %r14d, %r14d
je 0x70365
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq