// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movsd %xmm1, -0x40(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xb8(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x23f890
callq 0x23f870
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rdi
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x2100
movq $0x0, -0x58(%rbp)
movq $0x1, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x50(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpq %rcx, %rax
ja 0x2481bb
movq -0x50(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpq %rcx, %rax
je 0x247fed
movq -0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x2481a8
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jbe 0x24816d
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rax
movl %eax, %esi
xorl %edx, %edx
callq 0x23f8e0
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x248060
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq -0x68(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x24801f
movq -0x60(%rbp), %rax
shrq %rax
movq %rax, -0x70(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x24815f
movq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq -0x68(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
ja 0x248105
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x68(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rdx
shlq %rdx
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x68(%rbp), %rsi
movl (%rcx,%rsi,4), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x80(%rbp), %rax
cmpq -0x70(%rbp), %rax
jb 0x24814c
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x68(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rdx
shlq %rdx
addq $0x1, %rdx
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x68(%rbp), %rsi
movl (%rcx,%rsi,4), %ecx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x24814e
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x24807b
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x24819c
movq -0x50(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpq %rcx, %rax
jne 0x24817b
jmp 0x24819a
leaq 0x14690f(%rip), %rdi    # 0x38ea91
leaq 0x1462f2(%rip), %rsi    # 0x38e47b
movl $0x10af, %edx           # imm = 0x10AF
leaq 0x146904(%rip), %rcx    # 0x38ea99
callq 0x2130
jmp 0x24819c
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2481aa
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x247fbf
movq $0x0, -0x58(%rbp)
movq $0x1, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x50(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpq %rcx, %rax
ja 0x248427
movq -0x50(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpq %rcx, %rax
je 0x248201
movq -0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x248414
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jbe 0x2483d9
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
shlq %rax
movq %rax, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x2482b8
movq -0x88(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0x68(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
je 0x24827e
movq -0x88(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0x68(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jne 0x2482a5
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movq -0x90(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x90(%rbp)
movl %edx, (%rax,%rcx,4)
jmp 0x2482a7
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x248230
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x88(%rbp), %rax
movl %eax, %edx
movq -0x28(%rbp), %rcx
movq -0x90(%rbp), %rax
movl %eax, %r8d
movq -0x30(%rbp), %r9
leaq -0xb0(%rbp), %rax
movq %rax, (%rsp)
callq 0x2467a0
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
shlq %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x248398
movq -0x88(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0x68(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
je 0x24835e
movq -0x88(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0x68(%rbp), %rsi
movl (%rdx,%rsi,4), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
cmpq %rcx, %rax
jne 0x248385
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movq -0x90(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x90(%rbp)
movl %edx, (%rax,%rcx,4)
jmp 0x248387
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x248310
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x88(%rbp), %rax
movl %eax, %edx
movq -0x28(%rbp), %rcx
movq -0x90(%rbp), %rax
movl %eax, %r8d
movq -0x30(%rbp), %r9
leaq -0xb0(%rbp), %rax
movq %rax, (%rsp)
callq 0x2467a0
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x248408
movq -0x50(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpq %rcx, %rax
jne 0x2483e7
jmp 0x248406
leaq 0x1466a3(%rip), %rdi    # 0x38ea91
leaq 0x146086(%rip), %rsi    # 0x38e47b
movl $0x10c4, %edx           # imm = 0x10C4
leaq 0x146698(%rip), %rcx    # 0x38ea99
callq 0x2130
jmp 0x248408
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x248416
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2481d3
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x23fcd0
movq $0x0, -0xc8(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movl 0x4(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x24859c
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movq 0x8(%rcx), %rcx
movq -0x50(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
movq (%rax,%rcx,8), %rax
cmpl $0x1, %eax
ja 0x2484c3
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movq 0x8(%rcx), %rcx
movq -0x50(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rax
cmpl $0x1, %eax
ja 0x2484c3
jmp 0x24858b
jmp 0x2484c5
movq -0xc8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jne 0x24852c
cmpq $0x0, -0xc0(%rbp)
je 0x2484f2
movq -0xc0(%rbp), %rax
shlq %rax
movq %rax, -0xe0(%rbp)
jmp 0x248500
movl $0x2, %eax
movq %rax, -0xe0(%rbp)
jmp 0x248500
movq -0xe0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
shlq $0x4, %rsi
callq 0x2360
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq -0xc8(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0xc8(%rbp)
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq -0x50(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movsd 0x18(%rax), %xmm0
movq -0x98(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x50(%rbp), %rcx
movq -0x98(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x248446
movq -0xb8(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movl $0x10, %edx
leaq -0x1e76(%rip), %rcx     # 0x246740
callq 0x23e0
cmpq $0x1, 0x18(%rbp)
jne 0x2485d1
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
jmp 0x248619
movsd -0x40(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
subsd %xmm1, %xmm0
movq 0x18(%rbp), %rax
decq %rax
movq %rax, %xmm1
movaps 0x139a1e(%rip), %xmm2   # 0x382010
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x139a22(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd -0xe8(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq 0x18(%rbp), %rax
jae 0x2486b4
movsd -0xd8(%rbp), %xmm0
ucomisd -0x40(%rbp), %xmm0
jbe 0x248664
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0x20(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xc8(%rbp), %rdx
movsd -0xd8(%rbp), %xmm0
xorl %eax, %eax
movl %eax, %ecx
callq 0x246de0
movq %rax, -0xc8(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movsd -0xd0(%rbp), %xmm0
addsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
jmp 0x24863e
movq -0x20(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xc8(%rbp), %rdx
movsd 0x13e17a(%rip), %xmm0   # 0x386848
movl $0x1, %ecx
callq 0x246de0
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x23fcd0
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl -0x14(%rbp), %ecx
movq -0x28(%rbp), %r8
leaq -0xb0(%rbp), %rsi
callq 0x247410
movq -0xa0(%rbp), %rdi
callq 0x2210
movq -0xb8(%rbp), %rdi
callq 0x2210
addq $0xf0, %rsp
popq %rbp
retq