// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: output_trio_unitig_graph(asg_t*, ma_sub_t*, char*, unsigned char, ma_hit_t_alloc*, ma_hit_t_alloc*, long long, float, long long, R_to_U*, float, float, int, int, int, int, bub_label_t*, char*, unsigned char*, kvec_asg_arc_t_warp*)
ma_ug_t* output_trio_unitig_graph(asg_t *sg, ma_sub_t* coverage_cut, char* output_file_name, 
uint8_t flag, 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,
int is_bench, bub_label_t* b_mask_t, char *f_prefix, uint8_t *kpt_buf, kvec_asg_arc_t_warp *r_edges)
{
    char* gfa_name = (char*)malloc(strlen(output_file_name)+100);
    sprintf(gfa_name, "%s.%s.p_ctg.gfa", output_file_name, f_prefix?f_prefix:(flag==FATHER?"hap1":"hap2"));
    FILE* output_file = NULL;
    if(is_bench == 0) output_file = fopen(gfa_name, "w");

    // prt_phase_dbg_graph(gfa_name, sg, coverage_cut, sources, ruIndex, max_hang, min_ovlp);

    ma_ug_t *ug = NULL; uint64_t pscut = 0;
    // ug = ma_ug_gen(sg);
    pscut = (asm_opt.hom_global_coverage_set?(asm_opt.hom_global_coverage):(((double)asm_opt.hom_global_coverage)/((double)HOM_PEAK_RATE)));
    pscut *= PHASE_SEF; if(pscut < PHASE_SEP) pscut = PHASE_SEP;
    ug = ma_ug_gen_phase(sg, pscut, PHASE_SEP_RATE);

    kvec_asg_arc_t_warp new_rtg_edges;
    kv_init(new_rtg_edges.a);
    adjust_utg_by_trio(&ug, sg, flag, TRIO_THRES, sources, reverse_sources, coverage_cut, 
    tipsLen, tip_drop_ratio, stops_threshold, ruIndex, chimeric_rate, drop_ratio, max_hang, 
    min_ovlp, gap_fuzz, &new_rtg_edges, b_mask_t);    
    if(asm_opt.b_low_cov > 0)
    {
        break_ug_contig(&ug, sg, &R_INF, coverage_cut, sources, ruIndex, &new_rtg_edges, max_hang, min_ovlp, 
        &asm_opt.b_low_cov, NULL, asm_opt.m_rate);
    }
    if(asm_opt.b_high_cov > 0)
    {
        break_ug_contig(&ug, sg, &R_INF, coverage_cut, sources, ruIndex, &new_rtg_edges, max_hang, min_ovlp, 
        NULL, &asm_opt.b_high_cov, asm_opt.m_rate);
    }
    if(kpt_buf) 
    {
        update_dump_trio(R_INF.trio_flag, sg->n_seq, kpt_buf, ug);
    }
    if(is_bench)
    {
        free(gfa_name);
        if(r_edges && new_rtg_edges.a.n > 0) {
            kv_resize(asg_arc_t, r_edges->a, r_edges->a.n + new_rtg_edges.a.n);
            memcpy(r_edges->a.a + r_edges->a.n, new_rtg_edges.a.a, new_rtg_edges.a.n*sizeof(asg_arc_t));
            r_edges->a.n += new_rtg_edges.a.n;
        }
        kv_destroy(new_rtg_edges.a);
        return ug;
    }

    fprintf(stderr, "Writing %s to disk... \n", gfa_name);
    ///debug_utg_graph(ug, sg, 0, 0);
    ///debug_untig_length(ug, tipsLen, gfa_name);
    ///print_untig_by_read(ug, "m64011_190901_095311/125831121/ccs", 2310925, "end");
    ma_ug_seq(ug, sg, coverage_cut, sources, &new_rtg_edges, max_hang, min_ovlp, 0, 1);
    ma_ug_print(ug, sg, coverage_cut, sources, ruIndex, (flag==FATHER?"h1tg":"h2tg"), output_file);
    fclose(output_file);

    sprintf(gfa_name, "%s.%s.p_ctg.noseq.gfa", output_file_name, f_prefix?f_prefix:(flag==FATHER?"hap1":"hap2"));
    output_file = fopen(gfa_name, "w");
    ma_ug_print_simple(ug, sg, coverage_cut, sources, ruIndex, (flag==FATHER?"h1tg":"h2tg"), output_file);
    fclose(output_file);
    if(asm_opt.bed_inconsist_rate != 0)
    {
        sprintf(gfa_name, "%s.%s.p_ctg.lowQ.bed", output_file_name, f_prefix?f_prefix:(flag==FATHER?"hap1":"hap2"));
        output_file = fopen(gfa_name, "w");
        ma_ug_print_bed(ug, sg, &R_INF, coverage_cut, sources, &new_rtg_edges, 
        max_hang, min_ovlp, asm_opt.bed_inconsist_rate, (flag==FATHER?"h1tg":"h2tg"), output_file, NULL);
        fclose(output_file);
    }

    free(gfa_name);
    ma_ug_destroy(ug);
    kv_destroy(new_rtg_edges.a);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movss %xmm2, 0x8(%rsp)
movss %xmm1, 0x2c(%rsp)
movss %xmm0, 0x28(%rsp)
movq %r9, 0x70(%rsp)
movq %r8, %r15
movl %ecx, %ebp
movq %rdx, %r13
movq %rsi, 0x20(%rsp)
movq %rdi, %r12
movq 0xf0(%rsp), %rbx
movq %rdx, %rdi
callq 0x2090
leaq 0x64(%rax), %rdi
callq 0x2280
movl %ebp, 0x1c(%rsp)
cmpl $0x1, %ebp
leaq 0x169322(%rip), %rcx    # 0x22b54a
leaq 0x169320(%rip), %rbp    # 0x22b54f
cmoveq %rcx, %rbp
movq %rax, %r14
testq %rbx, %rbx
cmovneq %rbx, %rbp
movq %r13, %rbx
leaq 0x1692f3(%rip), %rsi    # 0x22b53a
movq %rax, %rdi
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0xe0(%rsp)
movq %r14, 0x10(%rsp)
je 0xc2274
movq $0x0, 0x38(%rsp)
movq %r15, %r13
jmp 0xc228b
movq %r15, %r13
leaq 0x17a180(%rip), %rsi    # 0x23c3fe
movq %r14, %rdi
callq 0x2210
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
movq %r15, (%rsp)
leaq 0x1c84bf(%rip), %r14    # 0x28a758
cmpl $0x0, 0x148(%r14)
xorps %xmm0, %xmm0
cvtsi2sdl 0x144(%r14), %xmm0
movq %rbx, 0x68(%rsp)
jne 0xc22bc
divsd 0x1678ac(%rip), %xmm0   # 0x229b68
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x162f9c(%rip), %xmm0   # 0x225268
cvttsd2si %xmm0, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
addq %rdx, %rdx
cmpq $0x41, %rdx
movl $0x40, %esi
cmovaeq %rdx, %rsi
movsd 0x1631f5(%rip), %xmm0   # 0x2254e8
movq %r12, %rdi
callq 0xb09be
movq %rsp, %rdi
movq %rax, (%rdi)
leaq 0x40(%rsp), %rbx
movq %r15, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
movzbl 0x1c(%rsp), %edx
movss 0x16789f(%rip), %xmm0   # 0x229bbc
movq %r12, %rsi
movq %r13, %r15
movq %r13, %rcx
movq 0x70(%rsp), %r8
movq 0x20(%rsp), %r9
movss 0x28(%rsp), %xmm1
movss 0x2c(%rsp), %xmm2
movss 0x8(%rsp), %xmm3
pushq 0xe8(%rsp)
pushq %rbx
movl 0xe8(%rsp), %eax
pushq %rax
movl 0xe8(%rsp), %eax
pushq %rax
movl 0xe8(%rsp), %eax
pushq %rax
movq 0xe8(%rsp), %r13
pushq %r13
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
callq 0xd5bde
addq $0x40, %rsp
cmpl $0x0, 0xd8(%r14)
jle 0xc23d1
movsd 0xe0(%r14), %xmm0
subq $0x30, %rsp
leaq 0xd8(%r14), %rax
movq %rax, 0x18(%rsp)
movq %rbx, (%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x1c8e76(%rip), %rdx    # 0x28b230
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r15, %r8
movq %r13, %r9
callq 0xba811
addq $0x30, %rsp
movq 0xf8(%rsp), %r13
cmpl $0x0, 0xdc(%r14)
jle 0xc242c
movsd 0xe0(%r14), %xmm0
subq $0x30, %rsp
leaq 0xdc(%r14), %rax
movq %rax, 0x20(%rsp)
movq %rbx, (%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x1c8e20(%rip), %rdx    # 0x28b230
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r15, %r8
movq 0xf0(%rsp), %r9
callq 0xba811
addq $0x30, %rsp
testq %r13, %r13
je 0xc2452
leaq 0x1c8df8(%rip), %rax    # 0x28b230
movq 0x28(%rax), %rdi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%r12), %esi
movq (%rsp), %rcx
movq %r13, %rdx
callq 0xc1ead
cmpl $0x0, 0xe0(%rsp)
je 0xc2537
movq 0x100(%rsp), %r14
movq 0x10(%rsp), %rdi
callq 0x2220
testq %r14, %r14
setne %cl
movq 0x40(%rsp), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xc251a
addq (%r14), %rax
cmpq %rax, 0x8(%r14)
jae 0xc24ec
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rax
leaq (%rax,%rax,2), %rdi
shlq $0x3, %rdi
addq 0x10(%r14), %rdi
movq 0x40(%rsp), %rax
movq 0x50(%rsp), %rsi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdx
callq 0x2160
movq 0x40(%rsp), %rax
addq %rax, (%r14)
movq 0x50(%rsp), %rdi
callq 0x2220
movq (%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1c7aa2(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x16833b(%rip), %rsi    # 0x22a883
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq (%rsp), %r13
subq $0x8, %rsp
movl $0x1, %r10d
leaq 0x48(%rsp), %r8
movq %r13, %rdi
movq %r13, 0x10(%rsp)
movq %r12, %rsi
movq 0x28(%rsp), %rbx
movq %rbx, %rdx
movq %r15, %rcx
movl 0xd0(%rsp), %eax
movq %rax, %r9
pushq %r10
pushq $0x0
movl 0xe8(%rsp), %eax
pushq %rax
callq 0xb5778
addq $0x20, %rsp
cmpb $0x1, 0x1c(%rsp)
leaq 0x168fac(%rip), %rax    # 0x22b554
movq %r15, %r8
movq %r15, 0x30(%rsp)
leaq 0x168fa2(%rip), %r15    # 0x22b559
cmoveq %rax, %r15
subq $0x8, %rsp
leaq 0x1c8c6a(%rip), %rsi    # 0x28b230
movq %r13, %rdi
movq %r12, %rdx
movq %rbx, %rcx
movq 0xc8(%rsp), %r9
movq 0x40(%rsp), %rbx
pushq %rbx
pushq %r15
movl $0x1, %eax
pushq %rax
callq 0xb688e
addq $0x20, %rsp
movq %rbx, %rdi
callq 0x2190
leaq 0x168f61(%rip), %rsi    # 0x22b55e
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq 0x68(%rsp), %rbx
movq %rbx, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2050
leaq 0x179de0(%rip), %rsi    # 0x23c3fe
movq %r13, %rdi
callq 0x2210
movq %rax, %r13
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
leaq 0x1c8bf7(%rip), %rsi    # 0x28b230
movq %r12, %rdx
movq 0x28(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0xc8(%rsp), %r9
pushq %rax
pushq %r15
pushq $0x0
callq 0xb688e
addq $0x20, %rsp
movq %r13, %rdi
callq 0x2190
cmpl $0x0, 0x150(%r14)
je 0xc26e9
leaq 0x168eff(%rip), %rsi    # 0x22b574
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2050
leaq 0x179d6d(%rip), %rsi    # 0x23c3fe
movq %r13, %rdi
callq 0x2210
movq %rax, %r13
movl 0x150(%r14), %eax
leaq 0x1c8b86(%rip), %rdx    # 0x28b230
leaq 0x40(%rsp), %r9
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %r8
pushq $0x0
pushq %r13
pushq %r15
pushq %rax
movl 0xf0(%rsp), %eax
pushq %rax
movl 0xf0(%rsp), %eax
pushq %rax
callq 0xb7f72
addq $0x30, %rsp
movq %r13, %rdi
callq 0x2190
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0xa0004
movq 0x50(%rsp), %rdi
callq 0x2220
xorl %eax, %eax
jmp 0xc2528