// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/horder.cpp
// Function: get_max_anchor(horder_t*, sc_lay_t*, unsigned char*, double*, unsigned char*, unsigned int*, unsigned int*, unsigned int*)
uint32_t get_max_anchor(horder_t *h, sc_lay_t *sl, uint8_t *vis, double *w, uint8_t *sgv, uint32_t *idx,
uint32_t *max_utg, uint32_t *max_sc)
{
    (*max_utg) = (*max_sc) = (uint32_t)-1;
    double max_utg_w = -1;
    uint32_t i, k, v, nv, max_k;
    osg_arc_t *av = NULL;
    for (i = 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 || idx[av[k].v>>1] == (uint32_t)-1) 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 || idx[av[k].v>>1] == (uint32_t)-1) 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)
        {
            if((*max_utg) == (uint32_t)-1 || max_utg_w < w[max_k])
            {
                max_utg_w = w[max_k];
                (*max_utg) = i;
                (*max_sc) = max_k;
            }
        }    
    }

    return (*max_utg) == (uint32_t)-1?0:1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %r10
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl %r11d, (%rax)
movl %r11d, (%r10)
movq 0xd8(%rdi), %r15
testl $0x7fffffff, 0x14(%r15) # imm = 0x7FFFFFFF
je 0x175e84
movsd 0xaf85a(%rip), %xmm0    # 0x225538
xorl %r11d, %r11d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpb $0x0, (%rdx,%r11,2)
je 0x175d0c
incq %r11
movq 0xd8(%rdi), %r15
movl 0x14(%r15), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmpq %r14, %r11
jb 0x175ce6
jmp 0x175e84
leaq (%r11,%r11), %r14
movq 0x20(%r15), %r15
cmpl $0x0, (%r15,%r14,8)
jne 0x175d23
cmpl $0x0, 0x8(%r15,%r14,8)
je 0x175ced
cmpq $0x0, 0x8(%rsi)
je 0x175d4a
movl $0x1, %ebp
xorl %r15d, %r15d
movq $0x0, (%rcx,%r15,8)
movb $0x0, (%r8,%r15)
movl %ebp, %r15d
incl %ebp
cmpq %r15, 0x8(%rsi)
ja 0x175d32
movq 0xd8(%rdi), %r13
movq 0x20(%r13), %r15
movq (%r15,%r14,8), %r12
testl %r12d, %r12d
je 0x175dad
movq %r12, %r15
shrq $0x1b, %r15
andq $-0x20, %r15
addq 0x8(%r13), %r15
movl %r12d, %r12d
shlq $0x5, %r12
xorl %r13d, %r13d
cmpl $0x0, 0x8(%r15,%r13)
js 0x175da4
movl (%r15,%r13), %ebp
shrl %ebp
movl (%r9,%rbp,4), %ebp
cmpq %rbx, %rbp
je 0x175da4
movsd 0x18(%r15,%r13), %xmm1
addsd (%rcx,%rbp,8), %xmm1
movsd %xmm1, (%rcx,%rbp,8)
movb $0x1, (%r8,%rbp)
addq $0x20, %r13
cmpq %r13, %r12
jne 0x175d77
movq 0xd8(%rdi), %r12
movq 0x20(%r12), %r15
movq 0x8(%r15,%r14,8), %r15
testl %r15d, %r15d
je 0x175e16
movq %r15, %r14
shrq $0x1b, %r14
andq $-0x20, %r14
addq 0x8(%r12), %r14
movl %r15d, %r15d
shlq $0x5, %r15
xorl %r12d, %r12d
cmpl $0x0, 0x8(%r14,%r12)
js 0x175e0d
movl (%r14,%r12), %r13d
shrl %r13d
movl (%r9,%r13,4), %r13d
cmpq %rbx, %r13
je 0x175e0d
movsd 0x18(%r14,%r12), %xmm1
addsd (%rcx,%r13,8), %xmm1
movsd %xmm1, (%rcx,%r13,8)
movb $0x1, (%r8,%r13)
addq $0x20, %r12
cmpq %r12, %r15
jne 0x175ddd
movq 0x8(%rsi), %r14
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %r14, %r14
je 0x175e52
xorl %r15d, %r15d
cmpb $0x0, (%r8,%r15)
je 0x175e47
cmpl $-0x1, %ebp
je 0x175e44
movl %ebp, %r12d
movsd (%rcx,%r15,8), %xmm1
ucomisd (%rcx,%r12,8), %xmm1
jbe 0x175e47
movl %r15d, %ebp
incq %r15
movl %r15d, %r12d
cmpq %r12, %r14
ja 0x175e27
cmpl $-0x1, %ebp
je 0x175ced
cmpl $-0x1, (%r10)
movl %ebp, %r14d
je 0x175e74
movsd (%rcx,%r14,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x175ced
movsd (%rcx,%r14,8), %xmm0
movl %r11d, (%r10)
movl %ebp, (%rax)
jmp 0x175ced
xorl %eax, %eax
cmpl $-0x1, (%r10)
setne %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq