// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movss %xmm0, -0x5c(%rbp)
movss %xmm1, -0x60(%rbp)
movss %xmm2, -0x64(%rbp)
movq -0x30(%rbp), %rdi
callq 0xfd5f0
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xb0(%rbp)
movq $0x0, -0xc8(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0xe7ae0
movq %rax, -0xc8(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0xc8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x48(%rbp), %rcx
movl 0x20(%rbp), %r9d
movl 0x28(%rbp), %r10d
leaq -0xa0(%rbp), %r8
leaq -0xc0(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0xed140
movq $0x0, -0xd0(%rbp)
movq $0x0, -0xd8(%rbp)
leaq 0x2b975c(%rip), %rax    # 0x3c47f8
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x10b0b6
movq -0x40(%rbp), %rdi
callq 0xfd250
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
jne 0x10b2b4
movq $0x0, -0xa0(%rbp)
movq -0x30(%rbp), %rdi
callq 0x10b830
movq %rax, -0xe0(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x10ba70
movq %rax, -0xe8(%rbp)
movq -0xe0(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x58(%rbp), %r9
movss -0x5c(%rbp), %xmm1
movq 0x10(%rbp), %r12
movq 0x18(%rbp), %r15
movss -0x60(%rbp), %xmm2
movss -0x64(%rbp), %xmm3
movl 0x20(%rbp), %r14d
movl 0x28(%rbp), %ebx
movq 0x38(%rbp), %rax
leaq -0xe8(%rbp), %rdi
movss 0x27b762(%rip), %xmm0   # 0x38689c
leaq -0xa0(%rbp), %r11
leaq -0xd0(%rbp), %r10
xorl %r13d, %r13d
movq %r12, (%rsp)
movq %r15, 0x8(%rsp)
movl %r14d, 0x10(%rsp)
movl %ebx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movl $0x1, 0x38(%rsp)
movl $0x0, 0x40(%rsp)
callq 0x10bde0
movq -0xe0(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
movq 0x18(%rbp), %r9
movl 0x20(%rbp), %r11d
movl 0x28(%rbp), %r10d
leaq -0xe8(%rbp), %rdi
leaq -0xa0(%rbp), %rax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xfcd70
leaq 0x2b9630(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0x1e4(%rax)
je 0x10b1f7
movq -0xe0(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq 0x18(%rbp), %r8
movl 0x20(%rbp), %r9d
movl 0x28(%rbp), %eax
movl %eax, (%rsp)
callq 0x10c710
movq -0xe8(%rbp), %rdi
callq 0xcd6f0
movq -0xe0(%rbp), %rdi
callq 0xcd170
movq $0x0, -0xa0(%rbp)
movq -0xc8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %r8
movl 0x20(%rbp), %ebx
movl 0x28(%rbp), %r11d
leaq 0x2b95bd(%rip), %rax    # 0x3c47f8
movl 0x154(%rax), %r10d
movq -0xd0(%rbp), %rax
movq 0xa8(%rax), %rax
leaq 0x2ba079(%rip), %rdx    # 0x3c52d0
leaq -0xa0(%rbp), %r9
xorl %r14d, %r14d
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0xf1c80
leaq 0x2b9567(%rip), %rax    # 0x3c47f8
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x10b2b2
movq -0xd0(%rbp), %rax
movq 0xa8(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0xfd080
jmp 0x10b2b4
movq 0x40(%rbp), %rax
movq %rax, -0x110(%rbp)
cmpq $0x0, -0xd0(%rbp)
je 0x10b2e6
movq -0xd0(%rbp), %rax
movq 0xa8(%rax), %rax
addq $0x138, %rax            # imm = 0x138
movq %rax, -0x118(%rbp)
jmp 0x10b2fa
movq -0xd8(%rbp), %rax
addq $0x138, %rax            # imm = 0x138
movq %rax, -0x118(%rbp)
movq -0x110(%rbp), %rdi
movq -0x118(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0xc8(%rbp), %rcx
callq 0x1074e0
movq -0xc8(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x120(%rbp)
cmpq $0x0, -0xd0(%rbp)
je 0x10b352
movq -0xd0(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rax, -0x130(%rbp)
jmp 0x10b360
movq -0xd8(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x140(%rbp)
movq 0x40(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq 0x2b9478(%rip), %rax    # 0x3c47f8
cmpq $0x0, 0x1f0(%rax)
je 0x10b397
leaq -0x70(%rbp), %rax
movq %rax, -0x148(%rbp)
jmp 0x10b3a2
xorl %eax, %eax
movq %rax, -0x148(%rbp)
jmp 0x10b3a2
movq -0x138(%rbp), %rcx
movq -0x140(%rbp), %rdx
movq -0x120(%rbp), %rsi
movq -0x128(%rbp), %rdi
movq -0x148(%rbp), %r9
xorl %eax, %eax
movl %eax, %r8d
callq 0x20d6b0
cmpq $0x0, -0x70(%rbp)
jne 0x10b3ec
cmpq $0x0, -0xd0(%rbp)
je 0x10b3ec
leaq -0xd0(%rbp), %rdi
callq 0x10c960
cmpq $0x0, -0x70(%rbp)
jne 0x10b409
cmpq $0x0, -0xd8(%rbp)
je 0x10b409
leaq -0xd8(%rbp), %rdi
callq 0xfc940
movq -0xc8(%rbp), %rdi
callq 0xcd6f0
movq -0x90(%rbp), %rdi
callq 0x2210
movq $0x0, -0xf0(%rbp)
movl $0x0, -0x100(%rbp)
movl -0x100(%rbp), %eax
cmpq -0xc0(%rbp), %rax
jae 0x10b577
movq -0xb0(%rbp), %rax
movl -0x100(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0xf4(%rbp)
movq -0xb0(%rbp), %rax
movl -0x100(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0xf8(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0xf4(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xf0(%rbp)
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movl -0xf4(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x104(%rbp)
movl $0x0, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
cmpl -0x104(%rbp), %eax
jae 0x10b561
movq -0xf0(%rbp), %rax
movl -0xfc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x10b504
jmp 0x10b54d
movq -0xf0(%rbp), %rax
movl -0xfc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0xf8(%rbp), %eax
jne 0x10b54b
movq -0xf0(%rbp), %rax
movl -0xfc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
jmp 0x10b561
jmp 0x10b54d
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x10b4d5
jmp 0x10b563
movl -0x100(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100(%rbp)
jmp 0x10b436
movq -0xb0(%rbp), %rdi
callq 0x2210
movq -0x30(%rbp), %rdi
callq 0xcdd10
movq -0x30(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq 0x2b922e(%rip), %rax    # 0x3c47f8
movl 0x11c(%rax), %eax
shll %eax
cltq
movq %rax, -0x170(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x168(%rbp)
movl 0x20(%rbp), %eax
movl %eax, -0x160(%rbp)
movl 0x28(%rbp), %eax
movl %eax, -0x15c(%rbp)
movq 0x30(%rbp), %rax
movq %rax, -0x158(%rbp)
movq 0x38(%rbp), %rax
movq %rax, -0x150(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x10b622
leaq -0x78(%rbp), %rax
movq %rax, -0x1a0(%rbp)
jmp 0x10b62d
xorl %eax, %eax
movq %rax, -0x1a0(%rbp)
jmp 0x10b62d
movq -0x1a0(%rbp), %rax
movq %rax, -0x1a8(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x10b64f
leaq -0x80(%rbp), %rax
movq %rax, -0x1b0(%rbp)
jmp 0x10b65a
xorl %eax, %eax
movq %rax, -0x1b0(%rbp)
jmp 0x10b65a
movq -0x1a8(%rbp), %r11
movq -0x150(%rbp), %rbx
movq -0x158(%rbp), %r14
movl -0x15c(%rbp), %r15d
movl -0x160(%rbp), %r12d
movq -0x168(%rbp), %r13
movq -0x170(%rbp), %r9
movq -0x178(%rbp), %r8
movq -0x180(%rbp), %rcx
movq -0x188(%rbp), %rdx
movq -0x190(%rbp), %rsi
movq -0x198(%rbp), %rdi
movq -0x1b0(%rbp), %r10
movq 0x40(%rbp), %rax
movss 0x27b1df(%rip), %xmm0   # 0x3868a0
movss 0x27b1db(%rip), %xmm1   # 0x3868a4
movss 0x27b1cb(%rip), %xmm2   # 0x38689c
movq $0x3, (%rsp)
movq %r13, 0x8(%rsp)
movl %r12d, 0x10(%rsp)
movl %r15d, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
callq 0x10ca20
cmpq $0x0, -0x70(%rbp)
je 0x10b819
movq -0x70(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x1b8(%rbp)
cmpq $0x0, -0xd0(%rbp)
je 0x10b764
movq -0xd0(%rbp), %rax
movq 0xa8(%rax), %rax
addq $0x138, %rax            # imm = 0x138
movq %rax, -0x1d8(%rbp)
jmp 0x10b778
movq -0xd8(%rbp), %rax
addq $0x138, %rax            # imm = 0x138
movq %rax, -0x1d8(%rbp)
movq -0x1b8(%rbp), %rcx
movq -0x1c0(%rbp), %rdx
movq -0x1c8(%rbp), %rsi
movq -0x1d0(%rbp), %rdi
movq -0x1d8(%rbp), %r8
movq 0x40(%rbp), %r9
movl $0x3, (%rsp)
callq 0x249e00
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x2210
movq -0x70(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2210
movq -0x70(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x2210
movq -0x70(%rbp), %rdi
callq 0x2210
cmpq $0x0, -0xd0(%rbp)
je 0x10b7f1
leaq -0xd0(%rbp), %rdi
callq 0x10c960
cmpq $0x0, -0xd8(%rbp)
je 0x10b807
leaq -0xd8(%rbp), %rdi
callq 0xfc940
movq -0x78(%rbp), %rdi
callq 0xcd6f0
movq -0x80(%rbp), %rdi
callq 0xcd6f0
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)