// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: output_bp_graph_adv(asg_t*, ma_sub_t*, char*, ma_hit_t_alloc*, ma_hit_t_alloc*, long long, float, long long, R_to_U*, float, float, int, int, int, bub_label_t*, ug_opt_t*)
void output_bp_graph_adv(asg_t *sg, ma_sub_t* coverage_cut, char* output_file_name, 
ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, 
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, 
int gap_fuzz, bub_label_t* b_mask_t, ug_opt_t *opt)
{ 
    hic_clean(sg);

    kvec_asg_arc_t_warp new_rtg_edges, d_edges;
    kv_init(new_rtg_edges.a); kv_init(d_edges.a);
    ma_ug_t *ug = NULL;
    ug = ma_ug_gen_primary(sg, PRIMARY_LABLE);

    new_rtg_edges.a.n = 0;
    ma_ug_seq(ug, sg, coverage_cut, sources, &new_rtg_edges, max_hang, min_ovlp, &d_edges, 1);///polish

    new_rtg_edges.a.n = 0;
    hap_cov_t *cov = NULL;
    asg_t *copy_sg = copy_read_graph(sg);
    ma_ug_t *copy_ug = copy_untig_graph(ug);
    /*******************************for debug************************************/
    adjust_utg_by_primary(&copy_ug, copy_sg, TRIO_THRES, sources, reverse_sources, coverage_cut, 
    tipsLen, tip_drop_ratio, stops_threshold, ruIndex, chimeric_rate, drop_ratio, 
    max_hang, min_ovlp, &new_rtg_edges, &cov, b_mask_t, 1, 0/**1**/);
    // adjust_utg_advance(copy_sg, copy_ug, reverse_sources, ruIndex, b_mask_t);
    // get_utg_ovlp(&copy_ug, copy_sg, sources, reverse_sources, coverage_cut, 
    // ruIndex, max_hang, min_ovlp, &new_rtg_edges, b_mask_t, NULL);
    // exit(1);
    /*******************************for debug************************************/
    print_utg(&copy_ug, copy_sg, coverage_cut, output_file_name, sources, ruIndex, max_hang, 
    min_ovlp, &new_rtg_edges);
    ma_ug_destroy(copy_ug);
    asg_destroy(copy_sg);

    // gen_bp_phasing(opt, &(cov->t_ch->k_trans), ug, sg);
    clean_u_trans_t_idx(&(cov->t_ch->k_trans), ug, sg);
    set_trio_flag_by_cov(ug, sg, cov);

    //for debug
    // char* gfa_name = (char*)malloc(strlen(output_file_name)+50);
    // sprintf(gfa_name, "%s.pre.clean_d_utg.noseq.gfa", output_file_name);
    // FILE* output_file = fopen(gfa_name, "w");
    // ma_ug_print(ug, sg, coverage_cut, sources, ruIndex, "utg", output_file);
    // fclose(output_file);
    // free(gfa_name);
    // exit(1);
    //for debug

    
    // print_untig_by_read(copy_ug, "m64011_190830_220126/88867583/ccs", 603738, NULL, NULL, "sb");
    
    ///debug_gfa_space(ug, cov);

    
    // print_r_het(cov, R_INF.trio_flag, "out-1");
    // print_debug_gfa(ug, sg, coverage_cut, output_file_name, sources, ruIndex);

    destory_hap_cov_t(&cov);
    ma_ug_destroy(ug);
    kv_destroy(new_rtg_edges.a);

    asg_arc_t* av = NULL;
    uint32_t v, w, k, i, nv;
    for (i = 0; i < d_edges.a.n; i++)
    {
        v = d_edges.a.a[i].ul>>32; 
        w = d_edges.a.a[i].v;
        av = asg_arc_a(sg, v);
        nv = asg_arc_n(sg, v);
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            if(av[k].v == w) 
            {
                av[k].del = 1;
                break;
            }
        }
    }    
    kv_destroy(d_edges.a);
    asg_cleanup(sg);


    output_trio_unitig_graph(sg, coverage_cut, output_file_name, FATHER, sources, reverse_sources, (asm_opt.max_short_tip*2), 0.15, 3, ruIndex, 
    0.05, 0.9, max_hang, min_ovlp, gap_fuzz, 0, b_mask_t, NULL, NULL, NULL);
    output_trio_unitig_graph(sg, coverage_cut, output_file_name, MOTHER, sources, reverse_sources, (asm_opt.max_short_tip*2), 0.15, 3, ruIndex, 
    0.05, 0.9, max_hang, min_ovlp, gap_fuzz, 0, b_mask_t, NULL, NULL, NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movss %xmm2, 0x8(%rsp)
movss %xmm1, (%rsp)
movss %xmm0, 0x14(%rsp)
movq %r9, 0x38(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, %r14
movq %rdx, 0x18(%rsp)
movq %rsi, %r15
movq %rdi, %r13
callq 0xbfa5a
xorps %xmm0, %xmm0
leaq 0x68(%rsp), %r12
movups %xmm0, 0x8(%r12)
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
xorl %ebp, %ebp
movq %r13, %rdi
xorl %esi, %esi
callq 0xb2a6d
movq %rax, %rbx
andq $0x0, (%r12)
subq $0x8, %rsp
pushq $0x1
popq %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %r12, %r8
movl 0xd8(%rsp), %r9d
pushq %rax
leaq 0x50(%rsp), %rax
pushq %rax
movl 0xf0(%rsp), %eax
pushq %rax
callq 0xb67c4
addq $0x20, %rsp
andq $0x0, (%r12)
leaq 0x30(%rsp), %rax
andq $0x0, (%rax)
movq %r13, %rdi
callq 0xc8dad
movq %rax, %r12
movq %rbx, %rdi
callq 0xc8eab
leaq 0x28(%rsp), %rdi
movq %rax, (%rdi)
subq $0x8, %rsp
movq %r12, %rsi
movq %r14, %rdx
movq 0x28(%rsp), %rcx
movq %r15, %r8
movq 0x40(%rsp), %r9
movss 0x1c(%rsp), %xmm1
movss 0x8(%rsp), %xmm2
movss 0x10(%rsp), %xmm3
pushq $0x0
pushq $0x1
popq %rax
pushq %rax
pushq 0x100(%rsp)
leaq 0x50(%rsp), %rax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
movl 0x108(%rsp), %eax
pushq %rax
movl 0x108(%rsp), %eax
pushq %rax
pushq 0x108(%rsp)
pushq 0x108(%rsp)
callq 0xc906a
addq $0x48, %rsp
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r15, 0x8(%rsp)
movq %r15, %rdx
movq 0x20(%rsp), %rcx
movq %r14, 0x10(%rsp)
movq %r14, %r8
movq 0xd0(%rsp), %r9
leaq 0x70(%rsp), %rax
pushq %rax
movl 0xe8(%rsp), %eax
pushq %rax
movl 0xe8(%rsp), %eax
pushq %rax
callq 0x7f44
addq $0x20, %rsp
leaq 0x28(%rsp), %rax
movq (%rax), %rdi
callq 0xa0761
movq %r12, %rdi
callq 0xa0494
leaq 0x30(%rsp), %r14
movq (%r14), %rax
movl $0x138, %edi            # imm = 0x138
addq 0xa8(%rax), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0xcd2ae
movq (%r14), %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xcb1ba
movq %r14, %rdi
callq 0xc9bdb
movq %rbx, %rdi
callq 0xa0761
leaq 0x68(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x2220
leaq 0x40(%rsp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x9be3
movq 0x50(%rsp), %rcx
imulq $0x18, %rbp, %rdx
movl 0x4(%rcx,%rdx), %esi
movq 0x28(%r13), %rdi
movq (%rdi,%rsi,8), %rsi
testl %esi, %esi
je 0x9bdc
movl 0x8(%rcx,%rdx), %edx
movq 0x8(%r13), %rcx
movq %rsi, %rdi
shrq $0x20, %rdi
movl %esi, %r9d
imulq $0x18, %rdi, %rsi
leaq (%rcx,%rsi), %r8
addq $0xc, %r8
imulq $0x18, %r9, %r9
xorl %edi, %edi
movl (%r8,%rdi), %r10d
testl %r10d, %r10d
js 0x9bbd
cmpl %edx, -0x4(%r8,%rdi)
je 0x9bc8
addq $0x18, %rdi
cmpq %rdi, %r9
jne 0x9bad
jmp 0x9bdc
addq %rsi, %rcx
orl $0x80000000, %r10d      # imm = 0x80000000
movl %r10d, 0xc(%rdi,%rcx)
movq 0x40(%rsp), %rax
incl %ebp
cmpq %rbp, %rax
ja 0x9b70
movq 0x50(%rsp), %rdi
callq 0x2220
movq %r13, %rdi
callq 0xa0a7d
leaq 0x28bb5c(%rip), %rax    # 0x295758
movslq 0x11c(%rax), %rax
addq %rax, %rax
subq $0x60, %rsp
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rsp)
movq 0x148(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movl 0x140(%rsp), %r14d
movl %r14d, 0x28(%rsp)
movl 0x138(%rsp), %ecx
movl %ecx, 0x20(%rsp)
movl 0x130(%rsp), %ecx
movl %ecx, 0x18(%rsp)
movq 0x128(%rsp), %rcx
movq %rcx, 0x10(%rsp)
pushq $0x3
popq %rcx
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
andq $0x0, 0x50(%rsp)
andl $0x0, 0x30(%rsp)
pushq $0x1
popq %rcx
movss 0x22c147(%rip), %xmm0   # 0x235db8
movss 0x22c143(%rip), %xmm1   # 0x235dbc
movss 0x22c133(%rip), %xmm2   # 0x235db4
movq %r13, %rdi
movq 0x60(%rsp), %rbx
movq %rbx, %rsi
movq 0x78(%rsp), %rbp
movq %rbp, %rdx
movq 0x68(%rsp), %r12
movq %r12, %r8
movq 0x80(%rsp), %r15
movq %r15, %r9
callq 0xc30dd
addq $0x60, %rsp
leaq 0x28baa1(%rip), %rax    # 0x295758
movslq 0x11c(%rax), %rax
addq %rax, %rax
subq $0x60, %rsp
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rsp)
movq 0x148(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movl %r14d, 0x28(%rsp)
movl 0x138(%rsp), %ecx
movl %ecx, 0x20(%rsp)
movl 0x130(%rsp), %ecx
movl %ecx, 0x18(%rsp)
andq $0x0, 0x50(%rsp)
movq 0x128(%rsp), %rcx
movq %rcx, 0x10(%rsp)
andl $0x0, 0x30(%rsp)
pushq $0x3
popq %rcx
movq %rcx, 0x8(%rsp)
movq %rax, (%rsp)
pushq $0x2
popq %rcx
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %r12, %r8
movq %r15, %r9
movss 0x22c085(%rip), %xmm0   # 0x235db8
movss 0x22c081(%rip), %xmm1   # 0x235dbc
movss 0x22c071(%rip), %xmm2   # 0x235db4
callq 0xc30dd
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq