// File: /workspace/github_cpp_makefile_star2_O1/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 0x5108d
movq %rdi, %r14
movq 0x218(%rdi), %rdi
movq 0x220(%r14), %rsi
leaq -0x3fe(%rip), %rcx      # 0x50b67
movl $0x28, %edx
callq 0x23f0
movl 0x1c4(%r14), %ebp
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x50e33
cmpl $0x4, %ebp
ja 0x50f91
testl %eax, %eax
jne 0x50fc2
jmp 0x5108d
testl %eax, %eax
jne 0x50fc2
movl 0x1c4(%r14), %ecx
decl %ecx
movl %ecx, %r15d
testl %ecx, %ecx
js 0x50fb8
movq %r14, %rdi
movl %r15d, %esi
callq 0x50e33
leal -0x1(%r15), %ecx
testl %eax, %eax
je 0x50f9e
cmpl $-0x1, %r15d
je 0x5108d
movslq 0x1c4(%r14), %r15
cmpl 0x220(%r14), %r15d
jge 0x5103c
movq 0x1e0(%r14), %r12
movq 0x218(%r14), %rax
leaq (%r15,%r15,4), %rcx
movslq 0x1c8(%r14), %rdx
movl (%rax,%rcx,8), %esi
imull %edx, %esi
addq 0x1d0(%r14), %rsi
testq %rdx, %rdx
jle 0x51025
xorl %eax, %eax
movb (%r12,%rax), %cl
movb (%rsi,%rax), %dil
cmpb %dil, %cl
setne %r8b
orb %cl, %dil
cmpb $0x2, %dil
setb %cl
testb %cl, %r8b
jne 0x5102d
incq %rax
cmpq %rax, %rdx
jne 0x50fff
movq %r14, %rdi
callq 0x50d60
incq %r15
movslq 0x220(%r14), %rax
cmpq %rax, %r15
jl 0x50fd9
cmpl $0x1, 0x1c4(%r14)
jne 0x51088
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 0x5108d
cmpl $0x4, %ecx
jl 0x5108d
cvtsi2sd %edx, %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd 0x1d4456(%rip), %xmm1   # 0x2254d8
ucomisd %xmm0, %xmm1
ja 0x5108d
movl $0x1, %ebx
movl %ebx, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq