// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq 0x60(%rbp), %rax
movl 0x58(%rbp), %eax
movl 0x50(%rbp), %eax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq 0x18(%rbp), %rax
cmpq 0x20(%rbp), %rax
jle 0x3bc8b
jmp 0x3c03f
movq $0x0, -0x50(%rbp)
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0x58(%rbp)
movl $0xffffffff, -0x60(%rbp) # imm = 0xFFFFFFFF
movb $-0x1, -0x61(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x5c(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0x3c03f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x48(%rbp), %rax
movl %eax, %edx
leaq -0x61(%rbp), %rcx
leaq -0x60(%rbp), %r8
callq 0x2c960
movq -0x38(%rbp), %rax
cmpq 0x20(%rbp), %rax
jle 0x3bd08
jmp 0x3c03f
movzbl -0x61(%rbp), %eax
cmpl $0x0, %eax
jne 0x3be0c
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x3be07
movq -0x38(%rbp), %rax
cmpq 0x18(%rbp), %rax
jl 0x3bde1
movq -0x38(%rbp), %rax
cmpq 0x20(%rbp), %rax
jg 0x3bde1
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
subq 0x30(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpq 0x40(%rbp), %rax
jle 0x3bde1
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x68(%rbp)
movl 0x28(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x30(%rbp), %rax
addq -0x38(%rbp), %rax
movl %eax, -0x7c(%rbp)
movq 0x10(%rbp), %rax
addq -0x40(%rbp), %rax
movl %eax, -0x74(%rbp)
movb $0x0, -0x70(%rbp)
cmpq $0x0, 0x48(%rbp)
je 0x3bdc0
movq 0x48(%rbp), %rdi
movslq 0x58(%rbp), %rsi
movl 0x50(%rbp), %eax
movb %al, %dl
movq 0x10(%rbp), %rcx
addq -0x40(%rbp), %rcx
movl 0x50(%rbp), %eax
leaq -0x50(%rbp), %r9
movzbl %dl, %edx
movzbl %al, %r8d
callq 0x10d80
movl %eax, -0x80(%rbp)
jmp 0x3bdca
movl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x3bdca
movl -0x80(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq 0x38(%rbp), %rdi
movq 0x60(%rbp), %rdx
leaq -0x7c(%rbp), %rsi
callq 0xa0e90
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x3bd1c
jmp 0x3c02c
movzbl -0x61(%rbp), %eax
cmpl $0x1, %eax
jne 0x3bf19
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x3bf14
movq -0x38(%rbp), %rax
cmpq 0x18(%rbp), %rax
jl 0x3beee
movq -0x38(%rbp), %rax
cmpq 0x20(%rbp), %rax
jg 0x3beee
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
subq 0x30(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpq 0x40(%rbp), %rax
jle 0x3beee
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x68(%rbp)
movl 0x28(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x30(%rbp), %rax
addq -0x38(%rbp), %rax
movl %eax, -0x7c(%rbp)
movq 0x10(%rbp), %rax
addq -0x40(%rbp), %rax
movl %eax, -0x74(%rbp)
movb $0x1, -0x70(%rbp)
cmpq $0x0, 0x48(%rbp)
je 0x3bec7
movq 0x48(%rbp), %rdi
movslq 0x58(%rbp), %rsi
movl 0x50(%rbp), %eax
movb %al, %dl
movq 0x10(%rbp), %rcx
addq -0x40(%rbp), %rcx
movl 0x50(%rbp), %eax
leaq -0x50(%rbp), %r9
movzbl %dl, %edx
movzbl %al, %r8d
callq 0x10d80
movl %eax, -0x84(%rbp)
jmp 0x3bed4
movl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x3bed4
movl -0x84(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq 0x38(%rbp), %rdi
movq 0x60(%rbp), %rdx
leaq -0x7c(%rbp), %rsi
callq 0xa0e90
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x3be20
jmp 0x3c02a
movzbl -0x61(%rbp), %eax
cmpl $0x2, %eax
jne 0x3bf32
movl -0x60(%rbp), %eax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x3c028
movzbl -0x61(%rbp), %eax
cmpl $0x3, %eax
jne 0x3c026
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0x3c024
movq -0x38(%rbp), %rax
cmpq 0x18(%rbp), %rax
jl 0x3c00a
movq -0x38(%rbp), %rax
cmpq 0x20(%rbp), %rax
jg 0x3c00a
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
subq 0x30(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpq 0x40(%rbp), %rax
jle 0x3c00a
movb $0x4e, -0x68(%rbp)
movl 0x28(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x30(%rbp), %rax
addq -0x38(%rbp), %rax
movl %eax, -0x7c(%rbp)
movq 0x10(%rbp), %rax
addq -0x40(%rbp), %rax
movl %eax, -0x74(%rbp)
movb $0x2, -0x70(%rbp)
cmpq $0x0, 0x48(%rbp)
je 0x3bfe3
movq 0x48(%rbp), %rdi
movslq 0x58(%rbp), %rsi
movl 0x50(%rbp), %eax
movb %al, %dl
movq 0x10(%rbp), %rcx
addq -0x40(%rbp), %rcx
movl 0x50(%rbp), %eax
leaq -0x50(%rbp), %r9
movzbl %dl, %edx
movzbl %al, %r8d
callq 0x10d80
movl %eax, -0x88(%rbp)
jmp 0x3bff0
movl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x3bff0
movl -0x88(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq 0x38(%rbp), %rdi
movq 0x60(%rbp), %rdx
leaq -0x7c(%rbp), %rsi
callq 0xa0e90
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x3bf46
jmp 0x3c026
jmp 0x3c028
jmp 0x3c02a
jmp 0x3c02c
jmp 0x3c02e
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x3bcd0
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)