// File: /workspace/github_cpp_makefile_star2_O3/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
movl 0x1c(%rdi), %eax
cmpq $0x1, %rax
jne 0x49c44
movq 0x28(%rsi), %r10
movl 0x18(%rdi), %r9d
cmpw $0x4000, (%r10,%r9,2)   # imm = 0x4000
jae 0x49c4c
movl $0x1, %eax
xorl %ecx, %ecx
jmp 0x49d78
testl %eax, %eax
jle 0x49d74
movq %rdx, -0x8(%rsp)
movq 0x28(%rsi), %rsi
movl 0x18(%rdi), %edi
xorl %r9d, %r9d
leaq 0x1e7a46(%rip), %rdx    # 0x2316a8
movl $0x1, %ebx
xorl %r14d, %r14d
xorl %r10d, %r10d
xorl %ebp, %ebp
leal (%rdi,%r14), %r11d
movzwl (%rsi,%r11,2), %r15d
movl %r15d, %r12d
shrl $0xe, %r12d
movl %r15d, %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
movslq (%rdx,%r12,4), %r12
addq %rdx, %r12
jmpq *%r12
testw %r11w, %r11w
je 0x49d52
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 0x49d65
incq %r11
cmpl %r11d, %r15d
jne 0x49cba
jmp 0x49d4f
addl %r11d, %r10d
jmp 0x49d52
testw %r11w, %r11w
je 0x49d52
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 0x49d65
incq %r11
cmpl %r11d, %r15d
jne 0x49cfc
jmp 0x49d4f
testw %r11w, %r11w
je 0x49d52
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 0x49d65
incq %r11
cmpl %r11d, %r15d
jne 0x49d38
addl %r11d, %ebp
incq %r14
cmpq %rax, %r14
jne 0x49c6f
movslq %r10d, %rcx
xorl %eax, %eax
jmp 0x49d6d
movslq %r10d, %rcx
movl $0x1, %eax
movq -0x8(%rsp), %rdx
jmp 0x49d78
xorl %ecx, %ecx
xorl %eax, %eax
movq %rcx, (%rdx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq