// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x28, %rsp
movq %rsi, (%rsp)
movq %rdi, %rbx
pushq $0xc
popq %rax
xorl %ecx, %ecx
movl 0x4(%rbx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
jae 0x132e2b
movq 0x8(%rbx), %rdx
orl $0x80000000, (%rdx,%rax) # imm = 0x80000000
incq %rcx
addq $0x18, %rax
jmp 0x132e09
xorl %eax, %eax
movl 0x14(%rbx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rax
jae 0x132e4b
movq 0x20(%rbx), %rcx
orl $0x80000000, (%rcx,%rax,8) # imm = 0x80000000
incq %rax
jmp 0x132e2d
xorl %r13d, %r13d
movq (%rsp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r13
jae 0x1330d3
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%r13,8)
js 0x1330cb
imulq $0x28, %r13, %rcx
movq (%rsp), %rax
movq 0x10(%rax), %r12
movq %rcx, 0x10(%rsp)
addq %rcx, %r12
pushq $0x1
popq %r14
movl 0x10(%r12), %ecx
cmpq %rcx, %r14
jae 0x132ece
movq 0x18(%r12), %rax
movl -0x4(%rax,%r14,8), %ebp
movl 0x4(%rax,%r14,8), %r15d
movq %rbx, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x1330e9
xorl $0x1, %r15d
xorl $0x1, %ebp
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x1330e9
incq %r14
jmp 0x132e8f
xorl %eax, %eax
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x132ef9
movq 0x20(%rbx), %rcx
movq 0x18(%r12), %rdx
movq (%rdx,%rax,8), %rdx
shrq $0x21, %rdx
andl $0x7fffffff, (%rcx,%rdx,8) # imm = 0x7FFFFFFF
incq %rax
movl 0x10(%r12), %ecx
jmp 0x132ed0
leaq (,%r13,2), %rax
movq %rax, 0x18(%rsp)
movq (%rsp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rcx
movq 0x28(%rax), %rax
movq %r13, 0x20(%rsp)
movq %r13, %rdx
shlq $0x4, %rdx
movq (%rax,%rdx), %rax
movl %eax, %edx
shrq $0x20, %rax
imulq $0x18, %rdx, %rdx
movq %rdx, 0x8(%rsp)
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %r13
addq $0xc, %r13
xorl %r14d, %r14d
cmpq %r14, 0x8(%rsp)
je 0x132fca
cmpl $0x0, (%r13,%r14)
js 0x132fc1
movl -0x4(%r13,%r14), %edx
movq (%rsp), %rax
movq 0x10(%rax), %rax
movl %edx, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
testb $0x1, %dl
jne 0x132f78
movq 0x18(%rax,%rcx), %rcx
movl 0x4(%rcx), %r15d
jmp 0x132f8c
movq 0x18(%rax,%rcx), %rdx
movl 0x10(%rax,%rcx), %ecx
decl %ecx
movl 0x4(%rdx,%rcx,8), %r15d
xorq $0x1, %r15
movq 0x10(%rsp), %rdx
movq 0x18(%rax,%rdx), %rcx
movl 0x10(%rax,%rdx), %eax
decl %eax
movl 0x4(%rcx,%rax,8), %ebp
movq %rbx, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x1330e9
xorl $0x1, %r15d
xorl $0x1, %ebp
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x1330e9
addq $0x18, %r14
jmp 0x132f44
movq (%rsp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rcx
movq 0x28(%rax), %rax
movq 0x18(%rsp), %rdx
movq 0x8(%rax,%rdx,8), %rax
movl %eax, %edx
shrq $0x20, %rax
imulq $0x18, %rdx, %rdx
movq %rdx, 0x8(%rsp)
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %r13
addq $0xc, %r13
xorl %r14d, %r14d
cmpq %r14, 0x8(%rsp)
je 0x13307e
cmpl $0x0, (%r13,%r14)
js 0x133078
movl -0x4(%r13,%r14), %edx
movq (%rsp), %rax
movq 0x10(%rax), %rax
movl %edx, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
testb $0x1, %dl
jne 0x133036
movq 0x18(%rax,%rcx), %rcx
movl 0x4(%rcx), %r15d
jmp 0x13304a
movq 0x18(%rax,%rcx), %rdx
movl 0x10(%rax,%rcx), %ecx
decl %ecx
movl 0x4(%rdx,%rcx,8), %r15d
xorq $0x1, %r15
movq 0x10(%rsp), %rcx
movq 0x18(%rax,%rcx), %rax
movl 0x4(%rax), %ebp
movl %ebp, %esi
xorl $0x1, %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x1330e9
xorl $0x1, %r15d
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x1330e9
addq $0x18, %r14
jmp 0x133002
cmpl $0x0, (%r12)
movq 0x20(%rsp), %r13
jns 0x1330cb
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdx
movq 0x18(%rax,%rdx), %rcx
movl 0x10(%rax,%rdx), %eax
decl %eax
movl 0x4(%rcx,%rax,8), %ebp
movl 0x4(%rcx), %r15d
movq %rbx, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x1330e9
xorl $0x1, %r15d
xorl $0x1, %ebp
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x1330e9
incq %r13
jmp 0x132e4e
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x694eb