// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movb %cl, %al
movq 0x60(%rbp), %rcx
movq 0x58(%rbp), %rcx
movq 0x50(%rbp), %rcx
movq 0x48(%rbp), %rcx
movl 0x40(%rbp), %ecx
movl 0x38(%rbp), %ecx
movl 0x30(%rbp), %ecx
movl 0x28(%rbp), %ecx
movq 0x20(%rbp), %rcx
movq 0x18(%rbp), %rcx
movq 0x10(%rbp), %rcx
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movb %al, -0x49(%rbp)
movq %r8, -0x58(%rbp)
movq %r9, -0x60(%rbp)
movss %xmm0, -0x64(%rbp)
movss %xmm1, -0x68(%rbp)
movss %xmm2, -0x6c(%rbp)
movq -0x48(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
addq $0x64, %rdi
callq 0x2270
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, 0x50(%rbp)
je 0x100d97
movq 0x50(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0x100dce
movzbl -0x49(%rbp), %eax
cmpl $0x1, %eax
jne 0x100db0
leaq 0x28757e(%rip), %rax    # 0x388325
movq %rax, -0xd0(%rbp)
jmp 0x100dc0
leaq 0x287573(%rip), %rax    # 0x38832a
movq %rax, -0xd0(%rbp)
jmp 0x100dc0
movq -0xd0(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xb8(%rbp), %rdx
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rcx
leaq 0x28752b(%rip), %rsi    # 0x388315
movb $0x0, %al
callq 0x2050
movq $0x0, -0x80(%rbp)
cmpl $0x0, 0x40(%rbp)
jne 0x100e13
movq -0x78(%rbp), %rdi
leaq 0x2985c8(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
leaq 0x2c39c8(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0x148(%rax)
je 0x100e52
leaq 0x2c39b8(%rip), %rax    # 0x3c47f8
cvtsi2sdl 0x144(%rax), %xmm0
movsd %xmm0, -0xd8(%rbp)
jmp 0x100e75
leaq 0x2c399f(%rip), %rax    # 0x3c47f8
cvtsi2sdl 0x144(%rax), %xmm0
movsd 0x2859d7(%rip), %xmm1   # 0x386840
divsd %xmm1, %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd -0xd8(%rbp), %xmm0
movsd 0x281173(%rip), %xmm2   # 0x381ff8
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
cvttsd2si %xmm1, %rcx
cvttsd2si %xmm0, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
shlq %rax
movq %rax, -0x90(%rbp)
cmpq $0x40, -0x90(%rbp)
jae 0x100ed0
movq $0x40, -0x90(%rbp)
movq -0x38(%rbp), %rdi
movq -0x90(%rbp), %rax
movl %eax, %esi
movsd 0x27e14b(%rip), %xmm0   # 0x37f030
callq 0xe62d0
movq %rax, -0x88(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0x38(%rbp), %rsi
movb -0x49(%rbp), %dl
movq -0x58(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0x40(%rbp), %r9
movq 0x10(%rbp), %r13
movss -0x64(%rbp), %xmm1
movq 0x18(%rbp), %r12
movq 0x20(%rbp), %r15
movss -0x68(%rbp), %xmm2
movss -0x6c(%rbp), %xmm3
movl 0x28(%rbp), %r14d
movl 0x30(%rbp), %ebx
movl 0x38(%rbp), %r11d
movq 0x48(%rbp), %rax
leaq -0x88(%rbp), %rdi
movss 0x28593e(%rip), %xmm0   # 0x38689c
leaq -0xb0(%rbp), %r10
movzbl %dl, %edx
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movl %r14d, 0x18(%rsp)
movl %ebx, 0x20(%rsp)
movl %r11d, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x120950
leaq 0x2c385e(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0xd8(%rax)
jle 0x101010
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rcx
movq -0x58(%rbp), %r8
movq 0x20(%rbp), %r9
movl 0x28(%rbp), %r11d
movl 0x30(%rbp), %r10d
leaq 0x2c3836(%rip), %rax    # 0x3c47f8
movsd 0xe0(%rax), %xmm0
leaq -0x88(%rbp), %rdi
leaq 0x2c42f8(%rip), %rdx    # 0x3c52d0
leaq -0xb0(%rbp), %rbx
leaq 0x2c3812(%rip), %rax    # 0x3c47f8
addq $0xd8, %rax
xorl %r14d, %r14d
movq %rbx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
callq 0xf6220
leaq 0x2c37e1(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0xdc(%rax)
jle 0x10108c
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rcx
movq -0x58(%rbp), %r8
movq 0x20(%rbp), %r9
movl 0x28(%rbp), %r11d
movl 0x30(%rbp), %r10d
leaq 0x2c37b9(%rip), %rax    # 0x3c47f8
movsd 0xe0(%rax), %xmm0
leaq -0x88(%rbp), %rdi
leaq 0x2c427b(%rip), %rdx    # 0x3c52d0
leaq -0xb0(%rbp), %rbx
xorl %eax, %eax
leaq 0x2c3793(%rip), %rax    # 0x3c47f8
addq $0xdc, %rax
movq %rbx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0xf6220
cmpq $0x0, 0x58(%rbp)
je 0x1010bb
leaq 0x2c4236(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rdi
movq -0x38(%rbp), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x58(%rbp), %rdx
movq -0x88(%rbp), %rcx
callq 0x100680
cmpl $0x0, 0x40(%rbp)
je 0x10122c
movq -0x78(%rbp), %rdi
callq 0x2210
cmpq $0x0, 0x60(%rbp)
je 0x101210
cmpq $0x0, -0xb0(%rbp)
jbe 0x101210
jmp 0x1010e9
movq 0x60(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x60(%rbp), %rcx
movq (%rcx), %rcx
addq -0xb0(%rbp), %rcx
cmpq %rcx, %rax
jae 0x1011d6
movq 0x60(%rbp), %rax
movq (%rax), %rcx
addq -0xb0(%rbp), %rcx
movq 0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x60(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq 0x60(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq 0x60(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x60(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq 0x60(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x60(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq 0x60(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x60(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq 0x60(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x60(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq 0x60(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq 0x60(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x60(%rbp), %rax
imulq $0x18, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq 0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x1011d8
movq 0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x60(%rbp), %rax
imulq $0x18, (%rax), %rax
addq %rax, %rdi
movq -0xa0(%rbp), %rsi
imulq $0x18, -0xb0(%rbp), %rdx
callq 0x2180
movq -0xb0(%rbp), %rcx
movq 0x60(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0xa0(%rbp), %rdi
callq 0x2210
movq -0x88(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x10162f
movq 0x2c2dad(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x78(%rbp), %rdx
leaq 0x286260(%rip), %rsi    # 0x3874a1
movb $0x0, %al
callq 0x22a0
movq -0x88(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x58(%rbp), %rcx
movl 0x28(%rbp), %r9d
movl 0x30(%rbp), %eax
leaq -0xb0(%rbp), %r8
xorl %r10d, %r10d
movl %eax, (%rsp)
movq $0x0, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0xed140
movq -0x88(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq 0x20(%rbp), %rax
movq %rax, -0xe0(%rbp)
movzbl -0x49(%rbp), %eax
cmpl $0x1, %eax
jne 0x1012d8
leaq 0x287060(%rip), %rax    # 0x38832f
movq %rax, -0x108(%rbp)
jmp 0x1012e8
leaq 0x287055(%rip), %rax    # 0x388334
movq %rax, -0x108(%rbp)
jmp 0x1012e8
movq -0xe0(%rbp), %r8
movq -0xe8(%rbp), %rcx
movq -0xf0(%rbp), %rdx
movq -0xf8(%rbp), %rsi
movq -0x100(%rbp), %rdi
movq -0x108(%rbp), %r9
movq -0x80(%rbp), %rax
movq %rax, (%rsp)
callq 0xefa60
movq -0x80(%rbp), %rdi
callq 0x21a0
movq -0x78(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x110(%rbp)
cmpq $0x0, 0x50(%rbp)
je 0x101352
movq 0x50(%rbp), %rax
movq %rax, -0x120(%rbp)
jmp 0x101389
movzbl -0x49(%rbp), %eax
cmpl $0x1, %eax
jne 0x10136b
leaq 0x286fc3(%rip), %rax    # 0x388325
movq %rax, -0x128(%rbp)
jmp 0x10137b
leaq 0x286fb8(%rip), %rax    # 0x38832a
movq %rax, -0x128(%rbp)
jmp 0x10137b
movq -0x128(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x110(%rbp), %rdx
movq -0x118(%rbp), %rdi
movq -0x120(%rbp), %rcx
leaq 0x286f94(%rip), %rsi    # 0x388339
movb $0x0, %al
callq 0x2050
movq -0x78(%rbp), %rdi
leaq 0x29801b(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x80(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x138(%rbp)
movq 0x20(%rbp), %rax
movq %rax, -0x130(%rbp)
movzbl -0x49(%rbp), %eax
cmpl $0x1, %eax
jne 0x101413
leaq 0x286f25(%rip), %rax    # 0x38832f
movq %rax, -0x158(%rbp)
jmp 0x101423
leaq 0x286f1a(%rip), %rax    # 0x388334
movq %rax, -0x158(%rbp)
jmp 0x101423
movq -0x130(%rbp), %r8
movq -0x138(%rbp), %rcx
movq -0x140(%rbp), %rdx
movq -0x148(%rbp), %rsi
movq -0x150(%rbp), %rdi
movq -0x158(%rbp), %r9
movq -0x80(%rbp), %rax
movq %rax, (%rsp)
callq 0xf1c10
movq -0x80(%rbp), %rdi
callq 0x21a0
leaq 0x2c338e(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0x150(%rax)
je 0x101606
movq -0x78(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x160(%rbp)
cmpq $0x0, 0x50(%rbp)
je 0x1014a1
movq 0x50(%rbp), %rax
movq %rax, -0x170(%rbp)
jmp 0x1014d8
movzbl -0x49(%rbp), %eax
cmpl $0x1, %eax
jne 0x1014ba
leaq 0x286e74(%rip), %rax    # 0x388325
movq %rax, -0x178(%rbp)
jmp 0x1014ca
leaq 0x286e69(%rip), %rax    # 0x38832a
movq %rax, -0x178(%rbp)
jmp 0x1014ca
movq -0x178(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x160(%rbp), %rdx
movq -0x168(%rbp), %rdi
movq -0x170(%rbp), %rcx
leaq 0x286e5b(%rip), %rsi    # 0x38834f
movb $0x0, %al
callq 0x2050
movq -0x78(%rbp), %rdi
leaq 0x297ecc(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x80(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x190(%rbp)
movl 0x28(%rbp), %eax
movl %eax, -0x184(%rbp)
movl 0x30(%rbp), %eax
movl %eax, -0x180(%rbp)
leaq 0x2c32a1(%rip), %rax    # 0x3c47f8
movl 0x150(%rax), %eax
movl %eax, -0x17c(%rbp)
movzbl -0x49(%rbp), %eax
cmpl $0x1, %eax
jne 0x10157c
leaq 0x286dbc(%rip), %rax    # 0x38832f
movq %rax, -0x1b0(%rbp)
jmp 0x10158c
leaq 0x286db1(%rip), %rax    # 0x388334
movq %rax, -0x1b0(%rbp)
jmp 0x10158c
movl -0x17c(%rbp), %r11d
movl -0x180(%rbp), %ebx
movl -0x184(%rbp), %r14d
movq -0x190(%rbp), %r8
movq -0x198(%rbp), %rcx
movq -0x1a0(%rbp), %rsi
movq -0x1a8(%rbp), %rdi
movq -0x1b0(%rbp), %r10
movq -0x80(%rbp), %rax
leaq 0x2c3d02(%rip), %rdx    # 0x3c52d0
leaq -0xb0(%rbp), %r9
xorl %r15d, %r15d
movl %r14d, (%rsp)
movl %ebx, 0x8(%rsp)
movl %r11d, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
callq 0xf1c80
movq -0x80(%rbp), %rdi
callq 0x21a0
movq -0x78(%rbp), %rdi
callq 0x2210
movq -0x88(%rbp), %rdi
callq 0xcd6f0
movq -0xa0(%rbp), %rdi
callq 0x2210
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)