// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: scan_cigar(window_list*, window_list_alloc*, long*, long, long)
int32_t scan_cigar(window_list *idx, window_list_alloc *cc, int64_t* get_error, int64_t scanXLen, int64_t direction)
{
    uint8_t c = (uint8_t)-1; uint32_t cl = (uint32_t)-1;
    (*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
    if(direction == 0) {
        for (c_i = 0; c_i < c_n; c_i++) {
            get_cigar_cell(idx, cc, c_i, &c, &cl);
            if (c == 0) { //match
                x_i += cl; y_i += cl;
                if(x_i >= scanXLen) {
                    (*get_error) = c_err;
                    return 1;
                }
            }
            else if (c == 1) {
                for (i = 0; i < cl; i++) {
                    x_i++; y_i++; c_err++;
                    if(x_i >= scanXLen) {
                        (*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) {///x has more bases than y
                for (i = 0; i < cl; i++) {
                    x_i++; c_err++;
                    if(x_i >= scanXLen) {
                        (*get_error) = c_err;
                        return 1;
                    }
                }
            }
        }
    } else {
        for (c_i = c_n-1; c_i >= 0; c_i--) {
            get_cigar_cell(idx, cc, c_i, &c, &cl);
            if (c == 0) { //match
                x_i += cl; y_i += cl;
                if(x_i >= scanXLen) {
                    (*get_error) = c_err;
                    return 1;
                }
            } else if (c == 1) { //mismatch
                for (i = 0; i < cl; i++) {
                    x_i++; y_i++; c_err++;
                    if(x_i >= scanXLen) {
                        (*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) {///x has more bases than y
                for (i = 0; i < cl; i++) {
                    x_i++; c_err++;
                    if(x_i >= scanXLen) {
                        (*get_error) = c_err;
                        return 1;
                    }
                }
            }
        }
    }

    (*get_error) = c_err;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl 0x1c(%rdi), %r9d
movl $0x1, %eax
cmpl $0x1, %r9d
jne 0x1d229
movq 0x28(%rsi), %r10
movl 0x18(%rdi), %r11d
cmpw $0x4000, (%r10,%r11,2)  # imm = 0x4000
jae 0x1d229
xorl %ecx, %ecx
jmp 0x1d395
xorl %r10d, %r10d
testq %r8, %r8
je 0x1d2e8
leaq 0x1c639c(%rip), %r11    # 0x1e35d8
movl $0x3fff, %ebx           # imm = 0x3FFF
xorl %r8d, %r8d
decl %r9d
leal 0x1(%r9), %ebp
testl %ebp, %ebp
jle 0x1d390
movq 0x28(%rsi), %r14
movl 0x18(%rdi), %r15d
addl %r9d, %r15d
movzwl (%r14,%r15,2), %r14d
movl %r14d, %r15d
shrl $0xe, %r15d
movl %r14d, %ebp
andl $0x3fff, %ebp           # imm = 0x3FFF
movslq (%r11,%r15,4), %r15
addq %r11, %r15
jmpq *%r15
addl %ebp, %r8d
jmp 0x1d244
addl %ebp, %r10d
movslq %r10d, %r14
cmpq %rcx, %r14
jl 0x1d244
jmp 0x1d38b
movslq %r10d, %r15
andl %ebx, %r14d
addl %r14d, %r10d
addl %r8d, %ebp
negl %r14d
xorl %r12d, %r12d
cmpl %r12d, %r14d
je 0x1d2e0
incq %r15
decl %r12d
cmpq %rcx, %r15
jl 0x1d2a4
jmp 0x1d388
movslq %r10d, %r15
andl %ebx, %r14d
addl %r14d, %r10d
addl %r8d, %ebp
negl %r14d
xorl %r12d, %r12d
cmpl %r12d, %r14d
je 0x1d2e0
incq %r15
decl %r12d
cmpq %rcx, %r15
jl 0x1d2cb
jmp 0x1d388
movl %ebp, %r8d
jmp 0x1d244
testl %r9d, %r9d
cmovlel %r10d, %r9d
leaq 0x1c62f2(%rip), %r11    # 0x1e35e8
xorl %ebx, %ebx
xorl %r8d, %r8d
cmpl %r9d, %ebx
je 0x1d390
movq 0x28(%rsi), %r14
movl 0x18(%rdi), %r15d
addl %ebx, %r15d
movzwl (%r14,%r15,2), %r14d
movl %r14d, %r15d
shrl $0xe, %r15d
andl $0x3fff, %r14d          # imm = 0x3FFF
movslq (%r11,%r15,4), %r15
addq %r11, %r15
jmpq *%r15
addl %r14d, %r10d
movslq %r10d, %r14
cmpq %rcx, %r14
jl 0x1d33c
jmp 0x1d38b
addl %r14d, %r8d
movl %r8d, %ebp
incl %ebx
movl %ebp, %r8d
jmp 0x1d2fb
movslq %r10d, %r15
addl %r14d, %r10d
leal (%r8,%r14), %ebp
negl %r14d
xorl %r12d, %r12d
cmpl %r12d, %r14d
je 0x1d33f
incq %r15
decl %r12d
cmpq %rcx, %r15
jl 0x1d356
jmp 0x1d388
movslq %r10d, %r15
addl %r14d, %r10d
leal (%r8,%r14), %ebp
negl %r14d
xorl %r12d, %r12d
cmpl %r12d, %r14d
je 0x1d33f
incq %r15
decl %r12d
cmpq %rcx, %r15
jl 0x1d378
subl %r12d, %r8d
movslq %r8d, %rcx
jmp 0x1d395
movslq %r8d, %rcx
xorl %eax, %eax
movq %rcx, (%rdx)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq