// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x14(%rbp), %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0x30(%rbp)
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0xdac3b
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x10(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x10(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0xda9e0
jmp 0xdac2d
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x10(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xdaa42
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0xdaa3d
movq -0x38(%rbp), %rax
movl -0x20(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xdaa01
jmp 0xdac2d
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0xdaa75
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movl -0x20(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movb $0x1, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xdaa49
movq -0x38(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
addl -0xc(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0xdabb1
movq -0x38(%rbp), %rax
movl -0x20(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x3c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movl -0x20(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
je 0xdab13
jmp 0xdaba3
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %ecx
xorl %eax, %eax
cmpl -0x44(%rbp), %ecx
movb %al, -0x51(%rbp)
jae 0xdab54
movq -0x50(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movl -0x20(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
addl %ecx, %eax
cmpl -0x1c(%rbp), %eax
setbe %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0xdab5d
jmp 0xdaba1
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movl -0x40(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
cmpb $0x0, (%rax,%rcx)
je 0xdab91
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movl -0x40(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movb $0x2, (%rax,%rcx)
jmp 0xdab93
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xdab1a
jmp 0xdaba3
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xdaa98
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0xdac2b
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movl -0x20(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jne 0xdac07
movq -0x38(%rbp), %rax
movl -0x20(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movl -0x20(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
movb $0x0, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xdabb8
jmp 0xdac2d
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xda991
movq -0x30(%rbp), %rdi
callq 0x2210
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0x8(%rbp), %rdi
callq 0xce020
movl -0x18(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop