// File: /workspace/github_cpp_makefile_star2_O1/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, 0x30(%rsp)
movq %r8, %rbp
movq %rdx, %r13
movq %rsi, 0x8(%rsp)
movq %rdi, 0x68(%rsp)
movl 0xd0(%rsp), %eax
movq %rax, 0x38(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xc0(%rsp), %r12
movl 0x10(%r12), %r15d
cmpq $0x0, 0x30(%r8)
movq %rcx, 0x18(%rsp)
jle 0x504b8
xorl %ebx, %ebx
movq 0x28(%rbp), %rax
movq (%rcx), %rdi
imulq $0xa8, (%rax,%rbx,8), %r8
movl 0x4(%rdi,%r8), %eax
imulq $0x57619f1, %rax, %rax  # imm = 0x57619F1
shrq $0x23, %rax
imull $0x177, %eax, %eax      # imm = 0x177
movq 0x8(%rsp), %r10
subq %rax, %r10
movq %r10, %rax
movabsq $-0x513cc1e098ead65b, %rdx # imm = 0xAEC33E1F671529A5
imulq %rdx
addq %r10, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x8, %rdx
addq %rax, %rdx
movq 0x58(%rdi,%r8), %r9
movq %rdx, %rsi
shlq $0x5, %rsi
cmpl $-0x1, 0xc(%r9,%rsi)
je 0x504ab
addq %r8, %rdi
cmpq $0x177, %r10            # imm = 0x177
jl 0x50447
movq 0x88(%rdi), %rax
addq %rsi, %rax
addq $-0x20, %rax
jmp 0x50449
xorl %eax, %eax
movq 0x48(%rdi), %rcx
decq %rcx
cmpq %rcx, %rdx
jge 0x50461
movq 0x88(%rdi), %rcx
addq %rsi, %rcx
jmp 0x50463
xorl %ecx, %ecx
addq %rsi, %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 0x10(%rsp), %rdi
pushq %r11
pushq %r14
pushq $0x0
pushq %r12
pushq %r10
pushq %rcx
pushq %r10
pushq %rax
pushq %r8
callq 0x4f986
addq $0x50, %rsp
movq 0x18(%rsp), %rcx
incq %rbx
cmpq 0x30(%rbp), %rbx
jl 0x503c5
subq 0x8(%rsp), %r13
movq %r15, 0x60(%rsp)
movq %r12, 0x40(%rsp)
jge 0x504e1
movq $-0x1, %r9
movl $0x0, 0x14(%rsp)
movq $-0x1, %r10
jmp 0x50539
incq %r13
movq $-0x1, %r10
xorl %eax, %eax
movq $-0x1, %r9
xorl %edi, %edi
movq 0x38(%rsp), %r8
movq %r10, %rdx
cmpq $-0x1, %r10
cmoveq %rax, %r10
movzbl 0x18(%r12,%rax), %esi
testl %esi, %esi
cmovneq %rax, %r9
cmoveq %rdx, %r10
cmpl %r8d, %esi
jle 0x50526
incl 0x230(%r12)
incq %rdi
incq %rax
cmpq %rax, %r13
jne 0x504fb
cmpq $0x2, %rdi
setge %al
movl %eax, 0x14(%rsp)
incq %r9
movq %r9, 0x58(%rsp)
sete %al
xorl %edx, %edx
testb $0x1, %al
movq %r10, %rax
cmovneq %rdx, %rax
movq %r10, 0x50(%rsp)
cmpq $-0x1, %r10
cmoveq %rdx, %rax
movq %rax, 0x48(%rsp)
cmpq %rdx, 0x30(%rbp)
jle 0x506ef
xorl %r14d, %r14d
movq %rbp, 0x70(%rsp)
movq 0x28(%rbp), %rax
movq (%rax,%r14,8), %rdi
movq (%rcx), %rsi
imulq $0xa8, %rdi, %rbp
movl 0x4(%rsi,%rbp), %eax
imulq $0x57619f1, %rax, %rax  # imm = 0x57619F1
shrq $0x23, %rax
imull $0x177, %eax, %eax      # imm = 0x177
movq 0x8(%rsp), %r12
subq %rax, %r12
movq %r12, %rax
movabsq $-0x513cc1e098ead65b, %rdx # imm = 0xAEC33E1F671529A5
imulq %rdx
movq %rdx, %rbx
addq %r12, %rbx
movq %rbx, %rax
shrq $0x3f, %rax
sarq $0x8, %rbx
addq %rax, %rbx
movq 0x58(%rsi,%rbp), %rax
movq %rbx, %r15
shlq $0x5, %r15
cmpl $-0x1, 0xc(%rax,%r15)
je 0x506dd
movq %rdi, 0x80(%rsp)
addq %rbp, %rsi
addq %r15, %rax
movslq (%rax), %rcx
movq %rcx, 0x78(%rsp)
movl 0x4(%rax), %eax
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x1c(%rsi)
movl 0x10(%rsi), %edx
je 0x50618
movq 0x28(%rsp), %r13
movq %r13, %rdi
movq 0x30(%rsp), %rsi
callq 0x33dc3
jmp 0x5062a
movq 0x28(%rsp), %r13
movq %r13, %rdi
movq 0x30(%rsp), %rsi
callq 0x33cf8
movq (%r13), %r8
cmpq $0x177, %r12            # imm = 0x177
movq 0x18(%rsp), %rcx
jl 0x50650
movq (%rcx), %rax
movq 0x88(%rax,%rbp), %rax
addq %r15, %rax
addq $-0x20, %rax
jmp 0x50652
xorl %eax, %eax
movq (%rcx), %rsi
leaq (%rsi,%rbp), %rdi
movq 0x48(%rsi,%rbp), %rcx
decq %rcx
cmpq %rcx, %rbx
jge 0x50672
movq 0x88(%rdi), %r9
addq %r15, %r9
jmp 0x50675
xorl %r9d, %r9d
movq 0x78(%rsp), %rdx
movl 0x24(%rsp), %ecx
subl %edx, %ecx
incl %ecx
movslq %ecx, %rcx
addq 0x58(%rdi), %r15
leaq (%rsi,%rbp), %r10
addq $0x48, %r10
leaq 0x78(%rdi), %r11
movl 0x10(%rdi), %ebx
movl 0x1c(%rdi), %r12d
movq 0x8(%rsp), %rdi
movq 0x80(%rsp), %rsi
xorl %r13d, %r13d
pushq %r13
pushq %rbx
pushq %r12
pushq %r13
pushq %r8
pushq 0x90(%rsp)
pushq 0x68(%rsp)
pushq 0x78(%rsp)
pushq %r11
pushq %r9
pushq %r11
pushq %rax
pushq %r10
pushq %r15
callq 0x500b2
addq $0x70, %rsp
movq 0x18(%rsp), %rcx
incq %r14
movq 0x70(%rsp), %rbp
cmpq 0x30(%rbp), %r14
jl 0x50573
xorl %eax, %eax
cmpq $-0x1, 0x50(%rsp)
movq 0x58(%rsp), %rdx
cmoveq %rax, %rdx
movq 0x48(%rsp), %rax
subq %rax, %rdx
movq 0x40(%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 0x14(%rsp), %cl
cmpb $0x1, %cl
jne 0x50763
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 0x46007
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq