// File: chhylp123[P]hifiasm/Correct.cpp
// Function: split_sub_list(haplotype_evdience_alloc*, haplotype_evdience*, long long, overlap_region_alloc*, All_reads*, UC_Read*)
int split_sub_list(haplotype_evdience_alloc* hap, 
haplotype_evdience* sub_list, long long sub_length, 
overlap_region_alloc* overlap_list, All_reads* R_INF, UC_Read* g_read)
{
    long long i = 0;
    long long occ_0 = 0;
    long long occ_1 = 0;
    long long occ_1_array[5];
    memset(occ_1_array, 0, sizeof(long long) * 5);
    long long occ_2 = 0;


    for (i = 0; i < sub_length; i++)
    {
        if(sub_list[i].type == 0)
        {
            occ_0++;
        }
        else if(sub_list[i].type == 1)
        {
            occ_1_array[seq_nt6_table[(uint8_t)(sub_list[i].misBase)]]++;
            occ_1++;
        }
        else if(sub_list[i].type == 2)
        {
            occ_2++;
        }
    }

    

    /**
     1. if occ_0 = 0, that means all overlaps are different with this read at this site
     2. it is not possible that occ_1 = 0,
     3. if occ_1 = 1, there are only one difference. It must be a sequencing error.
        (for repeat, it maybe a snp at repeat. but ...)
    **/
    ///if(occ_0 == 0 || occ_1 <= 1)
    if(occ_0 == 0 || occ_1 == 0)
    {
        return 0;
    }

    

    ///note: if the max value except type0 is type2
    ///that means this is no snp hapolyte
    long long max = occ_2;
    long long max_i = -1;

    for (i = 0; i < 5; i++)
    {
        if(occ_1_array[i] > max)
        {
            max = occ_1_array[i];
            max_i = i;
        }
    }



    if(max_i == -1)
    {
        return 0;
    }
    
    if(max <= 1)
    {
        return 0;
    }


    ///if we have two max
    for (i = 0; i < 5; i++)
    {
        if(occ_1_array[i] == max && i != max_i)
        {
            return 0;
        }
    }

    long long new_0 = occ_0 + 1;
    long long new_total = sub_length + 1;
    ///note: here occ_0++ since the read itself has a type0
    double available = new_0 + max;
    double threshold = 0.95;
    available = available/((double)(new_total));
    if(available < threshold)///Fix-attention: looks definitely wrong 
    {
        return 0;
    }

    ///new_total is the number of errors here
    new_total = new_total - new_0;
    ///available is the number of selected errors here
    available = max;
    threshold = 0.70;
    available = available/((double)(new_total));
    if(available < threshold)///Fix-attention: looks definitely wrong 
    {
        return 0;
    }
    
    InsertSNPVector(hap, sub_list, sub_length, s_H[max_i], g_read);

    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x2100
movq $0x0, -0x88(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x3dc6b
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movzbl 0xc(%rax), %eax
cmpl $0x0, %eax
jne 0x3dbdf
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x3dc58
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movzbl 0xc(%rax), %eax
cmpl $0x1, %eax
jne 0x3dc2d
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movzbl 0x14(%rax), %eax
movl %eax, %ecx
leaq 0x386a43(%rip), %rax    # 0x3c4650
movzbl (%rax,%rcx), %eax
movq -0x80(%rbp,%rax,8), %rcx
addq $0x1, %rcx
movq %rcx, -0x80(%rbp,%rax,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x3dc56
movq -0x18(%rbp), %rax
imulq $0x18, -0x40(%rbp), %rcx
addq %rcx, %rax
movzbl 0xc(%rax), %eax
cmpl $0x2, %eax
jne 0x3dc54
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x3dc56
jmp 0x3dc58
jmp 0x3dc5a
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x3dbae
cmpq $0x0, -0x48(%rbp)
je 0x3dc79
cmpq $0x0, -0x50(%rbp)
jne 0x3dc85
movl $0x0, -0x4(%rbp)
jmp 0x3de8a
movq -0x88(%rbp), %rax
movq %rax, -0x90(%rbp)
movq $-0x1, -0x98(%rbp)
movq $0x0, -0x40(%rbp)
cmpq $0x5, -0x40(%rbp)
jge 0x3dcea
movq -0x40(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rax
cmpq -0x90(%rbp), %rax
jle 0x3dcda
movq -0x40(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rax
movq %rax, -0x90(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x3dcdc
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x3dca6
cmpq $-0x1, -0x98(%rbp)
jne 0x3dd00
movl $0x0, -0x4(%rbp)
jmp 0x3de8a
cmpq $0x1, -0x90(%rbp)
jg 0x3dd16
movl $0x0, -0x4(%rbp)
jmp 0x3de8a
movq $0x0, -0x40(%rbp)
cmpq $0x5, -0x40(%rbp)
jge 0x3dd60
movq -0x40(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rax
cmpq -0x90(%rbp), %rax
jne 0x3dd50
movq -0x40(%rbp), %rax
cmpq -0x98(%rbp), %rax
je 0x3dd50
movl $0x0, -0x4(%rbp)
jmp 0x3de8a
jmp 0x3dd52
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x3dd1e
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
addq -0x90(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd 0x34447f(%rip), %xmm0   # 0x382220
movsd %xmm0, -0xb8(%rbp)
movsd -0xb0(%rbp), %xmm0
cvtsi2sdq -0xa8(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xb0(%rbp), %xmm1
movsd -0xb8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3dde8
movl $0x0, -0x4(%rbp)
jmp 0x3de8a
movq -0xa8(%rbp), %rax
subq -0xa0(%rbp), %rax
movq %rax, -0xa8(%rbp)
cvtsi2sdq -0x90(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd 0x344412(%rip), %xmm0   # 0x382228
movsd %xmm0, -0xb8(%rbp)
movsd -0xb0(%rbp), %xmm0
cvtsi2sdq -0xa8(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xb0(%rbp), %xmm1
movsd -0xb8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3de5a
movl $0x0, -0x4(%rbp)
jmp 0x3de8a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x98(%rbp), %rcx
leaq 0x3868dc(%rip), %rax    # 0x3c4750
movb (%rax,%rcx), %al
movq -0x38(%rbp), %r8
movsbl %al, %ecx
callq 0xa1020
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)