// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movb %r8b, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movb %cl, -0x21(%rbp)
movb %al, -0x22(%rbp)
movsd %xmm0, -0x30(%rbp)
movsd %xmm1, -0x38(%rbp)
movl %r9d, -0x3c(%rbp)
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x64(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x14(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x37a079
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3ab
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x28(%rcx), %rcx
movl -0x14(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movl -0x14(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x37a174
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x37a0f2
jmp 0x37a166
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl %eax
movq -0x20(%rbp), %rcx
movl -0x14(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
cmpl %ecx, %eax
je 0x37a13d
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shrl %eax
movq -0x20(%rbp), %rcx
movl -0x14(%rbp), %edx
xorl $0x1, %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0x37a149
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3ab
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x40(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x37a0cb
cmpl $0x2, -0x4c(%rbp)
jae 0x37a185
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x37a3ab
cmpb $0x0, -0x22(%rbp)
je 0x37a1cc
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
xorl $0x1, %esi
callq 0x318e70
movzbl %al, %eax
movl %eax, -0x64(%rbp)
cmpl $0x1, -0x64(%rbp)
jne 0x37a1b0
movl $0x2, %eax
movl %eax, -0x6c(%rbp)
jmp 0x37a1c6
movl -0x64(%rbp), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
cmpl $0x2, %edx
cmovel %ecx, %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x68(%rbp)
movl $0x0, -0x50(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x37a24e
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x37a204
jmp 0x37a243
movl -0x50(%rbp), %eax
movq -0x60(%rbp), %rcx
movl -0x44(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x37a241
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x50(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0x37a243
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x37a1e1
cmpl $-0x1, -0x54(%rbp)
jne 0x37a260
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3ab
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x37a366
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x37a294
movl -0x44(%rbp), %eax
cmpl -0x54(%rbp), %eax
jne 0x37a299
jmp 0x37a358
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2sd %eax, %xmm0
movq -0x60(%rbp), %rax
movl -0x54(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2sd %eax, %xmm1
mulsd -0x30(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x37a2e4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3ab
cmpb $0x0, -0x21(%rbp)
je 0x37a356
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movw 0x10(%rax), %ax
andw $0x3fff, %ax            # imm = 0x3FFF
movzwl %ax, %eax
cmpl -0x3c(%rbp), %eax
jbe 0x37a356
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movw 0x10(%rax), %ax
andw $0x3fff, %ax            # imm = 0x3FFF
movzwl %ax, %eax
cvtsi2sd %eax, %xmm0
movq -0x60(%rbp), %rax
movl -0x54(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movw 0x10(%rax), %ax
andw $0x3fff, %ax            # imm = 0x3FFF
movzwl %ax, %eax
cvtsi2sd %eax, %xmm1
mulsd -0x38(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x37a356
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3ab
jmp 0x37a358
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x37a267
cmpb $0x0, -0x22(%rbp)
je 0x37a397
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rax
movl -0x54(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
callq 0x318e70
movzbl %al, %eax
cmpl -0x68(%rbp), %eax
jne 0x37a397
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x37a3ab
movq -0x60(%rbp), %rax
movl -0x54(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)