// File: 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
movq %rsp, %rbp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq 0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movsd 0x13e988(%rip), %xmm0   # 0x382278
movsd %xmm0, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0xd8(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x243c93
movq -0x18(%rbp), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x243939
jmp 0x243c85
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
cmpl $0x0, %eax
jne 0x24397f
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
cmpl $0x0, %eax
jne 0x24397f
jmp 0x243c85
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x2439b8
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movl -0x40(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x243986
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0xd8(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl -0x44(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x243ac6
movq -0x58(%rbp), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x243a57
movq -0x30(%rbp), %rax
movq -0x58(%rbp), %rcx
movl -0x40(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x243a59
jmp 0x243ab8
movq -0x58(%rbp), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movsd 0x18(%rax), %xmm0
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rdx
movl -0x40(%rbp), %esi
shlq $0x5, %rsi
addq %rsi, %rdx
movl (%rdx), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rdx
movl -0x40(%rbp), %esi
shlq $0x5, %rsi
addq %rsi, %rdx
movl (%rdx), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
movb $0x1, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x243a14
movl -0x3c(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0xd8(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl -0x44(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x243bd7
movq -0x58(%rbp), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x243b68
movq -0x30(%rbp), %rax
movq -0x58(%rbp), %rcx
movl -0x40(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x243b6a
jmp 0x243bc9
movq -0x58(%rbp), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movsd 0x18(%rax), %xmm0
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rdx
movl -0x40(%rbp), %esi
shlq $0x5, %rsi
addq %rsi, %rdx
movl (%rdx), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x58(%rbp), %rdx
movl -0x40(%rbp), %esi
shlq $0x5, %rsi
addq %rsi, %rdx
movl (%rdx), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
movb $0x1, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x243b25
movl $0x0, -0x40(%rbp)
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x243c38
movq -0x28(%rbp), %rax
movl -0x40(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
jne 0x243c01
jmp 0x243c2d
cmpl $-0x1, -0x4c(%rbp)
je 0x243c25
movq -0x20(%rbp), %rax
movl -0x4c(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm1
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x243c2b
movl -0x40(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x243c2d
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x243be5
cmpl $-0x1, -0x4c(%rbp)
je 0x243c83
movq 0x10(%rbp), %rax
cmpl $-0x1, (%rax)
je 0x243c5e
movsd -0x38(%rbp), %xmm1
movq -0x20(%rbp), %rax
movl -0x4c(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x243c81
movq -0x20(%rbp), %rax
movl -0x4c(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x38(%rbp)
movl -0x3c(%rbp), %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movl -0x4c(%rbp), %ecx
movq 0x18(%rbp), %rax
movl %ecx, (%rax)
jmp 0x243c83
jmp 0x243c85
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x243904
movq 0x10(%rbp), %rax
movl (%rax), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $-0x1, %edx
cmovel %ecx, %eax
popq %rbp
retq
nopl (%rax,%rax)