// File: /workspace/github_cpp_makefile_star2_O2/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, (%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, 0x70(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x30(%rsp)
movl 0xd0(%rsp), %r12d
movq 0xc8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xc0(%rsp), %rbp
movl 0x10(%rbp), %eax
movq %rax, 0x60(%rsp)
xorl %ebx, %ebx
movq %r8, 0x20(%rsp)
movq 0x30(%r8), %rsi
cmpq %rsi, %rbx
jge 0x21ab5
movq 0x28(%r8), %rax
movq 0x8(%rsp), %rcx
movq (%rcx), %rdi
imulq $0xa8, (%rax,%rbx,8), %rsi
movl 0x4(%rdi,%rsi), %ecx
movq %rcx, %rax
xorl %edx, %edx
movq 0x30(%rsp), %r9
idivq %r9
leaq (%rdx,%r15), %rax
subq %rcx, %rax
cqto
idivq %r9
movq 0x58(%rdi,%rsi), %r9
movq %rax, %rdx
shlq $0x5, %rdx
cmpl $-0x1, 0xc(%r9,%rdx)
je 0x21aad
addq %rsi, %rdi
testq %rax, %rax
jle 0x21a4a
movq 0x88(%rdi), %rcx
addq %rdx, %rcx
addq $-0x20, %rcx
jmp 0x21a4c
xorl %ecx, %ecx
movq 0x48(%rdi), %rsi
decq %rsi
cmpq %rsi, %rax
jge 0x21a64
movq 0x88(%rdi), %rax
addq %rdx, %rax
jmp 0x21a66
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 %r15, %rdi
pushq %r11
pushq %r14
pushq 0x18(%rsp)
pushq %rbp
pushq %r10
pushq %rax
pushq %r10
pushq %rcx
pushq %r8
callq 0x20d95
addq $0x50, %rsp
movq 0x20(%rsp), %r8
incq %rbx
jmp 0x219e0
subq %r15, %r13
leaq 0x18(%rbp), %rax
movq %rax, 0x50(%rsp)
pushq $-0x1
popq %r9
xorl %edi, %edi
xorl %eax, %eax
movq %r9, %r10
cmpq %r13, %rax
jg 0x21afe
cmpq $-0x1, %r10
movq %r10, %rcx
cmoveq %rax, %rcx
movzbl 0x18(%rbp,%rax), %edx
testl %edx, %edx
cmovneq %rax, %r9
cmovneq %rcx, %r10
cmpl %r12d, %edx
jle 0x21af9
incl 0x230(%rbp)
incq %rdi
incq %rax
jmp 0x21acc
movq %r10, 0x40(%rsp)
movq %rdi, 0x58(%rsp)
movq %r12, 0x68(%rsp)
movq %r9, 0x48(%rsp)
leaq 0x1(%r9), %rax
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
movq %rbp, 0x78(%rsp)
movq %r15, 0x28(%rsp)
cmpq %rsi, %r14
jge 0x21c8f
movq 0x28(%r8), %rax
movq (%rax,%r14,8), %r13
movq 0x8(%rsp), %rax
movq (%rax), %rdi
imulq $0xa8, %r13, %r12
movl 0x4(%rdi,%r12), %ecx
movq %rcx, %rax
xorl %edx, %edx
movq 0x30(%rsp), %r9
idivq %r9
leaq (%rdx,%r15), %rax
subq %rcx, %rax
cqto
idivq %r9
movq %rax, %r15
movq 0x58(%rdi,%r12), %rax
movq %r15, %rbx
shlq $0x5, %rbx
cmpl $-0x1, 0xc(%rax,%rbx)
je 0x21c82
addq %r12, %rdi
addq %rbx, %rax
movslq (%rax), %rcx
movq %rcx, 0x80(%rsp)
movl 0x4(%rax), %ebp
movq (%rsp), %rax
movq (%rax), %rax
movl 0x10(%rdi), %ecx
imulq $0x28, %rcx, %rdx
addq 0x10(%rax), %rdx
movzbl 0x1c(%rdi), %ecx
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %r8d, %r8d
pushq $-0x1
popq %r9
pushq 0x20(%rsp)
callq 0x9f29
addq $0x10, %rsp
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movl $0x0, %eax
testq %r15, %r15
jle 0x21bea
movq 0x88(%rdi,%r12), %rax
addq %rbx, %rax
addq $-0x20, %rax
movq 0x10(%rsp), %rcx
movq (%rcx), %r9
leaq (%rdi,%r12), %rsi
movq 0x48(%rdi,%r12), %rcx
decq %rcx
cmpq %rcx, %r15
jge 0x21c0f
movq 0x88(%rsi), %r8
addq %rbx, %r8
jmp 0x21c12
xorl %r8d, %r8d
movq 0x80(%rsp), %rdx
subl %edx, %ebp
incl %ebp
movslq %ebp, %rcx
addq 0x58(%rsi), %rbx
leaq (%rdi,%r12), %r10
addq $0x48, %r10
leaq 0x78(%rsi), %r11
movl 0x10(%rsi), %r15d
movl 0x1c(%rsi), %r12d
movq 0x28(%rsp), %rdi
movl %r13d, %esi
pushq 0x18(%rsp)
pushq %r15
pushq %r12
pushq 0x18(%rsp)
pushq %r9
pushq 0x98(%rsp)
pushq 0x98(%rsp)
movq 0xb0(%rsp), %rbp
pushq %rbp
pushq %r11
pushq %r8
pushq %r11
pushq %rax
pushq %r10
pushq %rbx
callq 0x21307
addq $0x70, %rsp
movq 0x20(%rsp), %r8
movq 0x30(%r8), %rsi
incq %r14
movq 0x28(%rsp), %r15
jmp 0x21b28
xorl %eax, %eax
cmpq $-0x1, 0x48(%rsp)
movq 0x40(%rsp), %rdx
movq %rdx, %rcx
cmoveq %rax, %rcx
cmpq $-0x1, %rdx
cmoveq %rax, %rcx
movq 0x38(%rsp), %rdx
cmoveq %rax, %rdx
subq %rcx, %rdx
movq 0x50(%rsp), %rdi
addq %rcx, %rdi
xorl %esi, %esi
callq 0x20e0
movl 0x10(%rbp), %eax
movq %rax, %rcx
movq 0x60(%rsp), %rsi
subq %rsi, %rcx
cmpq $0x2, %rcx
setge %cl
cmpq $0x2, 0x58(%rsp)
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x21d13
movq (%rbp), %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