// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: addSNPtohaplotype_advance(long long, int, 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*, int, char*, char*, ul_idx_t const*, int, int, void*)
void addSNPtohaplotype_advance(
long long window_offset, int overlapID,
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, int snp_threshold, char* x_T_string, char* y_T_string, const ul_idx_t *uref, int strand, int yid, void *km)
{
    long long x_total_end = x_total_start + x_length - 1;
    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(cigar_record, hap->flag + inner_offset, x_interval_beg, 
            // x_interval_end, x_interval_beg);
            addSNPtohaplotype_details(beg_cigar, beg_cigar_s, hap->flag + inner_offset, 
            x_T_string + beg_cigar->x_start, y_T_string + beg_cigar->y_start, 
            beg_cigar->x_start, beg_cigar->y_start, x_interval_beg, x_interval_end, 
            overlapID, x_interval_beg, hap, snp_threshold, uref, strand, yid, km);
        }
    }


    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(cigar_record, hap->flag + end_cigar->x_start - window_offset,
            // x_interval_beg, x_interval_end, 0);
            addSNPtohaplotype_details(end_cigar, end_cigar_s, hap->flag + end_cigar->x_start - window_offset,
            x_T_string + end_cigar->x_start, y_T_string + end_cigar->y_start, 
            end_cigar->x_start, end_cigar->y_start, x_interval_beg, x_interval_end, 
            overlapID, 0, hap, snp_threshold, uref, strand, yid, km);
        }
    }

    // markSNP_detail(&(current_cigar->cigar), hap->flag + inner_offset, current_cigar_beg, 
    // current_cigar_end, 0);
    addSNPtohaplotype_details(current_cigar, current_cigar_s, hap->flag + inner_offset, 
    x_T_string + current_cigar->x_start, y_T_string + current_cigar->y_start, 
    current_cigar->x_start, current_cigar->y_start, current_cigar_beg, 
    current_cigar_end, overlapID, 0, hap, snp_threshold, uref, strand, yid, km);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r13
movq %rdx, %rbp
movl %esi, %r11d
movq %rdi, %r8
movl 0xb0(%rsp), %ebx
movq 0xa0(%rsp), %rcx
movq 0x98(%rsp), %r14
movq 0x80(%rsp), %r10
movq 0x70(%rsp), %rsi
movq 0x60(%rsp), %rdi
movq %rdx, %r15
subq %r8, %r15
movslq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
testq %rdi, %rdi
je 0x4f2c3
cmpl $-0x1, 0xc(%rdi)
je 0x4f2c3
movswq 0x10(%rdi), %rcx
movswq 0x12(%rdi), %rdx
movslq (%rdi), %r9
movq %rbp, %rax
subq %r9, %rax
movslq 0x4(%rdi), %r12
subq %rbp, %r12
cmpq %rcx, %rax
setle %cl
subq %rdx, %r12
setl %dl
orb %cl, %dl
je 0x4f2c8
xorl %r12d, %r12d
movq 0xa0(%rsp), %rcx
jmp 0x4f344
xorl %r12d, %r12d
jmp 0x4f344
movq 0x68(%rsp), %rsi
leaq (%rax,%r12), %rcx
incq %r12
leaq (%r10,%r15), %rdx
addq $0x18, %rdx
movslq 0x8(%rdi), %r10
movq %r8, 0x8(%rsp)
leaq (%r14,%r10), %r8
subq $0x8, %rsp
pushq 0xc0(%rsp)
pushq %rbx
movl 0xc0(%rsp), %ebx
pushq %rbx
movq 0xc0(%rsp), %rbx
pushq %rbx
pushq 0x38(%rsp)
pushq 0xb0(%rsp)
pushq %rax
pushq %r11
pushq %rcx
pushq %rax
pushq %r10
movq %r11, %rbx
callq 0x4ec9b
movq 0xd0(%rsp), %rsi
movq 0x68(%rsp), %r8
movq 0x100(%rsp), %rcx
movq 0xe0(%rsp), %r10
movq %rbx, %r11
addq $0x60, %rsp
movq 0x50(%rsp), %rbx
leaq -0x1(%r13), %rax
movq %rax, 0x8(%rsp)
testq %rsi, %rsi
je 0x4f427
cmpl $-0x1, 0xc(%rsi)
je 0x4f427
leaq -0x1(,%rbp), %rdx
addq %r13, %rdx
movswq 0x10(%rsi), %rax
movslq (%rsi), %r9
movq %rdx, %rcx
subq %r9, %rcx
cmpq %rax, %rcx
jle 0x4f397
movq %rsi, %rdi
movslq 0x4(%rsi), %rsi
subq %rdx, %rsi
movswq 0x12(%rdi), %rdx
cmpq %rdx, %rsi
jge 0x4f3a4
movq 0xa0(%rsp), %rcx
jmp 0x4f427
movq 0x78(%rsp), %rsi
movq %rcx, %rdx
notq %rdx
movq 0x8(%rsp), %r13
addq %rax, %r13
addq %rdx, %r13
movq %r13, 0x8(%rsp)
leaq (%r10,%r9), %rdx
subq %r8, %rdx
addq $0x18, %rdx
movslq 0x8(%rdi), %r10
leaq (%r14,%r10), %r8
subq $0x8, %rsp
pushq 0xc0(%rsp)
movl 0xc0(%rsp), %r13d
pushq %r13
movl 0xc0(%rsp), %r13d
pushq %r13
movq 0xc0(%rsp), %rbp
pushq %rbp
pushq 0x38(%rsp)
pushq 0xb0(%rsp)
pushq $0x0
pushq %r11
pushq %rcx
pushq %rax
pushq %r10
movq %r11, %r13
callq 0x4ec9b
movq %rbp, %rcx
movq 0xe0(%rsp), %r10
movq %r13, %r11
addq $0x60, %rsp
leaq (%r10,%r15), %rdx
addq $0x18, %rdx
movslq (%rbx), %r9
movslq 0x8(%rbx), %rax
addq %rax, %r14
subq $0x8, %rsp
movq %rbx, %rdi
movq 0x60(%rsp), %rsi
movq %r14, %r8
pushq 0xc0(%rsp)
movl 0xc0(%rsp), %ebx
pushq %rbx
movl 0xc0(%rsp), %ebx
pushq %rbx
pushq %rcx
pushq 0x38(%rsp)
pushq %r10
pushq $0x0
pushq %r11
pushq 0x50(%rsp)
pushq %r12
pushq %rax
callq 0x4ec9b
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq