// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movb $-0x1, -0x31(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
movq $-0x1, (%rax)
movq -0x10(%rbp), %rax
cmpl $0x1, 0x1c(%rax)
jne 0x35115
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
xorl %edx, %edx
leaq -0x31(%rbp), %rcx
leaq -0x38(%rbp), %r8
callq 0x2c960
movzbl -0x31(%rbp), %eax
cmpl $0x0, %eax
jne 0x35113
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x3540d
jmp 0x35115
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x352a0
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0x3529b
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x44(%rbp), %edx
leaq -0x31(%rbp), %rcx
leaq -0x38(%rbp), %r8
callq 0x2c960
movzbl -0x31(%rbp), %eax
cmpl $0x0, %eax
jne 0x351ab
movl -0x38(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movslq -0x3c(%rbp), %rax
cmpq -0x28(%rbp), %rax
jl 0x351a6
movslq -0x4c(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x3540d
jmp 0x3528b
movzbl -0x31(%rbp), %eax
cmpl $0x1, %eax
jne 0x3520e
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x3520c
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movslq -0x3c(%rbp), %rax
cmpq -0x28(%rbp), %rax
jl 0x351ff
movslq -0x4c(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x3540d
jmp 0x35201
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x351bb
jmp 0x35289
movzbl -0x31(%rbp), %eax
cmpl $0x2, %eax
jne 0x3522b
movl -0x38(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x38(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x35287
movzbl -0x31(%rbp), %eax
cmpl $0x3, %eax
jne 0x35285
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x35283
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movslq -0x3c(%rbp), %rax
cmpq -0x28(%rbp), %rax
jl 0x35276
movslq -0x4c(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x3540d
jmp 0x35278
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3523b
jmp 0x35285
jmp 0x35287
jmp 0x35289
jmp 0x3528b
jmp 0x3528d
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x35146
jmp 0x353fb
movl -0x48(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jl 0x353f9
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x44(%rbp), %edx
leaq -0x31(%rbp), %rcx
leaq -0x38(%rbp), %r8
callq 0x2c960
movzbl -0x31(%rbp), %eax
cmpl $0x0, %eax
jne 0x3530c
movl -0x38(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movslq -0x3c(%rbp), %rax
cmpq -0x28(%rbp), %rax
jl 0x35307
movslq -0x4c(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x3540d
jmp 0x353e9
movzbl -0x31(%rbp), %eax
cmpl $0x1, %eax
jne 0x3536f
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x3536d
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movslq -0x3c(%rbp), %rax
cmpq -0x28(%rbp), %rax
jl 0x35360
movslq -0x4c(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x3540d
jmp 0x35362
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3531c
jmp 0x353e7
movzbl -0x31(%rbp), %eax
cmpl $0x2, %eax
jne 0x3538c
movl -0x38(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x38(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x353e5
movzbl -0x31(%rbp), %eax
cmpl $0x3, %eax
jne 0x353e3
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x353e1
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movslq -0x3c(%rbp), %rax
cmpq -0x28(%rbp), %rax
jl 0x353d4
movslq -0x4c(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x3540d
jmp 0x353d6
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3539c
jmp 0x353e3
jmp 0x353e5
jmp 0x353e7
jmp 0x353e9
jmp 0x353eb
movl -0x44(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x352a9
jmp 0x353fb
movslq -0x4c(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)