// File: /workspace/github_cpp_makefile_star2_O3/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 0x174602(%rip), %rcx    # 0x23773a
leaq 0x174600(%rip), %rbp    # 0x23773f
cmoveq %rcx, %rbp
movq %rax, %r14
testq %r13, %r13
cmovneq %r13, %rbp
leaq 0x1745d6(%rip), %rsi    # 0x23772a
movq %rax, %rdi
movq %rbx, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0xe0(%rsp)
movq %r14, 0x8(%rsp)
je 0xc317e
movq $0x0, 0x38(%rsp)
jmp 0xc3192
leaq 0x18555d(%rip), %rsi    # 0x2486e2
movq %r14, %rdi
callq 0x2210
movq %rax, 0x38(%rsp)
movl 0xc8(%rsp), %r13d
leaq 0x1d25b7(%rip), %rax    # 0x295758
cmpl $0x0, 0x148(%rax)
xorps %xmm0, %xmm0
cvtsi2sdl 0x144(%rax), %xmm0
movq %rbx, 0x60(%rsp)
movq %rbp, 0x70(%rsp)
movq %r15, %r14
jne 0xc31ca
divsd 0x172b96(%rip), %xmm0   # 0x235d60
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x16e12e(%rip), %xmm0   # 0x231308
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 0x16e3e7(%rip), %xmm0   # 0x2315e8
movq %r12, %rdi
callq 0xb18e6
leaq 0x10(%rsp), %rdi
movq %rax, (%rdi)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rbx
movaps %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movzbl 0x1c(%rsp), %edx
movss 0x172b83(%rip), %xmm0   # 0x235db4
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 0xd6ec8
addq $0x40, %rsp
leaq 0x1d24be(%rip), %r15    # 0x295758
cmpl $0x0, 0xd8(%r15)
jle 0xc32e8
movsd 0xe0(%r15), %xmm0
subq $0x30, %rsp
leaq 0xd8(%r15), %rax
movq %rax, 0x18(%rsp)
movq %rbx, (%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x1d2f5f(%rip), %rdx    # 0x296230
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %r8
movq %r13, %r9
callq 0xbb86f
addq $0x30, %rsp
movq 0xf8(%rsp), %r13
cmpl $0x0, 0xdc(%r15)
movq %rbp, %r12
jle 0xc3348
movsd 0xe0(%r15), %xmm0
subq $0x30, %rsp
leaq 0xdc(%r15), %rax
movq %rax, 0x20(%rsp)
movq %rbx, (%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x1d2f06(%rip), %rdx    # 0x296230
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
movq 0x50(%rsp), %r8
movq 0xf0(%rsp), %r9
callq 0xbb86f
addq $0x30, %rsp
testq %r13, %r13
movq 0x70(%rsp), %rbp
je 0xc3374
leaq 0x1d2ed7(%rip), %rax    # 0x296230
movq 0x28(%rax), %rdi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl 0x14(%r12), %esi
movq 0x10(%rsp), %rcx
movq %r13, %rdx
callq 0xc2d98
cmpl $0x0, 0xe0(%rsp)
je 0xc3460
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 0xc3442
movq (%r14), %rdx
leaq (%rdx,%rcx), %rax
cmpq %rax, 0x8(%r14)
jae 0xc341c
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), %rdx
movq 0x40(%rsp), %rcx
jmp 0xc3420
movq 0x10(%r14), %rax
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rdi
movq 0x50(%rsp), %rsi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %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 0x1d1b79(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x173602(%rip), %rsi    # 0x236a73
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x22b0
movq 0x10(%rsp), %r13
subq $0x8, %rsp
movl $0x1, %r10d
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 %r10
pushq $0x0
movl 0xe8(%rsp), %eax
pushq %rax
callq 0xb67c4
addq $0x20, %rsp
cmpb $0x1, 0x1c(%rsp)
leaq 0x174272(%rip), %rax    # 0x237744
movq %r14, 0x30(%rsp)
leaq 0x17426b(%rip), %r15    # 0x237749
cmoveq %rax, %r15
subq $0x8, %rsp
leaq 0x1d2d43(%rip), %rsi    # 0x296230
movq %r13, %rdi
movq %r12, %rdx
movq %r14, %rcx
movq %rbx, %r8
movq 0xc8(%rsp), %r9
movq 0x40(%rsp), %r14
pushq %r14
pushq %r15
movl $0x1, %eax
pushq %rax
callq 0xb7823
addq $0x20, %rsp
movq %r14, %rdi
callq 0x2190
leaq 0x174226(%rip), %rsi    # 0x23774e
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq 0x60(%rsp), %r14
movq %r14, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2050
leaq 0x185199(%rip), %rsi    # 0x2486e2
movq %r13, %rdi
callq 0x2210
movq %rax, %r13
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
leaq 0x1d2ccc(%rip), %rsi    # 0x296230
movq %r12, %rdx
movq 0x38(%rsp), %rcx
movq %rbx, %r8
movq 0xc8(%rsp), %r9
pushq %rax
pushq %r15
pushq $0x0
callq 0xb7823
addq $0x20, %rsp
movq %r13, %rdi
callq 0x2190
leaq 0x1d21c4(%rip), %rax    # 0x295758
cmpl $0x0, 0x150(%rax)
je 0xc3621
leaq 0x1741bc(%rip), %rsi    # 0x237764
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2050
leaq 0x18511e(%rip), %rsi    # 0x2486e2
movq %rbx, %rdi
callq 0x2210
movq %rax, %r13
leaq 0x1d2182(%rip), %rax    # 0x295758
movl 0x150(%rax), %eax
leaq 0x1d2c4d(%rip), %rdx    # 0x296230
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 0xb8f0d
addq $0x30, %rsp
movq %r13, %rdi
callq 0x2190
movq 0x8(%rsp), %rdi
callq 0x2220
movq (%rsp), %rdi
callq 0xa0761
movq 0x50(%rsp), %rdi
callq 0x2220
xorl %eax, %eax
jmp 0xc3451