// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: generate_haplotypes(haplotype_evdience_alloc*)
int generate_haplotypes(haplotype_evdience_alloc* hap)
{
    int j;

    int vectorID2;
    int8_t *vector, *vector2;
    
    if(hap->core_snp == 0)
    {
        return 0;
    }

    ///sort by weight
    // qsort(hap->snp_stat, hap->available_snp, sizeof(SnpStats), cmp_snp_stats);
    qsort(hap->snp_stat.a, hap->snp_stat.n, sizeof(SnpStats), cmp_snp_stats);


    ///the hap->core_snp is used to find centriod    
    ///if there are <5 vectors in core_snp, we didn't allow different vector
    if (hap->core_snp < 5)
    {
        if(merge_snp_vectors_and_test(hap, -1) == 0)
        {
            return 0;
        }
    }
    else ///for vectors in core_snp, we allow at most one different vector when there are >= 5 vectors in core_snp
    {
        ///there are two condition: 1. vector 0 is the different one. 2. vector 0 is not the different one        
        ///first try to merge all vector together
        if(merge_snp_vectors_and_test(hap, -1) == 0)
        {
            for (j = hap->core_snp - 1; j >= 0; j--)
            {
                if(merge_snp_vectors_and_test(hap, j) == 1)
                {
                    break;
                }
            }

            if(j == -1)
            {
                return 0;
            }
        }
    }


    

    ///after merge, we get result vector
    vector = Get_Result_SNP_Vector((*hap));
    ///and for each non-core snp vector, if it has no conflict with result vector
    /// add it to result vector
    for (j = hap->core_snp; j < (int)hap->snp_stat.n/**hap->available_snp**/; j++)
    {
        vectorID2 = hap->snp_stat.a[j].id;
        vector2 = Get_SNP_Vector((*hap), vectorID2);
        if(calculate_distance_snp_vector(vector, vector2, Get_SNP_Vector_Length((*hap))) == 0)
        {
            add_to_result_snp_vector(hap, vector2, Get_SNP_Vector_Length((*hap)));
        }
    }


    ///for read only have 1 snp, we need a more strict condition
    if (hap->core_snp == 1 && 
    filter_one_snp(hap->result_stat.occ_0 + 1, hap->result_stat.occ_1, 
    hap->result_stat.overlap_num + 1) == 0)
    {
        return 0;
    }


    return 1;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
xorl %r15d, %r15d
cmpl $0x0, 0x1c4(%rdi)
je 0x22308
movq %rdi, %r14
movq 0x218(%rdi), %rdi
movq 0x220(%r14), %rsi
leaq -0x4f7(%rip), %rcx      # 0x21d25
pushq $0x28
popq %rdx
callq 0x23f0
movl 0x1c4(%r14), %ebx
pushq $-0x1
popq %rsi
movq %r14, %rdi
callq 0x220f3
cmpl $0x4, %ebx
ja 0x22244
testl %eax, %eax
jne 0x22270
jmp 0x22308
testl %eax, %eax
jne 0x22270
movl 0x1c4(%r14), %ebp
decl %ebp
testl %ebp, %ebp
js 0x22267
movq %r14, %rdi
movl %ebp, %esi
callq 0x220f3
decl %ebp
testl %eax, %eax
je 0x22251
jmp 0x22270
cmpl $-0x1, %ebp
je 0x22308
movq 0x1e0(%r14), %r15
movslq 0x1c4(%r14), %r13
imulq $0x28, %r13, %rbx
movslq 0x220(%r14), %rax
cmpq %rax, %r13
jge 0x222d2
movq 0x218(%r14), %rax
movl 0x1c8(%r14), %ebp
movl (%rax,%rbx), %r12d
imull %ebp, %r12d
addq 0x1d0(%r14), %r12
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x21eb1
testl %eax, %eax
jne 0x222c9
movq %r14, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x2201d
incq %r13
addq $0x28, %rbx
jmp 0x22282
cmpl $0x1, 0x1c4(%r14)
movl $0x0, %r15d
jne 0x22304
movl 0x1f0(%r14), %edx
movl 0x1f4(%r14), %edi
incl %edi
movl 0x1f8(%r14), %esi
incl %edx
callq 0x535b7
testl %eax, %eax
je 0x22308
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq