// File: /workspace/github_cpp_makefile_star2_O3/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
movslq 0x10(%rdi), %r10
cmpq %rdx, %r10
jg 0x7e0ef
testq %rsi, %rsi
jle 0x7e0ef
movslq 0x14(%rdi), %rax
incq %rax
cmpq %rcx, %rax
jl 0x7e0ef
movq 0xb8(%rsp), %r11
movq 0xb0(%rsp), %r15
movq 0xa8(%rsp), %rbx
movq 0xa0(%rsp), %r12
movq 0x98(%rsp), %r14
movq 0x90(%rsp), %r13
movq (%r8), %rax
movq %rax, 0x28(%rsp)
movq (%r9), %rax
movq %rax, 0x10(%rsp)
movq %r9, %rax
cmpq $0x1, %rsi
je 0x7dc8a
movq %r8, %rax
cmpq $0x2, %rsi
je 0x7dc8a
movq $-0x1, (%r9)
movq %r8, %rax
movq %r8, 0x48(%rsp)
movq %rsi, 0x18(%rsp)
movq %r9, 0x38(%rsp)
movq $-0x1, %rsi
movq %rsi, (%rax)
movq %rsi, (%r11)
movq %rsi, (%r15)
movq %rsi, (%rbx)
movq %rsi, (%r12)
movq %rsi, (%r14)
movq %rsi, (%r13)
movq 0xa8(%rdi), %r9
testq %r9, %r9
je 0x7dfe0
movq %rcx, 0x30(%rsp)
movl 0x1c(%rdi), %eax
movq 0xb8(%rdi), %r12
xorl %esi, %esi
movl $0x0, 0x8(%rsp)
xorl %r15d, %r15d
xorl %ecx, %ecx
movl $0x0, 0xc(%rsp)
movl %ecx, %r14d
movl %r10d, %r13d
movl %eax, %r8d
movzwl (%r12,%rsi,2), %ebp
movl %ebp, %r11d
shrl $0xe, %r11d
movl %ebp, %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
incl %r15d
cmpq %r15, %r9
jbe 0x7dd2a
movzwl (%r12,%r15,2), %eax
movl %eax, %esi
shrl $0xe, %esi
cmpl %r11d, %esi
jne 0x7dd2a
andl $0x3fff, %eax           # imm = 0x3FFF
addl %eax, %ecx
jmp 0x7dd0a
testw %bp, %bp
js 0x7dd65
leal (%rcx,%r13), %r10d
leal (%rcx,%r8), %eax
movslq %r10d, %rbx
cmpq %rbx, %rdx
jle 0x7dd8d
movzwl %bp, %esi
xorl %r11d, %r11d
cmpl $0x4000, %esi           # imm = 0x4000
cmovbl %r11d, %ecx
addl %ecx, %r14d
movl %r15d, %esi
movl %r10d, %r13d
movl %eax, %r8d
cmpq %rsi, %r9
ja 0x7dcf6
jmp 0x7dfeb
cmpw $0x3, %r11w
je 0x7dd84
movzwl %r11w, %esi
movl %r8d, %eax
movl %r13d, %r10d
cmpl $0x2, %esi
jne 0x7dd37
leal (%rcx,%r13), %r10d
movl %r8d, %eax
jmp 0x7dd37
leal (%rcx,%r8), %eax
movl %r13d, %r10d
jmp 0x7dd37
movq %rdi, 0x50(%rsp)
movslq %r13d, %rdi
cmpq 0x30(%rsp), %rdi
jg 0x7e03c
cmpw $0x0, 0x8(%rsp)
jne 0x7df12
movl $0x0, 0x8(%rsp)
cmpq %rdx, %rdi
jg 0x7deaf
movq %rdx, %rsi
subq %rbx, %rsi
jge 0x7deaf
testw %bp, %bp
js 0x7de0f
movq %rsi, %r9
movslq %eax, %rsi
addq %rsi, %r9
movq %r9, %rsi
cmpq $0x1, 0x18(%rsp)
jne 0x7dde9
cmpq 0x28(%rsp), %rsi
jne 0x7de31
movl %edx, %r9d
subl %r13d, %r9d
cmpw $0x1, %r11w
movq %rsi, 0x40(%rsp)
movl $0x0, %esi
cmovnel %esi, %r9d
addl %r14d, %r9d
movl %r9d, 0x24(%rsp)
jmp 0x7ded5
cmpw $0x3, %r11w
je 0x7de3e
movzwl %r11w, %esi
cmpl $0x2, %esi
jne 0x7de31
movslq %eax, %rsi
cmpq $0x1, 0x18(%rsp)
jne 0x7de5a
cmpq %rsi, 0x28(%rsp)
je 0x7de5a
movl $0x0, 0x8(%rsp)
jmp 0x7df1a
movslq %eax, %r9
cmpq $0x1, 0x18(%rsp)
jne 0x7de6f
cmpq %r9, 0x28(%rsp)
je 0x7de6f
movl $0x0, 0x8(%rsp)
jmp 0x7deaf
movl %edx, %r9d
subl %r13d, %r9d
addl %r14d, %r9d
movl %r9d, 0x24(%rsp)
movq %rsi, 0x40(%rsp)
jmp 0x7ded5
leal (%rcx,%r14), %esi
movl %esi, 0xc(%rsp)
movq 0x48(%rsp), %rsi
movq %r9, (%rsi)
leal -0x1(%r15), %esi
movq 0x90(%rsp), %r9
movq %rsi, (%r9)
movq 0xa0(%rsp), %rsi
movq %rdi, (%rsi)
movslq %r8d, %rsi
movq 0xb0(%rsp), %r9
movq %rsi, (%r9)
movw $0x1, %si
movl %esi, 0x8(%rsp)
cmpq %rdx, %rdi
jg 0x7df1a
cmpw $0x3, %r11w
jne 0x7df1a
movl %eax, %esi
subl %ecx, %esi
movq 0x28(%rsp), %r9
movq %r9, 0x40(%rsp)
movl %r14d, 0x24(%rsp)
cmpq %rsi, 0x28(%rsp)
jne 0x7df1a
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %r9
movq %r9, (%rsi)
leal -0x1(%r15), %esi
movq 0x90(%rsp), %r9
movq %rsi, (%r9)
movq 0xa0(%rsp), %rsi
movq %rdi, (%rsi)
movslq %r8d, %rsi
movq 0xb0(%rsp), %r8
movq %rsi, (%r8)
movl 0x24(%rsp), %esi
movl %esi, 0xc(%rsp)
movw $0x1, %si
movl %esi, 0x8(%rsp)
movq 0x30(%rsp), %r8
cmpq %r8, %rdi
movq 0x50(%rsp), %rdi
jge 0x7df9c
subq %rbx, %r8
jg 0x7df9c
testw %bp, %bp
js 0x7df51
movslq %eax, %rsi
addq %rsi, %r8
cmpq $0x2, 0x18(%rsp)
jne 0x7e04b
cmpq 0x10(%rsp), %r8
jne 0x7dfb8
jmp 0x7e04b
cmpw $0x3, %r11w
je 0x7df7c
movzwl %r11w, %esi
cmpl $0x2, %esi
jne 0x7dfb8
movslq %eax, %r8
cmpq $0x2, 0x18(%rsp)
jne 0x7e0df
cmpq %r8, 0x10(%rsp)
jne 0x7dfb8
jmp 0x7e0df
movl %eax, %esi
subl %ecx, %esi
cmpq $0x2, 0x18(%rsp)
movq 0x30(%rsp), %r8
jne 0x7e094
cmpq %rsi, 0x10(%rsp)
je 0x7e094
cmpw $0x3, %r11w
jne 0x7dfb8
cmpq %rbx, 0x30(%rsp)
jg 0x7dfb8
movslq %eax, %rsi
cmpq %rsi, 0x10(%rsp)
je 0x7e041
movzwl %bp, %esi
xorl %r11d, %r11d
cmpl $0x4000, %esi           # imm = 0x4000
cmovbl %r11d, %ecx
addl %r14d, %ecx
movq 0xa8(%rdi), %r9
movl %r15d, %esi
cmpq %rsi, %r9
ja 0x7dced
jmp 0x7dfeb
xorl %r11d, %r11d
movl $0x0, 0xc(%rsp)
movq 0x18(%rsp), %rax
cmpq $0x1, %rax
jne 0x7e002
movq 0x48(%rsp), %rax
movq 0x28(%rsp), %rcx
jmp 0x7e01c
cmpq $0x3, %rax
je 0x7e025
cmpq $0x2, %rax
jne 0x7e10e
movq 0x38(%rsp), %rax
movq 0x10(%rsp), %rcx
cmpq %rcx, (%rax)
jne 0x7e10e
subl 0xc(%rsp), %r11d
movslq %r11d, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r11d, %r11d
jmp 0x7dfeb
movq 0x10(%rsp), %rsi
movq %rsi, %r8
jmp 0x7e05d
movq 0x30(%rsp), %rax
subl %r13d, %eax
xorl %ecx, %ecx
cmpw $0x1, %r11w
cmovel %eax, %ecx
movq 0x38(%rsp), %rdi
movq 0xb8(%rsp), %r9
movq 0xa8(%rsp), %r10
movq 0x98(%rsp), %r11
addl %r14d, %ecx
movq %r8, (%rdi)
movl %r15d, %eax
movq %rax, (%r11)
movq %rbx, (%r10)
movq %rsi, (%r9)
movl %ecx, %r11d
jmp 0x7dfeb
movq 0x38(%rsp), %rdi
movq %rsi, (%rdi)
movl %r15d, %edx
movq 0x98(%rsp), %r11
movq %rdx, (%r11)
movq 0xa8(%rsp), %r10
movq %rbx, (%r10)
cltq
movq 0xb8(%rsp), %r9
movq %rax, (%r9)
cmpq %rbx, %r8
jg 0x7e0d7
movq 0x10(%rsp), %rdx
movq %rdx, %r8
movq %rdx, %rsi
cmpq %rax, %rdx
je 0x7e07a
movl %r14d, %r11d
jmp 0x7dfeb
movq 0x30(%rsp), %rcx
subl %r13d, %ecx
movq %r8, %rsi
jmp 0x7e05d
leaq 0x1b6dc2(%rip), %rdi    # 0x234eb8
leaq 0x1b3656(%rip), %rsi    # 0x231753
leaq 0x1b6ddf(%rip), %rcx    # 0x234ee3
movl $0x5acb, %edx           # imm = 0x5ACB
callq 0x2110
leaq 0x1b6e78(%rip), %rdi    # 0x234f8d
leaq 0x1b3637(%rip), %rsi    # 0x231753
leaq 0x1b6dc0(%rip), %rcx    # 0x234ee3
movl $0x5b44, %edx           # imm = 0x5B44
callq 0x2110