// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rax
movq 0x20(%rax), %r10
movl %esi, %edi
shrl %edi
cmpl $0x0, (%r10,%rdi,8)
js 0x21fd60
movl %ecx, 0x8(%rsp)
movl %r8d, %ebp
movl %r9d, 0xc(%rsp)
movq 0x8(%rax), %r8
movq 0x28(%rax), %rcx
movl %esi, %eax
movq (%rcx,%rax,8), %r14
movq %r14, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r9
testl %r14d, %r14d
movq %r8, 0x18(%rsp)
movq %r9, 0x10(%rsp)
je 0x21fd6b
movl %esi, %edi
xorl $0x1, %edi
movl %r14d, %ecx
leaq (%r8,%r9,8), %r8
addq $0xc, %r8
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r9
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
xorl %r11d, %r11d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x0, (%r8,%r10)
js 0x21fd4e
movl -0x4(%r8,%r10), %ecx
movl %ecx, %r15d
shrl %r15d
movl (%rdx,%rax,4), %r13d
shrl %r13d
cmpl %r13d, %r15d
je 0x21ff25
movl (%rdx,%rdi,4), %r13d
shrl %r13d
cmpl %r13d, %r15d
je 0x21ff25
incl %r11d
addq $0x18, %r10
cmpq %r10, %r9
jne 0x21fd19
cmpl $0x2, %r11d
setb %al
jmp 0x21fd72
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x21ff25
movb $0x1, %al
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, %r12d
testb %al, %al
jne 0x21ff25
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl %ebp, %r11d
testb %r11b, %r11b
je 0x21fdcc
xorl $0x1, %esi
movq %rbx, %rdi
movsd %xmm1, (%rsp)
movsd %xmm0, 0x20(%rsp)
callq 0x1ea636
movsd 0x20(%rsp), %xmm0
movl %ebp, %r11d
movsd (%rsp), %xmm1
xorl %ecx, %ecx
cmpb $0x2, %al
sete %cl
cmpb $0x1, %al
leal -0x1(%rcx,%rcx), %eax
movl $0x2, %r13d
cmovnel %eax, %r13d
testl %r14d, %r14d
movl 0xc(%rsp), %r10d
je 0x21fe19
movl %r14d, %eax
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
leaq (%rcx,%rdx,8), %rcx
addq $0xc, %rcx
xorl %edx, %edx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %esi, %esi
movl (%rcx), %edi
testl %edi, %edi
setns %r8b
cmpl %edi, %esi
setb %r9b
testb %r9b, %r8b
cmovnel %edi, %esi
cmovnel %edx, %r15d
incq %rdx
addq $0x18, %rcx
cmpq %rdx, %rax
jne 0x21fdf5
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $-0x1, %r15d
movl 0x8(%rsp), %ebp
je 0x21ff25
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rdi
leaq (%rdx,%rdi,8), %rax
movl %r15d, %ecx
movq %rcx, (%rsp)
movq %rax, %r15
testl %r14d, %r14d
je 0x21fef6
movq (%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%r15,%rax), %rcx
movl %r14d, %esi
leaq (%rdx,%rdi,8), %rdx
addq $0x10, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl -0x4(%rdx,%rdi), %r9d
testl %r9d, %r9d
js 0x21feed
cmpq %rdi, %rax
je 0x21feed
xorps %xmm3, %xmm3
cvtsi2sd %r9d, %xmm3
movl 0xc(%rcx), %r9d
andl %r8d, %r9d
xorps %xmm2, %xmm2
cvtsi2sd %r9d, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm3
ja 0x21ff25
testb %bpl, %bpl
je 0x21feed
movzwl (%rdx,%rdi), %r9d
andl $0x3fff, %r9d           # imm = 0x3FFF
cmpl %r10d, %r9d
jbe 0x21feed
movzwl %r9w, %r9d
xorps %xmm3, %xmm3
cvtsi2sd %r9d, %xmm3
movzwl 0x10(%rcx), %r9d
andl $0x3fff, %r9d           # imm = 0x3FFF
xorps %xmm2, %xmm2
cvtsi2sd %r9d, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm3
ja 0x21ff25
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0x21fe7d
testb %r11b, %r11b
je 0x21ff18
movq (%rsp), %rax
leaq (%rax,%rax,2), %rax
movl 0x8(%r15,%rax,8), %esi
movq %rbx, %rdi
callq 0x1ea636
movzbl %al, %eax
cmpl %eax, %r13d
je 0x21ff25
movq (%rsp), %rax
leaq (%rax,%rax,2), %rax
movl 0x8(%r15,%rax,8), %r12d
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq