// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: output_contig_graph_primary_pre(asg_t*, ma_sub_t*, char*, ma_hit_t_alloc*, ma_hit_t_alloc*, unsigned long, long long, R_to_U*, int, int, ug_opt_t const*)
void output_contig_graph_primary_pre(asg_t *sg, ma_sub_t* coverage_cut, char* output_file_name, 
ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, uint64_t bubble_dist, long long tipsLen, 
R_to_U* ruIndex, int max_hang, int min_ovlp, const ug_opt_t *uopt)
{
    kvec_asg_arc_t_warp new_rtg_edges;
    kv_init(new_rtg_edges.a);

    ma_ug_t *ug = NULL;
    ug = ma_ug_gen_primary(sg, PRIMARY_LABLE);

    asg_t* nsg = ug->g;
    uint32_t n_vtx = nsg->n_seq, v;
    for (v = 0; v < n_vtx; ++v) 
    {
        if(nsg->seq[v].del) continue;
        nsg->seq[v].c = PRIMARY_LABLE;
        EvaluateLen(ug->u, v) = ug->u.a[v].n;
    }

    if(bubble_dist > 0)
    {
        asg_pop_bubble_primary_trio(ug, &bubble_dist, (uint32_t)-1, DROP, NULL, NULL, 0, NULL);
        delete_useless_nodes(&ug);
        renew_utg(&ug, sg, &new_rtg_edges);
    }
    
    // reset_untig_hap_label(ug, 7, FATHER, R_INF.trio_flag);
    // reset_untig_hap_label(ug, 35, FATHER, R_INF.trio_flag);

    // reset_untig_hap_label(ug, 713, FATHER, R_INF.trio_flag);
    // reset_untig_hap_label(ug, 273, FATHER, R_INF.trio_flag);
    // reset_untig_hap_label(ug, 822, FATHER, R_INF.trio_flag);

    ma_ug_seq(ug, sg, coverage_cut, sources, &new_rtg_edges, max_hang, min_ovlp, 0, 1);

    fprintf(stderr, "Writing processed unitig GFA to disk... \n");
    char* gfa_name = (char*)malloc(strlen(output_file_name)+35);
    sprintf(gfa_name, "%s.p_utg.gfa", output_file_name);
    FILE* output_file = fopen(gfa_name, "w");
    ma_ug_print(ug, sg, coverage_cut, sources, ruIndex, "utg", output_file);
    fclose(output_file);
    
    sprintf(gfa_name, "%s.p_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);
    if(asm_opt.bed_inconsist_rate != 0)
    {
        sprintf(gfa_name, "%s.p_utg.lowQ.bed", output_file_name);
        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, "utg", output_file, NULL);
        fclose(output_file);
    }

    ///for debug
    // graph_ovlp_binning(ug, sg, uopt);
    // gen_hpc_re_t(ug);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xa8, %rsp
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %r8, -0x40(%rbp)
movq %r9, -0x48(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0xe7ae0
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x7c(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jae 0x14f050
movq -0x78(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x80(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x14f00b
jmp 0x14f045
movq -0x78(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x80(%rbp), %ecx
movb $0x0, 0x4(%rax,%rcx,8)
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x80(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x80(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x14efeb
cmpq $0x0, -0x48(%rbp)
jbe 0x14f0a2
movq -0x70(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x5, %ecx
xorl %eax, %eax
movl %eax, %r9d
xorl %eax, %eax
movq %r9, %r8
movl $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x11c760
leaq -0x70(%rbp), %rdi
callq 0x10ea80
movq -0x20(%rbp), %rsi
leaq -0x70(%rbp), %rdi
leaq -0x68(%rbp), %rdx
callq 0xf6030
movq -0x70(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x38(%rbp), %rcx
movl 0x20(%rbp), %r9d
movl 0x28(%rbp), %eax
leaq -0x68(%rbp), %r8
xorl %r10d, %r10d
movl %eax, (%rsp)
movq $0x0, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0xed140
movq 0x274f00(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x23a631(%rip), %rsi    # 0x38971b
movb $0x0, %al
callq 0x22a0
movq -0x30(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
addq $0x23, %rdi
callq 0x2270
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x23a626(%rip), %rsi    # 0x389745
movb $0x0, %al
callq 0x2050
movq -0x88(%rbp), %rdi
leaq 0x24a29e(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x90(%rbp)
movq -0x70(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq 0x18(%rbp), %r8
movq -0x90(%rbp), %rax
leaq 0x23d846(%rip), %r9     # 0x38c9a8
movq %rax, (%rsp)
callq 0xefa60
movq -0x90(%rbp), %rdi
callq 0x21a0
movq -0x88(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x23a5c9(%rip), %rsi    # 0x389752
movb $0x0, %al
callq 0x2050
movq -0x88(%rbp), %rdi
leaq 0x24a234(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x90(%rbp)
movq -0x70(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq 0x18(%rbp), %r8
movq -0x90(%rbp), %rax
leaq 0x23d7dc(%rip), %r9     # 0x38c9a8
movq %rax, (%rsp)
callq 0xf1c10
movq -0x90(%rbp), %rdi
callq 0x21a0
leaq 0x275610(%rip), %rax    # 0x3c47f8
cmpl $0x0, 0x150(%rax)
je 0x14f29a
movq -0x88(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x23a55e(%rip), %rsi    # 0x389765
movb $0x0, %al
callq 0x2050
movq -0x88(%rbp), %rdi
leaq 0x24a1b6(%rip), %rsi    # 0x3993d2
callq 0x2200
movq %rax, -0x90(%rbp)
movq -0x70(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rcx
movq -0x38(%rbp), %r8
movl 0x20(%rbp), %r14d
movl 0x28(%rbp), %ebx
leaq 0x2755b2(%rip), %rax    # 0x3c47f8
movl 0x150(%rax), %r11d
movq -0x90(%rbp), %rax
leaq 0x276075(%rip), %rdx    # 0x3c52d0
leaq -0x68(%rbp), %r9
leaq 0x23d742(%rip), %r10    # 0x38c9a8
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 -0x90(%rbp), %rdi
callq 0x21a0
movq -0x88(%rbp), %rdi
callq 0x2210
movq -0x70(%rbp), %rdi
callq 0xcd6f0
movq -0x58(%rbp), %rdi
callq 0x2210
addq $0xa8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)