// File: /workspace/github_cpp_makefile_star2_O2/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, (%rsp)
movss %xmm1, 0x2c(%rsp)
movss %xmm0, 0x28(%rsp)
movq %r9, 0x68(%rsp)
movq %r8, 0x20(%rsp)
movl %ecx, %ebp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r12
movq 0xf0(%rsp), %r13
movq %rdx, %rdi
callq 0x2090
leaq 0x64(%rax), %rdi
callq 0x2280
movl %ebp, 0x1c(%rsp)
cmpl $0x1, %ebp
leaq 0x162923(%rip), %rcx    # 0x1e963a
leaq 0x162921(%rip), %rbp    # 0x1e963f
cmoveq %rcx, %rbp
movq %rax, %r14
testq %r13, %r13
cmovneq %r13, %rbp
leaq 0x1628f7(%rip), %rsi    # 0x1e962a
movq %rax, %rdi
movq %rbx, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0xe0(%rsp)
movq %r14, 0x8(%rsp)
je 0x86d5d
movq $0x0, 0x38(%rsp)
jmp 0x86d71
leaq 0x17393d(%rip), %rsi    # 0x1fa6a1
movq %r14, %rdi
callq 0x2210
movq %rax, 0x38(%rsp)
movl 0xc8(%rsp), %r13d
leaq 0x1cc9d8(%rip), %rax    # 0x253758
cmpl $0x0, 0x148(%rax)
cvtsi2sdl 0x144(%rax), %xmm0
movq %rbx, 0x60(%rsp)
movq %rbp, 0x70(%rsp)
movq %r15, %r14
jne 0x86da6
divsd 0x160eba(%rip), %xmm0   # 0x1e7c60
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x15c4e2(%rip), %xmm0   # 0x1e3298
cvttsd2si %xmm0, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
addq %rdx, %rdx
cmpq $0x41, %rdx
pushq $0x40
popq %rsi
cmovaeq %rdx, %rsi
movsd 0x15c75d(%rip), %xmm0   # 0x1e3538
movq %r12, %rdi
callq 0x773c3
leaq 0x10(%rsp), %rdi
movq %rax, (%rdi)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rbx
movaps %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movzbl 0x1c(%rsp), %edx
movss 0x160eac(%rip), %xmm0   # 0x1e7cb4
movq %r12, %rsi
movq %r12, %rbp
movq 0x20(%rsp), %r12
movq %r12, %rcx
movq 0x68(%rsp), %r8
movq %r14, %r9
movss 0x28(%rsp), %xmm1
movss 0x2c(%rsp), %xmm2
movss (%rsp), %xmm3
pushq 0xe8(%rsp)
pushq %rbx
movl 0xe8(%rsp), %eax
pushq %rax
movl 0xe8(%rsp), %eax
pushq %rax
pushq %r13
movq 0xe8(%rsp), %r13
pushq %r13
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
callq 0x99c56
addq $0x40, %rsp
leaq 0x1cc8e7(%rip), %r15    # 0x253758
cmpl $0x0, 0xd8(%r15)
jle 0x86ebc
movsd 0xe0(%r15), %xmm0
subq $0x30, %rsp
leaq 0xd8(%r15), %rax
movq %rax, 0x18(%rsp)
movq %rbx, (%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x1cd38b(%rip), %rdx    # 0x254230
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %r8
movq %r13, %r9
callq 0x80454
addq $0x30, %rsp
movq 0xf8(%rsp), %r13
cmpl $0x0, 0xdc(%r15)
movq %rbp, %r12
jle 0x86f19
movsd 0xe0(%r15), %xmm0
subq $0x30, %rsp
leaq 0xdc(%r15), %rax
movq %rax, 0x20(%rsp)
movq %rbx, (%rsp)
andq $0x0, 0x18(%rsp)
leaq 0x1cd335(%rip), %rdx    # 0x254230
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
movq 0x50(%rsp), %r8
movq 0xf0(%rsp), %r9
callq 0x80454
addq $0x30, %rsp
testq %r13, %r13
movq 0x70(%rsp), %rbp
je 0x86f45
leaq 0x1cd306(%rip), %rax    # 0x254230
movq 0x28(%rax), %rdi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%r12), %esi
movq 0x10(%rsp), %rcx
movq %r13, %rdx
callq 0x8689c
cmpl $0x0, 0xe0(%rsp)
je 0x87023
movq 0x100(%rsp), %r14
movq 0x8(%rsp), %rdi
callq 0x2220
testq %r14, %r14
setne %al
movq 0x40(%rsp), %rcx
testq %rcx, %rcx
setne %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x87005
movq (%r14), %rdx
leaq (%rdx,%rcx), %rax
cmpq %rax, 0x8(%r14)
jae 0x86fe4
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
incq %rcx
movq %rcx, 0x8(%r14)
movq 0x10(%r14), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rdx
movq 0x40(%rsp), %rcx
jmp 0x86fe8
movq 0x10(%r14), %rax
imulq $0x18, %rdx, %rdi
addq %rax, %rdi
movq 0x50(%rsp), %rsi
imulq $0x18, %rcx, %rdx
callq 0x2160
movq 0x40(%rsp), %rax
addq %rax, (%r14)
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1cbfb6(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x16193f(%rip), %rsi    # 0x1e8973
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq 0x10(%rsp), %r13
subq $0x8, %rsp
leaq 0x48(%rsp), %r8
movq %r13, %rdi
movq %r13, 0x8(%rsp)
movq %r12, %rsi
movq %r14, %rdx
movq 0x28(%rsp), %rbx
movq %rbx, %rcx
movl 0xd0(%rsp), %eax
movq %rax, %r9
pushq $0x1
pushq $0x0
movl 0xe8(%rsp), %eax
pushq %rax
callq 0x7b888
addq $0x20, %rsp
cmpb $0x1, 0x1c(%rsp)
leaq 0x1625b5(%rip), %rax    # 0x1e9644
movq %r14, 0x30(%rsp)
leaq 0x1625ae(%rip), %r15    # 0x1e9649
cmoveq %rax, %r15
subq $0x8, %rsp
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq 0xc8(%rsp), %r8
movq %r15, %r9
movq 0x40(%rsp), %r14
pushq %r14
callq 0x7cd23
popq %rax
popq %rcx
movq %r14, %rdi
callq 0x2190
leaq 0x162577(%rip), %rsi    # 0x1e964e
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq 0x60(%rsp), %r14
movq %r14, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2050
leaq 0x1735a9(%rip), %rsi    # 0x1fa6a1
movq %r13, %rdi
callq 0x2210
movq %rax, %r13
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq 0x38(%rsp), %rdx
movq %rbx, %rcx
movq 0xc8(%rsp), %r8
movq %r15, %r9
pushq %rax
callq 0x7de8f
popq %rax
popq %rcx
movq %r13, %rdi
callq 0x2190
leaq 0x1cc61f(%rip), %rax    # 0x253758
cmpl $0x0, 0x150(%rax)
je 0x871c6
leaq 0x162517(%rip), %rsi    # 0x1e9664
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2050
leaq 0x173538(%rip), %rsi    # 0x1fa6a1
movq %rbx, %rdi
callq 0x2210
movq %rax, %r13
leaq 0x1cc5dd(%rip), %rax    # 0x253758
movl 0x150(%rax), %eax
leaq 0x1cd0a8(%rip), %rdx    # 0x254230
leaq 0x40(%rsp), %r9
movq (%rsp), %rdi
movq %r12, %rsi
movq 0x30(%rsp), %rcx
movq 0x20(%rsp), %r8
pushq $0x0
pushq %r13
pushq %r15
pushq %rax
movl 0xf0(%rsp), %eax
pushq %rax
movl 0xf0(%rsp), %eax
pushq %rax
callq 0x7debc
addq $0x30, %rsp
movq %r13, %rdi
callq 0x2190
movq 0x8(%rsp), %rdi
callq 0x2220
movq (%rsp), %rdi
callq 0x6921d
movq 0x50(%rsp), %rdi
callq 0x2220
xorl %eax, %eax
jmp 0x87014