// File: /workspace/github_cpp_makefile_star2_O2/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, 0x30(%rsp)
movsd %xmm0, 0x48(%rsp)
movq %r9, %r12
movq %r8, 0x20(%rsp)
movq %rcx, %r15
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, 0x40(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0xb0(%rsp), %rbp
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
movq (%rcx), %rdi
callq 0x13782f
callq 0x137824
movq %r15, 0x10(%rsp)
movq %rax, (%r15)
movq 0x18(%rbp), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
shlq $0x3, %rdx
movq %r12, %rdi
movl $0xff, %esi
callq 0x20e0
movl %ebx, 0x3c(%rsp)
movl %ebx, %edx
movq %rdx, %rcx
negq %rcx
pushq $0x1
popq %rax
xorl %r15d, %r15d
xorl %esi, %esi
movq %rcx, 0x18(%rsp)
movq %rdx, 0x28(%rsp)
movq %r15, %r13
incq %rax
movq %rax, %r15
leaq (%rcx,%r15), %rax
cmpq $0x2, %rax
je 0x13b93b
cmpq $0x1, %rax
je 0x13b872
cmpl $-0x1, -0x4(%r14,%r15,4)
je 0x13b87c
incq %r15
jmp 0x13b851
movq %r15, %rax
cmpq %rdx, %r13
jae 0x13b848
jmp 0x13b889
leaq -0x1(%r15), %rax
cmpq %r13, %rax
jbe 0x13bc18
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq %rsi, %rbx
xorl %edx, %edx
callq 0x13785d
leaq -0x1(%r15), %rax
movq %r13, %rdx
xorl %ecx, %ecx
cmpq %rax, %rdx
jae 0x13b8c6
movq 0x18(%rbp), %rsi
movq 0x20(%rsi), %rsi
movl (%r14,%rdx,4), %edi
movl (%rsi,%rdi,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %rcx
incq %rdx
jmp 0x13b8a4
shrq %rcx
movq %rbx, %rsi
leaq (%rbx,%rbx), %rdx
leaq 0x1(,%rbx,2), %r11
xorl %edi, %edi
cmpq %rax, %r13
jae 0x13b926
movq 0x18(%rbp), %r8
movq 0x20(%r8), %r9
movl (%r14,%r13,4), %r8d
movl (%r9,%r8,8), %r9d
cmpq %rcx, %rdi
ja 0x13b903
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 %rcx, %rdi
jb 0x13b921
movq (%r12,%r8,8), %r9
shlq $0x20, %r9
orq %r11, %r9
movq %r9, (%r12,%r8,8)
incq %r13
jmp 0x13b8da
incq %rsi
movq %r15, %rax
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %rdx
jmp 0x13b848
pushq $0x1
popq %rax
xorl %r13d, %r13d
xorl %edi, %edi
movq %r13, %r15
incq %rax
movq %rax, %r13
leaq (%rcx,%r13), %rax
cmpq $0x2, %rax
je 0x13ba66
cmpq $0x1, %rax
je 0x13b96d
cmpl $-0x1, -0x4(%r14,%r13,4)
je 0x13b977
incq %r13
jmp 0x13b94c
movq %r13, %rax
cmpq %rdx, %r15
jae 0x13b943
jmp 0x13b984
leaq -0x1(%r13), %rax
cmpq %r15, %rax
jbe 0x13bc37
leaq (%rdi,%rdi), %rbp
leaq -0x1(%r13), %rbx
movq %r15, %rax
xorl %r8d, %r8d
cmpq %rbx, %rax
jae 0x13b9c8
movl (%r14,%rax,4), %ecx
movq (%r12,%rcx,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
cmpq %rdx, %rbp
setne %dl
cmpq %rsi, %rbp
setne %sil
testb %sil, %dl
jne 0x13b9c3
movq 0x20(%rsp), %rdx
movl %ecx, (%rdx,%r8,4)
incq %r8
incq %rax
jmp 0x13b992
movq %rdi, 0x58(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl %ebp, %edx
movq 0x20(%rsp), %rcx
movq %r12, %r9
callq 0x13ac28
orq $0x1, %rbp
xorl %r8d, %r8d
cmpq %rbx, %r15
jae 0x13ba2a
movl (%r14,%r15,4), %eax
movq (%r12,%rax,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
cmpq %rcx, %rbp
setne %cl
cmpq %rdx, %rbp
setne %dl
testb %dl, %cl
jne 0x13ba25
movq 0x20(%rsp), %rcx
movl %eax, (%rcx,%r8,4)
incq %r8
incq %r15
jmp 0x13b9f6
leaq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl %ebp, %edx
movq 0x20(%rsp), %rcx
movq %r12, %r9
callq 0x13ac28
movq 0x58(%rsp), %rdi
incq %rdi
movq %r13, %rax
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rcx
jmp 0x13b943
movq 0x10(%rsp), %rbx
movq (%rbx), %rdi
callq 0x137a11
movq (%rbx), %rax
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %ecx, %ecx
xorl %r13d, %r13d
movl 0x4(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rbp
jae 0x13bb15
movq 0x8(%rax), %rsi
movq 0x20(%rax), %rdx
movl 0x4(%rsi,%r15), %edi
testl $0xfffffffe, (%rdx,%rdi,8) # imm = 0xFFFFFFFE
jne 0x13bab8
movl (%rsi,%r15), %esi
testl $0xfffffffe, (%rdx,%rsi,8) # imm = 0xFFFFFFFE
je 0x13bb09
cmpq %rcx, %r13
jne 0x13bae8
leaq (%rcx,%rcx), %rbx
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rbx
movq %rbx, %rsi
shlq $0x4, %rsi
movq %r12, %rdi
callq 0x2370
movq %rax, %r12
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rbx, %rcx
leaq 0x1(%r13), %rdx
shlq $0x4, %r13
movq 0x8(%rax), %rsi
movsd 0x18(%rsi,%r15), %xmm0
movsd %xmm0, 0x8(%r12,%r13)
movq %rbp, (%r12,%r13)
movq %rdx, %r13
incq %rbp
addq $0x20, %r15
jmp 0x13ba83
leaq -0xf18(%rip), %rcx      # 0x13ac04
pushq $0x10
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x23f0
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x50(%rsp), %r15
cmpq $0x1, %r15
movsd 0x48(%rsp), %xmm2
je 0x13bb80
leaq -0x1(%r15), %rax
movsd 0x30(%rsp), %xmm3
subsd %xmm2, %xmm3
movq %rax, %xmm0
punpckldq 0xa770e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xa7716(%rip), %xmm0    # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movsd %xmm3, 0x18(%rsp)
movq 0x10(%rsp), %rbx
subq $0x1, %r15
jb 0x13bbbc
movsd 0x30(%rsp), %xmm0
minsd %xmm2, %xmm0
movsd %xmm0, 0x28(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x13afce
movq %rax, %r13
movsd 0x28(%rsp), %xmm2
addsd 0x18(%rsp), %xmm2
jmp 0x13bb85
movsd 0xac0a4(%rip), %xmm0    # 0x1e7c68
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x13afce
movq (%rbx), %rdi
callq 0x137a11
leaq 0x60(%rsp), %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movl 0x3c(%rsp), %ecx
movq 0x20(%rsp), %r8
callq 0x13b211
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 0xb40e2(%rip), %rdi     # 0x1efd01
leaq 0xb3ac5(%rip), %rsi     # 0x1ef6eb
leaq 0xb40dc(%rip), %rcx     # 0x1efd09
movl $0x10af, %edx           # imm = 0x10AF
callq 0x2110
leaq 0xb40c3(%rip), %rdi     # 0x1efd01
leaq 0xb3aa6(%rip), %rsi     # 0x1ef6eb
leaq 0xb40bd(%rip), %rcx     # 0x1efd09
movl $0x10c4, %edx           # imm = 0x10C4
callq 0x2110