// File: /workspace/github_cpp_makefile_star2_O3/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, 0x40(%rsp)
movq %r8, 0x60(%rsp)
movq 0xb0(%rsp), %r8
movq 0xa8(%rsp), %r9
cmpq %r8, %r9
jg 0x4f219
movq $0x0, 0x58(%rsp)
movl 0x1c(%rdi), %r10d
testq %r10, %r10
je 0x4f219
movl 0xe0(%rsp), %r11d
movq 0xd8(%rsp), %rbp
movslq 0xe8(%rsp), %rax
movq %rax, 0x48(%rsp)
subq 0xc0(%rsp), %rdx
xorl %r15d, %r15d
leaq 0x1e29e7(%rip), %r14    # 0x2316f8
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %rdx, 0x50(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
movq %r10, 0x28(%rsp)
cmpq %r8, %r15
jg 0x4f219
movq 0x28(%rsi), %rax
movl 0x18(%rdi), %ecx
addl %r12d, %ecx
movzwl (%rax,%rcx,2), %ebx
movzwl %bx, %eax
andl $0x3fff, %ebx           # imm = 0x3FFF
shrl $0xe, %eax
movslq (%r14,%rax,4), %rax
addq %r14, %rax
movzbl %r11b, %ecx
movl %ecx, 0x4(%rsp)
jmpq *%rax
testw %bx, %bx
je 0x4f20d
movq %r12, 0x10(%rsp)
movq 0x40(%rsp), %rax
leal (%rax,%r15), %r14d
movq 0x60(%rsp), %rax
leaq (%rax,%r13), %r12
movq 0xa0(%rsp), %rax
movq %r13, %rcx
addq %rax, %r13
leal -0x1(%rbx), %eax
leaq 0x1(%rcx,%rax), %rcx
movq %rcx, 0x8(%rsp)
addq %r15, %rax
incq %rax
movq %rax, 0x20(%rsp)
movq 0xc8(%rsp), %rsi
cmpq %r9, %r15
setl %al
cmpq %r8, %r15
setg %cl
orb %al, %cl
jne 0x4eecf
movzbl (%rdx,%r15), %eax
cmpq 0xd0(%rsp), %rax
jle 0x4eecf
movb (%r12), %al
movb %al, 0x18(%rsp)
testq %rbp, %rbp
je 0x4ee09
movq %rbp, %rdi
movq 0x48(%rsp), %rsi
movl 0x4(%rsp), %edx
movq %r13, %rcx
movl %edx, %r8d
leaq 0x58(%rsp), %r9
callq 0x36265
movq 0xc8(%rsp), %rsi
movl %eax, %edi
jmp 0x4ee0e
movl $0x1, %edi
movl 0x10(%rsi), %eax
leal 0x1(%rax), %ecx
cmpl 0x14(%rsi), %ecx
jbe 0x4ee69
movl %edi, (%rsp)
movl %eax, %ecx
shrl %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x10, %ecx
orl %eax, %ecx
incl %ecx
leaq (,%rcx,8), %rax
movl %ecx, 0x14(%rsi)
movq %rsi, %rbp
movq (%rsi), %rsi
leaq (%rax,%rax,2), %rdx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x4ee6e
callq 0x1ee21d
jmp 0x4ee79
movq (%rsi), %rax
jmp 0x4ee83
movq %rsi, %rdi
movq %rdx, %rsi
callq 0x2370
movq %rax, (%rbp)
movq %rbp, %rsi
movl (%rsp), %edi
movl 0x10(%rsi), %ecx
leaq (%rcx,%rcx,2), %rcx
movl %r14d, (%rax,%rcx,8)
movl 0xb8(%rsp), %edx
movl %edx, 0x4(%rax,%rcx,8)
movl %r13d, 0x8(%rax,%rcx,8)
movb $0x0, 0xc(%rax,%rcx,8)
movl %edi, 0x10(%rax,%rcx,8)
movb 0x18(%rsp), %dl
movb %dl, 0x14(%rax,%rcx,8)
incl 0x10(%rsi)
movq 0x50(%rsp), %rdx
movq 0xb0(%rsp), %r8
movq 0xa8(%rsp), %r9
movq 0xd8(%rsp), %rbp
incq %r15
incl %r14d
incq %r12
incq %r13
decl %ebx
jne 0x4edad
jmp 0x4f1e0
addq %rbx, %r13
jmp 0x4f20d
testw %bx, %bx
je 0x4f20d
movq %r12, 0x10(%rsp)
movq 0xa0(%rsp), %rax
movq %r13, 0x8(%rsp)
addq %rax, %r13
movq 0x40(%rsp), %rax
leal (%rax,%r15), %r14d
leal -0x1(%rbx), %eax
addq %r15, %rax
incq %rax
movq %rax, 0x18(%rsp)
movq 0xc8(%rsp), %r12
cmpq %r9, %r15
setl %al
cmpq %r8, %r15
setg %cl
orb %al, %cl
jne 0x4f044
movzbl (%rdx,%r15), %eax
cmpq 0xd0(%rsp), %rax
jle 0x4f044
testq %rbp, %rbp
je 0x4ef81
movq %rbp, %rdi
movq 0x48(%rsp), %rsi
movl 0x4(%rsp), %edx
movq %r13, %rcx
movl %edx, %r8d
leaq 0x58(%rsp), %r9
callq 0x36265
movq 0xc8(%rsp), %r12
movl %eax, %ebp
jmp 0x4ef86
movl $0x1, %ebp
movl 0x10(%r12), %eax
leal 0x1(%rax), %ecx
cmpl 0x14(%r12), %ecx
jbe 0x4efe2
movl %eax, %ecx
shrl %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x10, %ecx
orl %eax, %ecx
incl %ecx
leaq (,%rcx,8), %rax
movl %ecx, 0x14(%r12)
movq (%r12), %rsi
leaq (%rax,%rax,2), %rdx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x4efe8
callq 0x1ee21d
jmp 0x4eff3
movq (%r12), %rax
jmp 0x4eff7
movq %rsi, %rdi
movq %rdx, %rsi
callq 0x2370
movq %rax, (%r12)
movl 0x10(%r12), %ecx
leaq (%rcx,%rcx,2), %rcx
movl %r14d, (%rax,%rcx,8)
movl 0xb8(%rsp), %edx
movl %edx, 0x4(%rax,%rcx,8)
movl %r13d, 0x8(%rax,%rcx,8)
movb $0x2, 0xc(%rax,%rcx,8)
movl %ebp, 0x10(%rax,%rcx,8)
movb $0x4e, 0x14(%rax,%rcx,8)
incl 0x10(%r12)
movq 0x50(%rsp), %rdx
movq 0xb0(%rsp), %r8
movq 0xa8(%rsp), %r9
movq 0xd8(%rsp), %rbp
incq %r15
incl %r14d
decl %ebx
jne 0x4ef2d
movq 0x18(%rsp), %r15
jmp 0x4f1e5
testw %bx, %bx
je 0x4f20d
movq %r12, 0x10(%rsp)
movq 0x40(%rsp), %rax
leal (%rax,%r15), %r14d
movq 0x60(%rsp), %rax
leaq (%rax,%r13), %r12
movq 0xa0(%rsp), %rax
movq %r13, %rcx
addq %rax, %r13
leal -0x1(%rbx), %eax
leaq 0x1(%rcx,%rax), %rcx
movq %rcx, 0x8(%rsp)
addq %r15, %rax
incq %rax
movq %rax, 0x20(%rsp)
movq 0xc8(%rsp), %rsi
cmpq %r9, %r15
setl %al
cmpq %r8, %r15
setg %cl
orb %al, %cl
jne 0x4f1cc
movzbl (%rdx,%r15), %eax
cmpq 0xd0(%rsp), %rax
jle 0x4f1cc
movb (%r12), %al
movb %al, 0x18(%rsp)
testq %rbp, %rbp
je 0x4f106
movq %rbp, %rdi
movq 0x48(%rsp), %rsi
movl 0x4(%rsp), %edx
movq %r13, %rcx
movl %edx, %r8d
leaq 0x58(%rsp), %r9
callq 0x36265
movq 0xc8(%rsp), %rsi
movl %eax, %edi
jmp 0x4f10b
movl $0x1, %edi
movl 0x10(%rsi), %eax
leal 0x1(%rax), %ecx
cmpl 0x14(%rsi), %ecx
jbe 0x4f166
movl %edi, (%rsp)
movl %eax, %ecx
shrl %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x10, %ecx
orl %eax, %ecx
incl %ecx
leaq (,%rcx,8), %rax
movl %ecx, 0x14(%rsi)
movq %rsi, %rbp
movq (%rsi), %rsi
leaq (%rax,%rax,2), %rdx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x4f16b
callq 0x1ee21d
jmp 0x4f176
movq (%rsi), %rax
jmp 0x4f180
movq %rsi, %rdi
movq %rdx, %rsi
callq 0x2370
movq %rax, (%rbp)
movq %rbp, %rsi
movl (%rsp), %edi
movl 0x10(%rsi), %ecx
leaq (%rcx,%rcx,2), %rcx
movl %r14d, (%rax,%rcx,8)
movl 0xb8(%rsp), %edx
movl %edx, 0x4(%rax,%rcx,8)
movl %r13d, 0x8(%rax,%rcx,8)
movb $0x1, 0xc(%rax,%rcx,8)
movl %edi, 0x10(%rax,%rcx,8)
movb 0x18(%rsp), %dl
movb %dl, 0x14(%rax,%rcx,8)
incl 0x10(%rsi)
movq 0x50(%rsp), %rdx
movq 0xb0(%rsp), %r8
movq 0xa8(%rsp), %r9
movq 0xd8(%rsp), %rbp
incq %r15
incl %r14d
incq %r12
incq %r13
decl %ebx
jne 0x4f0aa
movq 0x20(%rsp), %r15
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %r10
movl 0xe0(%rsp), %r11d
leaq 0x1e24f5(%rip), %r14    # 0x2316f8
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %r13
incq %r12
cmpq %r10, %r12
jne 0x4ed2b
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq