// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
xorl %ebx, %ebx
cmpl $0x0, 0x1c4(%rdi)
je 0x501fd
movq %rdi, %r14
movq 0x218(%rdi), %rdi
movq 0x220(%r14), %rsi
leaq -0x3f1(%rip), %rcx      # 0x4fcde
movl $0x28, %edx
callq 0x23f0
movl 0x1c4(%r14), %ebp
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4ff9b
cmpl $0x4, %ebp
ja 0x500fb
testl %eax, %eax
jne 0x50127
jmp 0x501fd
testl %eax, %eax
jne 0x50127
movl 0x1c4(%r14), %ebp
decl %ebp
testl %ebp, %ebp
js 0x5011e
movq %r14, %rdi
movl %ebp, %esi
callq 0x4ff9b
decl %ebp
testl %eax, %eax
je 0x50108
jmp 0x50127
cmpl $-0x1, %ebp
je 0x501fd
movl 0x1c4(%r14), %ecx
movq 0x220(%r14), %rax
cmpl %eax, %ecx
jge 0x501b1
movq 0x1e0(%r14), %r15
movslq %ecx, %r12
movq 0x218(%r14), %rcx
leaq (%r12,%r12,4), %rsi
movslq 0x1c8(%r14), %rdx
movl (%rcx,%rsi,8), %esi
imull %edx, %esi
addq 0x1d0(%r14), %rsi
testq %rdx, %rdx
jle 0x50190
xorl %ecx, %ecx
movb (%r15,%rcx), %dil
movb (%rsi,%rcx), %r8b
cmpb %r8b, %dil
setne %r9b
orb %dil, %r8b
cmpb $0x2, %r8b
setb %dil
testb %dil, %r9b
jne 0x5019f
incq %rcx
cmpq %rcx, %rdx
jne 0x50169
movq %r14, %rdi
callq 0x4fec8
movq 0x220(%r14), %rax
incq %r12
movslq %eax, %rcx
cmpq %rcx, %r12
jl 0x50143
movl 0x1c4(%r14), %ecx
cmpl $0x1, %ecx
jne 0x501f8
movl 0x1f0(%r14), %eax
movl 0x1f4(%r14), %ecx
incl %ecx
movl 0x1f8(%r14), %edx
incl %eax
cmpl %edx, %ecx
cmovll %ecx, %edx
cmpl $0xa, %eax
jl 0x501fd
cmpl $0x4, %ecx
jl 0x501fd
cvtsi2sd %edx, %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd 0x1e13e6(%rip), %xmm1   # 0x2315d8
ucomisd %xmm0, %xmm1
ja 0x501fd
movl $0x1, %ebx
movl %ebx, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq