// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: cluster_ul_advance(char*, long long, long long, overlap_region_alloc*, Correct_dumy*, ul_idx_t const*, haplotype_evdience_alloc*, UC_Read*, int, long long, void*)
void cluster_ul_advance(char* r_string, long long window_start, long long window_end, 
overlap_region_alloc* overlap_list, Correct_dumy* dumy, const ul_idx_t *uref, 
haplotype_evdience_alloc* hap, UC_Read* overlap_read, int snp_threshold, long long blockLen, void *km)
{
    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 / blockLen) * blockLen;
        ///window_start is the begining of this window in the whole x_read
        windowID = (window_start - correct_x_pos_s) / blockLen;

        ///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;

        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, uref, 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 / blockLen) * blockLen;
        ///window_start is the begining of this window in the whole x_read
        windowID = (window_start - correct_x_pos_s) / blockLen;

        ///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;

        retrieve_u_seq(overlap_read, NULL, &uref->ug->u.a[overlap_list->list[overlapID].y_id], 
        overlap_list->list[overlapID].y_pos_strand, 0, -1, km);
        
        
        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, uref, overlap_list->list[overlapID].y_pos_strand, overlap_list->list[overlapID].y_id, km);                
    }

    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, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, 0x78(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x50(%rsp)
movl 0xd0(%rsp), %eax
movq %rax, 0x28(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xc0(%rsp), %r12
movl 0x10(%r12), %r15d
movq 0x30(%r8), %rsi
testq %rsi, %rsi
movq %r8, 0x48(%rsp)
jle 0x4fa40
xorl %ebx, %ebx
movq 0x28(%r8), %rax
movq 0x20(%rsp), %rcx
movq (%rcx), %rdi
imulq $0xa8, (%rax,%rbx,8), %r10
movl 0x4(%rdi,%r10), %ecx
movq %rcx, %rax
xorl %edx, %edx
movq 0x50(%rsp), %r9
idivq %r9
leaq (%rdx,%r13), %rax
subq %rcx, %rax
cqto
idivq %r9
movq 0x58(%rdi,%r10), %r9
movq %rax, %rdx
shlq $0x5, %rdx
cmpl $-0x1, 0xc(%r9,%rdx)
je 0x4fa34
addq %r10, %rdi
testq %rax, %rax
jle 0x4f9cc
movq 0x88(%rdi), %rcx
addq %rdx, %rcx
addq $-0x20, %rcx
jmp 0x4f9ce
xorl %ecx, %ecx
movq 0x48(%rdi), %rsi
decq %rsi
cmpq %rsi, %rax
jge 0x4f9e6
movq 0x88(%rdi), %rax
addq %rdx, %rax
jmp 0x4f9e8
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), %r14d
subq $0x8, %rsp
movq %r13, %rdi
pushq %r11
pushq %r14
pushq 0x30(%rsp)
pushq %r12
pushq %r10
pushq %rax
pushq %r10
pushq %rcx
pushq %r8
callq 0x4eaed
addq $0x50, %rsp
movq 0x48(%rsp), %r8
movq 0x30(%r8), %rsi
incq %rbx
cmpq %rsi, %rbx
jl 0x4f96a
subq %r13, %rbp
movq %r15, 0x70(%rsp)
movq %r12, 0x30(%rsp)
jge 0x4fa67
movq $-0x1, %r10
movl $0x0, 0xc(%rsp)
movq $-0x1, %r11
jmp 0x4fabf
incq %rbp
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 0x4faac
incl 0x230(%r12)
incq %rcx
incq %rax
cmpq %rax, %rbp
jne 0x4fa81
cmpq $0x2, %rcx
setge %al
movl %eax, 0xc(%rsp)
incq %r10
movq %r10, 0x68(%rsp)
sete %al
xorl %ecx, %ecx
testb $0x1, %al
movq %r11, %rax
cmovneq %rcx, %rax
movq %r11, 0x60(%rsp)
cmpq $-0x1, %r11
cmoveq %rcx, %rax
movq %rax, 0x58(%rsp)
testq %rsi, %rsi
jle 0x4fc58
xorl %r12d, %r12d
movq %r13, 0x10(%rsp)
movq 0x28(%r8), %rax
movq (%rax,%r12,8), %rbp
movq 0x20(%rsp), %rax
movq (%rax), %rdi
imulq $0xa8, %rbp, %r14
movl 0x4(%rdi,%r14), %ecx
movq %rcx, %rax
xorl %edx, %edx
movq 0x50(%rsp), %r9
idivq %r9
leaq (%rdx,%r13), %rax
subq %rcx, %rax
cqto
idivq %r9
movq %rax, %r13
movq 0x58(%rdi,%r14), %rax
movq %r13, %r15
shlq $0x5, %r15
cmpl $-0x1, 0xc(%rax,%r15)
je 0x4fc47
addq %r14, %rdi
addq %r15, %rax
movslq (%rax), %rcx
movq %rcx, 0x80(%rsp)
movl 0x4(%rax), %ebx
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl 0x10(%rdi), %ecx
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
movzbl 0x1c(%rdi), %ecx
subq $0x8, %rsp
movq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %r8d, %r8d
movq $-0x1, %r9
pushq 0x48(%rsp)
callq 0x36049
addq $0x10, %rsp
movq 0x20(%rsp), %rax
movq (%rax), %r10
movl $0x0, %eax
testq %r13, %r13
jle 0x4fbba
movq 0x88(%r10,%r14), %rax
addq %r15, %rax
addq $-0x20, %rax
movq 0x38(%rsp), %rcx
movq (%rcx), %r9
leaq (%r10,%r14), %rsi
movq 0x48(%r10,%r14), %rcx
decq %rcx
cmpq %rcx, %r13
jge 0x4fbdf
movq 0x88(%rsi), %r8
addq %r15, %r8
jmp 0x4fbe2
xorl %r8d, %r8d
movq 0x10(%rsp), %rdi
movq 0x80(%rsp), %rdx
subl %edx, %ebx
incl %ebx
movslq %ebx, %rcx
addq 0x58(%rsi), %r15
addq %r14, %r10
addq $0x48, %r10
leaq 0x78(%rsi), %r11
movl 0x10(%rsi), %ebx
movl 0x1c(%rsi), %r14d
movl %ebp, %esi
pushq 0x40(%rsp)
pushq %rbx
pushq %r14
pushq 0x30(%rsp)
pushq %r9
pushq 0xa0(%rsp)
pushq 0x58(%rsp)
pushq 0x68(%rsp)
pushq %r11
pushq %r8
pushq %r11
pushq %rax
pushq %r10
pushq %r15
callq 0x4f228
addq $0x70, %rsp
movq 0x48(%rsp), %r8
movq 0x30(%r8), %rsi
incq %r12
cmpq %rsi, %r12
movq 0x10(%rsp), %r13
jl 0x4faf8
xorl %eax, %eax
cmpq $-0x1, 0x60(%rsp)
movq 0x68(%rsp), %rdx
cmoveq %rax, %rdx
movq 0x58(%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 0x70(%rsp), %rdx
subq %rdx, %rcx
cmpq $0x2, %rcx
setge %cl
andb 0xc(%rsp), %cl
cmpb $0x1, %cl
jne 0x4fccc
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