// File: /workspace/github_cpp_makefile_star2_O1/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
movq $-0x1, (%rdx)
movl 0x1c(%rdi), %r9d
cmpq $0x1, %r9
jne 0x4a38b
movq 0x28(%rsi), %rax
movl 0x18(%rdi), %r10d
cmpw $0x4000, (%rax,%r10,2)  # imm = 0x4000
jae 0x4a38b
movl $0x1, %eax
xorl %ecx, %ecx
jmp 0x4a47d
testq %r8, %r8
je 0x4a3ce
testl %r9d, %r9d
jle 0x4a468
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 0x1db1c5(%rip), %r10    # 0x225588
movslq (%r10,%rax,4), %rbx
addq %r10, %rbx
xorl %eax, %eax
jmpq *%rbx
testl %r9d, %r9d
jle 0x4a468
movq 0x28(%rsi), %rsi
movl 0x18(%rdi), %edi
xorl %r8d, %r8d
leaq 0x1db1b0(%rip), %r10    # 0x225598
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 0x4a45e
jmp 0x4a475
addl %ebx, %eax
jmp 0x4a45e
testw %bx, %bx
je 0x4a45e
movslq %r11d, %r14
incq %r14
negl %ebx
xorl %ebp, %ebp
cmpq %rcx, %r14
jge 0x4a471
decl %ebp
incq %r14
cmpl %ebp, %ebx
jne 0x4a42c
jmp 0x4a459
testw %bx, %bx
je 0x4a45e
movslq %r11d, %r14
incq %r14
negl %ebx
xorl %ebp, %ebp
cmpq %rcx, %r14
jge 0x4a471
decl %ebp
incq %r14
cmpl %ebp, %ebx
jne 0x4a44b
subl %ebp, %r11d
subl %ebp, %eax
incq %r8
cmpq %r9, %r8
jne 0x4a3ed
jmp 0x4a46a
xorl %eax, %eax
movslq %eax, %rcx
xorl %eax, %eax
jmp 0x4a47d
subl %ebp, %eax
incl %eax
movslq %eax, %rcx
movl $0x1, %eax
movq %rcx, (%rdx)
popq %rbx
popq %r14
popq %rbp
retq
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 0x4a518
jmp 0x4a475
movq %r9, %rbx
testw %bp, %bp
je 0x4a518
movslq %r8d, %r14
incq %r14
negl %ebp
xorl %r9d, %r9d
cmpq %rcx, %r14
jge 0x4a50b
decl %r9d
incq %r14
cmpl %r9d, %ebp
jne 0x4a4ce
jmp 0x4a503
movq %r9, %rbx
testw %bp, %bp
je 0x4a518
movslq %r8d, %r14
incq %r14
negl %ebp
xorl %r9d, %r9d
cmpq %rcx, %r14
jge 0x4a50b
decl %r9d
incq %r14
cmpl %r9d, %ebp
jne 0x4a4f3
subl %r9d, %r8d
subl %r9d, %eax
jmp 0x4a518
subl %r9d, %eax
jmp 0x4a473
movq %r9, %rbx
addl %ebp, %eax
movq %r11, %r9
cmpq $0x1, %rbx
jg 0x4a485
jmp 0x4a46a