// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/horder.cpp
// Function: get_backbone_layout(horder_t*, sc_lay_t*, osg_t*, unsigned char*)
void get_backbone_layout(horder_t *h, sc_lay_t *sl, osg_t *lg, uint8_t *vis)
{
    uint32_t k, v, nc = 0, c = 0;
    lay_t *p = NULL;
    osg_arc_t *t = NULL;
    sl->n = 0;
    ///in lg, there might be single-path paths or cycles
    memset(vis, 0, sizeof(uint8_t)*(lg->n_seq<<1));
    for (k = 0; k < lg->n_seq; k++)
    {
        ///I guess this should be (!!(asg_arc_n(lg, k<<1)))^(!!(asg_arc_n(lg, (k<<1)+1)))?
        ///no, since asg_arc_n is at most 1
        if((asg_arc_n(lg, k<<1))^(asg_arc_n(lg, (k<<1)+1)))///end scaffolding
        {
            v = (asg_arc_n(lg, k<<1)?(k<<1):((k<<1)+1));
            if(vis[k<<1] || vis[(k<<1)+1]) continue;
            kv_pushp(lay_t, *sl, &p);
            kv_init(*p);
            kv_push(uint32_t, *p, v^1);
            kv_push(uint32_t, *p, v);
            vis[v] = vis[v^1] = 1;

            while (asg_arc_n(lg, v))
            {
                v = (arc_first(lg, v).v)^1;
                kv_push(uint32_t, *p, v^1);
                kv_push(uint32_t, *p, v);
                vis[v] = vis[v^1] = 1;
            }
        }
    }
    
    nc = sl->n;
    for (k = 0; k < lg->n_seq; k++)
    {
        if(vis[k<<1] || vis[(k<<1)+1]) continue;
        if(asg_arc_n(lg, k<<1) && asg_arc_n(lg, (k<<1)+1))//circle
        {
            v = k<<1; t = NULL;
            while (asg_arc_n(lg, v))
            {
                if((!t) || (t->nw > arc_first(lg, v).nw) 
                            || (t->nw == arc_first(lg, v).nw && t->w > arc_first(lg, v).w))
                {
                    t = &(arc_first(lg, v));
                } 
                v = (arc_first(lg, v).v)^1;
                if(v == (k<<1)) break;
            }

            v = t->v^1;
            kv_pushp(lay_t, *sl, &p);
            kv_init(*p);
            kv_push(uint32_t, *p, v^1);
            kv_push(uint32_t, *p, v);
            vis[v] = vis[v^1] = 1;

            while (1)
            {
                v = (arc_first(lg, v).v)^1;
                if(vis[v]) break;
                kv_push(uint32_t, *p, v^1);
                kv_push(uint32_t, *p, v);
                vis[v] = vis[v^1] = 1;
            }
        }
    }

    c = sl->n - nc;
    fprintf(stderr, "[M::%s::] # Scaffolds: %u, # non-circles: %u, # circles: %u\n", 
    __func__, (uint32_t)sl->n, nc, c);
    /*******************************for debug************************************/
    // for (k = 0; k < sl->n; k++)
    // {
    //     p = &(sl->a[k]);
    //     if(k >= nc)
    //     {
    //         fprintf(stderr, "%s:\t", k < nc?"non-circle":"circle");
    //         for (i = 0; i < p->n; i+=2)
    //         {
    //             if((p->a[i]>>1) != (p->a[i+1]>>1)) fprintf(stderr, "ERROR-S\n");
    //             fprintf(stderr, "utg%.6ul[%u%u](%u)#", p->a[i]>>1, p->a[i]&1, p->a[i+1]&1, h->ug->u.a[p->a[i]>>1].len);
    //         }
    //         fprintf(stderr, "\n");
    //     }

    //     for (i = 1; i < p->n; i+=2)
    //     {
    //         if(k < nc)
    //         {
    //             if(i < p->n - 1)
    //             {
    //                 if(asg_arc_n(lg, p->a[i])!=1) fprintf(stderr, "ERROR-A\n");
    //                 if(arc_first(lg, p->a[i]).v!=p->a[i+1]) fprintf(stderr, "ERROR-B\n");
    //             }
                
    //             if(i == p->n - 1)
    //             {
    //                 if(asg_arc_n(lg, p->a[i])!=0) fprintf(stderr, "ERROR-A-0\n");
    //             }
    //         }


    //         if(k >= nc)
    //         {
    //             if(i < p->n - 1)
    //             {
    //                 if(asg_arc_n(lg, p->a[i])!=1) fprintf(stderr, "ERROR-A\n");
    //                 if(arc_first(lg, p->a[i]).v!=p->a[i+1]) fprintf(stderr, "ERROR-B\n");
    //                 fprintf(stderr, "i-%u, nw-%f\n", i, arc_first(lg, p->a[i]).nw);
    //             }

    //             if(i == p->n - 1)
    //             {
    //                 if(asg_arc_n(lg, p->a[i])!=1) fprintf(stderr, "ERROR-A\n");
    //                 if(arc_first(lg, p->a[i]).v!=p->a[0]) fprintf(stderr, "ERROR-B-0\n");
    //                 fprintf(stderr, "i-%u, nw-%f\n", i, arc_first(lg, p->a[i]).nw);
    //             }
    //         }
    //     }
        
    // }
    

    // for (k = 0; k < lg->n_seq; k++)
    // {
    //     if(!asg_arc_n(lg, k<<1) && !asg_arc_n(lg, (k<<1)+1))
    //     {
    //         if(vis[k<<1] || vis[(k<<1)+1]) fprintf(stderr, "ERROR-bone\n");
    //     }
    // }
    /*******************************for debug************************************/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r14
movq %rsi, 0x8(%rsp)
andq $0x0, 0x8(%rsi)
movq %rdx, %r15
movl 0x14(%rdx), %eax
addl %eax, %eax
movslq %eax, %rdx
xorl %ebp, %ebp
movq %rcx, %rdi
xorl %esi, %esi
callq 0x20e0
movl 0x14(%r15), %edx
movl %edx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %rbp
jae 0x138d84
movq 0x20(%r15), %rcx
leaq (,%rbp,2), %r13
movq %rbp, %rax
shlq $0x4, %rax
movq (%rcx,%rax), %rax
movl 0x8(%rcx,%r13,8), %ecx
xorl %eax, %ecx
je 0x138c2e
leal 0x1(,%rbp,2), %ecx
testl %eax, %eax
cmovel %ecx, %r13d
cmpb $0x0, (%r14,%rbp,2)
jne 0x138c2e
leaq 0x1(,%rbp,2), %rax
cmpb $0x0, (%r14,%rax)
je 0x138c33
incq %rbp
jmp 0x138bd7
movq 0x8(%rsp), %rdx
movq (%rdx), %rdi
movq 0x8(%rdx), %rax
cmpq 0x10(%rdx), %rax
jne 0x138c74
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
movq %rax, %rbx
cmoveq %rax, %rcx
movq %rcx, 0x10(%rdx)
imulq $0x18, %rcx, %rsi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, %rdi
movq %rax, (%rdx)
movq 0x8(%rdx), %rax
jmp 0x138c77
pushq $0x2
popq %rbx
movq %rdi, 0x10(%rsp)
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rdx)
imulq $0x18, %rax, %rax
movq %rax, 0x18(%rsp)
leaq (%rdi,%rax), %r12
movq %rbx, 0x10(%rdi,%rax)
pushq $0x8
popq %rdi
callq 0x2280
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rsi
movq %rax, (%rsi,%rdx)
movq %rbx, %rcx
movl %r13d, %ebx
xorl $0x1, %ebx
movl %ebx, (%rax)
movq %rcx, 0x8(%rsi,%rdx)
movl %r13d, 0x4(%rax)
movl %ebx, %eax
movb $0x1, %cl
movb %cl, (%r14,%rax)
movl %r13d, %eax
movb %cl, (%r14,%rax)
movq 0x20(%r15), %rcx
movq (%rcx,%rax,8), %rax
testl %eax, %eax
je 0x138c2e
movq 0x8(%r15), %rcx
shrq $0x1b, %rax
andq $-0x20, %rax
movl (%rcx,%rax), %ebx
movq (%r12), %rax
movq 0x8(%r12), %rdx
movq 0x10(%r12), %rcx
cmpq %rcx, %rdx
jne 0x138d31
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %r13
cmoveq %r13, %rsi
movq %rsi, 0x10(%r12)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r12)
movq 0x8(%r12), %rdx
movq 0x10(%r12), %rcx
jmp 0x138d35
pushq $0x2
popq %r13
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%r12)
movl %ebx, (%rax,%rdx,4)
cmpq %rcx, %rsi
jne 0x138d6b
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r13, %rsi
movq %rsi, 0x10(%r12)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r12)
movq 0x8(%r12), %rsi
movl %ebx, %r13d
xorl $0x1, %r13d
leaq 0x1(%rsi), %rcx
movq %rcx, 0x8(%r12)
movl %r13d, (%rax,%rsi,4)
jmp 0x138cc0
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movl %edx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r13
jae 0x138f91
cmpb $0x0, (%r14,%r13,2)
jne 0x138dbc
leaq (,%r13,2), %rax
cmpb $0x0, 0x1(%r14,%rax)
je 0x138dc1
incq %r13
jmp 0x138d95
movq 0x20(%r15), %rcx
cmpl $0x0, (%rcx,%rax,8)
je 0x138dbc
movq %rax, %rsi
orq $0x1, %rsi
cmpl $0x0, (%rcx,%rsi,8)
je 0x138dbc
movl %eax, %esi
xorl %edx, %edx
movl %esi, %esi
movq (%rcx,%rsi,8), %rsi
testl %esi, %esi
je 0x138e3d
movq 0x8(%r15), %rdi
testq %rdx, %rdx
je 0x138e1f
movsd 0x18(%rdx), %xmm0
shrq $0x20, %rsi
movq %rsi, %r9
shlq $0x5, %r9
leaq (%rdi,%r9), %r8
ucomisd 0x18(%rdi,%r9), %xmm0
ja 0x138e23
jne 0x138e2d
jp 0x138e2d
movsd 0x10(%rdx), %xmm0
ucomisd 0x10(%r8), %xmm0
ja 0x138e23
jmp 0x138e2d
shrq $0x20, %rsi
movq %rsi, %rdx
shlq $0x5, %rdx
addq %rdi, %rdx
shlq $0x5, %rsi
movl (%rdi,%rsi), %esi
xorq $0x1, %rsi
cmpq %rsi, %rax
jne 0x138ddc
movl (%rdx), %ebx
movq 0x8(%rsp), %rdx
movq (%rdx), %rdi
movq 0x8(%rdx), %rax
cmpq 0x10(%rdx), %rax
jne 0x138e7b
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rdx)
imulq $0x18, %rcx, %rsi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, %rdi
movq %rax, (%rdx)
movq 0x8(%rdx), %rax
movq %rdi, 0x10(%rsp)
movq %rbx, %r12
movq %rbx, 0x18(%rsp)
xorq $0x1, %r12
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rdx)
imulq $0x18, %rax, %rbp
leaq (%rdi,%rbp), %rbx
pushq $0x2
popq %rax
movq %rax, 0x10(%rdi,%rbp)
pushq $0x8
popq %rdi
callq 0x2280
movq 0x10(%rsp), %rsi
movq %rax, (%rsi,%rbp)
movq 0x18(%rsp), %rcx
movl %ecx, (%rax)
pushq $0x2
popq %rdx
movq %rdx, 0x8(%rsi,%rbp)
movl %r12d, 0x4(%rax)
movb $0x1, (%r14,%rcx)
movq %r12, %rcx
addq %r14, %rcx
movb $0x1, (%rcx)
movq 0x8(%r15), %rax
movq 0x20(%r15), %rcx
movl %r12d, %edx
movl 0x4(%rcx,%rdx,8), %ecx
shlq $0x5, %rcx
movl (%rax,%rcx), %ebp
movq %rbp, %r12
xorq $0x1, %r12
cmpb $0x0, (%r14,%r12)
jne 0x138f88
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq 0x10(%rbx), %rcx
cmpq %rcx, %rdx
jne 0x138f37
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rdx
movq 0x10(%rbx), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%rbx)
movl %ebp, (%rax,%rdx,4)
cmpq %rcx, %rsi
jne 0x138f6c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rsi
movq %r12, %rcx
addq %r14, %rcx
leaq 0x1(%rsi), %rdx
movq %rdx, 0x8(%rbx)
movl %r12d, (%rax,%rsi,4)
movb $0x1, (%r14,%rbp)
jmp 0x138ed3
movl 0x14(%r15), %edx
jmp 0x138dbc
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ecx
movl %ecx, %r9d
movq 0x20(%rsp), %r8
subl %r8d, %r9d
movq 0x11a035(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xb6944(%rip), %rsi     # 0x1ef8f9
leaq 0xb697a(%rip), %rdx     # 0x1ef936
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0