// File: /workspace/github_cpp_makefile_star2_O3/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 %rsi, %rcx
movq 0x220(%rdi), %rsi
xorl %eax, %eax
testq %rsi, %rsi
je 0x51249
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r15
cmpq $0x1, %rsi
movq %rcx, (%rsp)
je 0x50f09
xorl %eax, %eax
testl %esi, %esi
jle 0x50f00
movl $0x1, %ecx
xorl %edx, %edx
testq %rdx, %rdx
je 0x50e96
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 0x50ef0
cmpl %r9d, %r8d
je 0x50ed0
incl %r8d
cmpl 0x48(%rsi,%rdx), %r8d
jne 0x50ead
jmp 0x50ed0
movq 0x218(%r15), %rsi
movl 0x20(%rsi), %r8d
incl %r8d
movq %rsi, %rdi
cmpl 0x48(%rsi), %r8d
je 0x50ed0
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 0x50e60
jmp 0x50ef7
cmpl %r9d, %r8d
jne 0x50ead
jmp 0x50ed0
movslq %eax, %rsi
movq (%rsp), %rcx
jmp 0x50f02
xorl %esi, %esi
movq %rsi, 0x220(%r15)
movq %rcx, %rax
movq 0x10(%rcx), %rcx
testq %rcx, %rcx
jle 0x51056
movq (%rax), %rdx
xorl %eax, %eax
imulq $0xa8, %rax, %rdi
cmpb $0x1, 0x2c(%rdx,%rdi)
jne 0x5104a
testl %esi, %esi
jle 0x5104a
movl 0x8(%rdx,%rdi), %r8d
subl 0x4(%rdx,%rdi), %r8d
incl %r8d
movq 0x218(%r15), %r9
movq 0x1d0(%r15), %r10
movl 0x1c8(%r15), %r11d
movl %esi, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl (%r9), %r13d
imull %r11d, %r13d
addq %r10, %r13
cmpl $0x2, %ebp
je 0x50fa1
testl %ebp, %ebp
je 0x50f91
cmpl $-0x1, %ebp
jne 0x50fae
cmpb $0x2, (%r13,%rax)
movl $0x0, %ebp
adcl $-0x1, %ebp
jmp 0x50fae
xorl %ebp, %ebp
cmpb $0x2, (%r13,%rax)
setae %bpl
addl %ebp, %ebp
jmp 0x50fae
movl $0x2, %ebp
cmpb $0x2, (%r13,%rax)
jb 0x50fbc
addq $0x28, %r9
decq %r12
jne 0x50f69
jmp 0x5104a
testl %esi, %esi
jle 0x5103d
movl $0x10, %ecx
xorl %edx, %edx
movq 0x218(%r15), %r9
movl 0x1c8(%r15), %esi
imull -0x10(%r9,%rcx), %esi
addq 0x1d0(%r15), %rsi
movzbl (%rax,%rsi), %r10d
cmpl $0x2, %r10d
je 0x51018
cmpl $0x1, %r10d
je 0x50fff
testl %r10d, %r10d
jne 0x51012
decl -0x8(%r9,%rcx)
jmp 0x51004
decl -0x4(%r9,%rcx)
leaq (%rdx,%rdx,4), %r10
leaq (%r9,%r10,8), %r9
addq $0x10, %r9
jmp 0x51015
addq %rcx, %r9
incl (%r9)
movb $0x2, (%rsi,%rax)
incq %rdx
movq 0x220(%r15), %rsi
movslq %esi, %r9
addq $0x28, %rcx
cmpq %r9, %rdx
jl 0x50fc7
movq (%rsp), %rcx
movq (%rcx), %rdx
movq 0x10(%rcx), %rcx
movb $0x4, 0x2c(%rdx,%rdi)
movq (%rsp), %rdi
subq %r8, 0x18(%rdi)
incq %rax
cmpq %rax, %rcx
jg 0x50f1e
leaq 0x250(%r15), %rdi
callq 0x86db2
movq 0x220(%r15), %rsi
testl %esi, %esi
jle 0x511a4
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 0x51154
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 0x5112a
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 0x51122
orb %r14b, %bl
cmpb $0x2, %bl
jb 0x5114c
incq %rbp
cmpq %rbp, %r12
jne 0x5110c
movl (%rsi,%r11,4), %ebp
incl %ebp
cmpl %ebp, (%rsi,%rax,4)
jae 0x51143
movl %ebp, (%rsi,%rax,4)
movl %r11d, (%rdi)
movl $0x1, %r8d
jmp 0x5114c
jne 0x5114c
movl %r11d, (%rdi,%r8,4)
incq %r8
incq %r11
cmpq %rax, %r11
jne 0x510ef
movl %r8d, (%rdx,%rax,4)
incq %rax
movq 0x220(%r15), %rsi
movslq %esi, %rcx
cmpq %rcx, %rax
jl 0x51073
testl %esi, %esi
jle 0x511a4
movq 0x260(%r15), %rax
movq 0x268(%r15), %rdi
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
shlq $0x20, %rdx
addq %rcx, %rdx
movq %rdx, (%rdi,%rcx,8)
incq %rcx
movq 0x220(%r15), %rsi
movslq %esi, %rdx
cmpq %rdx, %rcx
jl 0x51182
jmp 0x511ab
movq 0x268(%r15), %rdi
leaq -0x14a7(%rip), %rcx     # 0x4fd0b
movl $0x8, %edx
callq 0x23f0
movabsq $-0x200000000, %rax     # imm = 0xFFFFFFFE00000000
movq %rax, 0x2a0(%r15)
movq 0x220(%r15), %rax
testl %eax, %eax
jle 0x51220
xorl %r12d, %r12d
movq 0x258(%r15), %rcx
movq 0x268(%r15), %rdx
movq (%rdx,%r12,8), %rdi
movslq %edi, %rdx
cmpb $0x0, (%rcx,%rdx)
jne 0x51215
movq %rdi, %rax
shrq $0x20, %rax
movl %eax, 0x2a8(%r15)
movq %r15, %rsi
xorl %edx, %edx
callq 0x50360
movq 0x220(%r15), %rax
incq %r12
movslq %eax, %rcx
cmpq %rcx, %r12
jl 0x511db
testq %rax, %rax
movl $0x0, %eax
je 0x5123b
movq %r15, %rdi
movq (%rsp), %rsi
callq 0x50a12
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq