// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, 0x38(%rsp)
movslq 0x10(%rdi), %rcx
cmpq %rdx, %rcx
jg 0x4aeef
testq %rsi, %rsi
jle 0x4aeef
movq %rdi, %rbp
movslq 0x14(%rdi), %rax
incq %rax
cmpq 0x38(%rsp), %rax
jl 0x4aeef
movq %rdx, %r13
movq 0xb8(%rsp), %rdx
movq 0xb0(%rsp), %r11
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rbx
movq 0x98(%rsp), %r10
movq 0x90(%rsp), %r14
movq (%r8), %rax
movq %rax, 0x40(%rsp)
movq (%r9), %rax
movq %rax, 0x28(%rsp)
movq %r9, %rax
cmpq $0x1, %rsi
je 0x4aad1
movq %r8, %rax
cmpq $0x2, %rsi
je 0x4aad1
orq $-0x1, (%r9)
movq %r8, %rax
movq %r8, 0x30(%rsp)
movq %rsi, 0x10(%rsp)
movq %r9, 0x48(%rsp)
orq $-0x1, (%rax)
orq $-0x1, (%rdx)
orq $-0x1, (%r11)
orq $-0x1, (%rdi)
orq $-0x1, (%rbx)
orq $-0x1, (%r10)
orq $-0x1, (%r14)
movl 0x1c(%rbp), %edx
addq $0xa8, %rbp
leaq 0xc(%rsp), %rbx
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x20(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
movl $0x0, 0x8(%rsp)
movl %edx, %r15d
movl %ecx, %r14d
movl %eax, %ecx
cmpq %rcx, (%rbp)
jbe 0x4ae25
movq %rbp, %rdi
movl %eax, %esi
leaq 0x26(%rsp), %rdx
movq %rbx, %rcx
callq 0x53a7d
movzwl 0x26(%rsp), %esi
cmpl $0x1, %esi
ja 0x4ab63
movl 0xc(%rsp), %edx
leal (%rdx,%r14), %ecx
addl %r15d, %edx
jmp 0x4ab89
cmpl $0x3, %esi
je 0x4ab7f
movl %r15d, %edx
movl %r14d, %ecx
cmpl $0x2, %esi
jne 0x4ab89
movl 0xc(%rsp), %ecx
addl %r14d, %ecx
movl %r15d, %edx
jmp 0x4ab89
movl 0xc(%rsp), %edx
addl %r15d, %edx
movl %r14d, %ecx
movslq %ecx, %rdi
cmpq %rdi, %r13
jle 0x4ab9e
testl %esi, %esi
je 0x4ab99
movl 0xc(%rsp), %esi
addl %esi, %r12d
jmp 0x4ab28
movslq %r14d, %r8
cmpq 0x38(%rsp), %r8
jg 0x4ae25
cmpw $0x0, 0x8(%rsp)
je 0x4ac2c
movw $0x1, %r9w
movl %r9d, 0x8(%rsp)
movq 0x38(%rsp), %r9
cmpq %r9, %r8
setge %r8b
subq %rdi, %r9
setg %r10b
orb %r8b, %r10b
movb $0x1, %r10b
movl %eax, %r8d
je 0x4ac00
cmpl $0x3, %esi
jne 0x4abf6
cmpq %rdi, 0x38(%rsp)
jg 0x4abf6
movslq %edx, %r9
cmpq %r9, 0x28(%rsp)
je 0x4ae83
testb %r10b, %r10b
jne 0x4ab91
jmp 0x4ae25
cmpw $0x1, %si
ja 0x4ac8a
movslq %edx, %r10
addq %r10, %r9
cmpq $0x2, 0x10(%rsp)
jne 0x4ae92
cmpq 0x28(%rsp), %r9
jne 0x4ab91
jmp 0x4ae92
cmpq %r13, %r8
setg %r9b
movq %r13, %r10
subq %rdi, %r10
setge %r11b
orb %r9b, %r11b
movslq %r15d, %r15
je 0x4acbb
movl $0x0, 0x8(%rsp)
cmpq %r13, %r8
setg %r10b
cmpl $0x3, %esi
setne %r11b
orb %r10b, %r11b
jne 0x4abbe
movq %r15, 0x50(%rsp)
movl %edx, %r15d
subl 0xc(%rsp), %r15d
movq 0x40(%rsp), %r9
movq %r9, %r10
movl %r12d, %r11d
cmpq %r15, %r9
jne 0x4abbe
jmp 0x4ade9
cmpl $0x3, %esi
je 0x4ad28
cmpl $0x2, %esi
jne 0x4ab91
movslq %edx, %r9
cmpq $0x2, 0x10(%rsp)
jne 0x4aea6
cmpq %r9, 0x28(%rsp)
jne 0x4ab91
jmp 0x4aea6
cmpw $0x1, %si
ja 0x4acf3
movslq %edx, %r11
addq %r11, %r10
cmpq $0x1, 0x10(%rsp)
jne 0x4acd6
cmpq 0x40(%rsp), %r10
jne 0x4ad1b
movq %r15, 0x50(%rsp)
movl %r13d, %r11d
subl %r14d, %r11d
cmpl $0x1, %esi
movl $0x0, %r9d
cmovnel %r9d, %r11d
jmp 0x4ade6
cmpl $0x3, %esi
je 0x4ad7c
cmpl $0x2, %esi
jne 0x4ad1b
movslq %edx, %r10
cmpq $0x1, 0x10(%rsp)
jne 0x4addb
cmpq %r10, 0x40(%rsp)
je 0x4addb
movl $0x0, 0x8(%rsp)
jmp 0x4abbe
movl %edx, %r9d
subl 0xc(%rsp), %r9d
cmpq $0x2, 0x10(%rsp)
jne 0x4ad43
cmpq %r9, 0x28(%rsp)
jne 0x4abdc
movq 0x48(%rsp), %r10
movq %r9, (%r10)
movq 0x98(%rsp), %r9
movq %r8, (%r9)
movq 0xa8(%rsp), %r9
movq %rdi, (%r9)
movslq %edx, %r9
movq 0xb8(%rsp), %r10
movq %r9, (%r10)
xorl %r10d, %r10d
movl %r12d, 0x1c(%rsp)
jmp 0x4abdc
movslq %edx, %r10
cmpq $0x1, 0x10(%rsp)
jne 0x4ad92
cmpq %r10, 0x40(%rsp)
jne 0x4ac45
movl 0xc(%rsp), %r9d
addl %r12d, %r9d
movl %r9d, 0x20(%rsp)
movq 0x30(%rsp), %r11
movq %r10, (%r11)
leal -0x1(%rax), %r10d
movq 0x90(%rsp), %r9
movq %r10, (%r9)
movq 0xa0(%rsp), %r9
movq %r8, (%r9)
movq 0xb0(%rsp), %r9
movq %r15, (%r9)
movw $0x1, %r9w
movl %r9d, 0x8(%rsp)
jmp 0x4ac4d
movq %r15, 0x50(%rsp)
movl %r13d, %r11d
subl %r14d, %r11d
addl %r12d, %r11d
movq 0x30(%rsp), %r15
movq %r10, (%r15)
leal -0x1(%rax), %r10d
movq 0x90(%rsp), %r9
movq %r10, (%r9)
movq 0xa0(%rsp), %r9
movq %r8, (%r9)
movq 0xb0(%rsp), %r9
movq 0x50(%rsp), %r10
movq %r10, (%r9)
movl %r11d, 0x20(%rsp)
jmp 0x4abb4
movq 0x48(%rsp), %rax
movq 0x10(%rsp), %rcx
movq 0x30(%rsp), %rdx
movl 0x1c(%rsp), %r11d
cmpq $0x1, %rcx
jne 0x4ae4e
movq 0x40(%rsp), %rax
cmpq %rax, (%rdx)
je 0x4ae6c
jmp 0x4af0e
cmpq $0x3, %rcx
je 0x4ae6c
cmpq $0x2, %rcx
jne 0x4af0e
movq 0x28(%rsp), %rcx
cmpq %rcx, (%rax)
jne 0x4af0e
subl 0x20(%rsp), %r11d
movslq %r11d, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %r11d
movq 0x28(%rsp), %r10
movq %r10, %r9
jmp 0x4aeb4
movq 0x38(%rsp), %rax
subl %r14d, %eax
xorl %r11d, %r11d
cmpl $0x1, %esi
cmovel %eax, %r11d
jmp 0x4aeb4
movq 0x38(%rsp), %rax
subl %r14d, %eax
movq %r9, %r10
movl %eax, %r11d
addl %r12d, %r11d
movq 0x48(%rsp), %rax
movq %r9, (%rax)
movq 0x98(%rsp), %rcx
movq %r8, (%rcx)
movq 0xa8(%rsp), %rcx
movq %rdi, (%rcx)
movq 0xb8(%rsp), %rcx
movq %r10, (%rcx)
movq 0x10(%rsp), %rcx
movq 0x30(%rsp), %rdx
jmp 0x4ae39
leaq 0x19bf12(%rip), %rdi    # 0x1e6e08
leaq 0x1987a6(%rip), %rsi    # 0x1e36a3
leaq 0x19bf2f(%rip), %rcx    # 0x1e6e33
movl $0x5acb, %edx           # imm = 0x5ACB
callq 0x2110
leaq 0x19bfc8(%rip), %rdi    # 0x1e6edd
leaq 0x198787(%rip), %rsi    # 0x1e36a3
leaq 0x19bf10(%rip), %rcx    # 0x1e6e33
movl $0x5b44, %edx           # imm = 0x5B44
callq 0x2110