// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/horder.cpp
// Function: horder_clean_sg_by_utg(asg_t*, ma_ug_t*)
void horder_clean_sg_by_utg(asg_t *sg, ma_ug_t *ug)
{
    uint32_t i, v, w, k, nv, vx, wx;
    asg_arc_t *av = NULL;
    ma_utg_t *u = NULL; 
    
    for (i = 0; i < sg->n_arc; i++) sg->arc[i].del = (!!1);
    for (i = 0; i < sg->n_seq; i++) sg->seq[i].del = (!!1);
    
    for (i = 0; i < ug->g->n_seq; ++i)  {
        if(ug->g->seq[i].del) continue;
        u = &(ug->u.a[i]);
        for (k = 0; (k + 1) < u->n; k++) {
            v = u->a[k]>>32; w = u->a[k+1]>>32;
            asg_arc_unique_del(sg, v, w, 0);
            asg_arc_unique_del(sg, w^1, v^1, 0);
        }
        for (k = 0; k < u->n; k++) sg->seq[u->a[k]>>33].del = (!!0);

        v = i<<1;
        nv = asg_arc_n(ug->g, v); av = asg_arc_a(ug->g, v);
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            w = av[k].v;

            vx = (v&1?((ug->u.a[v>>1].a[0]>>32)^1):(ug->u.a[v>>1].a[ug->u.a[v>>1].n-1]>>32));
            wx = (w&1?((ug->u.a[w>>1].a[ug->u.a[w>>1].n-1]>>32)^1):(ug->u.a[w>>1].a[0]>>32));
            asg_arc_unique_del(sg, vx, wx, 0); asg_arc_unique_del(sg, wx^1, vx^1, 0);
        }

        v = (i<<1)+1;
        nv = asg_arc_n(ug->g, v); av = asg_arc_a(ug->g, v);
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            w = av[k].v;

            vx = (v&1?((ug->u.a[v>>1].a[0]>>32)^1):(ug->u.a[v>>1].a[ug->u.a[v>>1].n-1]>>32));
            wx = (w&1?((ug->u.a[w>>1].a[ug->u.a[w>>1].n-1]>>32)^1):(ug->u.a[w>>1].a[0]>>32));
            asg_arc_unique_del(sg, vx, wx, 0); asg_arc_unique_del(sg, wx^1, vx^1, 0);
        }

        if(u->circ) {
            v = w = i<<1;
            vx = (v&1?((ug->u.a[v>>1].a[0]>>32)^1):(ug->u.a[v>>1].a[ug->u.a[v>>1].n-1]>>32));
            wx = (w&1?((ug->u.a[w>>1].a[ug->u.a[w>>1].n-1]>>32)^1):(ug->u.a[w>>1].a[0]>>32));
            asg_arc_unique_del(sg, vx, wx, 0); asg_arc_unique_del(sg, wx^1, vx^1, 0);
        }         
    }
    asg_cleanup(sg);

    /*******************************for debug************************************/
    // ma_ug_t *dbg = ma_ug_gen(sg);
    // print_N50(dbg);
    // print_N50(ug);

    // uint8_t *end = NULL; CALLOC(end, sg->n_seq<<1);
    // for (i = 0; i < dbg->g->n_seq; ++i) 
    // {
    //     u = &(dbg->u.a[i]);
    //     if(u->n == 0) continue;
    //     end[(u->a[0]>>32)^1] = 1;
    //     end[u->a[u->n-1]>>32] = 2;
    // }

    // for (i = 0; i < ug->g->n_seq; ++i) 
    // {
    //     u = &(ug->u.a[i]);
    //     if(u->n == 0) continue;
    //     for (k = 1; (k + 1) < u->n; k++)
    //     {
    //         if(end[(u->a[k]>>32)])
    //         {
    //             fprintf(stderr, "(1) node-%lu, v-%lu, w-%lu, sg(v).n: %u\n", 
    //             u->a[k]>>33, (u->a[k]>>32), (u->a[k+1]>>32), asg_arc_n(sg, (u->a[k]>>32)));
    //         }

    //         if(end[(u->a[k]>>32)^1])
    //         {
    //             fprintf(stderr, "(2) node-%lu, v-%lu, w-%lu, sg(v).n: %u\n", 
    //             u->a[k]>>33, (u->a[k]>>32)^1, (u->a[k-1]>>32)^1, asg_arc_n(sg, (u->a[k]>>32)^1));
    //         }
    //     }
    // }
    // free(end);
    // ma_ug_destroy(dbg);
    /*******************************for debug************************************/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, -0x10(%rsp)
