// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/horder.cpp
// Function: gen_mc_clus_backbone_layout(scg_t*, asg64_v*, unsigned int*, unsigned int, unsigned int*)
void gen_mc_clus_backbone_layout(scg_t *sg, asg64_v *res, uint32_t *out, uint32_t out_n, uint32_t *buf)
{
    uint64_t k, l, i, rn, n0, n1, v, z; int64_t m, s, e; osg_arc_t *t = NULL;
    kv_resize(uint64_t, *res, sg->g->n_seq); res->n = sg->g->n_seq;
    memset(res->a, 0, sizeof((*(res->a)))*res->n); rn = res->n;
    // fprintf(stderr, "[M::%s::]******Start******\n",__func__);
    
    for (k = 0; k < sg->g->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
        n0 = asg_arc_n(sg->g, (k<<1));
        n1 = asg_arc_n(sg->g, ((k<<1)+1));
        assert((n0 <= 1) && (n1 <= 1));
        ///1&&0; 0&&0; 1&&1;
        if((n0^n1) || ((!n0) && (!n1))) {
            v = (n0?(k<<1):((k<<1)+1));
            // if(vis[k<<1] || vis[(k<<1)+1]) continue;
            // if((res->a[k]>>32) || ((uint32_t)res->a[k])) continue;
            if(res->a[k]) 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;
            // kv_push(uint64_t, *res, (((uint64_t)((v^1)<<32))|((uint64_t)(v))|((uint64_t)(0x8000000000000000))));
            kv_push(uint64_t, *res, (((uint64_t)((v^1)<<32))|((uint64_t)(v))));
            // res->a[v>>1] |= ((uint64_t)(1))<<32;
            // res->a[v>>1] |= ((uint64_t)(1));
            res->a[v>>1] = 1;

            while (asg_arc_n(sg->g, v)) {
                v = (arc_first(sg->g, v).v)^1;
                // kv_push(uint32_t, *p, v^1);
                // kv_push(uint32_t, *p, v);
                // vis[v] = vis[v^1] = 1;
                kv_push(uint64_t, *res, ((uint64_t)((v^1)<<32))|((uint64_t)(v)));
                // res->a[v>>1] |= ((uint64_t)(1))<<32;
                // res->a[v>>1] |= ((uint64_t)(1));
                res->a[v>>1] = 1;
            }
        }
    }


    for (k = 0; k < sg->g->n_seq; k++) {
        // if(vis[k<<1] || vis[(k<<1)+1]) continue;
        // if((res->a[k]>>32) || ((uint32_t)res->a[k])) continue;
        if(res->a[k]) continue;
        n0 = asg_arc_n(sg->g, (k<<1));
        n1 = asg_arc_n(sg->g, ((k<<1)+1));
        assert((n0 == 1) && (n1 == 1));///must within a circle


        v = k<<1; t = NULL;
        while (asg_arc_n(sg->g, v)) {
            if((!t) || (t->nw > arc_first(sg->g, v).nw)) {
                t = &(arc_first(sg->g, v));
            } 
            v = (arc_first(sg->g, 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;
        // kv_push(uint64_t, *res, (((uint64_t)((v^1)<<32))|((uint64_t)(v))|((uint64_t)(0x8000000000000000))));
        kv_push(uint64_t, *res, ((uint64_t)((v^1)<<32))|((uint64_t)(v)));
        // res->a[v>>1] |= ((uint64_t)(1))<<32;
        // res->a[v>>1] |= ((uint64_t)(1));
        res->a[v>>1] = 1;

        while (1) {
            v = (arc_first(sg->g, v).v)^1;
            // if(vis[v]) break;
            if(res->a[v>>1]) break;
            // kv_push(uint32_t, *p, v^1);
            // kv_push(uint32_t, *p, v);
            // vis[v] = vis[v^1] = 1;
            kv_push(uint64_t, *res, ((uint64_t)((v^1)<<32))|((uint64_t)(v)));
            // res->a[v>>1] |= ((uint64_t)(1))<<32;
            // res->a[v>>1] |= ((uint64_t)(1));
            res->a[v>>1] = 1;
        }
    }

    // uint32_t db_on = 0, db_z = 0;
    assert((res->n-rn) == sg->g->n_seq);
    for (l = 0, k = 1, i = 0; k <= out_n; k++) {
       if(k == out_n || out[k] == (uint32_t)-1) {
            if(k > l) {
                res->a[i++] = ((l<<32)|(k)); ///db_on += k - l;
            } else {
                assert(k == out_n);
            }
            l = k + 1;
       }
    }
    assert(i == sg->g->n_seq && i == rn);
    for (k = rn, z = 0; k < res->n; k++) {
        v = (res->a[k]>>32); 
        s = res->a[v>>1]>>32; e = (uint32_t)res->a[v>>1];
        if(!(v&1)) {
            for (m = s; m < e; m++) buf[z++] = out[m]; 
        } else {
            for (m = e-1; m >= s; m--) buf[z++] = out[m]; 
        }

        // if(e > s) {
        //     fprintf(stderr, "[M::%s::]\t#chain::%ld\tutg%.6ul->utg%.6ul\n", 
        //         __func__, (e-s), buf[z-(e-s)]+1, buf[z-1]+1);
        // }
        
        buf[z++] = (uint32_t)-1; 
        // db_z += e - s;
    }
    // if(!(z == out_n)) {
    //     fprintf(stderr, "[M::%s] sg->g->n_arc::%u, sg->g->n_seq::%u, z::%lu, out_n::%u, db_z::%u, db_on::%u\n", __func__, 
	//         sg->g->n_arc, sg->g->n_seq, z, out_n, db_z, db_on);
    // }
    assert(z == out_n);
    memcpy(out, buf, sizeof((*out))*out_n);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r13
movl %ecx, 0xc(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r15
movq %rdi, %r12
movq (%rdi), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, 0x8(%rsi)
jae 0x17735c
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r15)
movq 0x10(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movabsq $0x100000000, %rbx      # imm = 0x100000000
movq (%r12), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdx, (%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x20e0
movq %rbx, %r11
movq (%r15), %rax
movq %rax, 0x20(%rsp)
movq (%r12), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0x1774e9
xorl %r14d, %r14d
movl $0x2, %ebp
movq 0x20(%rax), %rcx
movq %r14, %rax
shlq $0x4, %rax
movl (%rcx,%rax), %eax
cmpq $0x1, %rax
ja 0x17781d
leaq (%r14,%r14), %rbx
movl 0x8(%rcx,%rbx,8), %ecx
cmpl $0x2, %ecx
jae 0x17781d
cmpl %ecx, %eax
jne 0x1773db
orq %rax, %rcx
jne 0x1773f5
leaq 0x1(,%r14,2), %rcx
testq %rax, %rax
cmoveq %rcx, %rbx
movq 0x10(%r15), %rdi
cmpq $0x0, (%rdi,%r14,8)
je 0x17740f
incq %r14
movq (%r12), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r14
jb 0x1773a9
jmp 0x1774e9
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x17743e
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %rbp, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
callq 0x2370
movabsq $0x100000000, %r11      # imm = 0x100000000
movq %rax, 0x10(%r15)
movq %rbx, %rax
shlq $0x20, %rax
xorq %r11, %rax
orq %rbx, %rax
movq (%r15), %rcx
movq 0x10(%r15), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movq %rax, (%rdx,%rcx,8)
movq $0x1, (%rdx,%r14,8)
movq (%r12), %rcx
movq 0x20(%rcx), %rax
movq (%rax,%rbx,8), %rax
testl %eax, %eax
je 0x1773f5
movq 0x8(%rcx), %rcx
shrq $0x1b, %rax
andq $-0x20, %rax
movl (%rcx,%rax), %ebx
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x1774b7
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %rbp, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movabsq $0x100000000, %r11      # imm = 0x100000000
movq %rax, 0x10(%r15)
xorq $0x1, %rbx
leaq 0x1(%r11), %rax
imulq %rbx, %rax
xorq %r11, %rax
movq (%r15), %rcx
movq 0x10(%r15), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movq %rax, (%rdx,%rcx,8)
movl %ebx, %eax
shrl %eax
movq $0x1, (%rdx,%rax,8)
jmp 0x177465
movq %r13, 0x10(%rsp)
movq (%r12), %rax
movl 0x14(%rax), %ecx
andq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
je 0x1776c9
xorl %r14d, %r14d
leaq 0x1(%r11), %rbp
movq 0x10(%r15), %rdi
cmpq $0x0, (%rdi,%r14,8)
jne 0x1776b0
movq 0x20(%rax), %rcx
movq %r14, %rdx
shlq $0x4, %rdx
cmpl $0x1, (%rcx,%rdx)
jne 0x17783c
movl 0x8(%rcx,%rdx), %edx
cmpl $0x1, %edx
jne 0x17783c
leaq (%r14,%r14), %rsi
movq %rsi, %r8
xorl %edx, %edx
movq (%rcx,%r8,8), %r8
testl %r8d, %r8d
je 0x177596
testq %rdx, %rdx
je 0x17756e
movsd 0x18(%rdx), %xmm0
movq 0x8(%rax), %r9
movq %r8, %r10
shrq $0x1b, %r10
andq $-0x20, %r10
ucomisd 0x18(%r9,%r10), %xmm0
jbe 0x17757d
movq %r8, %rdx
shrq $0x1b, %rdx
andq $-0x20, %rdx
addq 0x8(%rax), %rdx
movq 0x8(%rax), %r9
shrq $0x1b, %r8
andq $-0x20, %r8
movl (%r9,%r8), %r8d
xorq $0x1, %r8
cmpq %r8, %rsi
jne 0x177543
movl (%rdx), %ebx
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x1775cc
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
callq 0x2370
movabsq $0x100000000, %r11      # imm = 0x100000000
movq %rax, 0x10(%r15)
xorq $0x1, %rbx
movq %rbx, %rax
imulq %rbp, %rax
xorq %r11, %rax
movq (%r15), %rcx
movq 0x10(%r15), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movq %rax, (%rdx,%rcx,8)
movl %ebx, %eax
shrl %eax
movq $0x1, (%rdx,%rax,8)
movq (%r12), %rax
movq 0x8(%rax), %rcx
movq 0x20(%rax), %rax
movl 0x4(%rax,%rbx,8), %eax
shlq $0x5, %rax
movl (%rcx,%rax), %r13d
movq 0x10(%r15), %rdi
movl %r13d, %ebx
shrl %ebx
cmpq $0x0, (%rdi,%rbx,8)
jne 0x1776b0
xorq $0x1, %r13
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x17765c
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
callq 0x2370
movabsq $0x100000000, %r11      # imm = 0x100000000
movq %rax, 0x10(%r15)
movq %r13, %rax
imulq %rbp, %rax
xorq %r11, %rax
movq (%r15), %rcx
movq 0x10(%r15), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r15)
movq %rax, (%rdx,%rcx,8)
movq $0x1, (%rdx,%rbx,8)
movq (%r12), %rax
movq 0x8(%rax), %rcx
movq 0x20(%rax), %rax
movl 0x4(%rax,%r13,8), %eax
shlq $0x5, %rax
movl (%rcx,%rax), %ebx
movq %rbx, %r13
xorq $0x1, %r13
movq 0x10(%r15), %rdi
shrl %ebx
cmpq $0x0, (%rdi,%rbx,8)
je 0x177628
incq %r14
movq (%r12), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r14
jb 0x177509
movq (%r15), %rdx
movq 0x20(%rsp), %r12
subq %r12, %rdx
cmpq %rcx, %rdx
jne 0x17787a
movl 0xc(%rsp), %ecx
movl %ecx, %edx
testl %ecx, %ecx
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdi
je 0x177749
movq %rdx, %r10
negq %r10
movl $0x1, %r11d
xorl %r8d, %r8d
xorl %ecx, %ecx
movq %r10, %r9
addq %r11, %r9
je 0x177711
cmpl $-0x1, (%rdi,%r11,4)
jne 0x177737
cmpq %r8, %r11
jbe 0x17772a
shlq $0x20, %r8
orq %r11, %r8
movq 0x10(%r15), %r9
movq %r8, (%r9,%rcx,8)
incq %rcx
jmp 0x177733
testq %r9, %r9
jne 0x17785b
leaq 0x1(%r11), %r8
leaq (%r10,%r11), %r9
incq %r9
incq %r11
cmpq $0x1, %r9
jne 0x177702
jmp 0x17774b
xorl %ecx, %ecx
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %rcx
movabsq $0x100000000, %r10      # imm = 0x100000000
jne 0x177899
cmpq %r12, %rcx
jne 0x177899
movq (%r15), %rcx
cmpq %rcx, %r12
jae 0x1777fb
movq 0x10(%r15), %rbx
xorl %eax, %eax
movq (%rbx,%r12,8), %r9
movq %r9, %r8
shrq $0x21, %r8
movq (%rbx,%r8,8), %r8
movl %r8d, %r14d
shrq $0x20, %r8
testq %r10, %r9
jne 0x1777ce
cmpl %r8d, %r14d
jbe 0x1777e7
subq %r8, %r14
leaq (%rdi,%r8,4), %r8
leaq (%rsi,%rax,4), %r10
xorl %r9d, %r9d
movl (%r8,%r9,4), %r11d
movl %r11d, (%r10,%r9,4)
incq %r9
cmpq %r9, %r14
jne 0x1777af
addq %r9, %rax
movabsq $0x100000000, %r10      # imm = 0x100000000
jmp 0x1777e7
cmpl %r8d, %r14d
jbe 0x1777e7
movl -0x4(%rdi,%r14,4), %r9d
decq %r14
movl %r9d, (%rsi,%rax,4)
incq %rax
cmpq %r8, %r14
jg 0x1777d3
movl $0xffffffff, (%rsi,%rax,4) # imm = 0xFFFFFFFF
incq %rax
incq %r12
cmpq %rcx, %r12
jne 0x177781
jmp 0x1777fd
xorl %eax, %eax
cmpq %rdx, %rax
jne 0x1778b8
shlq $0x2, %rdx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2160
leaq 0xba2f9(%rip), %rdi     # 0x231b1d
leaq 0xb9db0(%rip), %rsi     # 0x2315db
leaq 0xba302(%rip), %rcx     # 0x231b34
movl $0x101f, %edx           # imm = 0x101F
callq 0x2110
leaq 0xba348(%rip), %rdi     # 0x231b8b
leaq 0xb9d91(%rip), %rsi     # 0x2315db
leaq 0xba2e3(%rip), %rcx     # 0x231b34
movl $0x1046, %edx           # imm = 0x1046
callq 0x2110
leaq 0xba35c(%rip), %rdi     # 0x231bbe
leaq 0xb9d72(%rip), %rsi     # 0x2315db
leaq 0xba2c4(%rip), %rcx     # 0x231b34
movl $0x1073, %edx           # imm = 0x1073
callq 0x2110
leaq 0xba321(%rip), %rdi     # 0x231ba2
leaq 0xb9d53(%rip), %rsi     # 0x2315db
leaq 0xba2a5(%rip), %rcx     # 0x231b34
movl $0x106d, %edx           # imm = 0x106D
callq 0x2110
leaq 0xba329(%rip), %rdi     # 0x231bc9
leaq 0xb9d34(%rip), %rsi     # 0x2315db
leaq 0xba286(%rip), %rcx     # 0x231b34
movl $0x1078, %edx           # imm = 0x1078
callq 0x2110
leaq 0xba327(%rip), %rdi     # 0x231be6
leaq 0xb9d15(%rip), %rsi     # 0x2315db
leaq 0xba267(%rip), %rcx     # 0x231b34
movl $0x108e, %edx           # imm = 0x108E
callq 0x2110