// File: /workspace/github_cpp_makefile_star2_O3/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, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, 0x68(%rsp)
movl 0xd0(%rsp), %eax
movq %rax, 0x28(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xc0(%rsp), %r12
movl 0x10(%r12), %r15d
movq 0x30(%r8), %rsi
testq %rsi, %rsi
movq %r8, 0x38(%rsp)
jle 0x4f634
xorl %ebx, %ebx
movq 0x28(%r8), %rax
movq 0x10(%rsp), %rcx
movq (%rcx), %rdi
imulq $0xa8, (%rax,%rbx,8), %r10
movl 0x4(%rdi,%r10), %eax
imulq $0x57619f1, %rax, %rax  # imm = 0x57619F1
shrq $0x23, %rax
imull $0x177, %eax, %eax      # imm = 0x177
movq %rbp, %rcx
subq %rax, %rcx
movq %rcx, %rax
movabsq $-0x513cc1e098ead65b, %rdx # imm = 0xAEC33E1F671529A5
imulq %rdx
addq %rcx, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x8, %rdx
addq %rax, %rdx
movq 0x58(%rdi,%r10), %r9
movq %rdx, %r11
shlq $0x5, %r11
cmpl $-0x1, 0xc(%r9,%r11)
je 0x4f628
addq %r10, %rdi
cmpq $0x177, %rcx            # imm = 0x177
jl 0x4f5c2
movq 0x88(%rdi), %rax
addq %r11, %rax
addq $-0x20, %rax
jmp 0x4f5c4
xorl %eax, %eax
movq 0x48(%rdi), %rcx
decq %rcx
cmpq %rcx, %rdx
jge 0x4f5dc
movq 0x88(%rdi), %rcx
addq %r11, %rcx
jmp 0x4f5de
xorl %ecx, %ecx
addq %r11, %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), %r14d
subq $0x8, %rsp
movq %rbp, %rdi
pushq %r11
pushq %r14
pushq $0x0
pushq %r12
pushq %r10
pushq %rcx
pushq %r10
pushq %rax
pushq %r8
callq 0x4eaed
addq $0x50, %rsp
movq 0x38(%rsp), %r8
movq 0x30(%r8), %rsi
incq %rbx
cmpq %rsi, %rbx
jl 0x4f53d
subq %rbp, %r13
movq %r15, 0x60(%rsp)
movq %r12, 0x30(%rsp)
movq %rbp, 0x40(%rsp)
jge 0x4f660
movq $-0x1, %r10
movl $0x0, 0xc(%rsp)
movq $-0x1, %r11
jmp 0x4f6b8
incq %r13
movq $-0x1, %r11
xorl %eax, %eax
movq $-0x1, %r10
xorl %ecx, %ecx
movq 0x28(%rsp), %r9
movq %r11, %rdx
cmpq $-0x1, %r11
cmoveq %rax, %r11
movzbl 0x18(%r12,%rax), %edi
testl %edi, %edi
cmovneq %rax, %r10
cmoveq %rdx, %r11
cmpl %r9d, %edi
jle 0x4f6a5
incl 0x230(%r12)
incq %rcx
incq %rax
cmpq %rax, %r13
jne 0x4f67a
cmpq $0x2, %rcx
setge %al
movl %eax, 0xc(%rsp)
incq %r10
movq %r10, 0x58(%rsp)
sete %al
xorl %ecx, %ecx
testb $0x1, %al
movq %r11, %rax
cmovneq %rcx, %rax
movq %r11, 0x50(%rsp)
cmpq $-0x1, %r11
cmoveq %rcx, %rax
movq %rax, 0x48(%rsp)
testq %rsi, %rsi
jle 0x4f86a
xorl %edi, %edi
movq 0x28(%r8), %rax
movq (%rax,%rdi,8), %r9
movq 0x10(%rsp), %rax
movq (%rax), %rcx
imulq $0xa8, %r9, %rbp
movl 0x4(%rcx,%rbp), %eax
imulq $0x57619f1, %rax, %rax  # imm = 0x57619F1
shrq $0x23, %rax
imull $0x177, %eax, %eax      # imm = 0x177
movq 0x40(%rsp), %r12
subq %rax, %r12
movq %r12, %rax
movabsq $-0x513cc1e098ead65b, %rdx # imm = 0xAEC33E1F671529A5
imulq %rdx
movq %rdx, %r15
addq %r12, %r15
movq %r15, %rax
shrq $0x3f, %rax
sarq $0x8, %r15
addq %rax, %r15
movq 0x58(%rcx,%rbp), %rax
movq %r15, %r14
shlq $0x5, %r14
cmpl $-0x1, 0xc(%rax,%r14)
je 0x4f85e
movq %r9, 0x78(%rsp)
movq %rdi, 0x80(%rsp)
addq %rbp, %rcx
addq %r14, %rax
movslq (%rax), %rdx
movq %rdx, 0x70(%rsp)
movl 0x4(%rax), %r13d
cmpl $0x0, 0x1c(%rcx)
movl 0x10(%rcx), %edx
je 0x4f797
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x334a6
jmp 0x4f7a9
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x333e0
movq (%rbx), %r8
movq 0x10(%rsp), %rax
movq (%rax), %rdi
cmpq $0x177, %r12            # imm = 0x177
jl 0x4f7ce
movq 0x88(%rdi,%rbp), %rax
addq %r14, %rax
addq $-0x20, %rax
jmp 0x4f7d0
xorl %eax, %eax
leaq (%rdi,%rbp), %rsi
movq 0x48(%rdi,%rbp), %rcx
decq %rcx
cmpq %rcx, %r15
jge 0x4f7ed
movq 0x88(%rsi), %r9
addq %r14, %r9
jmp 0x4f7f0
xorl %r9d, %r9d
movq 0x70(%rsp), %rdx
subl %edx, %r13d
incl %r13d
movslq %r13d, %rcx
addq 0x58(%rsi), %r14
leaq (%rdi,%rbp), %r10
addq $0x48, %r10
leaq 0x78(%rsi), %r11
movl 0x10(%rsi), %r15d
movl 0x1c(%rsi), %r12d
movq 0x40(%rsp), %rdi
movq 0x78(%rsp), %rsi
xorl %ebx, %ebx
pushq %rbx
pushq %r15
pushq %r12
pushq %rbx
pushq %r8
pushq 0x90(%rsp)
pushq 0x58(%rsp)
pushq 0x68(%rsp)
pushq %r11
pushq %r9
pushq %r11
pushq %rax
pushq %r10
pushq %r14
callq 0x4f228
addq $0x70, %rsp
movq 0x38(%rsp), %r8
movq 0x30(%r8), %rsi
movq 0x80(%rsp), %rdi
incq %rdi
cmpq %rsi, %rdi
jl 0x4f6eb
xorl %eax, %eax
cmpq $-0x1, 0x50(%rsp)
movq 0x58(%rsp), %rdx
cmoveq %rax, %rdx
movq 0x48(%rsp), %rax
subq %rax, %rdx
movq 0x30(%rsp), %rbx
leaq (%rbx,%rax), %rdi
addq $0x18, %rdi
xorl %esi, %esi
callq 0x20e0
movl 0x10(%rbx), %eax
movq %rax, %rcx
movq 0x60(%rsp), %rdx
subq %rdx, %rcx
cmpq $0x2, %rcx
setge %cl
andb 0xc(%rsp), %cl
cmpb $0x1, %cl
jne 0x4f8de
movq (%rbx), %rcx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rsi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x45630
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq