// File: chhylp123[P]hifiasm/Correct.cpp
// Function: markSNP_advance(long long, long long, long long, long long, long long, window_list*, window_list_alloc*, window_list*, window_list_alloc*, window_list*, window_list_alloc*, haplotype_evdience_alloc*, ul_idx_t const*, int, int)
void markSNP_advance(
long long window_offset,
long long x_total_start, long long x_length, 
long long y_total_start, long long y_length, 
window_list *current_cigar, window_list_alloc *current_cigar_s, 
window_list *beg_cigar, window_list_alloc *beg_cigar_s,
window_list *end_cigar, window_list_alloc *end_cigar_s,
haplotype_evdience_alloc* hap, const ul_idx_t *uref, int strand, int yid)
{   
    long long x_total_end = x_total_start + x_length - 1;
    ///mismatches based on the offset of x
    long long inner_offset = x_total_start - window_offset;
    ///long long useless_side, xleftLen, xrightLen, x_interval_beg, x_interval_end;
    long long L_useless_side, R_useless_side, xleftLen, xrightLen, x_interval_beg, x_interval_end;
    long long current_cigar_beg, current_cigar_end;

    ///for current_cigar, [current_cigar_beg, current_cigar_end]
    current_cigar_beg = 0;
    current_cigar_end = x_length - 1;
    ///if the beg_cigar is available
    if(beg_cigar != NULL && beg_cigar->y_end!=-1)
    {
        ///useless_side = beg_cigar->error_threshold;
        L_useless_side = beg_cigar->extra_begin;
        R_useless_side = beg_cigar->extra_end;
        ///again, xleftLen does not include x_total_start itself, but includes beg_cigar->x_start
        xleftLen = x_total_start - beg_cigar->x_start;
        ///xrightLen includes both x_total_start and beg_cigar->x_end
        xrightLen = beg_cigar->x_end - x_total_start + 1;
        ///actually xleftLen could be no larger than useless_side
        ///but such window has already been filtered out at calculate_boundary_cigars
        ///if(xleftLen > useless_side && xrightLen > useless_side)
        if(xleftLen > L_useless_side && xrightLen > R_useless_side)
        {
            ///[x_interval_beg, x_interval_end] are the offsets to beg_cigar->x_start
            ///they are local postions, instead of global positions

            x_interval_beg = xleftLen;
            //x_interval_end = x_interval_beg + (xrightLen - useless_side) - 1;
            x_interval_end = x_interval_beg + (xrightLen - R_useless_side) - 1;
            ///current_cigar_beg is the offset of the current cigar
            ///that is the beg of current_cigar_beg
            ///current_cigar_beg = xrightLen - useless_side;
            current_cigar_beg = xrightLen - R_useless_side;

            markSNP_detail(beg_cigar, beg_cigar_s, hap->flag + inner_offset, x_interval_beg, x_interval_end, 
            x_interval_beg, uref, beg_cigar->y_start, strand, yid);
        }
    }

    if(end_cigar!=NULL && end_cigar->y_end!=-1)
    {
        ///useless_side = end_cigar->error_threshold;
        L_useless_side = end_cigar->extra_begin;
        R_useless_side = end_cigar->extra_end;
        ///again, xleftLen does not include x_total_end, but includes end_cigar->x_start
        ///it seems to be not what we want
        xleftLen = x_total_end - end_cigar->x_start;
        ///xrightLen includes both x_total_end and end_cigar->x_end
        ///it is also not what we want
        xrightLen = end_cigar->x_end - x_total_end + 1;
        ///we hope that x_total_end should be included in xleftLen, instead of xrightLen
        ///that means xleftLen should + 1, while xrightLen should -1
        ///but it is fine here 

        ///actually xrightLen could be no larger than useless_side
        ///but such window has already been filtered out in calculate_boundary_cigars
        ///if(xleftLen > useless_side && xrightLen > useless_side)
        if(xleftLen > L_useless_side && xrightLen > R_useless_side)
        {
            ///[x_interval_beg, x_interval_end] are the offsets to beg_cigar->x_start
            ///they are local postions, instead of global positions
            x_interval_end = xleftLen;
            ///the real left part length is (xleftLen + 1)
            ///so the useful left part length is ((xleftLen + 1) - useless_side)
            ///x_interval_beg = x_interval_end - ((xleftLen + 1) - useless_side) + 1;
            x_interval_beg = x_interval_end - ((xleftLen + 1) - L_useless_side) + 1;
            ///current_cigar_end = (x_length - 1) - ((xleftLen + 1) - useless_side);
            current_cigar_end = (x_length - 1) - ((xleftLen + 1) - L_useless_side);
            
            markSNP_detail(end_cigar, end_cigar_s, hap->flag + end_cigar->x_start - window_offset, x_interval_beg, 
            x_interval_end, 0, uref, end_cigar->y_start, strand, yid);
        }
    }

    markSNP_detail(current_cigar, current_cigar_s, hap->flag + inner_offset, current_cigar_beg, 
    current_cigar_end, 0, uref, current_cigar->y_start, strand, yid);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0xa8, %rsp
movl 0x50(%rbp), %eax
movl 0x48(%rbp), %eax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
subq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x20(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, 0x18(%rbp)
je 0x3babd
movq 0x18(%rbp), %rax
cmpl $-0x1, 0xc(%rax)
je 0x3babd
movq 0x18(%rbp), %rax
movswq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movq 0x18(%rbp), %rax
movswq 0x12(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rbp), %rcx
movslq (%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq 0x18(%rbp), %rax
movslq 0x4(%rax), %rax
subq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x50(%rbp), %rax
jle 0x3babb
movq -0x68(%rbp), %rax
cmpq -0x58(%rbp), %rax
jle 0x3babb
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x58(%rbp), %rcx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rax
subq -0x58(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x18(%rbp), %rdi
movq 0x20(%rbp), %rsi
movq 0x38(%rbp), %rdx
addq $0x18, %rdx
addq -0x48(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %r8
movq -0x70(%rbp), %r9
movq 0x40(%rbp), %rbx
movq 0x18(%rbp), %rax
movslq 0x8(%rax), %r11
movl 0x48(%rbp), %r10d
movl 0x50(%rbp), %eax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0x3b710
jmp 0x3babd
cmpq $0x0, 0x28(%rbp)
je 0x3bbcd
movq 0x28(%rbp), %rax
cmpl $-0x1, 0xc(%rax)
je 0x3bbcd
movq 0x28(%rbp), %rax
movswq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movq 0x28(%rbp), %rax
movswq 0x12(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movq 0x28(%rbp), %rcx
movslq (%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq 0x28(%rbp), %rax
movslq 0x4(%rax), %rax
subq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x50(%rbp), %rax
jle 0x3bbcb
movq -0x68(%rbp), %rax
cmpq -0x58(%rbp), %rax
jle 0x3bbcb
movq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x60(%rbp), %rcx
addq $0x1, %rcx
subq -0x50(%rbp), %rcx
subq %rcx, %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
subq $0x1, %rax
movq -0x60(%rbp), %rcx
addq $0x1, %rcx
subq -0x50(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x88(%rbp)
movq 0x28(%rbp), %rdi
movq 0x30(%rbp), %rsi
movq 0x38(%rbp), %rdx
addq $0x18, %rdx
movq 0x28(%rbp), %rax
movslq (%rax), %rax
addq %rax, %rdx
xorl %eax, %eax
subq -0x10(%rbp), %rax
addq %rax, %rdx
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %r8
movq 0x40(%rbp), %rbx
movq 0x28(%rbp), %rax
movslq 0x8(%rax), %r11
movl 0x48(%rbp), %r10d
movl 0x50(%rbp), %eax
xorl %r9d, %r9d
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0x3b710
jmp 0x3bbcd
movq -0x38(%rbp), %rdi
movq 0x10(%rbp), %rsi
movq 0x38(%rbp), %rdx
addq $0x18, %rdx
addq -0x48(%rbp), %rdx
movq -0x80(%rbp), %rcx
movq -0x88(%rbp), %r8
movq 0x40(%rbp), %rbx
movq -0x38(%rbp), %rax
movslq 0x8(%rax), %r11
movl 0x48(%rbp), %r10d
movl 0x50(%rbp), %eax
xorl %r9d, %r9d
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0x3b710
addq $0xa8, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)