// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, 0x8(%rsp)
movq %rdi, %r14
movq %rsi, 0x18(%rsp)
movq 0x8(%rsi), %r12
movq %r12, %rdi
callq 0x2280
movq %rax, %rbx
leaq (,%r12,8), %rdi
callq 0x2280
movq %rax, %r13
movq %r14, 0x20(%rsp)
movq 0xd8(%r14), %r14
movl 0x14(%r14), %r15d
movl %r15d, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
shlq $0x2, %rdi
movq %rdi, (%rsp)
callq 0x2280
movq %rax, %rbp
movq %rax, %rdi
movl $0xff, %esi
movq (%rsp), %rdx
callq 0x20e0
xorl %eax, %eax
movl %eax, %ecx
cmpq %rcx, %r12
jbe 0x139080
movq 0x18(%rsp), %rdx
movq (%rdx), %rdx
imulq $0x18, %rcx, %rsi
leaq (%rdx,%rsi), %rcx
movq 0x8(%rdx,%rsi), %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rdx
jbe 0x13907c
movq (%rcx), %rdi
movl (%rdi,%rsi,4), %edi
shrl %edi
movl %eax, (%rbp,%rdi,4)
incl %esi
jmp 0x139065
incl %eax
jmp 0x139047
xorl %r12d, %r12d
xorl %edi, %edi
movq $0x0, 0x10(%rsp)
movq $0x0, (%rsp)
movq 0x8(%rsp), %rdx
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
cmpq %r15, %r12
jae 0x139261
cmpb $0x0, (%rdx,%r12,2)
jne 0x139255
leaq (%r12,%r12), %rcx
movq 0x20(%r14), %r8
movq (%r8,%rcx,8), %r9
testl %r9d, %r9d
jne 0x1390d3
cmpl $0x0, 0x8(%r8,%rcx,8)
je 0x139255
movq 0x18(%rsp), %rdx
movq 0x8(%rdx), %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rdx
jbe 0x1390f3
andq $0x0, (%r13,%rsi,8)
movb $0x0, (%rbx,%rsi)
incl %esi
jmp 0x1390de
movq %rdi, %rax
movq 0x8(%r14), %rdi
movq %r9, %rsi
shrq $0x1b, %rsi
andq $-0x20, %rsi
addq %rdi, %rsi
movl %r9d, %r9d
shlq $0x5, %r9
xorl %r10d, %r10d
cmpq %r10, %r9
je 0x13914b
cmpl $0x0, 0x8(%rsi,%r10)
js 0x139145
movsd 0x18(%rsi,%r10), %xmm0
movl (%rsi,%r10), %r11d
shrl %r11d
movl (%rbp,%r11,4), %r11d
addsd (%r13,%r11,8), %xmm0
movsd %xmm0, (%r13,%r11,8)
movb $0x1, (%rbx,%r11)
addq $0x20, %r10
jmp 0x139112
movq %rcx, %rsi
orq $0x1, %rsi
movq 0x8(%r8,%rcx,8), %r9
movl %r9d, %r8d
shrq $0x1b, %r9
andq $-0x20, %r9
addq %r9, %rdi
shlq $0x5, %r8
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x1391a5
cmpl $0x0, 0x8(%rdi,%r9)
js 0x13919f
movsd 0x18(%rdi,%r9), %xmm0
movl (%rdi,%r9), %r10d
shrl %r10d
movl (%rbp,%r10,4), %r10d
addsd (%r13,%r10,8), %xmm0
movsd %xmm0, (%r13,%r10,8)
movb $0x1, (%rbx,%r10)
addq $0x20, %r9
jmp 0x13916c
xorl %edi, %edi
pushq $-0x1
popq %r8
movl %r8d, %r15d
movl %edi, %r8d
cmpq %r8, %rdx
jbe 0x1391dd
cmpb $0x0, (%rbx,%r8)
je 0x1391d9
cmpl $-0x1, %r15d
je 0x1391d6
movl %r15d, %r9d
movsd (%r13,%r8,8), %xmm0
ucomisd (%r13,%r9,8), %xmm0
jbe 0x1391d9
movl %edi, %r15d
incl %edi
jmp 0x1391ae
cmpl $-0x1, %r15d
je 0x13924d
movq 0x8(%rsp), %rdx
movb $0x1, %dil
movb %dil, (%rdx,%rsi)
movb %dil, (%rdx,%rcx)
movq 0x10(%rsp), %rcx
cmpq %rcx, (%rsp)
movq %rax, %rdi
jne 0x139229
leaq (%rcx,%rcx), %r14
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, %rdi
movq %r14, 0x10(%rsp)
shlq $0x20, %r15
orq %r12, %r15
movq (%rsp), %rax
movq %r15, (%rdi,%rax,8)
incq %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rcx
movq 0xd8(%rcx), %r14
jmp 0x139255
movq 0x8(%rsp), %rdx
movq %rax, %rdi
incq %r12
movl 0x14(%r14), %r15d
jmp 0x13909b
movq %rdi, %r14
movq %r13, %rdi
callq 0x2220
movq %rbp, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220