// File: chhylp123[P]hifiasm/Correct.cpp
// Function: hc_aln_exz_by_exist_cigar_with_p(bit_extz_t*, long, long, long, long*, long*, long*, long*, long*, long*, long*, long*)
int64_t hc_aln_exz_by_exist_cigar_with_p(bit_extz_t *ez, int64_t mode, int64_t ps, int64_t pe, 
int64_t *ts, int64_t *te, int64_t *cis, int64_t *cie, int64_t *cps, int64_t *cpe, int64_t *cts, int64_t *cte)
{
    int64_t ts0 = (*ts), te0 = (*te), ts1, te1;
    assert(ez->ps <= ps && ez->pe+1 >= pe && mode > 0); 
    if(mode == 1) (*te) = -1;
    else if(mode == 2) (*ts) = -1;
    else (*ts) = (*te) = -1;
    (*cis) = (*cie) = (*cps) = (*cpe) = (*cts) = (*cte) = -1;

    int32_t pi = ez->ps, ti = ez->ts, pi0, ti0, err[2], tot_err, ws, we; 
    uint32_t ci = 0, cl; uint16_t c, sset = 0, eset = 0;
    err[0] = err[1] = tot_err = 0;
    while (ci < ez->cigar.n) {
        ci = pop_trace(&(ez->cigar), ci, &c, &cl);
        ws = pi; pi0 = pi; ti0 = ti;
        if(c <= 1) {
            pi+=cl; ti+=cl;
        } else if(c == 2) {///more p
            pi+=cl;
        } else if(c == 3) {
            ti+=cl;
        }
        we = pi; 
        // if((ps == 5139) && (pe == 21733)) {
        //     fprintf(stderr, "[M::%s::ci->%u] wp::[%d, %d), pi::%d, ti::%d, c::%u, cl::%u, tot_err::%d, p::[%ld, %ld), t::[%ld, %ld)\n", 
        //     __func__, ci, ws, we, pi, ti, c, cl, tot_err, ps, pe, (*ts), (*te));
        // }
        if(we < ps) {///not we <= ps
            if(c != 0) tot_err += cl;
            continue;
        }
        if(ws > pe) {///not ws >= pe
            if(c != 0) tot_err += cl;
            break;
        }

        if(!sset) {
            if((ps>=ws) && (ps<we)) {
                // if((ps == 5139) && (pe == 21733)) {
                //     fprintf(stderr, "+[M::%s::ci->%u] c::%u, cl::%u\n", __func__, ci, c, cl);
                // }
                if(c <= 1) {
                    ts1 = ti - (we - ps); 
                    if((ts1 == ts0) || (mode != 1)) {
                        if(c == 1) err[0] = tot_err + (ps - ws); 
                        else err[0] = tot_err;
                        sset = 1; (*ts) = ts1; 
                        (*cis) = ci-1; (*cps) = pi0; (*cts) = ti0;
                    }
                } else if(c == 2) {///more p
                    // ts1 = ti - (we - ps); 
                    ts1 = ti;
                    if((ts1 == ts0) || (mode != 1)) {
                        err[0] = tot_err + (ps - ws); 
                        sset = 1; (*ts) = ts1; 
                        (*cis) = ci-1; (*cps) = pi0; (*cts) = ti0;
                    }
                } else if(c == 3) {
                    ts1 = ti; 
                    if((ts1 == ts0) || (mode != 1)) {
                        err[0] = tot_err + cl; 
                        sset = 1; (*ts) = ts1; 
                        (*cis) = ci-1; (*cps) = pi0; (*cts) = ti0;
                    }
                }
            } 

            if((c == 3) && (ps>=ws) && (ps<=we)) {
                // if((ps == 5139) && (pe == 21733)) {
                //     fprintf(stderr, "-[M::%s::ci->%u] c::%u, cl::%u\n", __func__, ci, c, cl);
                // }
                ts1 = ti - cl; 
                if(ts1 == ts0) {
                    err[0] = tot_err; sset = 1; (*ts) = ts1; 
                    (*cis) = ci-1; (*cps) = pi0; (*cts) = ti0;
                }
            }
        }

        if(!eset) {
            if((pe>ws) && (pe<=we)) {
                if(c <= 1) {
                    te1 = ti - (we - pe); 
                    if((te1 == te0) || (mode != 2)) {
                        if(c == 1) err[1] = tot_err + (pe - ws);
                        else err[1] = tot_err;
                        eset = 1; (*te) = te1; 
                        (*cie) = ci; (*cpe) = pi; (*cte) = ti;
                    }
                } else if(c == 2) {///more p
                    // te1 = ti - (we - pe); 
                    te1 = ti;
                    if((te1 == te0) || (mode != 2)) {
                        err[1] = tot_err + (pe - ws); 
                        eset = 1; (*te) = te1; 
                        (*cie) = ci; (*cpe) = pi; (*cte) = ti;
                    }
                } else if(c == 3) {
                    te1 = ti-cl; 
                    if((te1 == te0) || (mode != 2)) {
                        err[1] = tot_err; 
                        eset = 1; (*te) = te1; 
                        (*cie) = ci; (*cpe) = pi; (*cte) = ti;
                    }
                }
            }
            if((c == 3) && (pe>=ws) && (pe<=we)) {
                te1 = ti; 
                if(te1 == te0) {
                    err[1] = tot_err + cl; 
                    eset = 1; (*te) = te1; 
                    (*cie) = ci; (*cpe) = pi; (*cte) = ti;
                }
            }
            if(eset) break;
        }

        if(c != 0) tot_err += cl;
    }

    // if(!(((mode == 1)&&((*ts) == ts0))||((mode == 2)&&((*te) == te0))||(mode == 3))) {
    //     fprintf(stderr, "+[M::%s::mode->%ld] t0::[%ld, %ld), t::[%ld, %ld), err::%d\n", 
    //     __func__, mode, ts0, te0, (*ts), (*te), err[1] - err[0]);
    // }
    assert(((mode == 1)&&((*ts) == ts0))||((mode == 2)&&((*te) == te0))||(mode == 3));
    return err[1] - err[0];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movslq 0x10(%rax), %rcx
xorl %eax, %eax
cmpq -0x18(%rbp), %rcx
movb %al, -0x83(%rbp)
jg 0x8f170
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
addl $0x1, %eax
movslq %eax, %rcx
xorl %eax, %eax
cmpq -0x20(%rbp), %rcx
movb %al, -0x83(%rbp)
jl 0x8f170
cmpq $0x0, -0x10(%rbp)
setg %al
movb %al, -0x83(%rbp)
movb -0x83(%rbp), %al
testb $0x1, %al
jne 0x8f17c
jmp 0x8f17e
jmp 0x8f19d
leaq 0x2f68fb(%rip), %rdi    # 0x385a80
leaq 0x2f318f(%rip), %rsi    # 0x38231b
movl $0x5acb, %edx           # imm = 0x5ACB
leaq 0x2f6913(%rip), %rcx    # 0x385aab
callq 0x2130
cmpq $0x1, -0x10(%rbp)
jne 0x8f1b1
movq -0x30(%rbp), %rax
movq $-0x1, (%rax)
jmp 0x8f1dd
cmpq $0x2, -0x10(%rbp)
jne 0x8f1c5
movq -0x28(%rbp), %rax
movq $-0x1, (%rax)
jmp 0x8f1db
movq -0x30(%rbp), %rax
movq $-0x1, (%rax)
movq -0x28(%rbp), %rax
movq $-0x1, (%rax)
jmp 0x8f1dd
movq 0x38(%rbp), %rax
movq $-0x1, (%rax)
movq 0x30(%rbp), %rax
movq $-0x1, (%rax)
movq 0x28(%rbp), %rax
movq $-0x1, (%rax)
movq 0x20(%rbp), %rax
movq $-0x1, (%rax)
movq 0x18(%rbp), %rax
movq $-0x1, (%rax)
movq 0x10(%rbp), %rax
movq $-0x1, (%rax)
movq -0x8(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x78(%rbp)
movw $0x0, -0x80(%rbp)
movw $0x0, -0x82(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x78(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpq 0xa8(%rcx), %rax
jae 0x8f77f
movq -0x8(%rbp), %rdi
addq $0xa8, %rdi
movl -0x78(%rbp), %esi
leaq -0x7e(%rbp), %rdx
leaq -0x7c(%rbp), %rcx
callq 0xa1ee0
movl %eax, -0x78(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x60(%rbp)
movzwl -0x7e(%rbp), %eax
cmpl $0x1, %eax
jg 0x8f2bf
movl -0x7c(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x7c(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x8f2e9
movzwl -0x7e(%rbp), %eax
cmpl $0x2, %eax
jne 0x8f2d3
movl -0x7c(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0x8f2e7
movzwl -0x7e(%rbp), %eax
cmpl $0x3, %eax
jne 0x8f2e5
movl -0x7c(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x8f2e7
jmp 0x8f2e9
movl -0x54(%rbp), %eax
movl %eax, -0x74(%rbp)
movslq -0x74(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x8f310
movzwl -0x7e(%rbp), %eax
cmpl $0x0, %eax
je 0x8f30b
movl -0x7c(%rbp), %eax
addl -0x6c(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0x8f25e
movslq -0x70(%rbp), %rax
cmpq -0x20(%rbp), %rax
jle 0x8f331
movzwl -0x7e(%rbp), %eax
cmpl $0x0, %eax
je 0x8f32c
movl -0x7c(%rbp), %eax
addl -0x6c(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0x8f77f
cmpw $0x0, -0x80(%rbp)
jne 0x8f549
movq -0x18(%rbp), %rax
movslq -0x70(%rbp), %rcx
cmpq %rcx, %rax
jl 0x8f4ce
movq -0x18(%rbp), %rax
movslq -0x74(%rbp), %rcx
cmpq %rcx, %rax
jge 0x8f4ce
movzwl -0x7e(%rbp), %eax
cmpl $0x1, %eax
jg 0x8f3f2
movslq -0x58(%rbp), %rax
movslq -0x74(%rbp), %rcx
subq -0x18(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
je 0x8f38f
cmpq $0x1, -0x10(%rbp)
je 0x8f3ed
movzwl -0x7e(%rbp), %eax
cmpl $0x1, %eax
jne 0x8f3af
movslq -0x6c(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0x70(%rbp), %rdx
subq %rdx, %rcx
addq %rcx, %rax
movl %eax, -0x68(%rbp)
jmp 0x8f3b5
movl -0x6c(%rbp), %eax
movl %eax, -0x68(%rbp)
movw $0x1, -0x80(%rbp)
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl -0x78(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movslq -0x5c(%rbp), %rcx
movq 0x20(%rbp), %rax
movq %rcx, (%rax)
movslq -0x60(%rbp), %rcx
movq 0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x8f4cc
movzwl -0x7e(%rbp), %eax
cmpl $0x2, %eax
jne 0x8f463
movslq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
je 0x8f414
cmpq $0x1, -0x10(%rbp)
je 0x8f461
movslq -0x6c(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0x70(%rbp), %rdx
subq %rdx, %rcx
addq %rcx, %rax
movl %eax, -0x68(%rbp)
movw $0x1, -0x80(%rbp)
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl -0x78(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movslq -0x5c(%rbp), %rcx
movq 0x20(%rbp), %rax
movq %rcx, (%rax)
movslq -0x60(%rbp), %rcx
movq 0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x8f4ca
movzwl -0x7e(%rbp), %eax
cmpl $0x3, %eax
jne 0x8f4c8
movslq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
je 0x8f485
cmpq $0x1, -0x10(%rbp)
je 0x8f4c6
movl -0x6c(%rbp), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x68(%rbp)
movw $0x1, -0x80(%rbp)
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl -0x78(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movslq -0x5c(%rbp), %rcx
movq 0x20(%rbp), %rax
movq %rcx, (%rax)
movslq -0x60(%rbp), %rcx
movq 0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x8f4c8
jmp 0x8f4ca
jmp 0x8f4cc
jmp 0x8f4ce
movzwl -0x7e(%rbp), %eax
cmpl $0x3, %eax
jne 0x8f547
movq -0x18(%rbp), %rax
movslq -0x70(%rbp), %rcx
cmpq %rcx, %rax
jl 0x8f547
movq -0x18(%rbp), %rax
movslq -0x74(%rbp), %rcx
cmpq %rcx, %rax
jg 0x8f547
movl -0x58(%rbp), %eax
subl -0x7c(%rbp), %eax
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jne 0x8f545
movl -0x6c(%rbp), %eax
movl %eax, -0x68(%rbp)
movw $0x1, -0x80(%rbp)
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl -0x78(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movslq -0x5c(%rbp), %rcx
movq 0x20(%rbp), %rax
movq %rcx, (%rax)
movslq -0x60(%rbp), %rcx
movq 0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x8f547
jmp 0x8f549
cmpw $0x0, -0x82(%rbp)
jne 0x8f768
movq -0x20(%rbp), %rax
movslq -0x70(%rbp), %rcx
cmpq %rcx, %rax
jle 0x8f6e4
movq -0x20(%rbp), %rax
movslq -0x74(%rbp), %rcx
cmpq %rcx, %rax
jg 0x8f6e4
movzwl -0x7e(%rbp), %eax
cmpl $0x1, %eax
jg 0x8f60b
movslq -0x58(%rbp), %rax
movslq -0x74(%rbp), %rcx
subq -0x20(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0x8f5aa
cmpq $0x2, -0x10(%rbp)
je 0x8f606
movzwl -0x7e(%rbp), %eax
cmpl $0x1, %eax
jne 0x8f5ca
movslq -0x6c(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq -0x70(%rbp), %rdx
subq %rdx, %rcx
addq %rcx, %rax
movl %eax, -0x64(%rbp)
jmp 0x8f5d0
movl -0x6c(%rbp), %eax
movl %eax, -0x64(%rbp)
movw $0x1, -0x82(%rbp)
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl -0x78(%rbp), %eax
movl %eax, %ecx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
movslq -0x54(%rbp), %rcx
movq 0x28(%rbp), %rax
movq %rcx, (%rax)
movslq -0x58(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
jmp 0x8f6e2
movzwl -0x7e(%rbp), %eax
cmpl $0x2, %eax
jne 0x8f67a
movslq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0x8f62d
cmpq $0x2, -0x10(%rbp)
je 0x8f678
movslq -0x6c(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq -0x70(%rbp), %rdx
subq %rdx, %rcx
addq %rcx, %rax
movl %eax, -0x64(%rbp)
movw $0x1, -0x82(%rbp)
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl -0x78(%rbp), %eax
movl %eax, %ecx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
movslq -0x54(%rbp), %rcx
movq 0x28(%rbp), %rax
movq %rcx, (%rax)
movslq -0x58(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
jmp 0x8f6e0
movzwl -0x7e(%rbp), %eax
cmpl $0x3, %eax
jne 0x8f6de
movl -0x58(%rbp), %eax
subl -0x7c(%rbp), %eax
movl %eax, %eax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0x8f6a0
cmpq $0x2, -0x10(%rbp)
je 0x8f6dc
movl -0x6c(%rbp), %eax
movl %eax, -0x64(%rbp)
movw $0x1, -0x82(%rbp)
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl -0x78(%rbp), %eax
movl %eax, %ecx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
movslq -0x54(%rbp), %rcx
movq 0x28(%rbp), %rax
movq %rcx, (%rax)
movslq -0x58(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
jmp 0x8f6de
jmp 0x8f6e0
jmp 0x8f6e2
jmp 0x8f6e4
movzwl -0x7e(%rbp), %eax
cmpl $0x3, %eax
jne 0x8f75a
movq -0x20(%rbp), %rax
movslq -0x70(%rbp), %rcx
cmpq %rcx, %rax
jl 0x8f75a
movq -0x20(%rbp), %rax
movslq -0x74(%rbp), %rcx
cmpq %rcx, %rax
jg 0x8f75a
movslq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
jne 0x8f758
movl -0x6c(%rbp), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x64(%rbp)
movw $0x1, -0x82(%rbp)
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl -0x78(%rbp), %eax
movl %eax, %ecx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
movslq -0x54(%rbp), %rcx
movq 0x28(%rbp), %rax
movq %rcx, (%rax)
movslq -0x58(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
jmp 0x8f75a
cmpw $0x0, -0x82(%rbp)
je 0x8f766
jmp 0x8f77f
jmp 0x8f768
movzwl -0x7e(%rbp), %eax
cmpl $0x0, %eax
je 0x8f77a
movl -0x7c(%rbp), %eax
addl -0x6c(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0x8f25e
cmpq $0x1, -0x10(%rbp)
jne 0x8f79b
movq -0x28(%rbp), %rax
movq (%rax), %rcx
movb $0x1, %al
cmpq -0x38(%rbp), %rcx
movb %al, -0x84(%rbp)
je 0x8f7c5
cmpq $0x2, -0x10(%rbp)
jne 0x8f7b7
movq -0x30(%rbp), %rax
movq (%rax), %rcx
movb $0x1, %al
cmpq -0x40(%rbp), %rcx
movb %al, -0x84(%rbp)
je 0x8f7c5
cmpq $0x3, -0x10(%rbp)
sete %al
movb %al, -0x84(%rbp)
movb -0x84(%rbp), %al
testb $0x1, %al
jne 0x8f7d1
jmp 0x8f7d3
jmp 0x8f7f2
leaq 0x2f637b(%rip), %rdi    # 0x385b55
leaq 0x2f2b3a(%rip), %rsi    # 0x38231b
movl $0x5b44, %edx           # imm = 0x5B44
leaq 0x2f62be(%rip), %rcx    # 0x385aab
callq 0x2130
movl -0x64(%rbp), %eax
subl -0x68(%rbp), %eax
cltq
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)