// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %r12
movq %rsi, 0x28(%rsp)
movq 0x8(%rsi), %r15
movq %r15, %rdi
callq 0x2280
movq %rax, %rbx
leaq (,%r15,8), %rdi
callq 0x2280
movq %rax, %r13
movq %r12, 0x10(%rsp)
movq 0xd8(%r12), %rax
movl 0x14(%rax), %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
shlq $0x2, %r12
movq %r12, %rdi
callq 0x2280
movq %rax, %rbp
movq %rax, %rdi
movl $0xff, %esi
movq %r12, %rdx
callq 0x20e0
testq %r15, %r15
je 0x175a51
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 0x175a47
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 0x175a30
incq %rcx
movl %ecx, %edx
cmpq %rdx, %r15
ja 0x175a19
movq 0x10(%rsp), %rdx
movq 0xd8(%rdx), %rsi
testl $0x7fffffff, 0x14(%rsi) # imm = 0x7FFFFFFF
je 0x175c68
xorl %r12d, %r12d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
movq %r14, 0x30(%rsp)
cmpb $0x0, (%r14,%r12,2)
jne 0x175c4a
leaq (%r12,%r12), %rcx
movq 0x20(%rsi), %rdi
movq (%rdi,%rcx,8), %r8
testl %r8d, %r8d
jne 0x175ab4
cmpl $0x0, 0x8(%rdi,%rcx,8)
je 0x175c4a
movq 0x28(%rsp), %rdx
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
je 0x175ade
xorl %r9d, %r9d
movq $0x0, (%r13,%r9,8)
movb $0x0, (%rbx,%r9)
incq %r9
movl %r9d, %r10d
cmpq %r10, %rdx
ja 0x175ac5
movq 0x8(%rsi), %rsi
testl %r8d, %r8d
je 0x175b36
movq %r8, %r9
shrq $0x1b, %r9
andq $-0x20, %r9
addq %rsi, %r9
movl %r8d, %r8d
shlq $0x5, %r8
xorl %r10d, %r10d
cmpl $0x0, 0x8(%r9,%r10)
js 0x175b2d
movsd 0x18(%r9,%r10), %xmm0
movl (%r9,%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
cmpq %r10, %r8
jne 0x175aff
movq 0x8(%rdi,%rcx,8), %rdi
testl %edi, %edi
je 0x175b8d
movq %rdi, %r8
shrq $0x1b, %r8
andq $-0x20, %r8
addq %r8, %rsi
movl %edi, %edi
shlq $0x5, %rdi
xorl %r8d, %r8d
cmpl $0x0, 0x8(%rsi,%r8)
js 0x175b84
movsd 0x18(%rsi,%r8), %xmm0
movl (%rsi,%r8), %r9d
shrl %r9d
movl (%rbp,%r9,4), %r9d
addsd (%r13,%r9,8), %xmm0
movsd %xmm0, (%r13,%r9,8)
movb $0x1, (%rbx,%r9)
addq $0x20, %r8
cmpq %r8, %rdi
jne 0x175b56
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rdx, %rdx
je 0x175bc6
xorl %esi, %esi
cmpb $0x0, (%rbx,%rsi)
je 0x175bbc
cmpl $-0x1, %r15d
je 0x175bb9
movl %r15d, %edi
movsd (%r13,%rsi,8), %xmm0
ucomisd (%r13,%rdi,8), %xmm0
jbe 0x175bbc
movl %esi, %r15d
incq %rsi
movl %esi, %edi
cmpq %rdi, %rdx
ja 0x175b9a
cmpl $-0x1, %r15d
je 0x175c1c
movq %rcx, %rdx
orq $0x1, %rdx
movb $0x1, %al
movb %al, (%r14,%rdx)
movb %al, (%r14,%rcx)
movq 0x20(%rsp), %rax
cmpq %rax, 0x18(%rsp)
jne 0x175c23
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x2370
movq %r14, 0x20(%rsp)
movq 0x30(%rsp), %r14
movq 0x10(%rsp), %rdx
jmp 0x175c2d
movq 0x10(%rsp), %rdx
jmp 0x175c4a
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rax
shlq $0x20, %r15
orq %r12, %r15
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rcx
movq %r15, (%rax,%rcx,8)
incq %rcx
movq %rcx, 0x18(%rsp)
incq %r12
movq 0xd8(%rdx), %rsi
movl 0x14(%rsi), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r12
jb 0x175a8d
jmp 0x175c71
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