// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r13
movq 0x220(%rdi), %rsi
testq %rsi, %rsi
je 0x23192
movq %rdi, %r14
cmpq $0x1, %rsi
je 0x22e89
xorl %eax, %eax
xorl %edx, %edx
xorl %ecx, %ecx
movslq %esi, %rdi
cmpq %rdi, %rdx
jge 0x22e7f
testq %rax, %rax
je 0x22e2d
leal 0x1(%rdx), %r11d
movq 0x218(%r14), %rdi
leaq (%rdi,%rax), %r8
movl -0x8(%rdi,%rax), %r10d
movl 0x20(%rdi,%rax), %r9d
incl %r10d
cmpl %esi, %r11d
jge 0x22e78
cmpl %r10d, %r9d
je 0x22e65
incl %r9d
cmpl 0x48(%rdi,%rax), %r9d
jne 0x22e41
jmp 0x22e65
movq 0x218(%r14), %rdi
movl 0x20(%rdi), %esi
incl %esi
movq %rdi, %r8
cmpl 0x48(%rdi), %esi
je 0x22e65
movslq %ecx, %rcx
imulq $0x28, %rcx, %rsi
movq 0x20(%r8), %r9
movq %r9, 0x20(%rdi,%rsi)
movups (%r8), %xmm0
movups 0x10(%r8), %xmm1
movups %xmm1, 0x10(%rdi,%rsi)
movups %xmm0, (%rdi,%rsi)
incl %ecx
incq %rdx
movq 0x220(%r14), %rsi
addq $0x28, %rax
jmp 0x22dea
cmpl %r10d, %r9d
jne 0x22e41
jmp 0x22e65
movslq %ecx, %rsi
movq %rsi, 0x220(%r14)
xorl %eax, %eax
pushq $-0x1
popq %rcx
pushq $0x2
popq %rdx
pushq $0x10
popq %rdi
cmpq %rax, 0x10(%r13)
jle 0x22fb7
movq (%r13), %r10
imulq $0xa8, %rax, %r8
cmpb $0x1, 0x2c(%r10,%r8)
jne 0x22faf
movl 0x8(%r10,%r8), %r9d
subl 0x4(%r10,%r8), %r9d
incl %r9d
testl %esi, %esi
movl $0x0, %r10d
cmovgl %esi, %r10d
imulq $0x28, %r10, %r10
xorl %r11d, %r11d
movl %ecx, %ebx
cmpq %r11, %r10
je 0x22faf
movq 0x218(%r14), %r12
movl 0x1c8(%r14), %r15d
imull (%r12,%r11), %r15d
addq 0x1d0(%r14), %r15
cmpl $0x2, %ebx
je 0x22f25
testl %ebx, %ebx
je 0x22f17
cmpl $-0x1, %ebx
jne 0x22f2e
cmpb $0x2, (%r15,%rax)
movl $0x0, %ebx
adcl $-0x1, %ebx
jmp 0x22f2e
xorl %ebx, %ebx
cmpb $0x2, (%r15,%rax)
setae %bl
addl %ebx, %ebx
jmp 0x22f2e
cmpb $0x1, (%r15,%rax)
movl %edx, %ebx
jbe 0x22f34
addq $0x28, %r11
jmp 0x22ed7
movq %rdi, %r10
xorl %r11d, %r11d
movslq %esi, %rbx
cmpq %rbx, %r11
jge 0x22fa1
movq 0x218(%r14), %rbx
movl 0x1c8(%r14), %esi
imull -0x10(%rbx,%r10), %esi
addq 0x1d0(%r14), %rsi
movzbl (%rax,%rsi), %ebp
cmpl $0x2, %ebp
je 0x22f8d
cmpl $0x1, %ebp
je 0x22f76
testl %ebp, %ebp
jne 0x22f88
decl -0x8(%rbx,%r10)
jmp 0x22f7b
decl -0x4(%rbx,%r10)
imulq $0x28, %r11, %r15
addq %r15, %rbx
addq $0x10, %rbx
jmp 0x22f8b
addq %r10, %rbx
incl (%rbx)
movb $0x2, (%rsi,%rax)
incq %r11
movq 0x220(%r14), %rsi
addq $0x28, %r10
jmp 0x22f3a
movq (%r13), %r10
movb $0x4, 0x2c(%r10,%r8)
subq %r9, 0x18(%r13)
incq %rax
jmp 0x22e94
leaq 0x250(%r14), %rdi
callq 0x53954
xorl %r12d, %r12d
movq %r13, 0x18(%rsp)
movq 0x220(%r14), %rsi
movslq %esi, %rax
cmpq %rax, %r12
jge 0x230db
movq 0x218(%r14), %rax
imulq $0x28, %r12, %rcx
movl 0x1c8(%r14), %r15d
imull (%rax,%rcx), %r15d
addq 0x1d0(%r14), %r15
movq 0x258(%r14), %rax
movb $0x0, (%rax,%r12)
movq 0x260(%r14), %rbx
movl $0x1, (%rbx,%r12,4)
movq 0x278(%r14), %rax
movq %rax, 0x20(%rsp)
andl $0x0, (%rax,%r12,4)
movl 0x250(%r14), %eax
imull %r12d, %eax
shlq $0x2, %rax
addq 0x280(%r14), %rax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
movq $0x0, 0x8(%rsp)
cmpq %r13, %r12
je 0x230c0
movq 0x218(%r14), %rax
movl 0x1c8(%r14), %edx
movl (%rax,%rbp), %esi
imull %edx, %esi
addq 0x1d0(%r14), %rsi
movq %r15, %rdi
callq 0x21eb1
testl %eax, %eax
jne 0x230b7
movl (%rbx,%r13,4), %eax
incl %eax
cmpl %eax, (%rbx,%r12,4)
jae 0x2309f
movl %eax, (%rbx,%r12,4)
movq 0x10(%rsp), %rax
movl %r13d, (%rax)
pushq $0x1
popq %rax
movq %rax, 0x8(%rsp)
jmp 0x230b7
jne 0x230b7
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rcx
movl %r13d, (%rax,%rcx,4)
incq %rcx
movq %rcx, 0x8(%rsp)
incq %r13
addq $0x28, %rbp
jmp 0x23051
movq 0x20(%rsp), %rax
movq 0x8(%rsp), %rcx
movl %ecx, (%rax,%r12,4)
incq %r12
movq 0x18(%rsp), %r13
jmp 0x22fcb
xorl %eax, %eax
movslq %esi, %rcx
cmpq %rcx, %rax
jge 0x2310d
movq 0x260(%r14), %rcx
movq 0x268(%r14), %rdx
movl (%rcx,%rax,4), %ecx
shlq $0x20, %rcx
addq %rax, %rcx
movq %rcx, (%rdx,%rax,8)
incq %rax
movq 0x220(%r14), %rsi
jmp 0x230dd
movq 0x268(%r14), %rdi
leaq -0x13c9(%rip), %rcx     # 0x21d52
pushq $0x8
popq %rdx
callq 0x23f0
movabsq $-0x200000000, %rax     # imm = 0xFFFFFFFE00000000
movq %rax, 0x2a0(%r14)
xorl %ebx, %ebx
movq 0x220(%r14), %rax
movslq %eax, %rcx
cmpq %rcx, %rbx
jge 0x2317d
movq 0x258(%r14), %rax
movq 0x268(%r14), %rcx
movq (%rcx,%rbx,8), %rdi
movslq %edi, %rcx
cmpb $0x0, (%rax,%rcx)
jne 0x23178
movq %rdi, %rax
shrq $0x20, %rax
movl %eax, 0x2a8(%r14)
movq %r14, %rsi
xorl %edx, %edx
callq 0x2247b
incq %rbx
jmp 0x23136
testq %rax, %rax
je 0x23192
movq %r14, %rdi
movq %r13, %rsi
callq 0x22a04
pushq $0x1
popq %rax
jmp 0x23194
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq