// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: generate_haplotypes_DP(haplotype_evdience_alloc*, overlap_region_alloc*, All_reads*, long long, int)
int generate_haplotypes_DP(haplotype_evdience_alloc* hap, overlap_region_alloc* overlap_list, All_reads* R_INF, long long rLen, 
int force_repeat)
{
    int j, i;
    int vectorID, vectorID2;
    int8_t *vector, *vector2;

    
    // if(hap->available_snp == 0)
    if(hap->snp_stat.n == 0)
    {
        return 0;
    }

    ///debug_print_snp_stat("m64013_190324_024932/23660629/ccs", hap, overlap_list, R_INF);

    
    ///if hap->available_snp == 1, the following codes would have bugs
    ///filter snps that are highly likly false 
    // if(hap->available_snp > 1)
    if(hap->snp_stat.n > 1)
    {
        i = 0;
        ///if a snp is very close to others, it should not be a real snp
        for (j = 0; j < (int)hap->snp_stat.n/**hap->available_snp**/; j++)
        {
            if(j > 0 && j + 1 < (int)hap->snp_stat.n)
            {
                if(hap->snp_stat.a[j].site != hap->snp_stat.a[j - 1].site + 1
                    &&
                hap->snp_stat.a[j].site + 1 != hap->snp_stat.a[j + 1].site)
                {
                    hap->snp_stat.a[i] = hap->snp_stat.a[j];
                    i++;
                }

            }
            else if(j == 0)
            {
                if(hap->snp_stat.a[j].site + 1 != hap->snp_stat.a[j + 1].site)
                {
                    hap->snp_stat.a[i] = hap->snp_stat.a[j];
                    i++;
                }
            }
            else
            {
                if(hap->snp_stat.a[j].site != hap->snp_stat.a[j - 1].site + 1)
                {
                    hap->snp_stat.a[i] = hap->snp_stat.a[j];
                    i++;
                }
            }
        }
        // hap->available_snp = i;
        hap->snp_stat.n = i;
    }


    
    


    int flag;
    long long overlap_length, total_read, unuseful_read;
    total_read = unuseful_read = 0;
    ///check if any read may be conflict with others
    for (i = 0; i < (long long)overlap_list->length; i++)
    {
        overlap_length = overlap_list->list[i].x_pos_e - overlap_list->list[i].x_pos_s + 1;
        if (overlap_list->list[i].is_match == 1)
        {
            total_read++;
            flag = -1;
            for (j = 0; j < (int)hap->snp_stat.n; j++)
            {
                vectorID = hap->snp_stat.a[j].id;
                vector = Get_SNP_Vector((*hap), vectorID);

                ///flag == -1 means there are no useful signals yet
                if (flag == -1)
                {
                    if((vector[i] == 0 || vector[i] == 1 ))
                    {
                        flag = 0;
                    }
                }///flag == 0 means there is at least one useful signal yet
                else if (flag == 0) 
                {
                    if(vector[i] != 0 && vector[i] != 1)
                    {
                        flag = 2;
                    }
                }///flag == 0 means there is at least one useful signal first, and another unuseful signal after that
                else if(flag == 2) 
                {
                    if((vector[i] == 0 || vector[i] == 1 ))
                    {
                        flag = 3;
                        break;
                    }
                }
            }


            if(flag == 3) ///Fix-attention: definitely wrong
            {
                unuseful_read++;
                for (j = 0; j < (int)hap->snp_stat.n; j++)
                {
                    vectorID = hap->snp_stat.a[j].id;
                    vector = Get_SNP_Vector((*hap), vectorID);
                    

                    
                    if(vector[i] == 0)
                    {
                        hap->snp_stat.a[j].occ_0--;
                        hap->snp_stat.a[j].occ_2++;
                    }
                    else if(vector[i] == 1)
                    {
                        hap->snp_stat.a[j].occ_1--;
                        hap->snp_stat.a[j].occ_2++;
                    }
                    else if(vector[i] != 2)
                    {
                        hap->snp_stat.a[j].occ_2++;
                    }
                    

                    vector[i] = 2;
                }
                
                ///this read may be unuseful
                ///overlap_list->list[i].is_match = 0;
                ///overlap_list->list[i].is_match = 2;
                overlap_list->list[i].is_match = 4;
                ///overlap_list->mapped_overlaps--;
                overlap_list->mapped_overlaps_length -= overlap_length;
            }
        }
    }
    

    /*******************************DP********************************/
    init_DP_matrix(&(hap->dp), hap->snp_stat.n);

    long long equal_best = 0;
    uint32_t* column;

    

    for (i = 0; i < (int)hap->snp_stat.n; i++)
    {
        ///vector of snp i
        vectorID = hap->snp_stat.a[i].id;
        vector = Get_SNP_Vector((*hap), vectorID);
        hap->dp.visit[i] = 0;
        hap->dp.max[i] = 1;
        hap->dp.backtrack_length[i] = 0;
        equal_best = 0;
        column = Get_DP_Backtrack_Column(hap->dp, i);

        for (j = 0; j < i; j++)
        {
            ///vector of snp j
            vectorID2 = hap->snp_stat.a[j].id;
            vector2 = Get_SNP_Vector((*hap), vectorID2);

            ///vector is compatible with vector2
            if(calculate_distance_snp_vector(vector, vector2, Get_SNP_Vector_Length((*hap))) == 0)
            {
                
                if(hap->dp.max[i] < hap->dp.max[j] + 1)
                {
                    hap->dp.max[i] = hap->dp.max[j] + 1;

                    column[0] = j;
                    equal_best = 1;
                }
                else if(hap->dp.max[i] == hap->dp.max[j] + 1)
                {
                    column[equal_best] = j;
                    equal_best++;   
                }
                
                
            }
        }

        hap->dp.backtrack_length[i] = equal_best;
    }

    /*******************************DP********************************/

    uint64_t tmp_mode = 0;

    for (i = 0; i < (int)hap->snp_stat.n; i++)
    {
        tmp_mode = hap->dp.max[i];
        tmp_mode = tmp_mode << 32;
        tmp_mode = tmp_mode | (uint64_t)(i);
        hap->dp.max_for_sort[i] = tmp_mode;
    }
    
    qsort(hap->dp.max_for_sort, hap->snp_stat.n, sizeof(uint64_t), cmp_max_DP);


    int snpID;
    ///the minmum snp_num is 1
    hap->dp.max_snp_num = 0;
    hap->dp.max_score = -2;
    

    for (i = 0; i < (int)hap->snp_stat.n; i++)
    {
        snpID = Get_Max_DP_ID(hap->dp.max_for_sort[i]);
        if(hap->dp.visit[snpID] == 0)
        {
            hap->dp.current_snp_num = Get_Max_DP_Value(hap->dp.max_for_sort[i]);
            Preorder_Merge_Advance_Repeat(snpID, hap, 0);
        }
    }

    ///debug_print_snp_stat("m64013_190324_024932/23660629/ccs", hap, overlap_list, R_INF);


    //if(hap->dp.max_snp_num > 0)
    if(hap->snp_stat.n > 0)
    {
        process_repeat_snps(hap, overlap_list);
        return 1;
    }
    else
    {
        return 0;
    }
}

// Assembly:
movq 0x220(%rdi), %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x52128
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r15
cmpq $0x1, %rcx
je 0x51dd5
movq 0x220(%r15), %rsi
xorl %eax, %eax
testl %esi, %esi
jle 0x51dce
movl $0x1, %ecx
xorl %edx, %edx
testq %rdx, %rdx
je 0x51d6b
movslq %esi, %r10
movq 0x218(%r15), %rsi
leaq (%rsi,%rdx), %rdi
movl -0x8(%rsi,%rdx), %r9d
movl 0x20(%rsi,%rdx), %r8d
incl %r9d
cmpq %r10, %rcx
jge 0x51dc5
cmpl %r9d, %r8d
je 0x51da5
incl %r8d
cmpl 0x48(%rsi,%rdx), %r8d
jne 0x51d82
jmp 0x51da5
movq 0x218(%r15), %rsi
movl 0x20(%rsi), %r8d
incl %r8d
movq %rsi, %rdi
cmpl 0x48(%rsi), %r8d
je 0x51da5
cltq
leaq (%rax,%rax,4), %r8
movq 0x20(%rdi), %r9
movq %r9, 0x20(%rsi,%r8,8)
movups (%rdi), %xmm0
movups 0x10(%rdi), %xmm1
movups %xmm1, 0x10(%rsi,%r8,8)
movups %xmm0, (%rsi,%r8,8)
incl %eax
movq 0x220(%r15), %rsi
movslq %esi, %rdi
addq $0x28, %rdx
leaq 0x1(%rcx), %r8
cmpq %rdi, %rcx
movq %r8, %rcx
jl 0x51d35
jmp 0x51dcc
cmpl %r9d, %r8d
jne 0x51d82
jmp 0x51da5
cltq
movq %rax, 0x220(%r15)
cmpq $0x0, 0x10(%rbx)
jle 0x51f29
xorl %eax, %eax
movq (%rbx), %rdi
imulq $0xa8, %rax, %rcx
cmpb $0x1, 0x2c(%rdi,%rcx)
jne 0x51f1c
movq 0x220(%r15), %rsi
testl %esi, %esi
jle 0x51f1c
movl 0x8(%rdi,%rcx), %edx
subl 0x4(%rdi,%rcx), %edx
incl %edx
movq 0x218(%r15), %rdi
movq 0x1d0(%r15), %r8
movl 0x1c8(%r15), %r9d
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl (%rdi), %r11d
imull %r9d, %r11d
addq %r8, %r11
cmpl $0x2, %r10d
je 0x51e6e
testl %r10d, %r10d
je 0x51e5d
cmpl $-0x1, %r10d
jne 0x51e7b
cmpb $0x2, (%r11,%rax)
movl $0x0, %r10d
adcl $-0x1, %r10d
jmp 0x51e7b
xorl %r10d, %r10d
cmpb $0x2, (%r11,%rax)
setae %r10b
addl %r10d, %r10d
jmp 0x51e7b
movl $0x2, %r10d
cmpb $0x2, (%r11,%rax)
jb 0x51e86
addq $0x28, %rdi
decq %rsi
jne 0x51e31
jmp 0x51e8c
movl $0x3, %r10d
cmpl $0x3, %r10d
jne 0x51f1c
cmpl $0x0, 0x220(%r15)
jle 0x51f10
movl $0x10, %esi
xorl %edi, %edi
movq 0x218(%r15), %r9
movl 0x1c8(%r15), %r8d
imull -0x10(%r9,%rsi), %r8d
addq 0x1d0(%r15), %r8
movzbl (%rax,%r8), %r10d
cmpl $0x2, %r10d
je 0x51ef8
cmpl $0x1, %r10d
je 0x51edf
testl %r10d, %r10d
jne 0x51ef2
decl -0x8(%r9,%rsi)
jmp 0x51ee4
decl -0x4(%r9,%rsi)
leaq (%rdi,%rdi,4), %r10
leaq (%r9,%r10,8), %r9
addq $0x10, %r9
jmp 0x51ef5
addq %rsi, %r9
incl (%r9)
movb $0x2, (%r8,%rax)
incq %rdi
movslq 0x220(%r15), %r8
addq $0x28, %rsi
cmpq %r8, %rdi
jl 0x51ea7
movq (%rbx), %rsi
movb $0x4, 0x2c(%rsi,%rcx)
subq %rdx, 0x18(%rbx)
incq %rax
cmpq %rax, 0x10(%rbx)
jg 0x51de2
leaq 0x250(%r15), %rdi
movl 0x220(%r15), %esi
callq 0x8786d
cmpl $0x0, 0x220(%r15)
movq %rbx, (%rsp)
jle 0x52048
xorl %eax, %eax
movq 0x218(%r15), %rdx
leaq (%rax,%rax,4), %rsi
movl 0x1c8(%r15), %ecx
imull (%rdx,%rsi,8), %ecx
addq 0x1d0(%r15), %rcx
movq 0x258(%r15), %rdx
movb $0x0, (%rdx,%rax)
movq 0x260(%r15), %rsi
movl $0x1, (%rsi,%rax,4)
movq 0x278(%r15), %rdx
movl $0x0, (%rdx,%rax,4)
movl $0x0, %r8d
testq %rax, %rax
je 0x52031
movl 0x250(%r15), %edi
imull %eax, %edi
shlq $0x2, %rdi
addq 0x280(%r15), %rdi
movq 0x1d0(%r15), %r9
movq 0x218(%r15), %r10
xorl %r11d, %r11d
xorl %r8d, %r8d
movslq 0x1c8(%r15), %r12
testq %r12, %r12
jle 0x52007
leaq (%r11,%r11,4), %rbx
movl (%r10,%rbx,8), %r13d
imull %r12d, %r13d
addq %r9, %r13
xorl %ebp, %ebp
movb (%rcx,%rbp), %r14b
movb (%r13,%rbp), %bl
cmpb %bl, %r14b
je 0x51fff
orb %r14b, %bl
cmpb $0x2, %bl
jb 0x52029
incq %rbp
cmpq %rbp, %r12
jne 0x51fe9
movl (%rsi,%r11,4), %ebp
incl %ebp
cmpl %ebp, (%rsi,%rax,4)
jae 0x52020
movl %ebp, (%rsi,%rax,4)
movl %r11d, (%rdi)
movl $0x1, %r8d
jmp 0x52029
jne 0x52029
movl %r11d, (%rdi,%r8,4)
incq %r8
incq %r11
cmpq %rax, %r11
jne 0x51fcc
movl %r8d, (%rdx,%rax,4)
incq %rax
movslq 0x220(%r15), %rcx
cmpq %rcx, %rax
jl 0x51f50
movq 0x220(%r15), %rsi
testl %esi, %esi
jle 0x52083
movq 0x260(%r15), %rax
movq 0x268(%r15), %rcx
xorl %edx, %edx
movl (%rax,%rdx,4), %esi
shlq $0x20, %rsi
addq %rdx, %rsi
movq %rsi, (%rcx,%rdx,8)
incq %rdx
movq 0x220(%r15), %rsi
movslq %esi, %rdi
cmpq %rdi, %rdx
jl 0x52063
movq 0x268(%r15), %rdi
leaq -0x14fd(%rip), %rcx     # 0x50b94
movl $0x8, %edx
callq 0x23f0
movabsq $-0x200000000, %rax     # imm = 0xFFFFFFFE00000000
movq %rax, 0x2a0(%r15)
movq 0x220(%r15), %rax
testl %eax, %eax
jle 0x520ff
xorl %r12d, %r12d
movq 0x258(%r15), %rax
movq 0x268(%r15), %rcx
movq (%rcx,%r12,8), %rdi
movslq %edi, %rcx
cmpb $0x0, (%rax,%rcx)
jne 0x520ed
movq %rdi, %rax
shrq $0x20, %rax
movl %eax, 0x2a8(%r15)
movq %r15, %rsi
xorl %edx, %edx
callq 0x511eb
incq %r12
movq 0x220(%r15), %rax
movslq %eax, %rcx
cmpq %rcx, %r12
jl 0x520ba
testq %rax, %rax
movl $0x0, %eax
je 0x5211a
movq %r15, %rdi
movq (%rsp), %rsi
callq 0x518c3
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq