// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: cluster_advance(char*, long long, long long, overlap_region_alloc*, Correct_dumy*, All_reads*, haplotype_evdience_alloc*, UC_Read*, int)
void cluster_advance(char* r_string, long long window_start, long long window_end, 
overlap_region_alloc* overlap_list, Correct_dumy* dumy, All_reads* R_INF, 
haplotype_evdience_alloc* hap, UC_Read* overlap_read, int snp_threshold)
{
    window_list* beg_cigar;
    window_list* end_cigar;
    ///window_start, window_end, and useful_length correspond to x, instead of y
    long long useful_length = window_end - window_start + 1;
    long long x_start, x_length, ll = hap->length, lr;
    char* x_string;
    char* y_string;
    long long i;
    long long y_start, y_length;
    long long overlapID, windowID;

    long long correct_x_pos_s;


    ///all overlaps related to the current window [window_start, window_end]
    ///first mark all snp pos
    for (i = 0; i < (long long)dumy->length; i++)
    {
        ///overlap id, instead of the window id or the y id
        overlapID = dumy->overlapID[i];

        ///overlap_list->list[overlapID].x_pos_s is the begining of the whole overlap
        correct_x_pos_s = (overlap_list->list[overlapID].x_pos_s / WINDOW) * WINDOW;
        ///window_start is the begining of this window in the whole x_read
        windowID = (window_start - correct_x_pos_s) / WINDOW;

        ///skip if this window is not matched
        if (overlap_list->list[overlapID].w_list.a[windowID].y_end == -1)
        {
            continue;
        }
        
        ///both x_start and y_start are the offsets of the whole x_read and y_read
        ///instead of the offsets of window
        x_start = overlap_list->list[overlapID].w_list.a[windowID].x_start;
        x_length = overlap_list->list[overlapID].w_list.a[windowID].x_end + 1
                - overlap_list->list[overlapID].w_list.a[windowID].x_start;

        y_start = overlap_list->list[overlapID].w_list.a[windowID].y_start;
        y_length = overlap_list->list[overlapID].w_list.a[windowID].y_end + 1
                - overlap_list->list[overlapID].w_list.a[windowID].y_start;

        beg_cigar = end_cigar = NULL;
        
        if(windowID >= 1)
        {
            beg_cigar = &(overlap_list->list[overlapID].boundary_cigars.a[windowID-1]);
        }

        if(windowID < (long long)(overlap_list->list[overlapID].w_list.n) - 1)
        {
            end_cigar = &(overlap_list->list[overlapID].boundary_cigars.a[windowID]);
        }
            

        markSNP_advance(window_start, x_start, x_length, y_start, y_length, 
        &(overlap_list->list[overlapID].w_list.a[windowID]), &(overlap_list->list[overlapID].w_list),
        beg_cigar, &(overlap_list->list[overlapID].boundary_cigars),
        end_cigar, &(overlap_list->list[overlapID].boundary_cigars), hap, NULL, 
        overlap_list->list[overlapID].y_pos_strand, overlap_list->list[overlapID].y_id);
    }


    /****************************may have bugs********************************/
    long long last_snp = -1;
    long long first_snp = -1;
    for (i = 0, lr = 0; i < useful_length; i++)
    {
        if(hap->flag[i] != 0)
        {
            last_snp = i;
            if(first_snp == -1)
            {
                first_snp = i;
            }
        }
        ///for a real snp, the coverage should be at least 2
        if(hap->flag[i] > snp_threshold)
        {
            // hap->snp++; 
            hap->nn_snp++;
            lr++;
        }
    }
    ///if there are any >0 elements, both first_snp and last_snp should be != -1
    if(first_snp == -1 || last_snp == -1)
    {
        first_snp = 0;
        last_snp = -1;
    }
    /****************************may have bugs********************************/



    ///add the information related to snp to haplotype_evdience_alloc
    for (i = 0; i < (long long)dumy->length; i++)
    {
        ///overlap ID, instead of the window ID
        overlapID = dumy->overlapID[i];

        ///overlap_list->list[overlapID].x_pos_s is the begining of the whole overlap
        correct_x_pos_s = (overlap_list->list[overlapID].x_pos_s / WINDOW) * WINDOW;
        ///window_start is the begining of this window in the whole x_read
        windowID = (window_start - correct_x_pos_s) / WINDOW;

        ///skip if this window is not matched
        if (overlap_list->list[overlapID].w_list.a[windowID].y_end == -1)
        {
            continue;
        }
        
        ///both x_start and y_start are the offsets of the whole x_read and y_read
        ///instead of the offsets of window
        x_start = overlap_list->list[overlapID].w_list.a[windowID].x_start;
        x_length = overlap_list->list[overlapID].w_list.a[windowID].x_end 
                - overlap_list->list[overlapID].w_list.a[windowID].x_start + 1;

        y_start = overlap_list->list[overlapID].w_list.a[windowID].y_start;
        y_length = overlap_list->list[overlapID].w_list.a[windowID].y_end
                - overlap_list->list[overlapID].w_list.a[windowID].y_start + 1;


        if(overlap_list->list[overlapID].y_pos_strand == 0)
        {
            recover_UC_Read(overlap_read, R_INF, overlap_list->list[overlapID].y_id);
        }
        else
        {
            recover_UC_Read_RC(overlap_read, R_INF, overlap_list->list[overlapID].y_id);
        }
        
        x_string = r_string;
        y_string = overlap_read->seq;


        beg_cigar = end_cigar = NULL;
        if(windowID >= 1)
        {
            beg_cigar = &(overlap_list->list[overlapID].boundary_cigars.a[windowID-1]);
        }
        if(windowID < (long long)(overlap_list->list[overlapID].w_list.n) - 1)
        {
            end_cigar = &(overlap_list->list[overlapID].boundary_cigars.a[windowID]);
        }


        addSNPtohaplotype_advance(window_start, overlapID, x_start, x_length, y_start, y_length, 
        &(overlap_list->list[overlapID].w_list.a[windowID]), &(overlap_list->list[overlapID].w_list),
        beg_cigar, &(overlap_list->list[overlapID].boundary_cigars), 
        end_cigar, &(overlap_list->list[overlapID].boundary_cigars), 
        hap, snp_threshold, x_string, y_string, NULL, overlap_list->list[overlapID].y_pos_strand, 
        overlap_list->list[overlapID].y_id, NULL);                
    }

    RsetInitHaplotypeEvdienceFlag(hap, first_snp, last_snp + 1 - first_snp);

    if(hap->length - ll > 1 && lr > 1) radix_sort_haplotype_evdience_srt(hap->list+ll, hap->list + hap->length);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, (%rsp)
movq %rdi, 0x70(%rsp)
movl 0xd0(%rsp), %r13d
movq 0xc8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x10(%rax), %eax
movq %rax, 0x50(%rsp)
xorl %r14d, %r14d
movl $0x177, %ebp            # imm = 0x177
movl $0x177, %r15d           # imm = 0x177
movq %r8, 0x38(%rsp)
movq 0x30(%r8), %rsi
cmpq %rsi, %r14
jge 0x216ff
movq 0x28(%r8), %rax
movq 0x10(%rsp), %rcx
movq (%rcx), %rdi
imulq $0xa8, (%rax,%r14,8), %r10
movl 0x4(%rdi,%r10), %esi
movl %esi, %eax
xorl %edx, %edx
divl %ebp
subl %edx, %esi
movq (%rsp), %rcx
subq %rsi, %rcx
movq %rcx, %rax
cqto
idivq %r15
movq 0x58(%rdi,%r10), %r9
movq %rax, %rdx
shlq $0x5, %rdx
cmpl $-0x1, 0xc(%r9,%rdx)
je 0x216f7
addq %r10, %rdi
cmpq $0x177, %rcx            # imm = 0x177
jl 0x21691
movq 0x88(%rdi), %rcx
addq %rdx, %rcx
addq $-0x20, %rcx
jmp 0x21693
xorl %ecx, %ecx
movq 0x48(%rdi), %rsi
decq %rsi
cmpq %rsi, %rax
jge 0x216ab
movq 0x88(%rdi), %rax
addq %rdx, %rax
jmp 0x216ad
xorl %eax, %eax
addq %rdx, %r9
movslq (%r9), %rsi
movl 0x4(%r9), %edx
subl %esi, %edx
incl %edx
movslq %edx, %rdx
leaq 0x48(%rdi), %r8
leaq 0x78(%rdi), %r10
movl 0x10(%rdi), %r11d
movl 0x1c(%rdi), %r12d
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
pushq %r11
pushq %r12
pushq $0x0
pushq 0x28(%rsp)
pushq %r10
pushq %rax
pushq %r10
pushq %rcx
pushq %r8
callq 0x20d95
addq $0x50, %rsp
movq 0x38(%rsp), %r8
incq %r14
jmp 0x21620
subq (%rsp), %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rdi), %rax
movq %rax, 0x48(%rsp)
pushq $-0x1
popq %r9
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
movq %r9, %r10
cmpq %rbx, %rax
jg 0x21757
cmpq $-0x1, %r10
movq %r10, %rcx
cmoveq %rax, %rcx
movzbl 0x18(%rdi,%rax), %edx
testl %edx, %edx
cmovneq %rax, %r9
cmovneq %rcx, %r10
cmpl %r13d, %edx
jle 0x21752
incl 0x230(%rdi)
incq 0x18(%rsp)
incq %rax
jmp 0x21723
movq %r13, 0x68(%rsp)
leaq 0x1(%r9), %rax
movq %rax, 0x40(%rsp)
xorl %r11d, %r11d
movq %r9, 0x60(%rsp)
movq %r10, 0x58(%rsp)
cmpq %rsi, %r11
jge 0x218e2
movq 0x28(%r8), %rax
movq (%rax,%r11,8), %r14
movq 0x10(%rsp), %rax
movq (%rax), %rdi
imulq $0xa8, %r14, %r13
movl 0x4(%rdi,%r13), %ecx
movl %ecx, %eax
xorl %edx, %edx
movl $0x177, %ebx            # imm = 0x177
divl %ebx
subl %edx, %ecx
movq (%rsp), %r12
subq %rcx, %r12
movq %r12, %rax
cqto
movl $0x177, %ecx            # imm = 0x177
idivq %rcx
movq %rax, %r15
movq 0x58(%rdi,%r13), %rax
movq %r15, %rbp
shlq $0x5, %rbp
cmpl $-0x1, 0xc(%rax,%rbp)
je 0x218da
movq %r14, 0x30(%rsp)
movq %r11, 0x80(%rsp)
addq %r13, %rdi
addq %rbp, %rax
movslq (%rax), %rcx
movq %rcx, 0x78(%rsp)
movl 0x4(%rax), %ebx
cmpl $0x0, 0x1c(%rdi)
movl 0x10(%rdi), %edx
je 0x2180d
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq 0x28(%rsp), %rsi
callq 0x7215
jmp 0x2181f
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq 0x28(%rsp), %rsi
callq 0x715e
movq (%r14), %r8
movq 0x10(%rsp), %rax
movq (%rax), %r10
cmpq $0x177, %r12            # imm = 0x177
jl 0x21844
movq 0x88(%r10,%r13), %rax
addq %rbp, %rax
addq $-0x20, %rax
jmp 0x21846
xorl %eax, %eax
movq 0x30(%rsp), %rsi
leaq (%r10,%r13), %rdi
movq 0x48(%r10,%r13), %rcx
decq %rcx
cmpq %rcx, %r15
jge 0x21868
movq 0x88(%rdi), %r9
addq %rbp, %r9
jmp 0x2186b
xorl %r9d, %r9d
movq 0x78(%rsp), %rdx
subl %edx, %ebx
incl %ebx
movslq %ebx, %rcx
addq 0x58(%rdi), %rbp
addq %r13, %r10
addq $0x48, %r10
leaq 0x78(%rdi), %r11
movl 0x10(%rdi), %ebx
movl 0x1c(%rdi), %r15d
movq (%rsp), %rdi
pushq $0x0
pushq %rbx
pushq %r15
pushq $0x0
pushq %r8
pushq 0x98(%rsp)
pushq 0x98(%rsp)
pushq 0x40(%rsp)
pushq %r11
pushq %r9
pushq %r11
pushq %rax
pushq %r10
pushq %rbp
callq 0x21307
addq $0x70, %rsp
movq 0x38(%rsp), %r8
movq 0x30(%r8), %rsi
movq 0x60(%rsp), %r9
movq 0x58(%rsp), %r10
movq 0x80(%rsp), %r11
incq %r11
jmp 0x21772
xorl %eax, %eax
cmpq $-0x1, %r9
movq %r10, %rcx
cmoveq %rax, %rcx
cmpq $-0x1, %r10
cmoveq %rax, %rcx
movq 0x40(%rsp), %rdx
cmoveq %rax, %rdx
subq %rcx, %rdx
movq 0x48(%rsp), %rdi
addq %rcx, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x8(%rsp), %rdi
movl 0x10(%rdi), %eax
movq %rax, %rcx
movq 0x50(%rsp), %rsi
subq %rsi, %rcx
cmpq $0x2, %rcx
setge %cl
cmpq $0x2, 0x18(%rsp)
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x21963
movq (%rdi), %rcx
imulq $0x18, %rsi, %rdi
addq %rcx, %rdi
imulq $0x18, %rax, %rsi
addq %rcx, %rsi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x192d4
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq