// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, -0x10(%rsp)
movl 0x1c(%rdi), %r9d
movl $0x1, %eax
cmpl $0x1, %r9d
jne 0x1d3d7
movq 0x28(%rsi), %rcx
movl 0x18(%rdi), %r10d
cmpw $0x4000, (%rcx,%r10,2)  # imm = 0x4000
jae 0x1d3d7
xorl %ecx, %ecx
jmp 0x1d4cc
xorl %r10d, %r10d
testl %r9d, %r9d
cmovlel %r10d, %r9d
leaq 0x1(%r8), %rcx
movq %rcx, -0x8(%rsp)
leaq 0x1c6207(%rip), %r14    # 0x1e35f8
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpl %r9d, %r15d
je 0x1d4da
movq 0x28(%rsi), %rcx
movl 0x18(%rdi), %r11d
addl %r15d, %r11d
movzwl (%rcx,%r11,2), %r13d
movl %r13d, %ecx
shrl $0xe, %ecx
movl %r13d, %ebp
andl $0x3fff, %ebp           # imm = 0x3FFF
movslq (%r14,%rcx,4), %rcx
addq %r14, %rcx
movslq %r12d, %r11
jmpq *%rcx
movl $0x3fff, %ecx           # imm = 0x3FFF
andl %ecx, %r13d
leal (%r11,%r13), %r12d
movq %r8, %rcx
subq %r11, %rcx
movq -0x10(%rsp), %rbp
subq %r11, %rbp
xorl %r11d, %r11d
cmpl %r11d, %r13d
je 0x1d462
cmpq %r11, %rbp
cmovel %r10d, %ebx
cmpq %r11, %rcx
je 0x1d4c9
incq %r11
jmp 0x1d44a
addl %ebp, %ebx
incl %r15d
jmp 0x1d3f9
movl $0x3fff, %ecx           # imm = 0x3FFF
andl %ecx, %r13d
leal (%r11,%r13), %r12d
movq -0x8(%rsp), %rbp
subq %r11, %rbp
movq -0x10(%rsp), %rcx
subq %r11, %rcx
xorl %r11d, %r11d
cmpl %r11d, %r13d
je 0x1d462
incl %ebx
cmpq %r11, %rcx
cmovel %eax, %ebx
incq %r11
cmpq %r11, %rbp
jne 0x1d486
jmp 0x1d4c9
leal (%r11,%rbp), %r12d
movq %r8, %r13
subq %r11, %r13
movq -0x10(%rsp), %rcx
subq %r11, %rcx
xorl %r11d, %r11d
cmpl %r11d, %ebp
je 0x1d462
incl %ebx
cmpq %r11, %rcx
cmovel %eax, %ebx
cmpq %r11, %r13
je 0x1d4c9
incq %r11
jmp 0x1d4b2
movslq %ebx, %rcx
movq %rcx, (%rdx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq %ebx, %rcx
xorl %eax, %eax
jmp 0x1d4cc