// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: output_hic_graph(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(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);
    kvec_pe_hit *rhits = NULL;
    ma_ug_t *ug_fa = NULL, *ug_mo = 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 = 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
    
    hap_cov_t *cov = NULL;
    trans_chain* t_ch = NULL;
    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);

        if((asm_opt.flag & HA_F_VERBOSE_GFA)) write_trans_chain(cov->t_ch, output_file_name);
    }

    refine_hic_trans(opt, &(cov?cov->t_ch->k_trans:t_ch->k_trans), sg, ug);
    ///for debug
    // 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, NULL, asm_opt.scffold?&rhits:NULL);


    if(!rhits && cov) destory_hap_cov_t(&cov);
    if(!rhits && 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);

    // reduce_hamming_error_adv(NULL, sg, sources, coverage_cut, max_hang, min_ovlp, gap_fuzz, opt->ruIndex, NULL);

    // ug_fa = 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, rhits?1:0, b_mask_t, NULL, NULL, NULL);
    // ug_mo = 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, rhits?1:0, b_mask_t, NULL, NULL, NULL);


    output_trio_graph_joint(sg, coverage_cut, output_file_name, sources, reverse_sources, (asm_opt.max_short_tip*2), 0.15, 3, ruIndex, 
    0.05, 0.9, max_hang, min_ovlp, gap_fuzz, b_mask_t, rhits?(&ug_fa):NULL, rhits?(&ug_mo):NULL, opt);
    if(rhits)
    {
        ha_aware_order(rhits, sg, ug_fa, ug_mo, cov?&(cov->t_ch->k_trans):&(t_ch->k_trans), opt, 3);
        kv_destroy(rhits->a); kv_destroy(rhits->idx); kv_destroy(rhits->occ); free(rhits);
        if(cov) destory_hap_cov_t(&cov);
        if(t_ch) destory_trans_chain(&t_ch);
        ma_ug_destroy(ug_fa); ma_ug_destroy(ug_mo);
    } 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movss %xmm2, 0x4c(%rsp)
movss %xmm1, 0x48(%rsp)
movss %xmm0, 0x44(%rsp)
movq %r9, 0x68(%rsp)
movq %r8, 0x58(%rsp)
movq %rcx, %r13
movq %rdx, 0x28(%rsp)
movq %rsi, %r12
movq %rdi, %r14
callq 0xbfa5a
xorl %ebx, %ebx
movq %rbx, 0x8(%rsp)
movq %rbx, 0x38(%rsp)
movq %rbx, 0x30(%rsp)
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %r15
movups %xmm0, 0x8(%r15)
leaq 0x90(%rsp), %rbp
movaps %xmm0, (%rbp)
movq %rbx, 0x10(%rbp)
movq %r14, %rdi
xorl %esi, %esi
callq 0xb2a6d
movq %rbx, (%r15)
subq $0x8, %rsp
movq %rax, 0x28(%rsp)
movq %rax, %rdi
movq %r14, %rsi
movq %r12, 0x58(%rsp)
movq %r12, %rdx
movq %r13, 0x20(%rsp)
movq %r13, %rcx
movq %r15, %r8
movl 0x108(%rsp), %r9d
pushq $0x1
pushq %rbp
movl 0x120(%rsp), %eax
pushq %rax
callq 0xb67c4
addq $0x20, %rsp
movq %rbx, (%rsp)
movq %rbx, 0x10(%rsp)
leaq 0x1cce3d(%rip), %rax    # 0x295758
testb $0x4, (%rax)
je 0xc8938
movq 0x28(%rsp), %rdi
callq 0xbf9e8
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0xc8ae5
movq $0x0, 0x70(%rsp)
movq %r14, %rdi
callq 0xc8dad
movq %rax, %rbp
movq 0x20(%rsp), %rdi
callq 0xc8eab
movq 0x18(%rsp), %r12
leaq 0x60(%rsp), %rdi
movq %rax, (%rdi)
subq $0x8, %rsp
leaq 0x8(%rsp), %rax
movq %rbp, %rsi
movq %r12, %rdx
movq 0x60(%rsp), %rcx
movq 0x58(%rsp), %r13
movq %r13, %r8
movq 0x70(%rsp), %r9
movss 0x4c(%rsp), %xmm1
movss 0x50(%rsp), %xmm2
movss 0x54(%rsp), %xmm3
pushq $0x0
pushq $0x1
pushq 0x130(%rsp)
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
movl 0x138(%rsp), %eax
pushq %rax
movl 0x138(%rsp), %ebx
pushq %rbx
movq 0x138(%rsp), %r15
pushq %r15
pushq 0x138(%rsp)
callq 0xc906a
addq $0x48, %rsp
leaq 0x68(%rsp), %rdi
movq %r12, %r8
movl 0x110(%rsp), %r12d
movq %rbp, %rsi
movq %r13, %rdx
movq 0x30(%rsp), %r15
movq %r15, %rcx
movq 0x100(%rsp), %r9
leaq 0x78(%rsp), %rax
pushq %rax
pushq %r12
pushq %rbx
callq 0x7f44
addq $0x20, %rsp
leaq 0x1ccd44(%rip), %rax    # 0x295758
cmpl $0x0, 0x1e4(%rax)
je 0xc8a45
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq 0x20(%rsp), %rcx
movq 0x100(%rsp), %r8
movl %ebx, %r9d
pushq %r12
callq 0x8832
addq $0x10, %rsp
movq 0x60(%rsp), %rdi
callq 0xa0761
movq %rbp, %rdi
callq 0xa0494
leaq 0x70(%rsp), %r9
movq $0x0, (%r9)
leaq 0x1cccee(%rip), %rdx    # 0x295758
movl 0x154(%rdx), %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 %r12d, 0x8(%rsp)
movq %rdx, %r12
movl %ebx, (%rsp)
leaq 0x1cd78e(%rip), %rdx    # 0x296230
movq 0x50(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rcx
movq 0x48(%rsp), %r8
callq 0xb8f0d
addq $0x30, %rsp
testb $0x4, (%r12)
movq 0x120(%rsp), %r12
je 0xc8add
movq (%rsp), %rax
movq 0xa8(%rax), %rdi
movq %r15, %rsi
callq 0xbf888
movq (%rsp), %rax
xorl %ebp, %ebp
jmp 0xc8af2
movb $0x1, %bpl
xorl %eax, %eax
movq 0x120(%rsp), %r12
movl $0xa8, %r15d
leaq (%rax,%r15), %rcx
testq %rax, %rax
leaq 0x10(%rsp), %r13
cmoveq %r13, %rcx
movl $0x138, %esi            # imm = 0x138
addq (%rcx), %rsi
movq %r12, %rdi
movq %r14, %rdx
movq 0x20(%rsp), %rbx
movq %rbx, %rcx
callq 0xc678a
movq (%rsp), %rax
addq %rax, %r15
testq %rax, %rax
cmoveq %r13, %r15
movq (%r15), %rdx
leaq 0x1ccc1d(%rip), %r13    # 0x295758
movq 0x1f0(%r13), %r9
testq %r9, %r9
leaq 0x8(%rsp), %r15
cmovneq %r15, %r9
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x16364b
movq (%r15), %rax
testq %rax, %rax
jne 0xc8b7b
cmpq $0x0, (%rsp)
je 0xc8b7b
movq %rsp, %rdi
callq 0xc9bdb
movq 0x8(%rsp), %rax
testq %rax, %rax
setne %al
xorb $0x1, %bpl
orb %al, %bpl
jne 0xc8b94
leaq 0x10(%rsp), %rdi
callq 0xbf611
movq 0x110(%rsp), %r15
movq 0x20(%rsp), %rdi
callq 0xa0761
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0x90(%rsp), %rcx
testq %rcx, %rcx
movq 0x50(%rsp), %rbp
je 0xc8c4a
xorl %eax, %eax
movq 0xa0(%rsp), %rdx
leaq (%rax,%rax,2), %rsi
movl 0x4(%rdx,%rsi,8), %edi
movq 0x28(%r14), %r8
movq (%r8,%rdi,8), %rdi
testl %edi, %edi
je 0xc8c43
movl 0x8(%rdx,%rsi,8), %esi
movq 0x8(%r14), %rdx
movq %rdi, %r8
shrq $0x20, %r8
movl %edi, %r9d
leaq (%r8,%r8,2), %rdi
leaq (%rdx,%rdi,8), %r8
addq $0xc, %r8
shlq $0x3, %r9
leaq (%r9,%r9,2), %r10
xorl %r9d, %r9d
movl (%r8,%r9), %r11d
testl %r11d, %r11d
js 0xc8c20
cmpl %esi, -0x4(%r8,%r9)
je 0xc8c2b
addq $0x18, %r9
cmpq %r9, %r10
jne 0xc8c10
jmp 0xc8c43
leaq (%rdx,%rdi,8), %rcx
orl $0x80000000, %r11d      # imm = 0x80000000
movl %r11d, 0xc(%r9,%rcx)
movq 0x90(%rsp), %rcx
incl %eax
cmpq %rax, %rcx
ja 0xc8bcb
movq 0xa0(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0xa0a7d
movslq 0x11c(%r13), %r9
addq %r9, %r9
movq 0x8(%rsp), %rax
testq %rax, %rax
leaq 0x38(%rsp), %r10
cmoveq %rax, %r10
leaq 0x30(%rsp), %r11
cmoveq %rax, %r11
subq $0x8, %rsp
movss 0x16d129(%rip), %xmm0   # 0x235db8
movss 0x16d125(%rip), %xmm1   # 0x235dbc
movss 0x16d115(%rip), %xmm2   # 0x235db4
movq %r14, %rdi
movq %rbp, %rsi
movq 0x30(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x60(%rsp), %r8
pushq %r12
pushq %r11
pushq %r10
pushq 0x138(%rsp)
pushq %r15
movl 0x138(%rsp), %eax
pushq %rax
movl 0x138(%rsp), %eax
pushq %rax
pushq 0x138(%rsp)
pushq $0x3
callq 0x8a60
addq $0x50, %rsp
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xc8d9b
movq 0x38(%rsp), %rdx
movq (%rsp), %rax
leaq 0xa8(%rax), %rsi
testq %rax, %rax
leaq 0x10(%rsp), %rax
cmoveq %rax, %rsi
movq 0x30(%rsp), %rcx
movl $0x138, %r8d            # imm = 0x138
addq (%rsi), %r8
subq $0x8, %rsp
movq %r14, %rsi
movq %r12, %r9
pushq $0x3
callq 0x21a52
addq $0x10, %rsp
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x2220
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x2220
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2220
cmpq $0x0, (%rsp)
je 0xc8d75
movq %rsp, %rdi
callq 0xc9bdb
cmpq $0x0, 0x10(%rsp)
je 0xc8d87
leaq 0x10(%rsp), %rdi
callq 0xbf611
movq 0x38(%rsp), %rdi
callq 0xa0761
movq 0x30(%rsp), %rdi
callq 0xa0761
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq