// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: renew_longest_tip_by_drop(asg_t*, ma_ug_t*, asg_arc_t*, unsigned int, long long*, long long*, unsigned int, buf_t*, unsigned int)
void renew_longest_tip_by_drop(asg_t *g, ma_ug_t *ug, asg_arc_t *av, uint32_t nv,
long long* base_maxLen, long long* base_maxLen_i, uint32_t stops_threshold, buf_t* b, 
uint32_t trio_flag)
{
    if(trio_flag != FATHER && trio_flag != MOTHER) return;
    Trio_counter max, cur;
    memset(&max, 0, sizeof(Trio_counter));
    memset(&cur, 0, sizeof(Trio_counter));
    long long ll, tmp, max_stop_nodeLen, max_stop_baseLen, max_weight = 0, cur_weight = 0;
    uint32_t convex, i, return_flag, best_tip_i, best_tip_len;
    
    b->b.n = 0;
    return_flag = get_unitig(g, ug, av[(*base_maxLen_i)].v, &convex, &tmp, &ll, 
    &max_stop_nodeLen, &max_stop_baseLen, stops_threshold, b);
    if(return_flag!=END_TIPS) return;


    get_trio_labs(b, ug, &max);
    ///means this unitig might be at current haplotype
    ///if(max.drop_occ<(max.total*TRIO_DROP_THRES)) return;


    best_tip_i = (*base_maxLen_i);
    best_tip_len = (*base_maxLen);

    for (i = 0; i < nv; i++)
    {
        if(av[i].del) continue;
        if(i==(*base_maxLen_i)) continue;

        b->b.n = 0;
        return_flag = get_unitig(g, ug, av[i].v, &convex, &tmp, &ll, 
        &max_stop_nodeLen, &max_stop_baseLen, stops_threshold, b);

        if(return_flag!=END_TIPS) return;
        ///this tip should be long enough
        if(ll<(TRIO_DROP_LENGTH_THRES*(*base_maxLen))) continue;

        get_trio_labs(b, ug, &cur);

        if(trio_flag == FATHER)
        {
            max_weight = (long long)max.father_occ - (long long)max.drop_occ - (long long)max.mother_occ;
            cur_weight = (long long)cur.father_occ - (long long)cur.drop_occ - (long long)cur.mother_occ;
            ///this unitig is very likly at another haplotype, ignore it
            if((cur.drop_occ+cur.mother_occ)>=(cur.total*TRIO_DROP_THRES)) continue;
        }

        if(trio_flag == MOTHER)
        {
            max_weight = (long long)max.mother_occ - (long long)max.drop_occ - (long long)max.father_occ;
            cur_weight = (long long)cur.mother_occ - (long long)cur.drop_occ - (long long)cur.father_occ;
            ///this unitig is very likly at another haplotype, ignore it
            if((cur.drop_occ+cur.father_occ)>=(cur.total*TRIO_DROP_THRES)) continue;
        }


        if(cur_weight > max_weight)
        {
            max = cur;
            best_tip_i = i;
            best_tip_len = ll;
        }
        else if(cur_weight == max_weight && ll>best_tip_len)
        {
            max = cur;
            best_tip_i = i;
            best_tip_len = ll;
        }
    }

    (*base_maxLen_i) = best_tip_i;
    (*base_maxLen) = best_tip_len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, 0x68(%rsp)
movl 0xe0(%rsp), %eax
addl $-0x3, %eax
cmpl $-0x2, %eax
jb 0xcf73a
movq %r9, %r12
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r13
movq 0xd8(%rsp), %r10
movl 0xd0(%rsp), %r11d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
xorl %eax, %eax
movl %eax, 0x60(%rsp)
movapd %xmm0, 0x30(%rsp)
movl %eax, 0x40(%rsp)
movq $0x0, 0x38(%r10)
movq (%r9), %rax
leaq (%rax,%rax,2), %rax
movl 0x8(%rdx,%rax,8), %edx
leaq 0x80(%rsp), %rax
leaq 0x88(%rsp), %rbx
leaq 0x74(%rsp), %rcx
leaq 0x90(%rsp), %r8
leaq 0x78(%rsp), %r9
pushq %r10
pushq %r11
pushq %rax
pushq %rbx
callq 0x1053a7
addq $0x20, %rsp
cmpl $0x5, %eax
jne 0xcf73a
leaq 0x50(%rsp), %rdx
movq 0xd8(%rsp), %rdi
movq %r14, %rsi
callq 0xcdc34
movl (%r12), %eax
movl %eax, 0x2c(%rsp)
movq 0x68(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xc(%rsp)
testl %ebp, %ebp
je 0xcf726
movl %ebp, %eax
addq $0xc, %r15
xorl %ebx, %ebx
movq $0x0, 0x10(%rsp)
xorl %ebp, %ebp
movq %r12, 0x20(%rsp)
movq %rax, 0x18(%rsp)
cmpl $0x0, (%r15)
js 0xcf716
cmpq (%r12), %rbx
je 0xcf716
movq 0xd8(%rsp), %rax
movq $0x0, 0x38(%rax)
movl -0x4(%r15), %edx
movq %r13, %rdi
movq %r14, %rsi
leaq 0x74(%rsp), %rcx
leaq 0x90(%rsp), %r8
leaq 0x78(%rsp), %r9
pushq %rax
movl 0xd8(%rsp), %eax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
leaq 0xa0(%rsp), %rax
pushq %rax
callq 0x1053a7
addq $0x20, %rsp
cmpl $0x5, %eax
jne 0xcf73a
movq %r13, %r12
movq 0x78(%rsp), %r13
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
movq 0x68(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sdq (%rax), %xmm1
mulsd 0x161f8e(%rip), %xmm1   # 0x2315b0
ucomisd %xmm0, %xmm1
ja 0xcf709
movq 0xd8(%rsp), %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
callq 0xcdc34
movl 0xe0(%rsp), %eax
cmpl $0x2, %eax
je 0xcf673
cmpl $0x1, %eax
movq 0x10(%rsp), %rax
jne 0xcf6bf
movl 0x50(%rsp), %ebp
movl 0x54(%rsp), %eax
movl 0x5c(%rsp), %ecx
addq %rcx, %rax
subq %rax, %rbp
movl 0x30(%rsp), %edi
movl 0x34(%rsp), %eax
jmp 0xcf68d
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ebp
movl 0x5c(%rsp), %ecx
addq %rcx, %rax
subq %rax, %rbp
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %edi
movl 0x3c(%rsp), %ecx
leal (%rax,%rcx), %edx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
addq %rax, %rcx
movl 0x40(%rsp), %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
movq %rdi, %rax
subq %rcx, %rax
mulsd 0x161f3f(%rip), %xmm1   # 0x2315f8
ucomisd %xmm1, %xmm0
jae 0xcf704
cmpq %rbp, %rax
jg 0xcf6e0
movq %rax, 0x10(%rsp)
sete %al
movl 0xc(%rsp), %ecx
cmpq %rcx, %r13
setg %cl
andb %al, %cl
movq %rbp, %rax
cmpb $0x1, %cl
jne 0xcf709
movl 0x40(%rsp), %ecx
movl %ecx, 0x60(%rsp)
movapd 0x30(%rsp), %xmm0
movapd %xmm0, 0x50(%rsp)
movq %rax, 0x10(%rsp)
movl %ebx, 0x2c(%rsp)
movl %r13d, 0xc(%rsp)
jmp 0xcf709
movq %rax, 0x10(%rsp)
movq %r12, %r13
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %rax
incq %rbx
addq $0x18, %r15
cmpq %rbx, %rax
jne 0xcf590
movl 0x2c(%rsp), %eax
movq %rax, (%r12)
movl 0xc(%rsp), %eax
movq 0x68(%rsp), %rcx
movq %rax, (%rcx)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq