// File: /workspace/github_cpp_makefile_star2_O2/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
orl $-0x1, (%rax)
movq 0x38(%rsp), %r10
orl $-0x1, (%r10)
movsd 0xaa2cb(%rip), %xmm0    # 0x1e3580
xorl %r11d, %r11d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq 0xd8(%rdi), %rax
movl 0x14(%rax), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
cmpq %r15, %r11
jae 0x13943b
cmpb $0x0, (%rdx,%r11,2)
je 0x1392ea
incq %r11
jmp 0x1392c3
leaq (%r11,%r11), %r15
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%r15,8)
jne 0x139301
cmpl $0x0, 0x8(%rax,%r15,8)
je 0x1392e5
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, 0x8(%rsi)
jbe 0x139319
andq $0x0, (%rcx,%rax,8)
movb $0x0, (%r8,%rax)
incl %eax
jmp 0x139303
movq 0xd8(%rdi), %rax
movq 0x20(%rax), %r12
movq (%r12,%r15,8), %r13
movl %r13d, %r12d
shrq $0x1b, %r13
andq $-0x20, %r13
addq 0x8(%rax), %r13
shlq $0x5, %r12
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x139376
cmpl $0x0, 0x8(%r13,%rbp)
js 0x139370
movl (%r13,%rbp), %eax
shrl %eax
movl (%r9,%rax,4), %eax
cmpq %r14, %rax
je 0x139370
movsd 0x18(%r13,%rbp), %xmm1
addsd (%rcx,%rax,8), %xmm1
movsd %xmm1, (%rcx,%rax,8)
movb $0x1, (%r8,%rax)
addq $0x20, %rbp
jmp 0x13933d
movq 0xd8(%rdi), %rax
movq 0x20(%rax), %r12
movq 0x8(%r12,%r15,8), %r12
movl %r12d, %r15d
shrq $0x1b, %r12
andq $-0x20, %r12
addq 0x8(%rax), %r12
shlq $0x5, %r15
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x1393d4
cmpl $0x0, 0x8(%r12,%r13)
js 0x1393ce
movl (%r12,%r13), %eax
shrl %eax
movl (%r9,%rax,4), %eax
cmpq %r14, %rax
je 0x1393ce
movsd 0x18(%r12,%r13), %xmm1
addsd (%rcx,%rax,8), %xmm1
movsd %xmm1, (%rcx,%rax,8)
movb $0x1, (%r8,%rax)
addq $0x20, %r13
jmp 0x13939c
movq 0x8(%rsi), %r15
xorl %r12d, %r12d
movl %ebx, %ebp
movl %r12d, %eax
movl %ebp, %r13d
cmpq %rax, %r15
jbe 0x139409
cmpb $0x0, (%r8,%rax)
je 0x139404
cmpl $-0x1, %ebp
je 0x139401
movsd (%rcx,%rax,8), %xmm1
ucomisd (%rcx,%r13,8), %xmm1
jbe 0x139404
movl %r12d, %ebp
incl %r12d
jmp 0x1393dd
cmpl $-0x1, %ebp
je 0x1392e5
cmpl $-0x1, (%r10)
movsd (%rcx,%r13,8), %xmm1
je 0x139428
ucomisd %xmm0, %xmm1
jbe 0x1392e5
movl %r11d, (%r10)
movq 0x40(%rsp), %rax
movl %ebp, (%rax)
movapd %xmm1, %xmm0
jmp 0x1392e5
xorl %eax, %eax
cmpl $-0x1, (%r10)
setne %al
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq