// File: /workspace/github_cpp_makefile_star2_O2/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, 0x58(%rsp)
movl 0xe0(%rsp), %eax
addl $-0x3, %eax
cmpl $-0x2, %eax
jb 0x94032
movq %r9, %r14
movl %ecx, %ebx
movq %rdx, %r15
movq 0xd8(%rsp), %r10
xorpd %xmm0, %xmm0
andl $0x0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
andl $0x0, 0x30(%rsp)
movapd %xmm0, 0x20(%rsp)
andq $0x0, 0x38(%r10)
movl 0xd0(%rsp), %r11d
imulq $0x18, (%r9), %rax
movl 0x8(%rdx,%rax), %edx
leaq 0x80(%rsp), %rax
leaq 0x88(%rsp), %r12
leaq 0x6c(%rsp), %rcx
leaq 0x90(%rsp), %r8
leaq 0x70(%rsp), %r9
movq %rdi, 0x78(%rsp)
movq %rsi, 0x60(%rsp)
pushq %r10
pushq %r11
pushq %rax
pushq %r12
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x5, %eax
jne 0x94032
leaq 0x40(%rsp), %rdx
movq 0xd8(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x92de0
movl (%r14), %eax
movl %eax, 0x1c(%rsp)
movq 0x58(%rsp), %rax
movl (%rax), %eax
movl %ebx, %r13d
addq $0xc, %r15
xorl %ebp, %ebp
movq $0x0, 0x38(%rsp)
xorl %ebx, %ebx
movq %r14, 0x10(%rsp)
movl %eax, %r12d
cmpq %rbp, %r13
je 0x94023
cmpl $0x0, (%r15)
js 0x94017
cmpq (%r14), %rbp
je 0x94017
movl %eax, 0xc(%rsp)
movq 0xd8(%rsp), %rax
andq $0x0, 0x38(%rax)
movl -0x4(%r15), %edx
movq 0x78(%rsp), %rdi
movq 0x60(%rsp), %rsi
leaq 0x6c(%rsp), %rcx
leaq 0x90(%rsp), %r8
leaq 0x70(%rsp), %r9
pushq %rax
movl 0xd8(%rsp), %eax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
leaq 0xa0(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x5, %eax
jne 0x94032
movq 0x70(%rsp), %r14
cvtsi2sd %r14, %xmm0
movq 0x58(%rsp), %rax
cvtsi2sdq (%rax), %xmm1
mulsd 0x14f5ca(%rip), %xmm1   # 0x1e3500
ucomisd %xmm0, %xmm1
ja 0x9400e
movq 0xd8(%rsp), %rdi
movq 0x60(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x92de0
movl 0xe0(%rsp), %eax
cmpl $0x2, %eax
je 0x93f89
cmpl $0x1, %eax
jne 0x93fd1
movl 0x40(%rsp), %edx
movl 0x44(%rsp), %eax
movl 0x4c(%rsp), %ecx
addq %rcx, %rax
subq %rax, %rdx
movq %rdx, 0x38(%rsp)
movl 0x20(%rsp), %ebx
movl 0x24(%rsp), %eax
jmp 0x93fa8
movl 0x40(%rsp), %eax
movl 0x44(%rsp), %edx
movl 0x4c(%rsp), %ecx
addq %rcx, %rax
subq %rax, %rdx
movq %rdx, 0x38(%rsp)
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ebx
movl 0x2c(%rsp), %ecx
leal (%rax,%rcx), %edx
cvtsi2sd %rdx, %xmm0
addq %rax, %rcx
movl 0x30(%rsp), %eax
cvtsi2sd %rax, %xmm1
subq %rcx, %rbx
mulsd 0x14f575(%rip), %xmm1   # 0x1e3540
ucomisd %xmm1, %xmm0
jae 0x9400e
cmpq 0x38(%rsp), %rbx
jle 0x93fdd
movq %rbx, %rax
jmp 0x93fe9
jne 0x9400e
movq 0x38(%rsp), %rax
cmpq %r12, %r14
jle 0x9400e
movl 0x30(%rsp), %ecx
movl %ecx, 0x50(%rsp)
movapd 0x20(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
movq %rax, %rbx
movl %ebp, 0x1c(%rsp)
movl %r14d, %eax
movq 0x10(%rsp), %r14
jmp 0x94017
movq 0x10(%rsp), %r14
movl 0xc(%rsp), %eax
incq %rbp
addq $0x18, %r15
jmp 0x93e9d
movl 0x1c(%rsp), %eax
movq %rax, (%r14)
movq 0x58(%rsp), %rax
movq %r12, (%rax)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq