// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: output_hic_graph_mmhap(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, long long, bub_label_t*, ug_opt_t*)
void output_hic_graph_mmhap(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, long long gap_fuzz, bub_label_t* b_mask_t, ug_opt_t *opt)
{ 
    hic_clean(sg);
    mmhap_t *rh = NULL;

    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 = 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
    
    hap_cov_t *cov = NULL;
    trans_chain* t_ch = NULL;
    t_ch = load_trans_ovlp(output_file_name, ug);
    // if((asm_opt.flag & HA_F_VERBOSE_GFA)) t_ch = load_hc_trans(output_file_name);

    if(!t_ch) {
        new_rtg_edges.a.n = 0;
        asg_t *copy_sg = copy_read_graph(sg);
        ma_ug_t *copy_ug = copy_untig_graph(ug);    
        ///asm_opt.purge_overlap_len = asm_opt.purge_overlap_len_hic;
        ///asm_opt.purge_simi_thres = asm_opt.purge_simi_rate_hic;
        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);
        print_utg(&copy_ug, copy_sg, coverage_cut, output_file_name, sources, ruIndex, max_hang, 
        min_ovlp, &new_rtg_edges);

        if(asm_opt.is_alt) {
            output_contig_graph_alternative(copy_sg, coverage_cut, output_file_name, sources, ruIndex, max_hang, 
            min_ovlp);
        }
        ma_ug_destroy(copy_ug);
        asg_destroy(copy_sg);
        // clean_u_trans_t_idx(&(cov->t_ch->k_trans), ug, sg);


        new_rtg_edges.a.n = 0;
        ma_ug_print_bed(ug, sg, &R_INF, coverage_cut, sources, &new_rtg_edges, 
                max_hang, min_ovlp, asm_opt.hic_inconsist_rate, NULL, NULL, cov->t_ch);

        refine_hic_trans_mmhap(opt, &(cov->t_ch->k_trans), sg, ug);
        dump_trans_ovlp(cov->t_ch, output_file_name, ug);
        // if((asm_opt.flag & HA_F_VERBOSE_GFA)) write_trans_chain(cov->t_ch, output_file_name);
    }

    // dbg_prt_utg_trans(&(cov?cov->t_ch->k_trans:t_ch->k_trans), ug, "pre");
    clean_u_trans_t_idx_filter_mmhap_adv(&(cov?cov->t_ch->k_trans:t_ch->k_trans), ug, sg, opt->sources, NULL);
    // dbg_prt_utg_trans(&(cov?cov->t_ch->k_trans:t_ch->k_trans), ug, "after");

    // refine_hic_trans_mmhap(opt, &(cov?cov->t_ch->k_trans:t_ch->k_trans), sg, ug);
    ///for debug
    // dbg_prt_utg_trans(&(cov?cov->t_ch->k_trans:t_ch->k_trans), ug, "dd");
    // char* gfa_name = (char*)malloc(strlen(output_file_name)+50); FILE* output_file = NULL;

    // sprintf(gfa_name, "%s.pre.clean_d_utg.noseq.gfa", output_file_name);
    // output_file = fopen(gfa_name, "w");
    // ma_ug_print_simple(ug, sg, coverage_cut, sources, ruIndex, "utg", output_file);
    // fclose(output_file);

    // sprintf(gfa_name, "%s.pre.clean_d_utg.gfa", output_file_name);
    // 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


    hic_analysis(ug, sg, cov?cov->t_ch:t_ch, opt, &rh, NULL);


    if(cov) destory_hap_cov_t(&cov);
    if(t_ch) destory_trans_chain(&t_ch);


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

    // 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);

    // dbg_prt_trio_mmhap_label(ug, rh, output_file_name);

    output_trio_mmhap(sg, coverage_cut, output_file_name, sources, reverse_sources, tipsLen, tip_drop_ratio, 
    stops_threshold, ruIndex, chimeric_rate, drop_ratio, max_hang, min_ovlp, gap_fuzz, b_mask_t, opt, ug, rh, asm_opt.polyploidy);

    ma_ug_destroy(ug);
    free(rh->a.a); free(rh->h.a); free(rh);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movss %xmm2, 0x24(%rsp)
movss %xmm1, 0x20(%rsp)
movss %xmm0, 0x1c(%rsp)
movq %r9, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, %r13
movq %rdx, 0x8(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
callq 0xbeb19
xorl %r12d, %r12d
movq %r12, 0x10(%rsp)
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %r15
movaps %xmm0, (%r15)
movq %r12, 0x10(%r15)
leaq 0x60(%rsp), %rax
movaps %xmm0, (%rax)
movq %r12, 0x10(%rax)
movq %rbp, %rdi
xorl %esi, %esi
callq 0xb1a83
movq %rax, %rbx
movq %r12, (%r15)
subq $0x8, %rsp
movq %rax, %rdi
movq %rbp, %rsi
movq %r14, 0x48(%rsp)
movq %r14, %rdx
movl 0x100(%rsp), %r14d
movq %r13, 0x38(%rsp)
movq %r13, %rcx
movq %r15, %r13
movq %r15, %r8
movl 0xf8(%rsp), %r9d
pushq $0x1
leaq 0x70(%rsp), %rax
pushq %rax
pushq %r14
callq 0xb5778
addq $0x20, %rsp
movq %r12, (%rsp)
movq %r12, 0x28(%rsp)
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0xc8a3c
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0xc8ffb
movq %r14, %r13
movq (%rsp), %rax
movl $0xa8, %r14d
leaq (%rax,%r14), %rcx
testq %rax, %rax
leaq 0x28(%rsp), %r15
cmoveq %r15, %rcx
movl $0x138, %edi            # imm = 0x138
addq (%rcx), %rdi
movq 0x110(%rsp), %r12
movq 0x8(%r12), %rcx
movq %rbx, %rsi
movq %rbp, %rdx
xorl %r8d, %r8d
callq 0xc72f9
movq (%rsp), %rax
addq %rax, %r14
testq %rax, %rax
cmoveq %r15, %r14
movq (%r14), %rdx
leaq 0x10(%rsp), %r8
movq %rbx, %r14
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rcx
xorl %r9d, %r9d
callq 0x15cba4
cmpq $0x0, (%rsp)
je 0xc8e60
movq %rsp, %rdi
callq 0xc8943
cmpq $0x0, 0x28(%rsp)
movq 0x30(%rsp), %r15
movq 0x40(%rsp), %rbx
je 0xc8e7c
leaq 0x28(%rsp), %rdi
callq 0xbe6d4
movq 0x100(%rsp), %r12
movq 0x90(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0x60(%rsp)
je 0xc8f11
xorl %eax, %eax
movq 0x70(%rsp), %rcx
leaq (%rax,%rax,2), %rdx
movl 0x4(%rcx,%rdx,8), %esi
movq 0x28(%rbp), %rdi
movq (%rdi,%rsi,8), %rsi
testl %esi, %esi
je 0xc8f08
movl 0x8(%rcx,%rdx,8), %edx
movq 0x8(%rbp), %rcx
movq %rsi, %rdi
shrq $0x20, %rdi
movl %esi, %r8d
leaq (%rdi,%rdi,2), %rsi
leaq (%rcx,%rsi,8), %rdi
addq $0xc, %rdi
shlq $0x3, %r8
leaq (%r8,%r8,2), %r9
xorl %r8d, %r8d
movl (%rdi,%r8), %r10d
testl %r10d, %r10d
js 0xc8eed
cmpl %edx, -0x4(%rdi,%r8)
je 0xc8ef8
addq $0x18, %r8
cmpq %r8, %r9
jne 0xc8edd
jmp 0xc8f08
leaq (%rcx,%rsi,8), %rcx
orl $0x80000000, %r10d      # imm = 0x80000000
movl %r10d, 0xc(%r8,%rcx)
incl %eax
cmpq %rax, 0x60(%rsp)
ja 0xc8e9b
movq 0x70(%rsp), %rdi
callq 0x2220
movq %rbp, %rdi
callq 0xa02fa
movq 0x10(%rsp), %rax
leaq 0x1c1829(%rip), %rcx    # 0x28a758
movl 0x1bc(%rcx), %ecx
subq $0x50, %rsp
movl %ecx, 0x48(%rsp)
movq %rax, 0x40(%rsp)
movq %r14, 0x38(%rsp)
movq 0x158(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r12, 0x20(%rsp)
movl %r13d, 0x18(%rsp)
movl 0x140(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x130(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x58(%rsp), %rdx
movq %r15, %rcx
movq 0x98(%rsp), %r8
movq 0xa0(%rsp), %r9
movss 0x6c(%rsp), %xmm0
movss 0x70(%rsp), %xmm1
movss 0x74(%rsp), %xmm2
callq 0xc8b23
addq $0x50, %rsp
movq %r14, %rdi
callq 0xa0004
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x2220
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, 0x80(%rsp)
movq %rbp, %rdi
callq 0xc802c
movq %rax, %r15
movq %rbx, %rdi
callq 0xc815c
leaq 0x58(%rsp), %rdi
movq %rax, (%rdi)
subq $0x8, %rsp
leaq 0x8(%rsp), %rax
movq %r15, %rsi
movq 0x38(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq 0x48(%rsp), %r12
movq %r12, %r8
movq 0x58(%rsp), %r9
movss 0x24(%rsp), %xmm1
movss 0x28(%rsp), %xmm2
movss 0x2c(%rsp), %xmm3
pushq $0x0
pushq $0x1
pushq 0x120(%rsp)
pushq %rax
pushq %r13
pushq %r14
movl 0x128(%rsp), %eax
pushq %rax
movq 0x128(%rsp), %r14
pushq %r14
pushq 0x128(%rsp)
callq 0xc8339
addq $0x48, %rsp
leaq 0x60(%rsp), %rdi
movq %r15, 0x40(%rsp)
movq %r15, %rsi
movq 0x38(%rsp), %r15
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movq %r15, %r8
movq %r14, %r9
pushq %r13
movl 0x108(%rsp), %r13d
pushq %r13
movl 0x108(%rsp), %eax
pushq %rax
callq 0x7e72
addq $0x20, %rsp
leaq 0x1c168b(%rip), %r14    # 0x28a758
cmpl $0x0, 0x1e4(%r14)
jne 0xc9193
movq 0x58(%rsp), %rdi
callq 0xa0004
movq 0x38(%rsp), %rdi
callq 0x9fcdb
leaq 0x80(%rsp), %r9
movq $0x0, (%r9)
movl 0x154(%r14), %eax
movq (%rsp), %rcx
movq 0xa8(%rcx), %rcx
subq $0x30, %rsp
movq %rcx, 0x28(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movl %r13d, 0x8(%rsp)
movl 0x120(%rsp), %eax
movl %eax, (%rsp)
leaq 0x1c20f5(%rip), %rdx    # 0x28b230
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rcx
movq %r15, %r8
callq 0xb7f72
addq $0x30, %rsp
movq (%rsp), %rax
movl $0x138, %esi            # imm = 0x138
addq 0xa8(%rax), %rsi
movq 0x110(%rsp), %rdi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0xc7a45
movq (%rsp), %rax
movq 0xa8(%rax), %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0xc89bd
movq %r13, %r14
jmp 0xc8de7
subq $0x8, %rsp
movq 0x40(%rsp), %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
movq %r15, %rcx
movq 0xf0(%rsp), %r8
movl 0xf8(%rsp), %r9d
pushq %r13
callq 0x88b8
addq $0x10, %rsp
jmp 0xc90db