// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movsd %xmm1, 0x38(%rsp)
movsd %xmm0, 0x40(%rsp)
movq %r9, %r12
movq %r8, %rbx
movq %rcx, %r13
movl %edx, 0x1c(%rsp)
movq %rsi, %r14
movq %rdi, 0x30(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0xa0(%rsp), %rbp
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
movq (%rcx), %rdi
callq 0x174c02
movl $0x1, %r15d
movl $0x1, %edi
movl $0x28, %esi
callq 0x2120
movq %r13, 0x10(%rsp)
movq %rax, (%r13)
movq %rbp, 0x20(%rsp)
movq 0x18(%rbp), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
shlq $0x3, %rdx
movq %r12, %rdi
movl $0xff, %esi
callq 0x20e0
cmpl $0x0, 0x1c(%rsp)
je 0x177b69
movl 0x1c(%rsp), %ecx
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %rcx, 0x28(%rsp)
cmpq %rcx, %r15
je 0x1779a0
cmpl $-0x1, (%r14,%r15,4)
jne 0x177a5f
cmpq %r13, %r15
jbe 0x177a52
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x174c30
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rcx
xorl %eax, %eax
movq %r13, %rdx
movl (%r14,%rdx,4), %esi
movl (%rcx,%rsi,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %rax
incq %rdx
cmpq %rdx, %r15
jne 0x1779cc
shrq %rax
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
leaq (,%rbp,2), %rdx
leaq 0x1(,%rbp,2), %rsi
xorl %edi, %edi
movl (%r14,%r13,4), %r8d
movl (%rcx,%r8,8), %r9d
cmpq %rax, %rdi
ja 0x177a22
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 0x177a40
movq (%r12,%r8,8), %r9
shlq $0x20, %r9
orq %rsi, %r9
movq %r9, (%r12,%r8,8)
incq %r13
cmpq %r13, %r15
jne 0x177a06
incq %rbp
movq 0x28(%rsp), %rcx
jmp 0x177a5b
cmpq %rcx, %r15
jne 0x177d2f
leaq 0x1(%r15), %r13
leaq 0x1(%r15), %rax
cmpq %rcx, %r15
movq %rax, %r15
jne 0x177990
movl $0x1, %r15d
xorl %edx, %edx
xorl %r13d, %r13d
cmpq %rcx, %r15
je 0x177a8a
cmpl $-0x1, (%r14,%r15,4)
jne 0x177b59
cmpq %r13, %r15
jbe 0x177b4c
movq %rdx, 0x20(%rsp)
leaq (%rdx,%rdx), %rbp
xorl %r8d, %r8d
movq %r13, %rax
movl (%r14,%rax,4), %ecx
movq (%r12,%rcx,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
cmpq %rsi, %rbp
je 0x177abd
movl %edx, %edx
cmpq %rdx, %rbp
jne 0x177ac4
movl %ecx, (%rbx,%r8,4)
incq %r8
incq %rax
cmpq %rax, %r15
jne 0x177aa2
leaq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl %ebp, %edx
movq %rbx, %rcx
movq %r12, %r9
callq 0x176c8f
orq $0x1, %rbp
xorl %r8d, %r8d
movl (%r14,%r13,4), %eax
movq (%r12,%rax,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
cmpq %rdx, %rbp
je 0x177b0e
movl %ecx, %ecx
cmpq %rcx, %rbp
jne 0x177b15
movl %eax, (%rbx,%r8,4)
incq %r8
incq %r13
cmpq %r13, %r15
jne 0x177af3
leaq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl %ebp, %edx
movq %rbx, %rcx
movq %r12, %r9
callq 0x176c8f
movq 0x20(%rsp), %rdx
incq %rdx
movq 0x28(%rsp), %rcx
jmp 0x177b55
cmpq %rcx, %r15
jne 0x177d4e
leaq 0x1(%r15), %r13
leaq 0x1(%r15), %rax
cmpq %rcx, %r15
movq %rax, %r15
jne 0x177a7a
movq 0x10(%rsp), %r12
movq (%r12), %rdi
callq 0x174e07
movq (%r12), %rcx
testl $0x7fffffff, 0x4(%rcx)  # imm = 0x7FFFFFFF
je 0x177c27
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %eax, %eax
xorl %edi, %edi
xorl %ebp, %ebp
movq 0x8(%rcx), %rdx
movq 0x20(%rcx), %rcx
movl 0x4(%rdx,%r15), %esi
testl $0xfffffffe, (%rcx,%rsi,8) # imm = 0xFFFFFFFE
jne 0x177bb7
movl (%rdx,%r15), %edx
testl $0xfffffffe, (%rcx,%rdx,8) # imm = 0xFFFFFFFE
je 0x177c08
cmpq %rax, %r13
jne 0x177be3
leaq (%rax,%rax), %r12
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r12
movq %r12, %rsi
shlq $0x4, %rsi
callq 0x2370
movq %rax, %rdi
movq %r12, %rax
movq 0x10(%rsp), %r12
leaq 0x1(%r13), %rcx
shlq $0x4, %r13
movq (%r12), %rdx
movq 0x8(%rdx), %rdx
movsd 0x18(%rdx,%r15), %xmm0
movsd %xmm0, 0x8(%rdi,%r13)
movq %rbp, (%rdi,%r13)
movq %rcx, %r13
incq %rbp
movq (%r12), %rcx
movl 0x4(%rcx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
addq $0x20, %r15
cmpq %rdx, %rbp
jb 0x177b94
jmp 0x177c2c
xorl %edi, %edi
xorl %r13d, %r13d
leaq -0xfc8(%rip), %rcx      # 0x176c6b
movl $0x10, %edx
movq %rdi, %rbp
movq %r13, %rsi
callq 0x23f0
movq 0x48(%rsp), %r15
movq %r15, %rax
movsd 0x38(%rsp), %xmm2
movapd %xmm2, %xmm4
subq $0x1, %rax
movsd 0x40(%rsp), %xmm3
je 0x177c8e
movapd %xmm2, %xmm4
subsd %xmm3, %xmm4
movq %rax, %xmm0
punpckldq 0xad5ca(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xad5d2(%rip), %xmm0    # 0x225250
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm4
jb 0x177cd2
movsd %xmm4, 0x20(%rsp)
movapd %xmm2, %xmm0
minsd %xmm3, %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x17709d
movsd 0x20(%rsp), %xmm4
movsd 0x38(%rsp), %xmm2
movq %rax, %r13
movsd 0x10(%rsp), %xmm3
addsd %xmm4, %xmm3
decq %r15
jne 0x177c96
movsd 0xb1e96(%rip), %xmm0    # 0x229b70
movl $0x1, %ecx
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x17709d
movq (%r12), %rdi
callq 0x174e07
leaq 0x50(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movl 0x1c(%rsp), %ecx
movq %rbx, %r8
callq 0x1772da
movq 0x10(%r13), %rdi
callq 0x2220
movq %rbp, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220
leaq 0xb9ebb(%rip), %rdi     # 0x231bf1
leaq 0xb989e(%rip), %rsi     # 0x2315db
leaq 0xb9eb5(%rip), %rcx     # 0x231bf9
movl $0x10af, %edx           # imm = 0x10AF
callq 0x2110
leaq 0xb9e9c(%rip), %rdi     # 0x231bf1
leaq 0xb987f(%rip), %rsi     # 0x2315db
leaq 0xb9e96(%rip), %rcx     # 0x231bf9
movl $0x10c4, %edx           # imm = 0x10C4
callq 0x2110