// File: /workspace/github_cpp_makefile_star2_O1/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, %rbp
movq %rdx, 0x18(%rsp)
movq %rsi, %r14
movq %rdi, %r13
callq 0xbeb19
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %r12
movaps %xmm0, (%r12)
andq $0x0, 0x10(%r12)
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
xorl %r15d, %r15d
movq %r13, %rdi
xorl %esi, %esi
callq 0xb1a83
movq %rax, %rbx
andq $0x0, (%r12)
subq $0x8, %rsp
pushq $0x1
popq %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbp, %rcx
movq %r12, %r8
movl 0xd8(%rsp), %r9d
pushq %rax
leaq 0x50(%rsp), %rax
pushq %rax
movl 0xf0(%rsp), %eax
pushq %rax
callq 0xb5778
addq $0x20, %rsp
andq $0x0, (%r12)
leaq 0x30(%rsp), %rax
andq $0x0, (%rax)
movq %r13, %rdi
callq 0xc802c
movq %rax, %r12
movq %rbx, %rdi
callq 0xc815c
leaq 0x28(%rsp), %rdi
movq %rax, (%rdi)
subq $0x8, %rsp
movq %r12, %rsi
movq %rbp, %rdx
movq 0x28(%rsp), %rcx
movq %r14, %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 0x88(%rsp), %rax
pushq %rax
movl 0x108(%rsp), %eax
pushq %rax
movl 0x108(%rsp), %eax
pushq %rax
pushq 0x108(%rsp)
pushq 0x108(%rsp)
callq 0xc8339
addq $0x48, %rsp
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r14, 0x8(%rsp)
movq %r14, %rdx
movq 0x20(%rsp), %rcx
movq %rbp, 0x10(%rsp)
movq %rbp, %r8
movq 0xd0(%rsp), %r9
leaq 0x68(%rsp), %r14
pushq %r14
movl 0xe8(%rsp), %eax
pushq %rax
movl 0xe8(%rsp), %eax
movq %rax, %rbp
pushq %rax
callq 0x7e72
addq $0x20, %rsp
leaq 0x28(%rsp), %rax
movq (%rax), %rdi
callq 0xa0004
movq %r12, %rdi
callq 0x9fcdb
leaq 0x30(%rsp), %r12
movq (%r12), %rax
movl $0x138, %edi            # imm = 0x138
addq 0xa8(%rax), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0xcbfbf
movq (%r12), %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc9ee2
movq %r12, %rdi
callq 0xc8943
movq %rbx, %rdi
callq 0xa0004
movq 0x10(%r14), %rdi
callq 0x2220
leaq 0x40(%rsp), %rax
cmpq %r15, (%rax)
je 0x9c17
movq 0x50(%rsp), %rax
imulq $0x18, %r15, %rcx
movl 0x4(%rax,%rcx), %edx
movq 0x28(%r13), %rsi
movq (%rsi,%rdx,8), %rdx
testl %edx, %edx
je 0x9c0d
movl 0x8(%rax,%rcx), %ecx
movq 0x8(%r13), %rax
movq %rdx, %rsi
shrq $0x20, %rsi
movl %edx, %r8d
imulq $0x18, %rsi, %rdx
leaq (%rax,%rdx), %rdi
addq $0xc, %rdi
imulq $0x18, %r8, %r8
xorl %esi, %esi
movl (%rdi,%rsi), %r9d
testl %r9d, %r9d
js 0x9bf3
cmpl %ecx, -0x4(%rdi,%rsi)
je 0x9bfe
addq $0x18, %rsi
cmpq %rsi, %r8
jne 0x9be4
jmp 0x9c0d
addq %rdx, %rax
orl $0x80000000, %r9d       # imm = 0x80000000
movl %r9d, 0xc(%rsi,%rax)
incl %r15d
cmpq %r15, 0x40(%rsp)
ja 0x9ba7
movq 0x50(%rsp), %rdi
callq 0x2220
movq %r13, %rdi
callq 0xa02fa
leaq 0x280b28(%rip), %rax    # 0x28a758
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 %ebp, 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 0x21ff22(%rip), %xmm0   # 0x229bc0
movss 0x21ff1e(%rip), %xmm1   # 0x229bc4
movss 0x21ff0e(%rip), %xmm2   # 0x229bbc
movq %r13, %rdi
movq 0x60(%rsp), %rbp
movq %rbp, %rsi
movq 0x78(%rsp), %r12
movq %r12, %rdx
movq 0x68(%rsp), %rbx
movq %rbx, %r8
movq 0x80(%rsp), %r15
movq %r15, %r9
callq 0xc21cc
addq $0x60, %rsp
leaq 0x280a74(%rip), %rax    # 0x28a758
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 %rbp, %rsi
movq %r12, %rdx
movq %rbx, %r8
movq %r15, %r9
movss 0x21fe60(%rip), %xmm0   # 0x229bc0
movss 0x21fe5c(%rip), %xmm1   # 0x229bc4
movss 0x21fe4c(%rip), %xmm2   # 0x229bbc
callq 0xc21cc
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq