// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: scan_cigar_interval(window_list*, window_list_alloc*, long*, long, long)
int scan_cigar_interval(window_list *idx, window_list_alloc *cc, int64_t* get_error, int64_t scanXbeg, int64_t scanXend)
{
    uint8_t c; uint32_t cl;
    (*get_error) = -1;
     if(idx->clen == 1) {
        get_cigar_cell(idx, cc, 0, &c, &cl);
        if(c == 0) {
            (*get_error) = 0;
            return 1;
        }
    }



    int32_t x_i = 0, y_i = 0, c_i, c_n = idx->clen, c_err = 0;
    uint32_t i;
    

    ///0 is match, 1 is mismatch, 2 is up, 3 is left
    ///2: there are more bases at y, 3: there are more bases at x
    for (c_i = 0; c_i < c_n; c_i++) {
        get_cigar_cell(idx, cc, c_i, &c, &cl);
        if (c == 0) {//match
            for (i = 0; i < cl; i++) {
                if(x_i == scanXbeg) c_err = 0;
                x_i++; y_i++;

                if(x_i == scanXend + 1) {
                    (*get_error) = c_err;
                    return 1;
                }
            }
        } else if (c == 1) {//mismatch
            for (i = 0; i < cl; i++) {
                if(x_i == scanXbeg) c_err = 0;
                x_i++; y_i++; c_err++;

                if(x_i == scanXend + 1) {
                    (*get_error) = c_err;
                    return 1;
                }
            }
        } else if (c == 2) {///y has more bases than x
            c_err += cl; y_i += cl;
        }
        else if (c == 3) {
            for (i = 0; i < cl; i++) {
                if(x_i == scanXbeg) c_err = 0;
                x_i++; c_err++;

                if(x_i == scanXend + 1) {
                    (*get_error) = c_err;
                    return 1;
                }
            }
        }
    }

    (*get_error) = c_err;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq $-0x1, (%rdx)
movl 0x1c(%rdi), %eax
cmpq $0x1, %rax
jne 0x4a561
movq 0x28(%rsi), %r10
movl 0x18(%rdi), %r9d
cmpw $0x4000, (%r10,%r9,2)   # imm = 0x4000
jae 0x4a561
movl $0x1, %eax
xorl %ecx, %ecx
jmp 0x4a6af
testl %eax, %eax
jle 0x4a6ab
movq 0x28(%rsi), %rsi
movl 0x18(%rdi), %edi
xorl %r9d, %r9d
leaq 0x1db02e(%rip), %r13    # 0x2255a8
movl $0x1, %ebx
xorl %r14d, %r14d
xorl %r10d, %r10d
xorl %ebp, %ebp
movq %r8, -0x8(%rsp)
movq %rcx, -0x10(%rsp)
leal (%rdi,%r14), %r11d
movzwl (%rsi,%r11,2), %r15d
movl %r15d, %r12d
shrl $0xe, %r12d
movl %r15d, %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
movslq (%r13,%r12,4), %r12
addq %r13, %r12
jmpq *%r12
testw %r11w, %r11w
je 0x4a690
movslq %ebp, %r11
movq %r8, %r12
subq %r11, %r12
movq %rcx, %r13
subq %r11, %r13
movzwl %r15w, %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
xorl %r11d, %r11d
cmpq %r11, %r13
cmovel %r9d, %r10d
cmpq %r11, %r12
je 0x4a6a1
incq %r11
cmpl %r11d, %r15d
jne 0x4a5dd
jmp 0x4a67c
addl %r11d, %r10d
jmp 0x4a690
testw %r11w, %r11w
je 0x4a690
movslq %ebp, %r11
movq %r8, %r12
subq %r11, %r12
movq %rcx, %r13
subq %r11, %r13
movzwl %r15w, %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
xorl %r11d, %r11d
incl %r10d
cmpq %r11, %r13
cmovel %ebx, %r10d
cmpq %r11, %r12
je 0x4a6a1
incq %r11
cmpl %r11d, %r15d
jne 0x4a629
jmp 0x4a67c
testw %r11w, %r11w
je 0x4a690
movslq %ebp, %r11
movq %r8, %r12
subq %r11, %r12
movq %rcx, %r13
subq %r11, %r13
movzwl %r15w, %r15d
andl $0x3fff, %r15d          # imm = 0x3FFF
xorl %r11d, %r11d
incl %r10d
cmpq %r11, %r13
cmovel %ebx, %r10d
cmpq %r11, %r12
je 0x4a6a1
incq %r11
cmpl %r11d, %r15d
jne 0x4a665
addl %r11d, %ebp
movq -0x8(%rsp), %r8
movq -0x10(%rsp), %rcx
leaq 0x1daf18(%rip), %r13    # 0x2255a8
incq %r14
cmpq %rax, %r14
jne 0x4a591
movslq %r10d, %rcx
jmp 0x4a6ad
movslq %r10d, %rcx
movl $0x1, %eax
jmp 0x4a6af
xorl %ecx, %ecx
xorl %eax, %eax
movq %rcx, (%rdx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq