// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
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
jne 0x2066c
movq 0x8(%rbx), %r8
jmp 0x2082d
movq %rbx, 0x8(%rsp)
movq 0x20(%r15), %rax
xorl %ebp, %ebp
movb $0x1, %bl
movq (%rsp), %rdi
leaq (,%rbp,2), %r14
movq %rbp, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rcx
movl 0x8(%rax,%r14,8), %edx
xorl %ecx, %edx
je 0x2080a
leal 0x1(,%rbp,2), %edx
testl %ecx, %ecx
cmovel %edx, %r14d
cmpb $0x0, (%rdi,%rbp,2)
jne 0x2080a
leaq 0x1(,%rbp,2), %rcx
cmpb $0x0, (%rdi,%rcx)
jne 0x2080a
movq 0x8(%rsp), %rdx
movq (%rdx), %r13
movq 0x8(%rdx), %rax
cmpq 0x10(%rdx), %rax
jne 0x20705
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rdx)
imulq $0x18, %rcx, %rsi
movq %r13, %rdi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, %r13
movq %rax, (%rdx)
movq 0x8(%rdx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rdx)
imulq $0x18, %rax, %r12
pushq $0x2
popq %rax
movq %rax, 0x10(%r13,%r12)
pushq $0x8
popq %rdi
callq 0x2280
movq %rax, (%r13,%r12)
movl %r14d, %ecx
movq %rcx, %rdx
xorq $0x1, %rdx
movl %edx, (%rax)
pushq $0x2
popq %rsi
movq %rsi, 0x8(%r13,%r12)
movl %r14d, 0x4(%rax)
movq (%rsp), %rdi
movb %bl, (%rdi,%rdx)
movb %bl, (%rdi,%rcx)
movq 0x20(%r15), %rax
movq (%rax,%rcx,8), %rcx
testl %ecx, %ecx
je 0x2080a
addq %r12, %r13
movq 0x8(%r15), %rax
shrq $0x1b, %rcx
andq $-0x20, %rcx
movl (%rax,%rcx), %r14d
movq (%r13), %rax
movq 0x8(%r13), %rdx
movq 0x10(%r13), %rcx
cmpq %rcx, %rdx
jne 0x207a6
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r13)
movq 0x8(%r13), %rdx
movq 0x10(%r13), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%r13)
movl %r14d, (%rax,%rdx,4)
cmpq %rcx, %rsi
jne 0x207dd
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r13)
movq 0x8(%r13), %rsi
movq (%rsp), %rdi
movq %r14, %rcx
xorq $0x1, %rcx
leaq 0x1(%rsi), %rdx
movq %rdx, 0x8(%r13)
movl %ecx, (%rax,%rsi,4)
movb %bl, (%rdi,%r14)
movb %bl, (%rdi,%rcx)
movq 0x20(%r15), %rax
movq (%rax,%rcx,8), %rcx
testl %ecx, %ecx
jne 0x2075b
incq %rbp
movl 0x14(%r15), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbp
jb 0x2067d
movq 0x8(%rsp), %rax
movq 0x8(%rax), %r8
testl %ecx, %ecx
jne 0x20835
movq %r8, %rcx
jmp 0x20a63
movq %r8, 0x10(%rsp)
xorl %r13d, %r13d
cmpb $0x0, (%rdi,%r13,2)
jne 0x20853
leaq (,%r13,2), %rax
cmpb $0x0, 0x1(%rdi,%rax)
je 0x20869
incq %r13
movl 0x14(%r15), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r13
jb 0x2083d
jmp 0x20a55
movq 0x20(%r15), %rcx
cmpl $0x0, (%rcx,%rax,8)
je 0x20853
movq %rax, %rdx
orq $0x1, %rdx
cmpl $0x0, (%rcx,%rdx,8)
je 0x20853
movl %eax, %esi
xorl %edx, %edx
pushq $0x2
popq %r14
movl %esi, %esi
movq (%rcx,%rsi,8), %rsi
testl %esi, %esi
je 0x208e9
movq 0x8(%r15), %rdi
testq %rdx, %rdx
je 0x208cb
movsd 0x18(%rdx), %xmm0
shrq $0x20, %rsi
movq %rsi, %r9
shlq $0x5, %r9
leaq (%rdi,%r9), %r8
ucomisd 0x18(%rdi,%r9), %xmm0
ja 0x208cf
jne 0x208d9
jp 0x208d9
movsd 0x10(%rdx), %xmm0
ucomisd 0x10(%r8), %xmm0
ja 0x208cf
jmp 0x208d9
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 0x20888
movl (%rdx), %ebx
movq 0x8(%rsp), %rdx
movq (%rdx), %rbp
movq 0x8(%rdx), %rax
cmpq 0x10(%rdx), %rax
jne 0x20927
leaq (%rax,%rax), %rcx
testq %rax, %rax
cmoveq %r14, %rcx
movq %rcx, 0x10(%rdx)
imulq $0x18, %rcx, %rsi
movq %rbp, %rdi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, %rbp
movq %rax, (%rdx)
movq 0x8(%rdx), %rax
movq %r14, %rsi
movq %rbx, %r12
xorq $0x1, %r12
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rdx)
imulq $0x18, %rax, %r14
movq %rsi, 0x10(%rbp,%r14)
pushq $0x8
popq %rdi
callq 0x2280
movq %rax, (%rbp,%r14)
movl %ebx, (%rax)
pushq $0x2
popq %rcx
movq %rcx, 0x8(%rbp,%r14)
movl %r12d, 0x4(%rax)
movq (%rsp), %rdi
movb $0x1, %al
movb %al, (%rdi,%rbx)
movb %al, (%rdi,%r12)
movq 0x8(%r15), %rax
movq 0x20(%r15), %rcx
movl 0x4(%rcx,%r12,8), %ecx
shlq $0x5, %rcx
movl (%rax,%rcx), %r12d
movq %r12, %rbx
xorq $0x1, %rbx
cmpb $0x0, (%rdi,%rbx)
jne 0x20853
addq %r14, %rbp
movq %rbx, %r14
addq %rdi, %r14
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
movq 0x10(%rbp), %rcx
cmpq %rcx, %rdx
jne 0x209d4
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbp)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbp)
movq 0x8(%rbp), %rdx
movq 0x10(%rbp), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%rbp)
movl %r12d, (%rax,%rdx,4)
cmpq %rcx, %rsi
jne 0x20a0b
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%rbp)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbp)
movq 0x8(%rbp), %rsi
movq (%rsp), %rdi
leaq 0x1(%rsi), %rcx
movq %rcx, 0x8(%rbp)
movl %ebx, (%rax,%rsi,4)
movl %r12d, %eax
movb $0x1, %cl
movb %cl, (%rdi,%rax)
movb %cl, (%r14)
movq 0x8(%r15), %rax
movq 0x20(%r15), %rcx
movl %ebx, %edx
movl 0x4(%rcx,%rdx,8), %ecx
shlq $0x5, %rcx
movl (%rax,%rcx), %r12d
movq %r12, %rbx
xorq $0x1, %rbx
leaq (%rdi,%rbx), %r14
cmpb $0x0, (%rdi,%rbx)
je 0x20999
jmp 0x20853
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rsp), %r8
movl %ecx, %r9d
subl %r8d, %r9d
movq 0x274570(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x21cfbf(%rip), %rsi    # 0x23da39
leaq 0x21cff5(%rip), %rdx    # 0x23da76
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0