// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: adjust_utg_by_primary(ma_ug_t**, asg_t*, float, ma_hit_t_alloc*, ma_hit_t_alloc*, ma_sub_t*, long long, float, long long, R_to_U*, float, float, int, int, kvec_asg_arc_t_warp*, hap_cov_t**, bub_label_t*, unsigned int, unsigned int)
void adjust_utg_by_primary(ma_ug_t **ug, asg_t* read_g, float drop_rate,
ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, ma_sub_t* coverage_cut, 
long long tipsLen, float tip_drop_ratio, long long stops_threshold, 
R_to_U* ruIndex, float chimeric_rate, float drop_ratio, int max_hang, int min_ovlp,
kvec_asg_arc_t_warp* new_rtg_edges, hap_cov_t **i_cov, bub_label_t* b_mask_t, 
uint32_t collect_p_trans, uint32_t collect_p_trans_f)
{
    asg_t* nsg = (*ug)->g;
    uint32_t v, n_vtx = nsg->n_seq, k, rId, just_contain;
    ma_utg_t* u = NULL;
    hap_cov_t *cov = init_hap_cov_t(*ug, read_g, sources, ruIndex, reverse_sources, 
                            coverage_cut, max_hang, min_ovlp, (asm_opt.purge_level_primary>0||i_cov)?1:0);
    if(cov->t_ch) cov->t_ch->ir_het = cov->is_r_het;
    adjust_utg_advance(read_g, (*ug), reverse_sources, ruIndex, b_mask_t, cov->is_r_het);

    nsg = (*ug)->g;
    n_vtx = nsg->n_seq;
    for (v = 0; v < n_vtx; ++v) 
    {
        if(nsg->seq[v].del) continue;
        nsg->seq[v].c = PRIMARY_LABLE;
        EvaluateLen((*ug)->u, v) = (*ug)->u.a[v].n;
    }

    clean_primary_untig_graph(*ug, read_g, sources, reverse_sources, coverage_cut, tipsLen, tip_drop_ratio, 
    stops_threshold, ruIndex, NULL, NULL, 0, 0, 0, chimeric_rate, 0, 0, drop_ratio, cov);
    delete_useless_nodes(ug);
    renew_utg(ug, read_g, new_rtg_edges);
    if(i_cov && collect_p_trans == 0) goto skip_purge;
    if(asm_opt.purge_level_primary > 0)
    {
        // print_debug_gfa(read_g, *ug, coverage_cut, "debug_purge", sources, ruIndex, asm_opt.max_hang_Len, asm_opt.min_overlap_Len);
        just_contain = 0;
        if(asm_opt.purge_level_primary == 1) just_contain = 1;
        purge_dups(*ug, read_g, coverage_cut, sources, reverse_sources, ruIndex, new_rtg_edges, 
        asm_opt.purge_simi_thres, asm_opt.purge_overlap_len, max_hang, min_ovlp, drop_ratio, 
        just_contain, 0, cov, !!(cov->t_ch&&collect_p_trans), collect_p_trans_f);
        delete_useless_nodes(ug);
        renew_utg(ug, read_g, new_rtg_edges);
    }

    if (!(asm_opt.flag & HA_F_BAN_POST_JOIN))
    {
        rescue_missing_overlaps_aggressive(*ug, read_g, sources, coverage_cut, ruIndex, max_hang,
        min_ovlp, 0, 1, NULL, b_mask_t);
        renew_utg(ug, read_g, new_rtg_edges);
        rescue_contained_reads_aggressive(*ug, read_g, sources, coverage_cut, ruIndex, max_hang, 
        min_ovlp, 10, 0, 1, NULL, NULL, b_mask_t);
        renew_utg(ug, read_g, new_rtg_edges);
    }


    if(asm_opt.max_contig_tip > 0) {
        discard_small_ctg(ug, read_g, new_rtg_edges, asm_opt.max_contig_tip, 0, 1); 
    }


    n_vtx = read_g->n_seq;
    for (v = 0; v < n_vtx; v++)
    {
        read_g->seq[v].c = ALTER_LABLE;
    }
    
    
    nsg = (*ug)->g;
    n_vtx = nsg->n_seq;
    for (v = 0; v < n_vtx; ++v) 
    {
        if(nsg->seq[v].del) continue;
        if(nsg->seq[v].c == ALTER_LABLE) continue;
        u = &((*ug)->u.a[v]);
        if(u->m == 0) continue;
        for (k = 0; k < u->n; k++)
        {
            rId = u->a[k]>>33;
            read_g->seq[rId].c = nsg->seq[v].c;
        }
    }

    n_vtx = read_g->n_seq;
    for (v = 0; v < n_vtx; v++)
    {
        if(read_g->seq[v].c == ALTER_LABLE)
        {
            asg_seq_drop(read_g, v);
        }
    }

    if(asm_opt.recover_atg_cov_min == -1024)
    {
        asm_opt.recover_atg_cov_max = (asm_opt.hom_global_coverage_set?
            (asm_opt.hom_global_coverage):(((double)asm_opt.hom_global_coverage)/((double)HOM_PEAK_RATE)));
        asm_opt.recover_atg_cov_min = asm_opt.recover_atg_cov_max * 0.85;
        asm_opt.recover_atg_cov_max = INT32_MAX;
    }

    if(asm_opt.recover_atg_cov_max != INT32_MAX)
    {
        fprintf(stderr, "[M::%s] primary contig coverage range: [%d, %d]\n", 
        __func__, asm_opt.recover_atg_cov_min, asm_opt.recover_atg_cov_max);
    }
    else
    {
        fprintf(stderr, "[M::%s] primary contig coverage range: [%d, infinity]\n", 
        __func__, asm_opt.recover_atg_cov_min);
    }

    skip_purge:
    recover_utg_by_coverage(ug, read_g, coverage_cut, sources, ruIndex, cov->t_ch);
    if(i_cov)
    {
        (*i_cov) = cov;
    } 
    else
    {
        destory_hap_cov_t(&cov);
    }     
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movss %xmm3, 0x4(%rsp)
movss %xmm2, 0x1c(%rsp)
movss %xmm1, 0x18(%rsp)
movq %r9, %rbx
movq %r8, %r9
movq %rcx, %r13
movq %rsi, %r15
movq %rdi, %r12
movl 0x78(%rsp), %r11d
movl 0x70(%rsp), %r10d
movq 0x68(%rsp), %rcx
movq (%rdi), %rdi
leaq 0x1c6bc4(%rip), %rax    # 0x253758
cmpl $0x0, 0x130(%rax)
setg %al
cmpq $0x0, 0x88(%rsp)
setne %sil
orb %al, %sil
movzbl %sil, %eax
subq $0x8, %rsp
movq %r15, %rsi
movq %rdx, 0x10(%rsp)
movq %r13, %r8
movq %r9, 0x18(%rsp)
pushq %rax
pushq %r11
pushq %r10
callq 0x9a2a3
addq $0x20, %rsp
movq %rax, %rbp
movq %rax, 0x20(%rsp)
movq 0xa0(%rax), %r9
movq 0xa8(%rax), %rax
testq %rax, %rax
je 0x8cbf3
movq %r9, 0x10(%rax)
movq 0x60(%rsp), %r14
movq (%r12), %rsi
movq %r15, %rdi
movq %r13, %rdx
movq 0x68(%rsp), %rcx
movq 0x90(%rsp), %r8
callq 0x8e9c7
movq (%r12), %rdi
movq 0x18(%rdi), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
pushq $0x2
popq %rdx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x8cc56
movq 0x20(%rax), %r8
cmpl $0x0, (%r8,%rsi,8)
js 0x8cc4d
movb $0x0, 0x4(%r8,%rsi,8)
movq 0x10(%rdi), %r8
movl (%r8,%rdx,8), %r9d
movl %r9d, -0xc(%r8,%rdx,8)
incq %rsi
addq $0x5, %rdx
jmp 0x8cc2a
subq $0x50, %rsp
movq %rbp, 0x40(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x8(%rsp)
andl $0x0, 0x38(%rsp)
movq %r14, (%rsp)
movq %r15, %rsi
movq %r13, %rcx
movq %rbx, %r9
movss 0x68(%rsp), %xmm0
movss 0x6c(%rsp), %xmm2
movss 0x54(%rsp), %xmm3
callq 0x9814b
addq $0x50, %rsp
movq %r12, %rdi
callq 0x8ea44
movq %r12, %rdi
movq %r15, %rsi
movq 0x80(%rsp), %rdx
callq 0x80328
movq 0x88(%rsp), %rbx
testq %rbx, %rbx
movl 0x98(%rsp), %edx
je 0x8cd10
testl %edx, %edx
jne 0x8cd10
movq 0xa8(%rbp), %r9
movq %r12, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq 0x68(%rsp), %r8
callq 0xb3955
testq %rbx, %rbx
je 0x8ccf7
movq %rbp, (%rbx)
jmp 0x8cd01
leaq 0x20(%rsp), %rdi
callq 0x8d243
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c6a41(%rip), %rbx    # 0x253758
movl 0x130(%rbx), %eax
testl %eax, %eax
jle 0x8cdc5
xorl %r10d, %r10d
cmpl $0x1, %eax
sete %r10b
movq (%r12), %rdi
movss 0x16c(%rbx), %xmm0
movl 0x138(%rbx), %eax
cmpq $0x0, 0xa8(%rbp)
setne %cl
testl %edx, %edx
setne %dl
andb %cl, %dl
movzbl %dl, %r11d
subq $0x8, %rsp
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %r13, %r8
movq 0x70(%rsp), %r9
movss 0xc(%rsp), %xmm1
movl 0xa8(%rsp), %r14d
pushq %r14
pushq %r11
pushq %rbp
pushq $0x0
pushq %r10
movl 0xa8(%rsp), %r10d
pushq %r10
movl 0xa8(%rsp), %r10d
pushq %r10
pushq %rax
movq 0xc8(%rsp), %r14
pushq %r14
callq 0xd3406
addq $0x50, %rsp
movq %r12, %rdi
callq 0x8ea44
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x80328
testb $0x1, 0x1(%rbx)
jne 0x8ce9f
pushq $0x1
popq %r10
movq (%r12), %rdi
subq $0x8, %rsp
movq %r15, %rsi
movq 0x10(%rsp), %r13
movq %r13, %rdx
movq 0x18(%rsp), %r14
movq %r14, %rcx
movq 0x70(%rsp), %r8
movl 0x78(%rsp), %eax
movq %rax, %r9
movq 0x98(%rsp), %rax
pushq %rax
pushq $0x0
pushq %r10
pushq $0x0
movl 0xa0(%rsp), %ebx
pushq %rbx
callq 0x9a6fd
addq $0x30, %rsp
movq %r12, %rdi
movq %r15, %rsi
movq 0x80(%rsp), %rdx
callq 0x80328
movq (%r12), %rdi
subq $0x40, %rsp
movq 0xd0(%rsp), %rax
movq %rax, 0x30(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x18(%rsp)
andl $0x0, 0x10(%rsp)
movl %ebx, (%rsp)
movl $0xa, 0x8(%rsp)
movq %r15, %rsi
movq %r13, %rdx
movq %r14, %rcx
leaq 0x1c68e5(%rip), %rbx    # 0x253758
movq 0xa8(%rsp), %r8
movl 0xb0(%rsp), %r9d
callq 0x9b10e
addq $0x40, %rsp
movq %r12, %rdi
movq %r15, %rsi
movq 0x80(%rsp), %rdx
callq 0x80328
movl 0x124(%rbx), %ecx
testl %ecx, %ecx
jle 0x8cec3
pushq $0x1
popq %r9
movq %r12, %rdi
movq %r15, %rsi
movq 0x80(%rsp), %rdx
xorl %r8d, %r8d
callq 0x99b8b
movl 0x14(%r15), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x8cee1
movq 0x20(%r15), %rdx
movb $0x1, 0x4(%rdx,%rcx,8)
incq %rcx
jmp 0x8cece
movq (%r12), %rax
movq 0x18(%rax), %rcx
movl 0x14(%rcx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %esi, %esi
cmpq %rdx, %rsi
je 0x8cf4c
movq 0x20(%rcx), %rdi
cmpl $0x0, (%rdi,%rsi,8)
js 0x8cf47
movb 0x4(%rdi,%rsi,8), %dil
cmpb $0x1, %dil
je 0x8cf47
movq 0x10(%rax), %r8
imulq $0x28, %rsi, %r9
cmpl $0x0, 0xc(%r8,%r9)
je 0x8cf47
addq %r9, %r8
movl 0x10(%r8), %r9d
xorl %r10d, %r10d
cmpq %r10, %r9
je 0x8cf47
movq 0x18(%r8), %r11
movq (%r11,%r10,8), %r11
movq 0x20(%r15), %rbx
shrq $0x21, %r11
movb %dil, 0x4(%rbx,%r11,8)
incq %r10
jmp 0x8cf28
incq %rsi
jmp 0x8cef4
movl 0x14(%r15), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x8cf78
movq 0x20(%r15), %rax
cmpb $0x1, 0x4(%rax,%rbx,8)
jne 0x8cf73
movq %r15, %rdi
movl %ebx, %esi
callq 0x80870
incq %rbx
jmp 0x8cf59
leaq 0x1c67d9(%rip), %rax    # 0x253758
movl 0x13c(%rax), %ecx
cmpl $0xfffffc00, %ecx       # imm = 0xFFFFFC00
jne 0x8cfd4
cmpl $0x0, 0x148(%rax)
cvtsi2sdl 0x144(%rax), %xmm0
movq 0x88(%rsp), %rbx
jne 0x8cfae
divsd 0x15acb2(%rip), %xmm0   # 0x1e7c60
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
mulsd 0x15acc2(%rip), %xmm0   # 0x1e7c80
cvttsd2si %xmm0, %ecx
movl %ecx, 0x13c(%rax)
movl $0x7fffffff, 0x140(%rax) # imm = 0x7FFFFFFF
jmp 0x8cfec
movl 0x140(%rax), %r8d
cmpl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x88(%rsp), %rbx
jne 0x8d010
movq 0x1c5fed(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x15c5c4(%rip), %rsi    # 0x1e95c1
leaq 0x15d9d6(%rip), %rdx    # 0x1ea9da
xorl %eax, %eax
callq 0x22b0
jmp 0x8cccc
movq 0x1c5fc9(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x15c55c(%rip), %rsi    # 0x1e957d
leaq 0x15d9b2(%rip), %rdx    # 0x1ea9da
xorl %eax, %eax
callq 0x22b0
jmp 0x8cccc