// File: /workspace/github_cpp_makefile_star2_O1/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), %r11
cmpq %rdx, %r11
jg 0x7e708
testq %rsi, %rsi
jle 0x7e708
movslq 0x14(%rdi), %rax
incq %rax
cmpq %rcx, %rax
jl 0x7e708
movq 0xb8(%rsp), %r10
movq 0xb0(%rsp), %rbx
movq 0xa8(%rsp), %r14
movq 0xa0(%rsp), %r15
movq 0x98(%rsp), %r12
movq 0x90(%rsp), %r13
movq (%r8), %rax
movq %rax, 0x38(%rsp)
movq (%r9), %rax
movq %rax, 0x40(%rsp)
movq %r9, %rax
cmpq $0x1, %rsi
je 0x7e215
movq %r8, %rax
cmpq $0x2, %rsi
je 0x7e215
movq $-0x1, (%r9)
movq %r8, %rax
movq %r8, 0x50(%rsp)
movq %rsi, 0x18(%rsp)
movq %r9, 0x30(%rsp)
movq $-0x1, %rsi
movq %rsi, (%rax)
movq %rsi, (%r10)
movq %rsi, (%rbx)
movq %rsi, (%r14)
movq %rsi, (%r15)
movq %rsi, (%r12)
movq %rsi, (%r13)
movq 0xa8(%rdi), %rsi
testq %rsi, %rsi
je 0x7e6aa
movq %rdx, 0x20(%rsp)
movq %rcx, 0x48(%rsp)
movl 0x1c(%rdi), %edx
movq 0xb8(%rdi), %rbx
xorl %r15d, %r15d
movl $0x0, 0x8(%rsp)
xorl %r13d, %r13d
xorl %r10d, %r10d
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movl %r11d, %r12d
movl %edx, %ecx
movzwl (%rbx,%r15,2), %ebp
movl %ebp, %eax
shrl $0xe, %eax
movl %ebp, %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
incl %r13d
cmpq %r13, %rsi
jbe 0x7e2d2
movq 0x20(%rsp), %r15
movzwl (%rbx,%r13,2), %r8d
movl %r8d, %r9d
shrl $0xe, %r9d
cmpl %eax, %r9d
jne 0x7e2d7
andl $0x3fff, %r8d           # imm = 0x3FFF
addl %r8d, %r14d
incl %r13d
cmpq %r13, %rsi
ja 0x7e2ad
jmp 0x7e2d7
movq 0x20(%rsp), %r15
testw %bp, %bp
js 0x7e316
leal (%r14,%r12), %r11d
leal (%r14,%rcx), %edx
movslq %r11d, %r9
cmpq %r9, %r15
jle 0x7e33c
movzwl %bp, %eax
cmpl $0x4000, %eax           # imm = 0x4000
movl $0x0, %eax
cmovbl %eax, %r14d
addl %r14d, %r10d
movl %r13d, %r15d
movl %r11d, %r12d
movl %edx, %ecx
cmpq %r15, %rsi
ja 0x7e28c
jmp 0x7e6ba
cmpw $0x3, %ax
je 0x7e333
movzwl %ax, %r9d
movl %ecx, %edx
movl %r12d, %r11d
cmpl $0x2, %r9d
jne 0x7e2e4
leal (%r14,%r12), %r11d
movl %ecx, %edx
jmp 0x7e2e4
leal (%r14,%rcx), %edx
movl %r12d, %r11d
jmp 0x7e2e4
movslq %r12d, %rsi
cmpq 0x48(%rsp), %rsi
jg 0x7e6ba
cmpw $0x0, 0x8(%rsp)
je 0x7e3f0
movw $0x1, %cx
movl %ecx, 0x8(%rsp)
xorl %ecx, %ecx
movl %r13d, %r15d
movq 0x48(%rsp), %r8
cmpq %r8, %rsi
jge 0x7e562
movq %r8, %rsi
subq %r9, %rsi
jg 0x7e562
testw %bp, %bp
js 0x7e4ab
movslq %edx, %rcx
addq %rcx, %rsi
cmpq $0x2, 0x18(%rsp)
jne 0x7e39f
cmpq 0x40(%rsp), %rsi
jne 0x7e560
movq 0x48(%rsp), %r8
subl %r12d, %r8d
cmpw $0x1, %ax
movl $0x0, %r12d
cmovnel %r12d, %r8d
addl %r10d, %r8d
movl %r8d, 0x10(%rsp)
movq 0x30(%rsp), %r12
movq %rsi, (%r12)
movq 0x98(%rsp), %rsi
movq %r15, (%rsi)
movq 0xa8(%rsp), %rsi
movq %r9, (%rsi)
movq 0xb8(%rsp), %rsi
movq %rcx, (%rsi)
movw $0x1, %cx
jmp 0x7e562
movl $0x0, 0x8(%rsp)
cmpq %r15, %rsi
jg 0x7e644
movq %r15, %r8
subq %r9, %r8
jge 0x7e644
testw %bp, %bp
js 0x7e4f3
movslq %edx, %r15
addq %r15, %r8
cmpq $0x1, 0x18(%rsp)
jne 0x7e42f
cmpq 0x38(%rsp), %r8
jne 0x7e532
movq %r8, 0x8(%rsp)
movq 0x20(%rsp), %r8
movl %r8d, %r15d
subl %r12d, %r15d
cmpw $0x1, %ax
movl $0x0, %r8d
cmovnel %r8d, %r15d
movl %r15d, %r8d
addl %r10d, %r8d
movl %r8d, 0x2c(%rsp)
movq 0x50(%rsp), %r8
movq 0x8(%rsp), %r15
movq %r15, (%r8)
leal -0x1(%r13), %r15d
movq 0x90(%rsp), %r8
movq %r15, (%r8)
movq 0xa0(%rsp), %r8
movq %rsi, (%r8)
movslq %ecx, %r15
movq 0xb0(%rsp), %r8
movq %r15, (%r8)
movq 0x20(%rsp), %r15
movw $0x1, %r8w
movl %r8d, 0x8(%rsp)
movl 0x2c(%rsp), %r8d
movl %r8d, 0x14(%rsp)
jmp 0x7e644
cmpw $0x3, %ax
je 0x7e544
movzwl %ax, %ecx
cmpl $0x2, %ecx
jne 0x7e560
movslq %edx, %rcx
cmpq $0x2, 0x18(%rsp)
jne 0x7e4d7
cmpq %rcx, 0x40(%rsp)
jne 0x7e560
movq 0x48(%rsp), %rsi
subl %r12d, %esi
addl %r10d, %esi
movl %esi, 0x10(%rsp)
movq 0x30(%rsp), %rsi
movq %rcx, (%rsi)
jmp 0x7e3c6
cmpw $0x3, %ax
je 0x7e60e
movzwl %ax, %r15d
cmpl $0x2, %r15d
jne 0x7e532
movslq %edx, %r8
cmpq $0x1, 0x18(%rsp)
movq 0x20(%rsp), %r15
jne 0x7e522
cmpq %r8, 0x38(%rsp)
jne 0x7e63c
movq %r8, 0x8(%rsp)
movl %r15d, %r8d
subl %r12d, %r8d
jmp 0x7e450
movl $0x0, 0x8(%rsp)
movq 0x20(%rsp), %r15
jmp 0x7e644
movl %edx, %ecx
subl %r14d, %ecx
cmpq $0x2, 0x18(%rsp)
jne 0x7e5e3
cmpq %rcx, 0x40(%rsp)
je 0x7e5e3
xorl %ecx, %ecx
cmpw $0x3, %ax
jne 0x7e5ae
cmpq %r9, 0x48(%rsp)
jg 0x7e5ae
movslq %edx, %rax
cmpq %rax, 0x40(%rsp)
jne 0x7e5ae
leal (%r14,%r10), %ecx
movl %ecx, 0x10(%rsp)
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
movq 0x98(%rsp), %rcx
movq %r15, (%rcx)
movq 0xa8(%rsp), %rcx
movq %r9, (%rcx)
movq 0xb8(%rsp), %rcx
movq %rax, (%rcx)
movw $0x1, %cx
testw %cx, %cx
jne 0x7e6ba
movzwl %bp, %eax
cmpl $0x4000, %eax           # imm = 0x4000
movl $0x0, %eax
cmovbl %eax, %r14d
addl %r10d, %r14d
movq 0xa8(%rdi), %rsi
movl %r14d, %r10d
cmpq %r15, %rsi
ja 0x7e287
jmp 0x7e6ba
movq 0x30(%rsp), %rsi
movq %rcx, (%rsi)
movq 0x98(%rsp), %rcx
movq %r15, (%rcx)
movq 0xa8(%rsp), %rcx
movq %r9, (%rcx)
movslq %edx, %rcx
movl %r10d, 0x10(%rsp)
jmp 0x7e3dc
movslq %edx, %r8
cmpq $0x1, 0x18(%rsp)
movq %r8, 0x8(%rsp)
jne 0x7e6a2
movl %r14d, 0x2c(%rsp)
movq 0x8(%rsp), %r8
cmpq %r8, 0x38(%rsp)
movl 0x2c(%rsp), %r8d
je 0x7e450
movl $0x0, 0x8(%rsp)
cmpq %r15, %rsi
jg 0x7e35e
cmpw $0x3, %ax
jne 0x7e35e
movl %edx, %r15d
subl %r14d, %r15d
cmpq %r15, 0x38(%rsp)
jne 0x7e35e
movq 0x50(%rsp), %r8
movq %r15, (%r8)
leal -0x1(%r13), %r15d
movq 0x90(%rsp), %r8
movq %r15, (%r8)
movq 0xa0(%rsp), %r8
movq %rsi, (%r8)
movslq %ecx, %rcx
movq 0xb0(%rsp), %r8
movq %rcx, (%r8)
movl %r10d, 0x14(%rsp)
jmp 0x7e356
movl %r14d, %r8d
jmp 0x7e450
movl $0x0, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
movq 0x18(%rsp), %rax
cmpq $0x1, %rax
jne 0x7e6d4
movq 0x50(%rsp), %rcx
movq 0x38(%rsp), %rdx
cmpq %rdx, (%rcx)
je 0x7e6ef
cmpq $0x3, %rax
je 0x7e6ef
cmpq $0x2, %rax
jne 0x7e727
movq 0x30(%rsp), %rax
movq 0x40(%rsp), %rcx
cmpq %rcx, (%rax)
jne 0x7e727
movl 0x10(%rsp), %eax
subl 0x14(%rsp), %eax
cltq
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1aa699(%rip), %rdi    # 0x228da8
leaq 0x1a6f2d(%rip), %rsi    # 0x225643
leaq 0x1aa6b6(%rip), %rcx    # 0x228dd3
movl $0x5acb, %edx           # imm = 0x5ACB
callq 0x2110
leaq 0x1aa74f(%rip), %rdi    # 0x228e7d
leaq 0x1a6f0e(%rip), %rsi    # 0x225643
leaq 0x1aa697(%rip), %rcx    # 0x228dd3
movl $0x5b44, %edx           # imm = 0x5B44
callq 0x2110