// File: /workspace/github_cpp_makefile_star2_O3/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 %r14
pushq %rbx
movl 0x1c(%rdi), %r9d
cmpq $0x1, %r9
jne 0x49a69
movq 0x28(%rsi), %rax
movl 0x18(%rdi), %r10d
cmpw $0x4000, (%rax,%r10,2)  # imm = 0x4000
jae 0x49a78
movl $0x1, %eax
xorl %ecx, %ecx
jmp 0x49c0c
testq %r8, %r8
je 0x49ab2
testl %r9d, %r9d
jg 0x49a7d
jmp 0x49b4f
testq %r8, %r8
je 0x49abb
movq 0x28(%rsi), %rsi
movl 0x18(%rdi), %edi
xorl %r8d, %r8d
movq %r9, %r11
decq %r11
leal (%r11,%rdi), %eax
movzwl (%rsi,%rax,2), %ebp
movl %ebp, %eax
shrl $0xe, %eax
andl $0x3fff, %ebp           # imm = 0x3FFF
leaq 0x1e7be1(%rip), %r10    # 0x231688
movslq (%r10,%rax,4), %rbx
addq %r10, %rbx
xorl %eax, %eax
jmpq *%rbx
testl %r9d, %r9d
jle 0x49b4f
movq 0x28(%rsi), %rsi
movl 0x18(%rdi), %edi
xorl %r8d, %r8d
leaq 0x1e7bcc(%rip), %r10    # 0x231698
xorl %eax, %eax
xorl %r11d, %r11d
leal (%rdi,%r8), %ebx
movzwl (%rsi,%rbx,2), %ebx
movl %ebx, %r14d
shrl $0xe, %r14d
andl $0x3fff, %ebx           # imm = 0x3FFF
movslq (%r10,%r14,4), %r14
addq %r10, %r14
jmpq *%r14
addl %ebx, %r11d
movslq %r11d, %rbx
cmpq %rcx, %rbx
jl 0x49b42
jmp 0x49b5a
addl %ebx, %eax
jmp 0x49b42
testw %bx, %bx
je 0x49b42
movslq %r11d, %r14
incq %r14
negl %ebx
xorl %ebp, %ebp
cmpq %rcx, %r14
jge 0x49b56
decl %ebp
incq %r14
cmpl %ebp, %ebx
jne 0x49b10
jmp 0x49b3d
testw %bx, %bx
je 0x49b42
movslq %r11d, %r14
incq %r14
negl %ebx
xorl %ebp, %ebp
cmpq %rcx, %r14
jge 0x49b56
decl %ebp
incq %r14
cmpl %ebp, %ebx
jne 0x49b2f
subl %ebp, %r11d
subl %ebp, %eax
incq %r8
cmpq %r9, %r8
jne 0x49ad1
jmp 0x49c07
xorl %eax, %eax
jmp 0x49c07
subl %ebp, %eax
incl %eax
movslq %eax, %rcx
movl $0x1, %eax
jmp 0x49c0c
leaq -0x1(%r9), %r11
leal (%rdi,%r11), %ebx
movzwl (%rsi,%rbx,2), %ebp
movl %ebp, %ebx
shrl $0xe, %ebx
andl $0x3fff, %ebp           # imm = 0x3FFF
movslq (%r10,%rbx,4), %rbx
addq %r10, %rbx
jmpq *%rbx
movq %r9, %rbx
movl %r8d, %r9d
movl %ebp, %r8d
addl %r9d, %r8d
movslq %r8d, %r9
cmpq %rcx, %r9
jl 0x49bfa
jmp 0x49b5a
movq %r9, %rbx
testw %bp, %bp
je 0x49bfa
movslq %r8d, %r14
incq %r14
negl %ebp
xorl %r9d, %r9d
cmpq %rcx, %r14
jge 0x49bed
decl %r9d
incq %r14
cmpl %r9d, %ebp
jne 0x49bb0
jmp 0x49be5
movq %r9, %rbx
testw %bp, %bp
je 0x49bfa
movslq %r8d, %r14
incq %r14
negl %ebp
xorl %r9d, %r9d
cmpq %rcx, %r14
jge 0x49bed
decl %r9d
incq %r14
cmpl %r9d, %ebp
jne 0x49bd5
subl %r9d, %r8d
subl %r9d, %eax
jmp 0x49bfa
subl %r9d, %eax
jmp 0x49b58
movq %r9, %rbx
addl %ebp, %eax
movq %r11, %r9
cmpq $0x1, %rbx
jg 0x49b67
movslq %eax, %rcx
xorl %eax, %eax
movq %rcx, (%rdx)
popq %rbx
popq %r14
popq %rbp
retq