testl $0x7fffffff, 0x4(%rdi)  # imm = 0x7FFFFFFF
je 0x176a2f
movl $0xc, %eax
xorl %ecx, %ecx
movq 0x8(%rdi), %rdx
orl $0x80000000, (%rdx,%rax) # imm = 0x80000000
incq %rcx
movl 0x4(%rdi), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
addq $0x18, %rax
cmpq %rdx, %rcx
jb 0x176a0f
testl $0x7fffffff, 0x14(%rdi) # imm = 0x7FFFFFFF
je 0x176a56
xorl %eax, %eax
movq 0x20(%rdi), %rcx
orl $0x80000000, (%rcx,%rax,8) # imm = 0x80000000
incq %rax
movl 0x14(%rdi), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rax
jb 0x176a3a
movq -0x10(%rsp), %rax
movq 0x18(%rax), %rdx
testl $0x7fffffff, 0x14(%rdx) # imm = 0x7FFFFFFF
je 0x176f1e
xorl %r14d, %r14d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq 0x20(%rdx), %rax
cmpl $0x0, (%rax,%r14,8)
js 0x176e30
movq -0x10(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%r14,%r14,4), %rdx
leaq (%rax,%rdx,8), %r8
movl 0x10(%r8), %r11d
cmpl $0x2, %r11d
movq %r14, -0x8(%rsp)
jb 0x176b7a
movl $0x1, %r9d
xorl %r10d, %r10d
movq 0x18(%r8), %rax
movq (%rax,%r10,8), %r11
shrq $0x20, %r11
movq (%rax,%r9,8), %r14
shrq $0x20, %r14
movq 0x8(%rdi), %rbx
movq 0x28(%rdi), %r15
movq (%r15,%r11,8), %rax
movq %rax, %rcx
andq %rsi, %rcx
je 0x176b15
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r13
leaq (%rax,%rax,2), %r12
leaq (%rbx,%r12,8), %rax
addq $0xc, %rax
xorl %ebp, %ebp
cmpl %r14d, -0x4(%rax,%rbp)
je 0x176b04
addq $0x18, %rbp
cmpq %rbp, %r13
jne 0x176af2
jmp 0x176b15
leaq (%rbx,%r12,8), %rax
andb $0x7f, 0xf(%rbp,%rax)
movq 0x8(%rdi), %rbx
movq 0x28(%rdi), %r15
xorq $0x1, %r14
movq (%r15,%r14,8), %rax
movq %rax, %rcx
andq %rsi, %rcx
je 0x176b62
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r15
leaq (%rax,%rax,2), %r14
leaq (%rbx,%r14,8), %r13
addq $0xc, %r13
xorl %r12d, %r12d
movl -0x4(%r13,%r12), %eax
xorl %r11d, %eax
cmpl $0x1, %eax
je 0x176b58
addq $0x18, %r12
cmpq %r12, %r15
jne 0x176b40
jmp 0x176b62
leaq (%rbx,%r14,8), %rax
andb $0x7f, 0xf(%r12,%rax)
incq %r9
movl 0x10(%r8), %r11d
incq %r10
cmpq %r11, %r9
jb 0x176ab0
movq -0x8(%rsp), %r14
testl %r11d, %r11d
je 0x176ba5
xorl %r9d, %r9d
movq 0x20(%rdi), %rax
movq 0x18(%r8), %rcx
movq (%rcx,%r9,8), %rcx
shrq $0x21, %rcx
andl $0x7fffffff, (%rax,%rcx,8) # imm = 0x7FFFFFFF
incq %r9
movl 0x10(%r8), %eax
cmpq %rax, %r9
jb 0x176b82
movq -0x10(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %r9
movq 0x28(%rax), %r10
movq %r14, %rax
shlq $0x4, %rax
movq (%r10,%rax), %rax
testl %eax, %eax
je 0x176cfc
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%r9,%rcx,8), %r9
movl %eax, %r10d
xorl %r11d, %r11d
leaq (%r11,%r11,2), %rax
cmpl $0x0, 0xc(%r9,%rax,8)
js 0x176cda
leaq (%r9,%rax,8), %rax
movl 0x8(%rax), %ecx
movq -0x10(%rsp), %rax
movq 0x10(%rax), %rbx
movl %ecx, %eax
shrl %eax
leaq (%rax,%rax,4), %rax
testb $0x1, %cl
jne 0x176c16
movq 0x18(%rbx,%rax,8), %rax
movl 0x4(%rax), %r15d
jmp 0x176c2a
movq 0x18(%rbx,%rax,8), %rcx
movl 0x10(%rbx,%rax,8), %eax
decl %eax
movl 0x4(%rcx,%rax,8), %r15d
xorq $0x1, %r15
movq 0x18(%rbx,%rdx,8), %rax
movl 0x10(%rbx,%rdx,8), %ecx
decl %ecx
movq (%rax,%rcx,8), %rbx
shrq $0x20, %rbx
movq 0x8(%rdi), %r14
movq 0x28(%rdi), %r12
movq (%r12,%rbx,8), %rax
movq %rax, %rcx
andq %rsi, %rcx
je 0x176c8e
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rbp
leaq (%rax,%rax,2), %r13
leaq (%r14,%r13,8), %rcx
addq $0xc, %rcx
xorl %eax, %eax
cmpl %r15d, -0x4(%rcx,%rax)
je 0x176c7d
addq $0x18, %rax
cmpq %rax, %rbp
jne 0x176c6b
jmp 0x176c8e
leaq (%r14,%r13,8), %rcx
andb $0x7f, 0xf(%rax,%rcx)
movq 0x8(%rdi), %r14
movq 0x28(%rdi), %r12
xorq $0x1, %r15
movq (%r12,%r15,8), %rax
movq %rax, %rcx
andq %rsi, %rcx
je 0x176cda
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r12
leaq (%rax,%rax,2), %r15
leaq (%r14,%r15,8), %rbp
addq $0xc, %rbp
xorl %r13d, %r13d
movl -0x4(%rbp,%r13), %eax
xorl %ebx, %eax
cmpl $0x1, %eax
je 0x176cd0
addq $0x18, %r13
cmpq %r13, %r12
jne 0x176cb9
jmp 0x176cda
leaq (%r14,%r15,8), %rax
andb $0x7f, 0xf(%r13,%rax)
incq %r11
cmpq %r10, %r11
jne 0x176bde
movq -0x10(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %r9
movq 0x28(%rax), %r10
movq -0x8(%rsp), %r14
leaq (%r14,%r14), %rax
movq 0x8(%r10,%rax,8), %rax
testl %eax, %eax
je 0x176e25
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%r9,%rcx,8), %r9
movl %eax, %r10d
xorl %r11d, %r11d
leaq (%r11,%r11,2), %rax
cmpl $0x0, 0xc(%r9,%rax,8)
js 0x176e19
leaq (%r9,%rax,8), %rax
movl 0x8(%rax), %ecx
movq -0x10(%rsp), %rax
movq 0x10(%rax), %rbx
movl %ecx, %eax
shrl %eax
leaq (%rax,%rax,4), %rax
testb $0x1, %cl
jne 0x176d5a
movq 0x18(%rbx,%rax,8), %rax
movl 0x4(%rax), %r15d
jmp 0x176d6e
movq 0x18(%rbx,%rax,8), %rcx
movl 0x10(%rbx,%rax,8), %eax
decl %eax
movl 0x4(%rcx,%rax,8), %r15d
xorq $0x1, %r15
movq 0x18(%rbx,%rdx,8), %rax
movq (%rax), %rbx
shrq $0x20, %rbx
movq %rbx, %rax
xorq $0x1, %rax
movq 0x8(%rdi), %r14
movq 0x28(%rdi), %r12
movq (%r12,%rax,8), %rax
movq %rax, %rcx
andq %rsi, %rcx
je 0x176dd2
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rbp
leaq (%rax,%rax,2), %r13
leaq (%r14,%r13,8), %rcx
addq $0xc, %rcx
xorl %eax, %eax
cmpl %r15d, -0x4(%rcx,%rax)
je 0x176dc1
addq $0x18, %rax
cmpq %rax, %rbp
jne 0x176daf
jmp 0x176dd2
leaq (%r14,%r13,8), %rcx
andb $0x7f, 0xf(%rax,%rcx)
movq 0x8(%rdi), %r14
movq 0x28(%rdi), %r12
xorq $0x1, %r15
movq (%r12,%r15,8), %rax
movq %rax, %rcx
andq %rsi, %rcx
je 0x176e19
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r12
leaq (%rax,%rax,2), %r15
leaq (%r14,%r15,8), %rbp
addq $0xc, %rbp
xorl %r13d, %r13d
cmpl %ebx, -0x4(%rbp,%r13)
je 0x176e0f
addq $0x18, %r13
cmpq %r13, %r12
jne 0x176dfd
jmp 0x176e19
leaq (%r14,%r15,8), %rax
andb $0x7f, 0xf(%r13,%rax)
incq %r11
cmpq %r10, %r11
jne 0x176d22
cmpl $0x0, (%r8)
movq -0x8(%rsp), %r14
js 0x176e52
incq %r14
movq -0x10(%rsp), %rax
movq 0x18(%rax), %rdx
movl 0x14(%rdx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r14
jb 0x176a74
jmp 0x176f1e
movq -0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax,%rdx,8), %rcx
movl 0x10(%rax,%rdx,8), %eax
decl %eax
movq (%rcx,%rax,8), %rdx
shrq $0x20, %rdx
movq (%rcx), %r9
shrq $0x20, %r9
movq 0x8(%rdi), %r8
movq 0x28(%rdi), %r10
movq (%r10,%rdx,8), %rax
movq %rax, %rcx
andq %rsi, %rcx
je 0x176ec6
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rbx
leaq (%rax,%rax,2), %r11
leaq (%r8,%r11,8), %rcx
addq $0xc, %rcx
xorl %eax, %eax
cmpl %r9d, -0x4(%rcx,%rax)
je 0x176eb5
addq $0x18, %rax
cmpq %rax, %rbx
jne 0x176ea3
jmp 0x176ec6
leaq (%r8,%r11,8), %rcx
andb $0x7f, 0xf(%rax,%rcx)
movq 0x8(%rdi), %r8
movq 0x28(%rdi), %r10
xorq $0x1, %r9
movq (%r10,%r9,8), %rax
movq %rax, %rcx
andq %rsi, %rcx
je 0x176e30
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r10
leaq (%rax,%rax,2), %r9
leaq (%r8,%r9,8), %rbx
addq $0xc, %rbx
xorl %r11d, %r11d
movl -0x4(%rbx,%r11), %eax
xorl %edx, %eax
cmpl $0x1, %eax
je 0x176f0f
addq $0x18, %r11
cmpq %r11, %r10
jne 0x176ef5
jmp 0x176e30
leaq (%r8,%r9,8), %rax
andb $0x7f, 0xf(%r11,%rax)
jmp 0x176e30
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa0a7d