// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: addSNPtohaplotype_details(window_list*, window_list_alloc*, unsigned char*, char*, char*, long long, long long, long long, long long, int, long long, haplotype_evdience_alloc*, long long, ul_idx_t const*, int, int, void*)
void addSNPtohaplotype_details(window_list *cigar_idx, window_list_alloc *cigar_s, uint8_t* flag, 
char* x_string, char* y_string, long long x_total_start, long long y_total_start,
long long xBeg, long long xEnd, int overlapID, long long flag_offset, 
haplotype_evdience_alloc* hap, long long snp_threshold, const ul_idx_t *uref, int y_strand, int yid, void *km)
{
    if(xBeg > xEnd) return;
    int64_t x_i, y_i, c_i, pi = 0, c_n = cigar_idx->clen; 
    uint32_t i, operLen = (uint32_t)-1; uint8_t oper = (uint8_t)-1; i = c_i = x_i = y_i = 0;
    haplotype_evdience ev;

    ///note that node 0 is the start node
    ///0 is match, 1 is mismatch, 2 is up, 3 is left
    ///2 represents thre are more bases at y
    ///3 represents thre are more bases at x
    for (c_i = 0; c_i < c_n; c_i++) {
        get_cigar_cell(cigar_idx, cigar_s, c_i, &oper, &operLen);
        if(x_i > xEnd) break;

        if (oper == 0) { ///matches
            for (i = 0; i < operLen; i++) {
                ///should be at least 2 mismatches
                /// note we need to deal with flag_offset carefully
                ///if(flag[x_i - flag_offset] > snp_threshold && x_i >= xBeg && x_i <= xEnd)
                if(x_i >= xBeg && x_i <= xEnd && flag[x_i - flag_offset] > snp_threshold) {
                    ev.misBase =  y_string[y_i];
                    ev.overlapID = overlapID;
                    ev.site = x_total_start + x_i;
                    ev.overlapSite = y_total_start + y_i;
                    ev.type = 0;
                    ev.cov = uref?retrieve_u_cov(uref, yid, y_strand, y_total_start + y_i, y_strand, &pi):1;
                    addHaplotypeEvdience(hap, &ev, km);
                }
                ///inner_offset++;
                x_i++; y_i++;
            }
        }
        else if(oper == 1) {
            for (i = 0; i < operLen; i++) {
                /// should be at least 2 mismatches
                /// note we need to deal with flag_offset carefully
                ///if(flag[x_i - flag_offset] > snp_threshold && x_i >= xBeg && x_i <= xEnd)
                if(x_i >= xBeg && x_i <= xEnd && flag[x_i - flag_offset] > snp_threshold) {
                    ev.misBase =  y_string[y_i];
                    ev.overlapID = overlapID;
                    ev.site = x_total_start + x_i;
                    ev.overlapSite = y_total_start + y_i;
                    ev.type = 1;
                    ev.cov = uref?retrieve_u_cov(uref, yid, y_strand, y_total_start + y_i, y_strand, &pi):1;
                    addHaplotypeEvdience(hap, &ev, km);
                }
                ///inner_offset++;
                x_i++; y_i++;
            }
        }///insertion, 2 represents thre are more bases at y
        else if (oper == 2) {
            y_i += operLen;
        }///3 represents thre are more bases at x
        else if (oper == 3) {
            /****************************may have bugs********************************/
            for (i = 0; i < operLen; i++) {
                ///if(hap->flag[inner_offset] > snp_threshold)
                /// should be at least 2 mismatches
                /// note we need to deal with flag_offset carefully
                ///if(flag[x_i - flag_offset] > snp_threshold && x_i >= xBeg && x_i <= xEnd)
                if(x_i >= xBeg && x_i <= xEnd && flag[x_i - flag_offset] > snp_threshold) {
                    ev.misBase =  'N';
                    ev.overlapID = overlapID;
                    ev.site = x_total_start + x_i;
                    ev.overlapSite = y_total_start + y_i;
                    ev.type = 2;
                    ev.cov = uref?retrieve_u_cov(uref, yid, y_strand, y_total_start + y_i, y_strand, &pi):1;
                    addHaplotypeEvdience(hap, &ev, km);
                }

                ///inner_offset++;
                x_i++;
            }
            /****************************may have bugs********************************/
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x38(%rsp)
movq %r8, 0x58(%rsp)
movq 0xb0(%rsp), %r9
movq 0xa8(%rsp), %r8
cmpq %r9, %r8
jle 0x20f7f
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %rbx
movl 0xe8(%rsp), %eax
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %r10
movq 0xc0(%rsp), %rcx
leaq 0x50(%rsp), %r11
andq $0x0, (%r11)
movl 0x1c(%rbx), %r11d
movq %r11, 0x60(%rsp)
cltq
movq %rax, 0x40(%rsp)
subq %rcx, %rdx
xorl %r15d, %r15d
xorl %r11d, %r11d
xorl %ebp, %ebp
movq %rdx, 0x48(%rsp)
movq %rsi, 0x30(%rsp)
movq %rbx, 0x28(%rsp)
cmpq 0x60(%rsp), %r11
je 0x20f70
cmpq %r9, %rbp
jg 0x20f70
movq 0x28(%rsi), %rax
movl 0x18(%rbx), %ecx
addl %r11d, %ecx
movzwl (%rax,%rcx,2), %r13d
movzwl %r13w, %eax
andl $0x3fff, %r13d          # imm = 0x3FFF
shrl $0xe, %eax
leaq 0x1c2644(%rip), %rcx    # 0x1e3648
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movl 0xe0(%rsp), %ecx
movzbl %cl, %ecx
movl %ecx, 0x4(%rsp)
jmpq *%rax
movq %r11, 0x20(%rsp)
movq %r15, %r14
negq %r14
movq 0xa0(%rsp), %rax
leaq (%rax,%r15), %rbx
addq 0x58(%rsp), %r15
movq 0x38(%rsp), %rax
leal (%rax,%rbp), %r12d
subl $0x1, %r13d
jb 0x212ea
cmpq %r8, %rbp
setl %al
cmpq %r9, %rbp
setg %cl
orb %al, %cl
jne 0x210f7
movzbl (%rdx,%rbp), %eax
cmpq %r10, %rax
jle 0x210f7
movb (%r15), %al
movb %al, 0x1c(%rsp)
movl 0xb8(%rsp), %eax
movl %eax, 0xc(%rsp)
movl %r12d, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movb $0x0, 0x14(%rsp)
pushq $0x1
popq %rax
testq %rdi, %rdi
je 0x210b4
movq 0xd8(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl 0x4(%rsp), %edx
movq %rbx, %rcx
movl %edx, %r8d
leaq 0x50(%rsp), %r9
callq 0xa105
movl %eax, 0x18(%rsp)
movq 0xc8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq 0xf0(%rsp), %rdx
callq 0x532ee
movq 0x48(%rsp), %rdx
movq 0xb0(%rsp), %r9
movq 0xa8(%rsp), %r8
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %r10
incq %rbp
decq %r14
incq %rbx
incq %r15
incl %r12d
jmp 0x21040
addq %r13, %r15
jmp 0x212ff
movq %r11, 0x20(%rsp)
movq 0xa0(%rsp), %rax
leaq (%r15,%rax), %rbx
movq 0x38(%rsp), %rax
leal (%rax,%rbp), %r14d
subl $0x1, %r13d
jb 0x211e6
cmpq %r8, %rbp
setl %al
cmpq %r9, %rbp
setg %cl
orb %al, %cl
jne 0x211db
movzbl (%rdx,%rbp), %eax
cmpq %r10, %rax
jle 0x211db
movb $0x4e, 0x1c(%rsp)
movl 0xb8(%rsp), %eax
movl %eax, 0xc(%rsp)
movl %r14d, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movb $0x2, 0x14(%rsp)
pushq $0x1
popq %rax
testq %rdi, %rdi
movq %rdi, %r12
je 0x2119d
movq %r12, %rdi
movq 0x40(%rsp), %rsi
movl 0x4(%rsp), %edx
movq %rbx, %rcx
movl %edx, %r8d
leaq 0x50(%rsp), %r9
callq 0xa105
movl %eax, 0x18(%rsp)
movq 0xc8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq 0xf0(%rsp), %rdx
callq 0x532ee
movq 0x48(%rsp), %rdx
movq 0xb0(%rsp), %r9
movq 0xa8(%rsp), %r8
movq %r12, %rdi
movq 0xd0(%rsp), %r10
incq %rbp
incl %r14d
jmp 0x2112d
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r11
movq 0x30(%rsp), %rsi
jmp 0x212ff
movq %r11, 0x20(%rsp)
movq %r15, %r14
negq %r14
movq 0xa0(%rsp), %rax
leaq (%rax,%r15), %rbx
addq 0x58(%rsp), %r15
movq 0x38(%rsp), %rax
leal (%rax,%rbp), %r12d
subl $0x1, %r13d
jb 0x212ea
cmpq %r8, %rbp
setl %al
cmpq %r9, %rbp
setg %cl
orb %al, %cl
jne 0x212d6
movzbl (%rdx,%rbp), %eax
cmpq %r10, %rax
jle 0x212d6
movb (%r15), %al
movb %al, 0x1c(%rsp)
movl 0xb8(%rsp), %eax
movl %eax, 0xc(%rsp)
movl %r12d, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movb $0x1, 0x14(%rsp)
pushq $0x1
popq %rax
testq %rdi, %rdi
je 0x21293
movq 0xd8(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl 0x4(%rsp), %edx
movq %rbx, %rcx
movl %edx, %r8d
leaq 0x50(%rsp), %r9
callq 0xa105
movl %eax, 0x18(%rsp)
movq 0xc8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq 0xf0(%rsp), %rdx
callq 0x532ee
movq 0x48(%rsp), %rdx
movq 0xb0(%rsp), %r9
movq 0xa8(%rsp), %r8
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %r10
incq %rbp
decq %r14
incq %rbx
incq %r15
incl %r12d
jmp 0x2121f
negq %r14
movq %r14, %r15
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r11
incq %r11
jmp 0x20fd4