// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rdx, 0x10(%rsp)
movq %rdi, %r14
movq %rsi, 0x28(%rsp)
movq 0x8(%rsi), %r12
movq %r12, %rdi
callq 0x2280
movq %rax, %rbx
leaq (,%r12,8), %rdi
callq 0x2280
movq %rax, %r13
movq %r14, 0x30(%rsp)
movq 0xd8(%r14), %r14
movl 0x14(%r14), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %r15, 0x8(%rsp)
shlq $0x2, %r15
movq %r15, %rdi
callq 0x2280
movq %rax, %rbp
movq %rax, %rdi
movl $0xff, %esi
movq %r15, %rdx
callq 0x20e0
testq %r12, %r12
je 0x17c5e6
movq 0x28(%rsp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rsi
movq 0x8(%rax,%rsi,8), %rdx
testq %rdx, %rdx
je 0x17c5dc
leaq (%rax,%rsi,8), %rsi
movq (%rsi), %rsi
xorl %edi, %edi
movl (%rsi,%rdi,4), %r8d
shrl %r8d
movl %ecx, (%rbp,%r8,4)
incq %rdi
movl %edi, %r8d
cmpq %r8, %rdx
ja 0x17c5c5
incq %rcx
movl %ecx, %edx
cmpq %rdx, %r12
ja 0x17c5ae
cmpl $0x0, 0x8(%rsp)
je 0x17c7da
xorl %r15d, %r15d
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r11
cmpb $0x0, (%r11,%r15,2)
je 0x17c631
incq %r15
movl 0x14(%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r15
jb 0x17c614
jmp 0x17c7e3
leaq (%r15,%r15), %rax
movq 0x20(%r14), %rsi
movq (%rsi,%rax,8), %rdi
testl %edi, %edi
jne 0x17c648
cmpl $0x0, 0x8(%rsi,%rax,8)
je 0x17c61b
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
je 0x17c670
xorl %edx, %edx
movq $0x0, (%r13,%rdx,8)
movb $0x0, (%rbx,%rdx)
incq %rdx
movl %edx, %r8d
cmpq %r8, %rcx
ja 0x17c658
movq 0x8(%r14), %rdx
testl %edi, %edi
je 0x17c6c6
movq %rdi, %r8
shrq $0x1b, %r8
andq $-0x20, %r8
addq %rdx, %r8
movl %edi, %edi
shlq $0x5, %rdi
xorl %r9d, %r9d
cmpl $0x0, 0x8(%r8,%r9)
js 0x17c6bd
movsd 0x18(%r8,%r9), %xmm0
movl (%r8,%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
cmpq %r9, %rdi
jne 0x17c68f
movq 0x8(%rsi,%rax,8), %rsi
testl %esi, %esi
je 0x17c71a
movq %rsi, %rdi
shrq $0x1b, %rdi
andq $-0x20, %rdi
addq %rdi, %rdx
movl %esi, %esi
shlq $0x5, %rsi
xorl %edi, %edi
cmpl $0x0, 0x8(%rdx,%rdi)
js 0x17c711
movsd 0x18(%rdx,%rdi), %xmm0
movl (%rdx,%rdi), %r8d
shrl %r8d
movl (%rbp,%r8,4), %r8d
addsd (%r13,%r8,8), %xmm0
movsd %xmm0, (%r13,%r8,8)
movb $0x1, (%rbx,%r8)
addq $0x20, %rdi
cmpq %rdi, %rsi
jne 0x17c6e5
testq %rcx, %rcx
je 0x17c61b
movq %rax, %rdx
orq $0x1, %rdx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %esi, %esi
cmpb $0x0, (%rbx,%rsi)
je 0x17c754
cmpl $-0x1, %r12d
je 0x17c751
movl %r12d, %edi
movsd (%r13,%rsi,8), %xmm0
ucomisd (%r13,%rdi,8), %xmm0
jbe 0x17c754
movl %esi, %r12d
incq %rsi
movl %esi, %edi
cmpq %rdi, %rcx
ja 0x17c732
cmpl $-0x1, %r12d
je 0x17c61b
movb $0x1, %cl
movb %cl, (%r11,%rdx)
movb %cl, (%r11,%rax)
movq 0x20(%rsp), %r14
cmpq 0x18(%rsp), %r14
jne 0x17c7ac
leaq (%r14,%r14), %rcx
testq %r14, %r14
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x18(%rsp)
leaq (,%rcx,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x2370
movq 0x10(%rsp), %r11
jmp 0x17c7b1
movq 0x8(%rsp), %rax
shlq $0x20, %r12
orq %r15, %r12
movq %rax, 0x8(%rsp)
movq %r12, (%rax,%r14,8)
incq %r14
movq %r14, 0x20(%rsp)
movq 0x30(%rsp), %rax
movq 0xd8(%rax), %r14
jmp 0x17c61b
movq $0x0, 0x8(%rsp)
movq %r13, %rdi
callq 0x2220
movq %rbp, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220