// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movq %rsi, 0x8(%rsp)
andq $0x0, 0x8(%rsi)
movq %rdx, %r15
movl 0x14(%rdx), %eax
addl %eax, %eax
movslq %eax, %rdx
movq %rcx, (%rsp)
movq %rcx, %rdi
xorl %esi, %esi
callq 0x20e0
testl $0x7fffffff, 0x14(%r15) # imm = 0x7FFFFFFF
je 0x21427
xorl %r13d, %r13d
movb $0x1, %r12b
movq 0x20(%r15), %rcx
leaq (,%r13,2), %r14
movq %r13, %rax
shlq $0x4, %rax
movq (%rcx,%rax), %rax
movl 0x8(%rcx,%r14,8), %ecx
xorl %eax, %ecx
je 0x21412
leal 0x1(,%r13,2), %ecx
testl %eax, %eax
cmovel %ecx, %r14d
movq (%rsp), %rax
cmpb $0x0, (%rax,%r13,2)
jne 0x21412
leaq 0x1(,%r13,2), %rax
movq (%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
jne 0x21412
movq 0x8(%rsp), %rbp
movq 0x10(%rbp), %rax
cmpq %rax, 0x8(%rbp)
jne 0x212f5
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rbp)
movq (%rbp), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, (%rbp)
movq (%rbp), %rbx
movq 0x8(%rbp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rbp)
imulq $0x18, %rax, %rbp
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx,%rbp)
pushq $0x2
popq %rax
movq %rax, 0x10(%rbx,%rbp)
pushq $0x8
popq %rdi
callq 0x2280
movq %rax, (%rbx,%rbp)
movl %r14d, %ecx
movq %rcx, %rdx
xorq $0x1, %rdx
movq $0x1, 0x8(%rbx,%rbp)
movl %edx, (%rax)
pushq $0x2
popq %rsi
movq %rsi, 0x8(%rbx,%rbp)
movl %r14d, 0x4(%rax)
movq (%rsp), %rax
movb %r12b, (%rax,%rdx)
movb %r12b, (%rax,%rcx)
movq 0x20(%r15), %rax
movq (%rax,%rcx,8), %rax
testl %eax, %eax
je 0x21412
addq %rbp, %rbx
movq 0x8(%r15), %rcx
shrq $0x1b, %rax
andq $-0x20, %rax
movl (%rcx,%rax), %r14d
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x213a1
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, (%rbx)
movq (%rbx), %rdi
movq 0x8(%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rbx)
movl %r14d, (%rdi,%rax,4)
movq 0x10(%rbx), %rax
cmpq %rax, %rcx
jne 0x213db
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %r14, %rax
xorq $0x1, %rax
movq (%rbx), %rcx
movq 0x8(%rbx), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%rbx)
movl %eax, (%rcx,%rdx,4)
movq (%rsp), %rcx
movb %r12b, (%rcx,%r14)
movb %r12b, (%rcx,%rax)
movq 0x20(%r15), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rax
testl %eax, %eax
jne 0x21366
incq %r13
movl 0x14(%r15), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r13
jb 0x2126c
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x10(%rsp)
testl $0x7fffffff, 0x14(%r15) # imm = 0x7FFFFFFF
jne 0x2147f
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ecx
movl %ecx, %r9d
movq 0x10(%rsp), %r8
subl %r8d, %r9d
movq 0x268b87(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x210386(%rip), %rsi    # 0x2317e9
leaq 0x2103bc(%rip), %rdx    # 0x231826
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0
xorl %r12d, %r12d
movq (%rsp), %rax
cmpb $0x0, (%rax,%r12,2)
jne 0x2149c
leaq (%r12,%r12), %rax
movq (%rsp), %rcx
cmpb $0x0, 0x1(%rcx,%rax)
je 0x214af
incq %r12
movl 0x14(%r15), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r12
jb 0x21482
jmp 0x2143f
movq 0x20(%r15), %rcx
cmpl $0x0, (%rcx,%rax,8)
je 0x2149c
movq %rax, %rdx
orq $0x1, %rdx
cmpl $0x0, (%rcx,%rdx,8)
je 0x2149c
movl %eax, %esi
xorl %edx, %edx
movq 0x8(%rsp), %rbx
movl %esi, %esi
movq (%rcx,%rsi,8), %rsi
testl %esi, %esi
je 0x21536
testq %rdx, %rdx
je 0x2150f
movsd 0x18(%rdx), %xmm0
movq 0x8(%r15), %r8
movq %rsi, %r9
shrq $0x1b, %r9
andq $-0x20, %r9
leaq (%r8,%r9), %rdi
ucomisd 0x18(%r8,%r9), %xmm0
ja 0x2150f
jne 0x2151e
jp 0x2151e
movsd 0x10(%rdx), %xmm0
ucomisd 0x10(%rdi), %xmm0
jbe 0x2151e
movq %rsi, %rdx
shrq $0x1b, %rdx
andq $-0x20, %rdx
addq 0x8(%r15), %rdx
movq 0x8(%r15), %rdi
shrq $0x1b, %rsi
andq $-0x20, %rsi
movl (%rdi,%rsi), %esi
xorq $0x1, %rsi
cmpq %rsi, %rax
jne 0x214cf
movl (%rdx), %r13d
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x21564
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rbx)
movq (%rbx), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %rbx, %rdx
movq %r13, %rbp
xorq $0x1, %rbp
movq (%rbx), %rbx
movq 0x8(%rdx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rdx)
imulq $0x18, %rax, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx,%r14)
pushq $0x2
popq %rax
movq %rax, 0x10(%rbx,%r14)
pushq $0x8
popq %rdi
callq 0x2280
movq %rax, (%rbx,%r14)
movq $0x1, 0x8(%rbx,%r14)
movl %r13d, (%rax)
pushq $0x2
popq %rcx
movq %rcx, 0x8(%rbx,%r14)
movl %ebp, 0x4(%rax)
movq (%rsp), %rsi
movb $0x1, %al
movb %al, (%rsi,%r13)
movb %al, (%rsi,%rbp)
movq 0x8(%r15), %rax
movq 0x20(%r15), %rcx
movl %ebp, %edx
movl 0x4(%rcx,%rdx,8), %ecx
shlq $0x5, %rcx
movl (%rax,%rcx), %ebp
movq %rbp, %r13
xorq $0x1, %r13
cmpb $0x0, (%rsi,%r13)
jne 0x2149c
addq %r14, %rbx
movq %r13, %r14
addq (%rsp), %r14
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x2161f
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x10(%rbx)
movq (%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, (%rbx)
movq (%rbx), %rdi
movq 0x8(%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rbx)
movl %ebp, (%rdi,%rax,4)
movq 0x10(%rbx), %rax
cmpq %rax, %rcx
jne 0x21658
leaq (%rax,%rax), %rsi
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x10(%rbx)
shlq $0x2, %rsi
callq 0x2370
movq %rax, (%rbx)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
movl %r13d, (%rax,%rcx,4)
movl %ebp, %eax
movq (%rsp), %rsi
movb $0x1, %cl
movb %cl, (%rsi,%rax)
movb %cl, (%r14)
movq 0x8(%r15), %rax
movq 0x20(%r15), %rcx
movl %r13d, %edx
movl 0x4(%rcx,%rdx,8), %ecx
shlq $0x5, %rcx
movl (%rax,%rcx), %ebp
movq %rbp, %r13
xorq $0x1, %r13
leaq (%rsi,%r13), %r14
cmpb $0x0, (%rsi,%r13)
je 0x215f4
jmp 0x2149c