// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c4(%rax)
jne 0x3e5c5
movl $0x0, -0x4(%rbp)
jmp 0x3e776
movq -0x10(%rbp), %rax
movq 0x218(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x220(%rax), %rsi
movl $0x28, %edx
leaq -0xb77(%rip), %rcx      # 0x3da70
callq 0x23e0
movq -0x10(%rbp), %rax
cmpl $0x5, 0x1c4(%rax)
jae 0x3e61a
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x3e3a0
cmpl $0x0, %eax
jne 0x3e618
movl $0x0, -0x4(%rbp)
jmp 0x3e776
jmp 0x3e679
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x3e3a0
cmpl $0x0, %eax
jne 0x3e677
movq -0x10(%rbp), %rax
movl 0x1c4(%rax), %eax
subl $0x1, %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jl 0x3e663
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x3e3a0
cmpl $0x1, %eax
jne 0x3e656
jmp 0x3e663
jmp 0x3e658
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3e63d
cmpl $-0x1, -0x14(%rbp)
jne 0x3e675
movl $0x0, -0x4(%rbp)
jmp 0x3e776
jmp 0x3e677
jmp 0x3e679
movq -0x10(%rbp), %rax
movq 0x1e0(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c4(%rax), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x220(%rcx), %rcx
cmpl %ecx, %eax
jge 0x3e72b
movq -0x10(%rbp), %rax
movq 0x218(%rax), %rax
movslq -0x14(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x1d0(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x1c8(%rcx), %ecx
imull -0x18(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x1c8(%rax), %edx
callq 0x3dea0
cmpl $0x0, %eax
jne 0x3e71b
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x1c8(%rax), %edx
callq 0x3e190
jmp 0x3e71d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3e695
movq -0x10(%rbp), %rax
cmpl $0x1, 0x1c4(%rax)
jne 0x3e76f
movq -0x10(%rbp), %rax
movl 0x1f4(%rax), %edi
addl $0x1, %edi
movq -0x10(%rbp), %rax
movl 0x1f8(%rax), %esi
movq -0x10(%rbp), %rax
movl 0x1f0(%rax), %edx
addl $0x1, %edx
callq 0xa14a0
cmpl $0x0, %eax
jne 0x3e76f
movl $0x0, -0x4(%rbp)
jmp 0x3e776
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop