// File: /workspace/github_cpp_makefile_star2_O2/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, (%rsp)
movss %xmm1, 0x14(%rsp)
movss %xmm0, 0x10(%rsp)
movq %r9, 0x38(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r15
movq %rdi, %r13
callq 0x84414
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r14
movups %xmm0, 0x8(%r14)
leaq 0x60(%rsp), %r12
movaps %xmm0, (%r12)
andq $0x0, 0x10(%r12)
xorl %ebp, %ebp
movq %r13, %rdi
xorl %esi, %esi
callq 0x781bd
movq %rax, %rbx
movq %r14, %r8
andq $0x0, (%r14)
subq $0x8, %rsp
pushq $0x1
popq %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq 0x10(%rsp), %r14
movq %r14, %rcx
movl 0xd8(%rsp), %r9d
pushq %rax
pushq %r12
movl 0xf0(%rsp), %eax
pushq %rax
callq 0x7b888
addq $0x20, %rsp
leaq 0x40(%rsp), %rax
andq $0x0, (%rax)
leaq 0x30(%rsp), %rax
andq $0x0, (%rax)
movq %r13, %rdi
callq 0x8c8a9
movq %rax, %r12
movq %rbx, %rdi
callq 0x8c99a
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 0x18(%rsp), %xmm1
movss 0x1c(%rsp), %xmm2
movss 0x8(%rsp), %xmm3
pushq $0x0
pushq $0x1
popq %rax
pushq %rax
pushq 0x100(%rsp)
leaq 0x50(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
movl 0x108(%rsp), %eax
pushq %rax
movl 0x108(%rsp), %eax
pushq %rax
movq 0x108(%rsp), %r14
pushq %r14
pushq 0x108(%rsp)
callq 0x8cb4c
addq $0x48, %rsp
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r15, 0x8(%rsp)
movq %r15, %rdx
movq 0x20(%rsp), %rcx
movq 0x10(%rsp), %r8
movq %r14, %r9
leaq 0x48(%rsp), %rax
pushq %rax
movl 0xe8(%rsp), %eax
pushq %rax
movl 0xe8(%rsp), %eax
pushq %rax
callq 0x83df0
addq $0x20, %rsp
leaq 0x28(%rsp), %rax
movq (%rax), %rdi
callq 0x6921d
movq %r12, %rdi
callq 0x68f68
leaq 0x30(%rsp), %r14
movq (%r14), %rax
movl $0x138, %edi            # imm = 0x138
addq 0xa8(%rax), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x9146f
movq (%r14), %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x8f7b0
movq %r14, %rdi
callq 0x8d243
movq %rbx, %rdi
callq 0x6921d
leaq 0x40(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x2220
movl %ebp, %ebp
movq 0x70(%rsp), %rdi
cmpq %rbp, 0x60(%rsp)
jbe 0x917d3
imulq $0x18, %rbp, %rax
movl 0x4(%rdi,%rax), %edx
movl 0x8(%rdi,%rax), %ecx
movq 0x8(%r13), %rax
movq 0x28(%r13), %rsi
movq (%rsi,%rdx,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
imulq $0x18, %rsi, %rdi
imulq $0x18, %rdx, %rdx
leaq (%rax,%rdx), %r8
addq $0xc, %r8
xorl %esi, %esi
cmpq %rsi, %rdi
je 0x917cf
movl (%r8,%rsi), %r9d
testl %r9d, %r9d
js 0x917ba
cmpl %ecx, -0x4(%r8,%rsi)
je 0x917c0
addq $0x18, %rsi
jmp 0x917a5
addq %rdx, %rax
orl $0x80000000, %r9d       # imm = 0x80000000
movl %r9d, 0xc(%rsi,%rax)
incl %ebp
jmp 0x91767
callq 0x2220
movq %r13, %rdi
callq 0x694eb
leaq 0x1c1f71(%rip), %rax    # 0x253758
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 0x15645c(%rip), %xmm0   # 0x1e7cb8
movss 0x156458(%rip), %xmm1   # 0x1e7cbc
movss 0x156448(%rip), %xmm2   # 0x1e7cb4
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 0x86cbc
addq $0x60, %rsp
leaq 0x1c1eb6(%rip), %rax    # 0x253758
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 0x15639a(%rip), %xmm0   # 0x1e7cb8
movss 0x156396(%rip), %xmm1   # 0x1e7cbc
movss 0x156386(%rip), %xmm2   # 0x1e7cb4
callq 0x86cbc
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq