// File: /workspace/github_cpp_makefile_star2_O3/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 0x2f3fc
movl 0x14(%rbx), %r14d
addl %r14d, %r14d
movl $0x1, %esi
movq %r14, %rdi
callq 0x2120
testl %r14d, %r14d
je 0xa9ed3
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 0xa9e9d
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 0xa9e7a
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 0xa9d9b
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 0xa9e31
movq (%rsi,%rdi,8), %rbx
movl %ebx, %edi
testq %rdi, %rdi
je 0xa9e31
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 0xa9e31
movl (%r15,%r8), %edi
cmpb $0x0, (%rax,%rdi)
je 0xa9e28
movb $0x2, (%rax,%rdi)
addq $0x18, %r8
cmpq %r8, %rbx
jne 0xa9e0d
incq %r13
cmpq %r9, %r13
jne 0xa9dc9
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 0xa9e6a
orb $-0x80, 0x3(%rsi,%rdi)
incl %ebp
movb $0x0, (%rax,%r8)
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0xa9e57
jmp 0xa9e9d
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 0xa9e8c
addl %r11d, %ebp
incq %rcx
cmpq %r14, %rcx
jne 0xa9d4e
movq %rax, %rdi
callq 0x2220
testl %ebp, %ebp
je 0xa9edb
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
jmp 0xa9edd
movq %rax, %rdi
callq 0x2220
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq