// File: /workspace/github_cpp_makefile_star2_O1/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 %r8, %r13
movq %rdx, %rbp
movq %rsi, (%rsp)
movq %rdi, 0x68(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0xd0(%rsp), %eax
movq %rax, 0x20(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xc0(%rsp), %r12
movl 0x10(%r12), %r15d
cmpq $0x0, 0x30(%r8)
movq %rcx, 0x10(%rsp)
jle 0x508bb
xorl %ebx, %ebx
movq 0x28(%r13), %rax
movq (%rcx), %rdi
imulq $0xa8, (%rax,%rbx,8), %rsi
movl 0x4(%rdi,%rsi), %r8d
movq %r8, %rax
xorl %edx, %edx
movq 0x40(%rsp), %r9
idivq %r9
movq (%rsp), %rax
addq %rdx, %rax
subq %r8, %rax
cqto
idivq %r9
movq 0x58(%rdi,%rsi), %r9
movq %rax, %rdx
shlq $0x5, %rdx
cmpl $-0x1, 0xc(%r9,%rdx)
je 0x508ae
addq %rsi, %rdi
testq %rax, %rax
jle 0x50848
movq 0x88(%rdi), %rcx
addq %rdx, %rcx
addq $-0x20, %rcx
jmp 0x5084a
xorl %ecx, %ecx
movq 0x48(%rdi), %rsi
decq %rsi
cmpq %rsi, %rax
jge 0x50862
movq 0x88(%rdi), %rax
addq %rdx, %rax
jmp 0x50864
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 0x8(%rsp), %rdi
pushq %r11
pushq %r14
pushq 0x30(%rsp)
pushq %r12
pushq %r10
pushq %rax
pushq %r10
pushq %rcx
pushq %r8
callq 0x4f986
addq $0x50, %rsp
movq 0x10(%rsp), %rcx
incq %rbx
cmpq 0x30(%r13), %rbx
jl 0x507ec
subq (%rsp), %rbp
movq %r15, 0x60(%rsp)
movq %r12, 0x28(%rsp)
jge 0x508e3
movq $-0x1, %r9
movl $0x0, 0xc(%rsp)
movq $-0x1, %r10
jmp 0x5093b
incq %rbp
movq $-0x1, %r10
xorl %eax, %eax
movq $-0x1, %r9
xorl %edi, %edi
movq 0x20(%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 0x50928
incl 0x230(%r12)
incq %rdi
incq %rax
cmpq %rax, %rbp
jne 0x508fd
cmpq $0x2, %rdi
setge %al
movl %eax, 0xc(%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(%r13)
jle 0x50ae1
xorl %r15d, %r15d
movq %r13, 0x70(%rsp)
movq 0x28(%r13), %rax
movq (%rax,%r15,8), %rbp
movq (%rcx), %rsi
imulq $0xa8, %rbp, %r12
movl 0x4(%rsi,%r12), %edi
movq %rdi, %rax
xorl %edx, %edx
movq 0x40(%rsp), %r8
idivq %r8
movq (%rsp), %rax
addq %rdx, %rax
subq %rdi, %rax
cqto
idivq %r8
movq %rax, %r14
movq 0x58(%rsi,%r12), %rax
movq %r14, %r13
shlq $0x5, %r13
cmpl $-0x1, 0xc(%rax,%r13)
je 0x50acf
addq %r12, %rsi
addq %r13, %rax
movslq (%rax), %rcx
movq %rcx, 0x80(%rsp)
movl 0x4(%rax), %ebx
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl 0x10(%rsi), %ecx
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
movzbl 0x1c(%rsi), %ecx
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
xorl %esi, %esi
xorl %r8d, %r8d
movq $-0x1, %r9
pushq 0x40(%rsp)
callq 0x36b0d
movq 0x20(%rsp), %rcx
addq $0x10, %rsp
movl $0x0, %eax
testq %r14, %r14
jle 0x50a35
movq (%rcx), %rax
movq 0x88(%rax,%r12), %rax
addq %r13, %rax
addq $-0x20, %rax
movq 0x30(%rsp), %rdx
movq (%rdx), %r8
movq (%rcx), %rsi
leaq (%rsi,%r12), %rdx
movq 0x48(%rsi,%r12), %rcx
decq %rcx
cmpq %rcx, %r14
movq %rax, 0x78(%rsp)
jge 0x50a62
movq 0x88(%rdx), %r9
addq %r13, %r9
jmp 0x50a65
xorl %r9d, %r9d
movq 0x80(%rsp), %rax
subl %eax, %ebx
incl %ebx
movslq %ebx, %rcx
addq 0x58(%rdx), %r13
leaq (%rsi,%r12), %r10
addq $0x48, %r10
leaq 0x78(%rdx), %r11
movl 0x10(%rdx), %ebx
movl 0x1c(%rdx), %r14d
movq (%rsp), %rdi
movl %ebp, %esi
movq %rax, %rdx
pushq 0x38(%rsp)
pushq %rbx
pushq %r14
pushq 0x30(%rsp)
pushq %r8
pushq 0x90(%rsp)
pushq 0x50(%rsp)
pushq 0x60(%rsp)
pushq %r11
pushq %r9
pushq %r11
pushq 0xd0(%rsp)
pushq %r10
pushq %r13
callq 0x500b2
addq $0x70, %rsp
movq 0x10(%rsp), %rcx
incq %r15
movq 0x70(%rsp), %r13
cmpq 0x30(%r13), %r15
jl 0x50975
xorl %eax, %eax
cmpq $-0x1, 0x50(%rsp)
movq 0x58(%rsp), %rdx
cmoveq %rax, %rdx
movq 0x48(%rsp), %rax
subq %rax, %rdx
movq 0x28(%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 0x50b55
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