// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/horder.cpp
// Function: layout_mc_clus_t(mc_match_t const*, unsigned int*, unsigned int, scg_t*, unsigned int*, unsigned long*, ma_ug_t*, double, double, unsigned long)
void layout_mc_clus_t(const mc_match_t *ma, uint32_t *a, uint32_t an, scg_t *sg, uint32_t *buf, uint64_t *idx, ma_ug_t* ug,
double min_cut, double max_cut, uint64_t cut_round)
{
    uint64_t i, k, l, len, z, cutoff, s, e, v, vn; mc_edge_t *sp;
    asg64_v srt; kv_init(srt); kvec_t(mc_edge_t) sm; kv_init(sm);
    osg_destroy(sg->g); sg->g = osg_init();
    memset(idx, -1, sizeof((*idx))*ug->g->n_seq);

    // for (k = 0; k < an; k++) {
    //     fprintf(stderr, "[M::%s] k::%lu, a[k]::%u, an::%u\n", __func__, k, a[k], an);
    // }
    for (l = 0, k = 1, i = 0; k <= an; k++) {
       if(k == an || a[k] == (uint32_t)-1) {
            // fprintf(stderr, "[M::%s] l::%lu, k::%lu\n", __func__, l, k);
            if(k > l) {
                osg_seq_set(sg->g, i, 0);
                for (z = l, len = 0; z < k; z++) len += ug->g->seq[a[z]].len;
                cutoff = len >> 1;
                for (z = l, len = 0; z < k; z++) {
                    s = len; len += ug->g->seq[a[z]].len; e = len;
                    if(s <= cutoff) {
                        idx[a[z]] <<= 32; idx[a[z]] |= (i<<1);
                    } 
                    if(e >= cutoff) {
                        idx[a[z]] <<= 32; idx[a[z]] |= ((i<<1)+1);
                    }
                }
                i++;
            } else {
                assert(k == an);
            }
            l = k+1;
       }
    }

    for (l = 0, k = 1, i = 0; k <= an; k++) {
       if(k == an || a[k] == (uint32_t)-1) {
            if(k > l) {
                for (z = l, v = (i<<1), vn = 0; z < k; z++) {
                    if((v == (idx[a[z]]>>32)) || v == ((uint32_t)idx[a[z]])) buf[vn++] = a[z];
                }
                cal_chain_arch(sg, ma, v, buf, vn, idx, &srt);

                for (z = l, v = (i<<1)+1, vn = 0; z < k; z++) {
                    if((v == (idx[a[z]]>>32)) || v == ((uint32_t)idx[a[z]])) buf[vn++] = a[z];
                }
                cal_chain_arch(sg, ma, v, buf, vn, idx, &srt);

                i++;
            } else {
                assert(k == an);
            }
            l = k + 1;
       }
    }
    osg_cleanup(sg->g);

    sm.n = 0;
    // kv_resize(mc_edge_t, sm, sg->g->n_arc);
    for (k = 0; k < sg->g->n_arc; k++) {
        if((asg_arc_n(sg->g, sg->g->arc[k].u)<=1) && (asg_arc_n(sg->g, sg->g->arc[k].v)<=1)) continue;
        kv_pushp(mc_edge_t, sm, &sp);
        sp->w = sg->g->arc[k].nw; sp->x = k;
    }
    // fprintf(stderr, "sm.n::%lu\n", (uint64_t)sm.n);
    qsort(sm.a, sm.n, sizeof(mc_edge_t), cmp_mc_edge_t_w);



    double step = (cut_round==1?max_cut:((max_cut-min_cut)/(cut_round-1)));
    double drop = min_cut;

    for (i = 0; i < cut_round; i++, drop += step) {
        if(drop > max_cut) drop = max_cut;
        sm.n = mc_clus_cut(sg, sm.a, sm.n, drop, 0);    
    }
    mc_clus_cut(sg, sm.a, sm.n, 1.1, 1);
    osg_cleanup(sg->g);

    gen_mc_clus_backbone_layout(sg, &srt, a, an, buf);


    kv_destroy(srt); kv_destroy(sm);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movsd %xmm1, 0x38(%rsp)
movsd %xmm0, 0x60(%rsp)
movq %r9, %r12
movq %r8, 0x18(%rsp)
movq %rcx, %r15
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, 0x20(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0xb0(%rsp), %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq (%rcx), %rdi
callq 0x17b748
movl $0x1, %r13d
movl $0x1, %edi
movl $0x28, %esi
callq 0x2120
movq %r15, 0x8(%rsp)
movq %rax, (%r15)
movq 0x18(%rbx), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
shlq $0x3, %rdx
movq %r12, %rdi
movl $0xff, %esi
callq 0x20e0
movl %ebp, 0x14(%rsp)
testl %ebp, %ebp
je 0x17e760
movl 0x14(%rsp), %eax
leaq 0x1(%rax), %rcx
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %rax, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
cmpq %rax, %r13
jne 0x17e504
cmpq %rax, %r15
jb 0x17e51c
jmp 0x17e5cc
cmpl $-0x1, (%r14,%r13,4)
je 0x17e513
incq %r13
jmp 0x17e5d2
cmpq %r15, %r13
jbe 0x17e928
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x17b776
cmpq %r13, %r15
jae 0x17e5bf
movq 0x18(%rbx), %rax
movq 0x20(%rax), %rcx
xorl %eax, %eax
movq %r15, %rdx
movl (%r14,%rdx,4), %esi
movl (%rcx,%rsi,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %rax
incq %rdx
cmpq %rdx, %r13
jne 0x17e543
cmpq %r13, %r15
jae 0x17e5bf
shrq %rax
movq 0x18(%rbx), %rcx
movq 0x20(%rcx), %rcx
leaq (,%rbp,2), %rdx
leaq 0x1(,%rbp,2), %rsi
xorl %edi, %edi
movl (%r14,%r15,4), %r8d
movl (%rcx,%r8,8), %r9d
cmpq %rax, %rdi
ja 0x17e599
movq (%r12,%r8,8), %r10
shlq $0x20, %r10
orq %rdx, %r10
movq %r10, (%r12,%r8,8)
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
addq %r9, %rdi
cmpq %rax, %rdi
jb 0x17e5b7
movq (%r12,%r8,8), %r9
shlq $0x20, %r9
orq %rsi, %r9
movq %r9, (%r12,%r8,8)
incq %r15
cmpq %r15, %r13
jne 0x17e57d
incq %rbp
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
incq %r13
movq %r13, %r15
cmpq %rcx, %r13
jne 0x17e4f5
cmpl $0x0, 0x14(%rsp)
je 0x17e760
movl $0x1, %r15d
xorl %edx, %edx
leaq 0x40(%rsp), %r10
xorl %ebx, %ebx
cmpq %rax, %r15
jne 0x17e604
cmpq %rax, %rbx
jb 0x17e61c
jmp 0x17e751
cmpl $-0x1, (%r14,%r15,4)
je 0x17e613
incq %r15
jmp 0x17e757
cmpq %rbx, %r15
jbe 0x17e947
leaq (%rdx,%rdx), %rbp
cmpq %r15, %rbx
movq %rdx, 0x70(%rsp)
jae 0x17e6d4
xorl %r8d, %r8d
movq %rbx, %rax
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl (%r14,%rax,4), %ecx
movq (%r12,%rcx,8), %rdx
movq %rdx, %r9
shrq $0x20, %r9
cmpq %r9, %rbp
je 0x17e659
movl %edx, %edx
cmpq %rdx, %rbp
jne 0x17e660
movl %ecx, (%rdi,%r8,4)
incq %r8
incq %rax
cmpq %rax, %r15
jne 0x17e63e
movq %r10, (%rsp)
movq %rdi, %rcx
movq 0x8(%rsp), %rdi
movl %ebp, %edx
movq %r12, %r9
callq 0x17d811
orq $0x1, %rbp
xorl %r13d, %r13d
cmpq %r15, %rbx
jae 0x17e70f
xorl %r13d, %r13d
movq 0x18(%rsp), %rsi
leaq 0x40(%rsp), %rdi
movl (%r14,%rbx,4), %eax
movq (%r12,%rax,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
cmpq %rdx, %rbp
je 0x17e6b6
movl %ecx, %ecx
cmpq %rcx, %rbp
jne 0x17e6bd
movl %eax, (%rsi,%r13,4)
incq %r13
incq %rbx
cmpq %rbx, %r15
jne 0x17e69b
movq %rsi, %rcx
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %rbx
jmp 0x17e723
movq %r10, (%rsp)
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
movl %ebp, %edx
movq 0x18(%rsp), %rcx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x17d811
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rcx
leaq 0x40(%rsp), %rdi
orq $0x1, %rbp
jmp 0x17e723
movq 0x18(%rsp), %rcx
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %rbx
leaq 0x40(%rsp), %rdi
movq %rdi, (%rsp)
movq %rax, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movl %r13d, %r8d
movq %r12, %r9
callq 0x17d811
leaq 0x40(%rsp), %r10
movq 0x70(%rsp), %rdx
incq %rdx
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
incq %r15
movq %r15, %rbx
cmpq %rcx, %r15
jne 0x17e5f5
movq 0x8(%rsp), %rbx
movq (%rbx), %rdi
callq 0x17b968
movq (%rbx), %rax
testl $0x7fffffff, 0x4(%rax)  # imm = 0x7FFFFFFF
je 0x17e819
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
movq 0x8(%rax), %rcx
movq 0x20(%rax), %rdx
movl 0x4(%rcx,%rbx), %esi
testl $0xfffffffe, (%rdx,%rsi,8) # imm = 0xFFFFFFFE
jne 0x17e7ab
movl (%rcx,%rbx), %esi
testl $0xfffffffe, (%rdx,%rsi,8) # imm = 0xFFFFFFFE
je 0x17e7fe
cmpq %rbp, %r13
jne 0x17e7e2
leaq (,%r13,2), %rbp
testq %r13, %r13
movl $0x2, %eax
cmoveq %rax, %rbp
movq %rbp, %rsi
shlq $0x4, %rsi
movq %r12, %rdi
callq 0x2370
movq %rax, %r12
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rcx
leaq 0x1(%r13), %rdx
shlq $0x4, %r13
movsd 0x18(%rcx,%rbx), %xmm0
movsd %xmm0, 0x8(%r12,%r13)
movq %r15, (%r12,%r13)
movq %rdx, %r13
incq %r15
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq $0x20, %rbx
cmpq %rcx, %r15
jb 0x17e78a
jmp 0x17e81f
xorl %r12d, %r12d
xorl %r13d, %r13d
leaq -0x1039(%rip), %rcx     # 0x17d7ed
movl $0x10, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x23f0
movq 0x68(%rsp), %r15
movq %r15, %rax
movsd 0x38(%rsp), %xmm2
movapd %xmm2, %xmm4
subq $0x1, %rax
movsd 0x60(%rsp), %xmm3
je 0x17e881
movapd %xmm2, %xmm4
subsd %xmm3, %xmm4
movq %rax, %xmm0
punpckldq 0xb2a17(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xb2a1f(%rip), %xmm0    # 0x231290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm4
movq 0x8(%rsp), %rbx
jb 0x17e8ca
movsd %xmm4, 0x28(%rsp)
movapd %xmm2, %xmm0
minsd %xmm3, %xmm0
movsd %xmm0, 0x30(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x17dc3d
movsd 0x28(%rsp), %xmm4
movsd 0x38(%rsp), %xmm2
movq %rax, %r13
movsd 0x30(%rsp), %xmm3
addsd %xmm4, %xmm3
decq %r15
jne 0x17e88e
movsd 0xb7496(%rip), %xmm0    # 0x235d68
movl $0x1, %ecx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x17dc3d
movq (%rbx), %rdi
callq 0x17b968
leaq 0x40(%rsp), %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movl 0x14(%rsp), %ecx
movq 0x18(%rsp), %r8
callq 0x17de60
movq 0x10(%r13), %rdi
callq 0x2220
movq %r12, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
leaq 0xbf512(%rip), %rdi     # 0x23de41
leaq 0xbeef5(%rip), %rsi     # 0x23d82b
leaq 0xbf50c(%rip), %rcx     # 0x23de49
movl $0x10af, %edx           # imm = 0x10AF
callq 0x2110
leaq 0xbf4f3(%rip), %rdi     # 0x23de41
leaq 0xbeed6(%rip), %rsi     # 0x23d82b
leaq 0xbf4ed(%rip), %rcx     # 0x23de49
movl $0x10c4, %edx           # imm = 0x10C4
callq 0x2110