// File: /workspace/github_cpp_makefile_star2_O2/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 0x18(%rdi), %rax
movq 0x20(%rax), %r11
movl %esi, %r10d
shrl %r10d
cmpl $0x0, (%r11,%r10,8)
js 0x1dd668
movl %ecx, 0x18(%rsp)
movl %r8d, 0xc(%rsp)
movl %r9d, 0x1c(%rsp)
movq 0x8(%rax), %r14
movq 0x28(%rax), %rcx
movl %esi, %eax
movq (%rcx,%rax,8), %rcx
movl %ecx, %r15d
shrq $0x20, %rcx
imulq $0x18, %rcx, %rbp
leaq (%r14,%rbp), %rcx
movq %rcx, 0x10(%rsp)
xorl $0x1, %esi
imulq $0x18, %r15, %r13
leaq (%r14,%rbp), %rbx
addq $0xc, %rbx
xorl %r8d, %r8d
pushq $-0x1
popq %rcx
movl %ecx, %r12d
xorl %r9d, %r9d
cmpq %r8, %r13
je 0x1dd679
cmpl $0x0, (%rbx,%r8)
js 0x1dd662
movl -0x4(%rbx,%r8), %r12d
movl %r12d, %r10d
shrl %r10d
movl (%rdx,%rax,4), %r11d
shrl %r11d
cmpl %r11d, %r10d
je 0x1dd671
movl (%rdx,%rsi,4), %r11d
shrl %r11d
cmpl %r11d, %r10d
je 0x1dd671
incl %r9d
addq $0x18, %r8
jmp 0x1dd630
pushq $-0x1
popq %r12
jmp 0x1dd7cf
movl %ecx, %r12d
jmp 0x1dd7cf
cmpl $0x2, %r9d
jb 0x1dd7cf
movq %rdi, 0x20(%rsp)
pushq $-0x1
popq %rbx
movl %ebx, %r11d
cmpb $0x0, 0xc(%rsp)
je 0x1dd6cc
movq 0x20(%rsp), %rdi
movsd %xmm1, 0x30(%rsp)
movsd %xmm0, 0x28(%rsp)
callq 0x1aa250
movsd 0x28(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
xorl %ecx, %ecx
cmpb $0x2, %al
sete %cl
cmpb $0x1, %al
leal -0x1(%rcx,%rcx), %eax
pushq $0x2
popq %r11
cmovnel %eax, %r11d
leaq (%r14,%rbp), %rax
addq $0xc, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rcx, %r15
je 0x1dd6fd
movl (%rax), %esi
testl %esi, %esi
setns %dil
cmpl %esi, %edx
setb %r8b
testb %r8b, %dil
cmovnel %esi, %edx
cmovnel %ecx, %ebx
incq %rcx
addq $0x18, %rax
jmp 0x1dd6d8
pushq $-0x1
popq %r12
cmpl $-0x1, %ebx
je 0x1dd7cf
movl %ebx, %eax
imulq $0x18, %rax, %r15
movq 0x10(%rsp), %rax
addq %r15, %rax
leaq (%r14,%rbp), %rcx
addq $0x10, %rcx
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %rdi
movl 0x1c(%rsp), %r9d
movl 0x18(%rsp), %r10d
cmpq %rdx, %r13
je 0x1dd7a5
movl -0x4(%rcx,%rdx), %r8d
testl %r8d, %r8d
js 0x1dd79f
cmpq %rdx, %r15
je 0x1dd79f
cvtsi2sd %r8d, %xmm3
movl 0xc(%rax), %r8d
andl %esi, %r8d
cvtsi2sd %r8d, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm3
ja 0x1dd7cf
testb %r10b, %r10b
je 0x1dd79f
movzwl (%rcx,%rdx), %r8d
andl $0x3fff, %r8d           # imm = 0x3FFF
cmpl %r9d, %r8d
jbe 0x1dd79f
movzwl %r8w, %r8d
cvtsi2sd %r8d, %xmm3
movzwl 0x10(%rax), %r8d
andl $0x3fff, %r8d           # imm = 0x3FFF
cvtsi2sd %r8d, %xmm2
mulsd %xmm1, %xmm2
ucomisd %xmm2, %xmm3
ja 0x1dd7cf
addq $0x18, %rdx
jmp 0x1dd736
cmpb $0x0, 0xc(%rsp)
je 0x1dd7c5
movq 0x10(%rsp), %rax
movl 0x8(%rax,%r15), %esi
movl %r11d, %ebx
callq 0x1aa250
movzbl %al, %eax
cmpl %eax, %ebx
je 0x1dd7cf
movq 0x10(%rsp), %rax
movl 0x8(%rax,%r15), %r12d
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq