// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movss %xmm3, 0x10(%rsp)
movss %xmm2, 0x2c(%rsp)
movss %xmm1, 0x28(%rsp)
movq %r9, %rbx
movq %r8, %rbp
movq %rcx, %r13
movq %rsi, %r15
movq 0x98(%rsp), %r12
movl 0x88(%rsp), %r11d
movl 0x80(%rsp), %r10d
movq 0x78(%rsp), %rcx
movq %rdi, 0x8(%rsp)
movq (%rdi), %rdi
leaq 0x1c23c7(%rip), %rax    # 0x28a758
cmpl $0x0, 0x130(%rax)
setg %al
testq %r12, %r12
setne %sil
orb %al, %sil
movzbl %sil, %eax
subq $0x8, %rsp
movq %r15, %rsi
movq %rdx, 0x20(%rsp)
movq %r13, %r8
movq %rbp, %r9
pushq %rax
pushq %r11
pushq %r10
callq 0xd621b
addq $0x20, %rsp
movq %rax, 0x30(%rsp)
movq 0xa8(%rax), %rdx
testq %rdx, %rdx
je 0xc83e5
movq 0xa0(%rax), %rcx
movq %rcx, 0x10(%rdx)
movq 0x8(%rsp), %r14
movq (%r14), %rsi
movq %rax, 0x20(%rsp)
movq 0xa0(%rax), %r9
movq %r15, %rdi
movq %r13, %rdx
movq 0x78(%rsp), %rcx
movq 0xa0(%rsp), %r8
callq 0xc9692
movq (%r14), %rdi
movq 0x18(%rdi), %rax
movl 0x14(%rax), %ecx
andq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
je 0xc845a
movq 0x20(%rax), %rax
addq $0x4, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %edx, %edx
cmpl $0x0, -0x4(%rax)
js 0xc844d
movb $0x0, (%rax)
movq 0x10(%rdi), %rsi
movl 0x10(%rsi,%rdx), %r8d
movl %r8d, 0x4(%rsi,%rdx)
addq $0x8, %rax
addq $0x28, %rdx
cmpq %rdx, %rcx
jne 0xc8436
movl 0xa8(%rsp), %r14d
subq $0x50, %rsp
movq 0x70(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, (%rsp)
movl $0x0, 0x38(%rsp)
movq %r15, %rsi
movq %r13, %rcx
movq %rbx, %r9
movss 0x78(%rsp), %xmm0
movss 0x7c(%rsp), %xmm2
movss 0x60(%rsp), %xmm3
callq 0xd384c
addq $0x50, %rsp
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xc971c
movq %rbx, %rdi
movq %r15, %rsi
movq 0x90(%rsp), %rdx
callq 0xba760
testq %r12, %r12
je 0xc8529
testl %r14d, %r14d
jne 0xc8529
movq 0x20(%rsp), %rbx
movq 0xa8(%rbx), %r9
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
movq 0x78(%rsp), %r8
callq 0xf0fa9
testq %r12, %r12
je 0xc8510
movq %rbx, (%r12)
jmp 0xc851a
leaq 0x30(%rsp), %rdi
callq 0xc8943
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c2228(%rip), %rcx    # 0x28a758
movl 0x130(%rcx), %eax
testl %eax, %eax
jle 0xc85ef
xorl %r10d, %r10d
cmpl $0x1, %eax
sete %r10b
movq 0x8(%rsp), %r12
movq (%r12), %rdi
movss 0x16c(%rcx), %xmm0
movl 0x138(%rcx), %eax
movq 0x20(%rsp), %r11
cmpq $0x0, 0xa8(%r11)
setne %cl
testl %r14d, %r14d
setne %dl
andb %cl, %dl
movzbl %dl, %ebx
subq $0x8, %rsp
movq %r15, %rsi
movq %rbp, %rdx
movq 0x20(%rsp), %rcx
movq %r13, %r8
movq 0x80(%rsp), %r9
movss 0x18(%rsp), %xmm1
movl 0xb8(%rsp), %r14d
pushq %r14
pushq %rbx
pushq %r11
pushq $0x0
pushq %r10
movl 0xb8(%rsp), %r10d
pushq %r10
movl 0xb8(%rsp), %r10d
pushq %r10
pushq %rax
movq 0xd8(%rsp), %rbx
pushq %rbx
callq 0x12fee
addq $0x50, %rsp
movq %r12, %rdi
callq 0xc971c
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0xba760
leaq 0x1c2169(%rip), %rcx    # 0x28a758
testb $0x1, 0x1(%rcx)
jne 0xc86d0
movq 0x8(%rsp), %r13
movq (%r13), %rdi
subq $0x8, %rsp
movl $0x1, %r10d
xorl %r11d, %r11d
movq %r15, %rsi
movq 0x20(%rsp), %r12
movq %r12, %rdx
movq %rbp, %rcx
movq 0x80(%rsp), %r8
movl 0x88(%rsp), %eax
movq %rax, %r9
movq 0xa8(%rsp), %r14
pushq %r14
pushq $0x0
pushq %r10
pushq %r11
movl 0xb0(%rsp), %ebx
pushq %rbx
callq 0xd674d
addq $0x30, %rsp
movq %r13, %rdi
movq %r15, %rsi
movq 0x90(%rsp), %rdx
callq 0xba760
movq (%r13), %rdi
subq $0x40, %rsp
movq %r14, 0x30(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%rsp)
movl $0x1, %eax
movl %eax, 0x18(%rsp)
xorl %eax, %eax
movl %eax, 0x10(%rsp)
movl %ebx, (%rsp)
movl $0xa, 0x8(%rsp)
movq %r15, %rsi
movq %r12, %rdx
movq %rbp, %rcx
movq 0xb8(%rsp), %r8
movl 0xc0(%rsp), %r9d
callq 0xd7438
addq $0x40, %rsp
movq %r13, %rdi
movq %r15, %rsi
movq 0x90(%rsp), %rdx
callq 0xba760
leaq 0x1c2088(%rip), %rcx    # 0x28a758
movl 0x124(%rcx), %ecx
testl %ecx, %ecx
jle 0xc86f8
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x90(%rsp), %rdx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xd5a1b
movl 0x14(%r15), %eax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
je 0xc8717
movq 0x20(%r15), %rcx
xorl %edx, %edx
movb $0x1, 0x4(%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0xc870a
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdx
movl 0x14(%rdx), %ecx
andq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
je 0xc878b
movq 0x20(%rdx), %rdx
xorl %esi, %esi
cmpl $0x0, (%rdx,%rsi,8)
js 0xc8783
cmpb $0x1, 0x4(%rdx,%rsi,8)
je 0xc8783
movq 0x10(%rax), %rdi
leaq (%rsi,%rsi,4), %r8
cmpl $0x0, 0xc(%rdi,%r8,8)
je 0xc8783
leaq (%rdi,%r8,8), %r8
movl 0x10(%r8), %edi
testq %rdi, %rdi
je 0xc8783
movq 0x18(%r8), %r8
movq 0x20(%r15), %r9
xorl %r10d, %r10d
movq (%r8,%r10,8), %r11
movb 0x4(%rdx,%rsi,8), %bl
shrq $0x21, %r11
movb %bl, 0x4(%r9,%r11,8)
incq %r10
cmpq %r10, %rdi
jne 0xc876a
incq %rsi
cmpq %rcx, %rsi
jne 0xc8735
movq %rbp, 0x10(%rsp)
movl 0x14(%r15), %eax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
je 0xc888f
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x20(%r15), %rsi
cmpb $0x1, 0x4(%rsi,%rcx,8)
jne 0xc8883
leaq (%rcx,%rcx), %rsi
xorl %edi, %edi
leaq (%rdi,%rsi), %r8
movq 0x28(%r15), %r9
movq (%r9,%r8,8), %r10
movq %r10, %r9
andq %rdx, %r9
je 0xc8873
shrq $0x20, %r10
leaq (%r10,%r10,2), %r10
shlq $0x3, %r10
addq 0x8(%r15), %r10
xorl %r11d, %r11d
leaq (%r11,%r11,2), %r14
movl 0xc(%r10,%r14,8), %ebx
testl %ebx, %ebx
js 0xc8867
leaq (%r10,%r14,8), %r13
movq 0x20(%r15), %r12
movl 0x8(%r13), %ebp
movl %ebp, %r14d
shrl %r14d
cmpb $0x1, 0x4(%r12,%r14,8)
je 0xc8867
orl $0x80000000, %ebx       # imm = 0x80000000
movl %ebx, 0xc(%r13)
xorq $0x1, %rbp
movq 0x28(%r15), %rbx
movq (%rbx,%rbp,8), %r14
movq %r14, %rbx
andq %rdx, %rbx
je 0xc8867
movq 0x8(%r15), %r12
shrq $0x20, %r14
shlq $0x3, %rbx
leaq (%rbx,%rbx,2), %rbx
leaq (%r14,%r14,2), %r14
leaq (%r12,%r14,8), %r14
addq $0xc, %r14
xorl %r13d, %r13d
movl -0x4(%r14,%r13), %ebp
xorl %r8d, %ebp
cmpl $0x1, %ebp
jne 0xc885e
orb $-0x80, 0x3(%r14,%r13)
addq $0x18, %r13
cmpq %r13, %rbx
jne 0xc884b
incq %r11
cmpq %r9, %r11
jne 0xc87e7
leaq 0x1(%rdi), %r8
testq %rdi, %rdi
movq %r8, %rdi
je 0xc87bc
incq %rcx
cmpq %rax, %rcx
jne 0xc87a7
leaq 0x1c1ec2(%rip), %rdx    # 0x28a758
cmpl $0xfffffc00, 0x13c(%rdx) # imm = 0xFFFFFC00
movq 0x98(%rsp), %r12
movq 0x10(%rsp), %rbp
jne 0xc88ef
cmpl $0x0, 0x148(%rdx)
xorps %xmm0, %xmm0
cvtsi2sdl 0x144(%rdx), %xmm0
jne 0xc88cb
divsd 0x16129d(%rip), %xmm0   # 0x229b68
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
mulsd 0x1612ad(%rip), %xmm0   # 0x229b88
cvttsd2si %xmm0, %eax
movl %eax, 0x13c(%rdx)
movl $0x7fffffff, 0x140(%rdx) # imm = 0x7FFFFFFF
movl 0x13c(%rdx), %ecx
movl 0x140(%rdx), %r8d
movq 0x1c16dd(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
cmpl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
jne 0xc8929
leaq 0x162bbb(%rip), %rsi    # 0x22b4d1
leaq 0x163fcd(%rip), %rdx    # 0x22c8ea
xorl %eax, %eax
callq 0x22b0
jmp 0xc84df
leaq 0x162b5d(%rip), %rsi    # 0x22b48d
leaq 0x163fb3(%rip), %rdx    # 0x22c8ea
xorl %eax, %eax
callq 0x22b0
jmp 0xc84df