// File: chhylp123[P]hifiasm/horder.cpp
// Function: refine_layout_back(horder_t*, sc_lay_t*, unsigned char*)
void refine_layout_back(horder_t *h, sc_lay_t *sl, uint8_t *vis)
{
    uint32_t i, k, m, v, nv, max_k;
    osg_arc_t *av = NULL;
    lay_t *p = NULL;
    uint8_t *sgv = NULL; MALLOC(sgv, sl->n);
    double *w = NULL; MALLOC(w, sl->n);
    uint32_t *idx = NULL; MALLOC(idx, h->sg.g->n_seq);
    memset(idx, -1, sizeof(uint32_t)*h->sg.g->n_seq);
    kvec_t(uint64_t) p_refine; kv_init(p_refine);

    for (k = 0; k < sl->n; k++)
    {
        p = &(sl->a[k]);
        for (m = 0; m < p->n; m++)
        {
            idx[p->a[m]>>1] = k;
        }
    }

    for (i = 0, p_refine.n = 0; i < h->sg.g->n_seq; i++)
    {
        if(vis[i<<1]) continue;
        if(!asg_arc_n(h->sg.g, i<<1)&&!asg_arc_n(h->sg.g, (i<<1)+1)) continue;
        for (k = 0; k < sl->n; k++) w[k] = 0, sgv[k] = 0;

        v = i<<1;
        nv = asg_arc_n(h->sg.g, v);
        av = asg_arc_a(h->sg.g, v);
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            w[idx[av[k].v>>1]] += av[k].nw;
            sgv[idx[av[k].v>>1]] = 1;
        }
        

        v = (i<<1) + 1;
        nv = asg_arc_n(h->sg.g, v);
        av = asg_arc_a(h->sg.g, v);
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            w[idx[av[k].v>>1]] += av[k].nw;
            sgv[idx[av[k].v>>1]] = 1;
        }

        for (k = 0, max_k = (uint32_t)-1; k < sl->n; k++)
        {
            if(!sgv[k]) continue;
            if(max_k == (uint32_t)-1 || w[max_k] < w[k]) max_k = k;
        }     

        if(max_k != (uint32_t)-1)
        {
            vis[i<<1] = vis[(i<<1) + 1] = 1;
            kv_push(uint64_t, p_refine, (((uint64_t)(max_k))<<32)|((uint64_t)(i)));
        }    
    }
    free(w); free(idx); free(sgv);

    for (i = 0; i < p_refine.n; i++)
    {
        p = &(sl->a[p_refine.a[i]>>32]);
        // uid = (uint32_t)p_refine.a[i];
    }

    kv_destroy(p_refine);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
shlq $0x0, %rdi
callq 0x2270
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
shlq $0x3, %rdi
callq 0x2270
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x2270
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
shlq $0x2, %rdx
movl $0xff, %esi
callq 0x2100
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x24348e
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x20(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x243481
movl -0x20(%rbp), %edx
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rcx), %rcx
movl -0x24(%rbp), %esi
movl (%rcx,%rsi,4), %ecx
shrl %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x24344e
jmp 0x243483
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x243425
movl $0x0, -0x1c(%rbp)
movq $0x0, -0x70(%rbp)
movl -0x1c(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0xd8(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x24383e
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
shll %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x2434d2
jmp 0x243830
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x1c(%rbp), %ecx
shll %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
cmpl $0x0, %eax
jne 0x243518
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x1c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
cmpl $0x0, %eax
jne 0x243518
jmp 0x243830
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x243551
movq -0x50(%rbp), %rax
movl -0x20(%rbp), %ecx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movl -0x20(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x24351f
movl -0x1c(%rbp), %eax
shll %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x28(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0xd8(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl -0x28(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x243641
movq -0x38(%rbp), %rax
movl -0x20(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2435d4
jmp 0x243633
movq -0x38(%rbp), %rax
movl -0x20(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movsd 0x18(%rax), %xmm0
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rdx
movl -0x20(%rbp), %esi
shlq $0x5, %rsi
addq %rsi, %rdx
movl (%rdx), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rdx
movl -0x20(%rbp), %esi
shlq $0x5, %rsi
addq %rsi, %rdx
movl (%rdx), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
movb $0x1, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2435ad
movl -0x1c(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x28(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0xd8(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl -0x28(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x243734
movq -0x38(%rbp), %rax
movl -0x20(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2436c7
jmp 0x243726
movq -0x38(%rbp), %rax
movl -0x20(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movsd 0x18(%rax), %xmm0
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rdx
movl -0x20(%rbp), %esi
shlq $0x5, %rsi
addq %rsi, %rdx
movl (%rdx), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rdx
movl -0x20(%rbp), %esi
shlq $0x5, %rsi
addq %rsi, %rdx
movl (%rdx), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
movb $0x1, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2436a0
movl $0x0, -0x20(%rbp)
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x243795
movq -0x48(%rbp), %rax
movl -0x20(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
jne 0x24375e
jmp 0x24378a
cmpl $-0x1, -0x30(%rbp)
je 0x243782
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm1
movq -0x50(%rbp), %rax
movl -0x20(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x243788
movl -0x20(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x24378a
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x243742
cmpl $-0x1, -0x30(%rbp)
je 0x24382e
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
shll %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jne 0x243806
cmpq $0x0, -0x68(%rbp)
je 0x2437de
movq -0x68(%rbp), %rax
shlq %rax
movq %rax, -0x78(%rbp)
jmp 0x2437e9
movl $0x2, %eax
movq %rax, -0x78(%rbp)
jmp 0x2437e9
movq -0x78(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0x60(%rbp)
movl -0x30(%rbp), %eax
movl %eax, %edx
shlq $0x20, %rdx
movl -0x1c(%rbp), %eax
orq %rax, %rdx
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x70(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0x24382e
jmp 0x243830
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x24349d
movq -0x50(%rbp), %rdi
callq 0x2210
movq -0x58(%rbp), %rdi
callq 0x2210
movq -0x48(%rbp), %rdi
callq 0x2210
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpq -0x70(%rbp), %rax
jae 0x243895
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
movl -0x1c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x243860
movq -0x60(%rbp), %rdi
callq 0x2210
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)