// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: get_best_het_node(ma_ug_t*, unsigned int, unsigned int*, unsigned char, unsigned char, double, double, unsigned int)
uint32_t get_best_het_node(ma_ug_t *ug, uint32_t v, uint32_t *bid, uint8_t is_ou, uint8_t is_trio, double len_rat, double ou_rat, uint32_t min_ou)
{
    uint32_t w = (uint32_t)-1, k, nv, kv, ol_max, ol_k;
    asg_arc_t *av; uint32_t trioF = (uint32_t)-1, ntrioF = (uint32_t)-1;
    if(ug->g->seq[v>>1].del) return (uint32_t)-1;
    av = asg_arc_a(ug->g, v); nv = asg_arc_n(ug->g, v);
    for (k = kv = 0; k < nv; k++) {
        if(av[k].del) continue;
        ///could not connect to the beg/sink node
        if((av[k].v>>1)==(bid[v]>>1) || (av[k].v>>1)==(bid[v^1]>>1)) return (uint32_t)-1;
        kv++; w = av[k].v;
    }
    if(kv < 2) return w;///kv == 0, return (uint32_t)-1; kv == 1, return node; 
    // if(v == 351) fprintf(stderr, "-0-[M::%s]\tutg%.6ul(%c)\tv::%u\tkv::%u\n", __func__, (v>>1)+1, "+-"[v&1], v, kv);

    if(is_trio) {
        trioF = get_ug_tip_trio_infor(ug, v^1);
        ntrioF = (trioF==FATHER? MOTHER : (trioF==MOTHER? FATHER : (uint32_t)-1));
    }

    ol_max = 0; ol_k = (uint32_t)-1;
    for (k = 0; k < nv; ++k) {
        if(av[k].del) continue;
        // if(is_trio && get_ug_tip_trio_infor(ug, av[k].v) == ntrioF) continue;
        if(ol_max < av[k].ol) ol_max = av[k].ol, ol_k = k;
    }
    // if(v == 351) fprintf(stderr, "-1-[M::%s]\tutg%.6ul(%c)\tv::%u\tol_k::%u\n", __func__, (v>>1)+1, "+-"[v&1], v, ol_k);
    if(ol_k == (uint32_t)-1) return (uint32_t)-1;
    for (k = 0; k < nv; ++k) {
        if(av[k].del || k == ol_k) continue;
        // if(is_trio && get_ug_tip_trio_infor(ug, av[k].v) == ntrioF) continue;
        if(av[k].ol > av[ol_k].ol*len_rat) return (uint32_t)-1;
        if((is_ou) && (av[k].ou > min_ou) && ((av[k].ou) > (av[ol_k].ou*ou_rat))) return (uint32_t)-1;
    }
    if(is_trio && get_ug_tip_trio_infor(ug, av[ol_k].v) == ntrioF) return (uint32_t)-1;
    // if(v == 351) fprintf(stderr, "-2-[M::%s]\tutg%.6ul(%c)\tv::%u\tav[ol_k].v::%u\n", __func__, (v>>1)+1, "+-"[v&1], v, av[ol_k].v);
    return av[ol_k].v;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %r10
movq 0x20(%r10), %rax
movl %esi, %edi
shrl %edi
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x0, (%rax,%rdi,8)
js 0x22b6e5
movl %ecx, 0x18(%rsp)
movl %r9d, 0x1c(%rsp)
movl %r8d, 0xc(%rsp)
movq 0x28(%r10), %rdi
movl %esi, %eax
movq (%rdi,%rax,8), %r13
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
andq %r13, %r14
je 0x22b6e5
movq 0x8(%r10), %rdi
movq %r13, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r15
leaq (%rdi,%r15,8), %rcx
movq %rcx, 0x30(%rsp)
xorl $0x1, %esi
movq %rdi, 0x28(%rsp)
leaq (%rdi,%r15,8), %rbp
addq $0xc, %rbp
leaq (,%r14,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
xorl %r8d, %r8d
cmpl $0x0, (%rbp,%r9)
js 0x22b54d
movl -0x4(%rbp,%r9), %edi
movl %edi, %r10d
shrl %r10d
movl (%rdx,%rax,4), %r11d
shrl %r11d
cmpl %r11d, %r10d
je 0x22b6e5
movl (%rdx,%rsi,4), %r11d
shrl %r11d
cmpl %r11d, %r10d
je 0x22b6e5
incl %r8d
addq $0x18, %r9
cmpq %r9, %rcx
jne 0x22b517
movl %edi, %r12d
cmpl $0x2, %r8d
jb 0x22b6e5
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
cmpb $0x0, 0xc(%rsp)
je 0x22b5ad
movq %rbx, %rdi
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x20(%rsp)
callq 0x1f5161
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
xorl %ecx, %ecx
cmpb $0x2, %al
sete %cl
cmpb $0x1, %al
leal -0x1(%rcx,%rcx), %eax
movl $0x2, %r9d
cmovnel %eax, %r9d
testq %r14, %r14
je 0x22b6e5
xorl %ecx, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %edx, %edx
movl (%rbp), %esi
testl %esi, %esi
setns %dil
cmpl %esi, %edx
setb %r8b
testb %r8b, %dil
cmovnel %esi, %edx
cmovnel %ecx, %eax
incq %rcx
addq $0x18, %rbp
cmpq %rcx, %r14
jne 0x22b5bf
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0x22b6e5
movl %r9d, 0x10(%rsp)
movl %eax, %eax
testq %r14, %r14
movl 0x1c(%rsp), %r11d
movl 0x18(%rsp), %ebp
movq 0x30(%rsp), %r14
je 0x22b6ba
leaq (,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%r14,%rcx), %rdx
movl %r13d, %edi
movq 0x28(%rsp), %rsi
leaq (%rsi,%r15,8), %rsi
addq $0x10, %rsi
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rdi
xorl %r8d, %r8d
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl -0x4(%rsi,%r8), %r10d
testl %r10d, %r10d
js 0x22b6b1
cmpq %r8, %rcx
je 0x22b6b1
xorps %xmm3, %xmm3
cvtsi2sd %r10d, %xmm3
movl 0xc(%rdx), %r10d
andl %r9d, %r10d
xorps %xmm2, %xmm2
cvtsi2sd %r10d, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm3
ja 0x22b6e5
testb %bpl, %bpl
je 0x22b6b1
movzwl (%rsi,%r8), %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
cmpl %r11d, %r10d
jbe 0x22b6b1
movzwl %r10w, %r10d
xorps %xmm3, %xmm3
cvtsi2sd %r10d, %xmm3
movzwl 0x10(%rdx), %r10d
andl $0x3fff, %r10d          # imm = 0x3FFF
xorps %xmm2, %xmm2
cvtsi2sd %r10d, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm3
ja 0x22b6e5
addq $0x18, %r8
cmpq %r8, %rdi
jne 0x22b641
leaq (%rax,%rax,2), %rax
movl 0x8(%r14,%rax,8), %r12d
cmpb $0x0, 0xc(%rsp)
je 0x22b6e5
movq %rbx, %rdi
movl %r12d, %esi
callq 0x1f5161
movzbl %al, %eax
cmpl %eax, 0x10(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovel %eax, %r12d
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq