// File: /workspace/github_cpp_makefile_star2_O1/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
testl $0x7fffffff, 0x4(%rdi)  # imm = 0x7FFFFFFF
je 0x16fe68
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 0x16fe48
testl $0x7fffffff, 0x14(%rdi) # imm = 0x7FFFFFFF
je 0x16fe8f
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 0x16fe73
movq 0x18(%rsi), %rcx
testl $0x7fffffff, 0x14(%rcx) # imm = 0x7FFFFFFF
je 0x170348
xorl %eax, %eax
movq 0x20(%rcx), %rcx
cmpl $0x0, (%rcx,%rax,8)
js 0x17025b
movq 0x10(%rsi), %rdx
leaq (%rax,%rax,4), %rcx
leaq (%rdx,%rcx,8), %rdx
cmpl $0x2, 0x10(%rdx)
jb 0x16ff9b
movl $0x1, %r8d
xorl %r9d, %r9d
movq 0x18(%rdx), %r11
movq (%r11,%r9,8), %r10
shrq $0x20, %r10
movq (%r11,%r8,8), %r11
shrq $0x20, %r11
movq 0x28(%rdi), %rbx
movq (%rbx,%r10,8), %r14
testl %r14d, %r14d
je 0x16ff31
movq 0x8(%rdi), %rbx
movq %r14, %r15
shrq $0x20, %r15
movl %r14d, %r12d
leaq (%r15,%r15,2), %r14
leaq (%rbx,%r14,8), %r15
addq $0xc, %r15
shlq $0x3, %r12
leaq (%r12,%r12,2), %r13
xorl %r12d, %r12d
cmpl %r11d, -0x4(%r15,%r12)
je 0x16ff27
addq $0x18, %r12
cmpq %r12, %r13
jne 0x16ff15
jmp 0x16ff31
leaq (%rbx,%r14,8), %rbx
andb $0x7f, 0xf(%r12,%rbx)
xorq $0x1, %r11
movq 0x28(%rdi), %rbx
movq (%rbx,%r11,8), %rbx
testl %ebx, %ebx
je 0x16ff88
movq 0x8(%rdi), %r11
movq %rbx, %r14
shrq $0x20, %r14
movl %ebx, %r15d
leaq (%r14,%r14,2), %rbx
leaq (%r11,%rbx,8), %r14
addq $0xc, %r14
shlq $0x3, %r15
leaq (%r15,%r15,2), %r12
xorl %r15d, %r15d
movl -0x4(%r14,%r15), %ebp
xorl %r10d, %ebp
cmpl $0x1, %ebp
je 0x16ff7e
addq $0x18, %r15
cmpq %r15, %r12
jne 0x16ff66
jmp 0x16ff88
leaq (%r11,%rbx,8), %r10
andb $0x7f, 0xf(%r15,%r10)
incq %r8
movl 0x10(%rdx), %r10d
incq %r9
cmpq %r10, %r8
jb 0x16fecf
cmpl $0x0, 0x10(%rdx)
je 0x16ffc8
xorl %r8d, %r8d
movq 0x20(%rdi), %r9
movq 0x18(%rdx), %r10
movq (%r10,%r8,8), %r10
shrq $0x21, %r10
andl $0x7fffffff, (%r9,%r10,8) # imm = 0x7FFFFFFF
incq %r8
movl 0x10(%rdx), %r9d
cmpq %r9, %r8
jb 0x16ffa4
movq 0x18(%rsi), %r9
movq 0x28(%r9), %r8
movq %rax, %r10
shlq $0x4, %r10
movq (%r8,%r10), %r10
testl %r10d, %r10d
je 0x170114
movq %r10, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
shlq $0x3, %r8
addq 0x8(%r9), %r8
movl %r10d, %r9d
xorl %r10d, %r10d
leaq (%r10,%r10,2), %r11
cmpl $0x0, 0xc(%r8,%r11,8)
js 0x170108
leaq (%r8,%r11,8), %r11
movl 0x8(%r11), %ebp
movq 0x10(%rsi), %r11
movl %ebp, %ebx
shrl %ebx
leaq (%rbx,%rbx,4), %rbx
testb $0x1, %bpl
jne 0x170031
movq 0x18(%r11,%rbx,8), %rbx
movl 0x4(%rbx), %ebx
jmp 0x170046
movq 0x18(%r11,%rbx,8), %r14
movl 0x10(%r11,%rbx,8), %ebx
decl %ebx
movl 0x4(%r14,%rbx,8), %ebx
xorq $0x1, %rbx
movq 0x18(%r11,%rcx,8), %r14
movl 0x10(%r11,%rcx,8), %r11d
decl %r11d
movq (%r14,%r11,8), %r11
shrq $0x20, %r11
movq 0x28(%rdi), %r14
movq (%r14,%r11,8), %r15
testl %r15d, %r15d
je 0x1700b0
movq 0x8(%rdi), %r14
movq %r15, %r12
shrq $0x20, %r12
movl %r15d, %r13d
leaq (%r12,%r12,2), %r15
leaq (%r14,%r15,8), %r12
addq $0xc, %r12
shlq $0x3, %r13
leaq (,%r13,2), %rbp
addq %r13, %rbp
xorl %r13d, %r13d
cmpl %ebx, -0x4(%r12,%r13)
je 0x1700a6
addq $0x18, %r13
cmpq %r13, %rbp
jne 0x170094
jmp 0x1700b0
leaq (%r14,%r15,8), %r14
andb $0x7f, 0xf(%r13,%r14)
movq 0x28(%rdi), %r14
xorq $0x1, %rbx
movq (%r14,%rbx,8), %r14
testl %r14d, %r14d
je 0x170108
movq 0x8(%rdi), %rbx
movq %r14, %r15
shrq $0x20, %r15
movl %r14d, %r12d
leaq (%r15,%r15,2), %r14
leaq (%rbx,%r14,8), %r15
addq $0xc, %r15
shlq $0x3, %r12
leaq (%r12,%r12,2), %r13
xorl %r12d, %r12d
movl -0x4(%r15,%r12), %ebp
xorl %r11d, %ebp
cmpl $0x1, %ebp
je 0x1700fe
addq $0x18, %r12
cmpq %r12, %r13
jne 0x1700e6
jmp 0x170108
leaq (%rbx,%r14,8), %r11
andb $0x7f, 0xf(%r12,%r11)
incq %r10
cmpq %r9, %r10
jne 0x16fffd
leaq (%rax,%rax), %r8
movq 0x18(%rsi), %r10
movq 0x28(%r10), %r9
movq 0x8(%r9,%r8,8), %r9
testl %r9d, %r9d
je 0x170256
movq %r9, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
shlq $0x3, %r8
addq 0x8(%r10), %r8
movl %r9d, %r9d
xorl %r10d, %r10d
leaq (%r10,%r10,2), %r11
cmpl $0x0, 0xc(%r8,%r11,8)
js 0x17024a
leaq (%r8,%r11,8), %r11
movl 0x8(%r11), %ebp
movq 0x10(%rsi), %r11
movl %ebp, %ebx
shrl %ebx
leaq (%rbx,%rbx,4), %rbx
testb $0x1, %bpl
jne 0x17017b
movq 0x18(%r11,%rbx,8), %rbx
movl 0x4(%rbx), %ebx
jmp 0x170190
movq 0x18(%r11,%rbx,8), %r14
movl 0x10(%r11,%rbx,8), %ebx
decl %ebx
movl 0x4(%r14,%rbx,8), %ebx
xorq $0x1, %rbx
movq 0x18(%r11,%rcx,8), %r11
movq (%r11), %r11
shrq $0x20, %r11
movq %r11, %r14
xorq $0x1, %r14
movq 0x28(%rdi), %r15
movq (%r15,%r14,8), %r15
testl %r15d, %r15d
je 0x1701f8
movq 0x8(%rdi), %r14
movq %r15, %r12
shrq $0x20, %r12
movl %r15d, %r13d
leaq (%r12,%r12,2), %r15
leaq (%r14,%r15,8), %r12
addq $0xc, %r12
shlq $0x3, %r13
leaq (,%r13,2), %rbp
addq %r13, %rbp
xorl %r13d, %r13d
cmpl %ebx, -0x4(%r12,%r13)
je 0x1701ee
addq $0x18, %r13
cmpq %r13, %rbp
jne 0x1701dc
jmp 0x1701f8
leaq (%r14,%r15,8), %r14
andb $0x7f, 0xf(%r13,%r14)
movq 0x28(%rdi), %r14
xorq $0x1, %rbx
movq (%r14,%rbx,8), %r14
testl %r14d, %r14d
je 0x17024a
movq 0x8(%rdi), %rbx
movq %r14, %r15
shrq $0x20, %r15
movl %r14d, %r12d
leaq (%r15,%r15,2), %r14
leaq (%rbx,%r14,8), %r15
addq $0xc, %r15
shlq $0x3, %r12
leaq (%r12,%r12,2), %r13
xorl %r12d, %r12d
cmpl %r11d, -0x4(%r15,%r12)
je 0x170240
addq $0x18, %r12
cmpq %r12, %r13
jne 0x17022e
jmp 0x17024a
leaq (%rbx,%r14,8), %r11
andb $0x7f, 0xf(%r12,%r11)
incq %r10
cmpq %r9, %r10
jne 0x170147
cmpl $0x0, (%rdx)
js 0x170279
incq %rax
movq 0x18(%rsi), %rcx
movl 0x14(%rcx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rax
jb 0x16fea2
jmp 0x170348
movq 0x10(%rsi), %rdx
movq 0x18(%rdx,%rcx,8), %r8
movl 0x10(%rdx,%rcx,8), %ecx
decl %ecx
movq (%r8,%rcx,8), %rcx
shrq $0x20, %rcx
movq (%r8), %rdx
shrq $0x20, %rdx
movq 0x28(%rdi), %r8
movq (%r8,%rcx,8), %r9
testl %r9d, %r9d
je 0x1702e5
movq 0x8(%rdi), %r8
movq %r9, %r10
shrq $0x20, %r10
movl %r9d, %r11d
leaq (%r10,%r10,2), %r9
leaq (%r8,%r9,8), %r10
addq $0xc, %r10
shlq $0x3, %r11
leaq (%r11,%r11,2), %rbx
xorl %r11d, %r11d
cmpl %edx, -0x4(%r10,%r11)
je 0x1702db
addq $0x18, %r11
cmpq %r11, %rbx
jne 0x1702c9
jmp 0x1702e5
leaq (%r8,%r9,8), %r8
andb $0x7f, 0xf(%r11,%r8)
xorq $0x1, %rdx
movq 0x28(%rdi), %r8
movq (%r8,%rdx,8), %r8
testl %r8d, %r8d
je 0x17025b
movq 0x8(%rdi), %rdx
movq %r8, %r9
shrq $0x20, %r9
movl %r8d, %r10d
leaq (%r9,%r9,2), %r8
leaq (%rdx,%r8,8), %r9
addq $0xc, %r9
shlq $0x3, %r10
leaq (%r10,%r10,2), %r11
xorl %r10d, %r10d
movl -0x4(%r9,%r10), %ebx
xorl %ecx, %ebx
cmpl $0x1, %ebx
je 0x170339
addq $0x18, %r10
cmpq %r10, %r11
jne 0x17031f
jmp 0x17025b
leaq (%rdx,%r8,8), %rcx
andb $0x7f, 0xf(%r10,%rcx)
jmp 0x17025b
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa02fa