// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: iter_sub_cigar_sv(long, ul_ov_t*, bit_extz_t*, long*, long*, long*, char*, long, char*, long, long, long, long, long, long*, long*, long)
uint64_t iter_sub_cigar_sv(int64_t zid, ul_ov_t *cp, bit_extz_t *ez, int64_t *ck, int64_t *xk, int64_t *yk, char *qstr, int64_t ql, char *tstr, int64_t tl, int64_t ts, int64_t te, int64_t hpc_len, int64_t hpc_rr, int64_t *rys, int64_t *rye, int64_t exd_err_bd)
{
    int64_t err = 0, herr = 0, ex[2], ey[2], s, e, cn = ez->cigar.n, wx[2], wy[2], p; uint16_t op; uint32_t cl;
    ex[0] = ex[1] = -1; ey[0] = ey[1] = -1; 
    (*rys) = (*rye) = -1;
        // fprintf(stderr, "+[M::%s] rid::%u\t%.*s\tq::[%u,%u)\tql::%ld\tt::[%u,%u)\ttl::%ld\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), cp->qs, cp->qe, ql, cp->ts, cp->te, tl);
        
    s = cp->qs; e = cp->qe;
    while ((*ck) > 0 && (*xk) >= s) {///x -> t; y -> p
        --(*ck);
        op = ez->cigar.a[*ck]>>14;
        if(op!=2) (*xk) -= (ez->cigar.a[*ck]&(0x3fff));
        if(op!=3) (*yk) -= (ez->cigar.a[*ck]&(0x3fff)); 
    }

    while ((*ck) < cn && (*xk) < e) {//[s, e)
        wx[0] = *xk; wy[0] = *yk;
        (*ck) = pop_trace(&(ez->cigar), *ck, &op, &cl);
        if(op!=2) (*xk) += cl;
        if(op!=3) (*yk) += cl; 
        wx[1] = (*xk); wy[1] = (*yk);
        if(op == 0) continue;
        if(wx[0] >= s && wx[1] <= e) {
            if(ex[0] < 0) ex[0] = wx[0];
            if(ey[0] < 0) ey[0] = wy[0];
            ex[1] = wx[1]; ey[1] = wy[1];
            err += cl;
            if(hpc_len > 0) {
                assert(wy[0] >= ts && wy[1] <= te);
                if(hpc_mask_ff_region(qstr, ql, wx[0], wx[1], hpc_len, hpc_rr, 6, 0.51) || hpc_mask_ff_region(tstr, tl, wy[0] - ts, wy[1] - ts, hpc_len, hpc_rr, 6, 0.51)) {
                    herr += cl;
                }
            }
        }
    }

    while ((*ck) < cn && (*xk) <= e) {//[s, e)
        wx[0] = *xk; wy[0] = *yk;
        op = ez->cigar.a[*ck]>>14; 
        cl = (ez->cigar.a[*ck]&(0x3fff));
        if(op != 2) break;
        (*yk) += (ez->cigar.a[*ck]&(0x3fff)); 
        (*ck)++; wx[1] = (*xk); wy[1] = (*yk);
        if(wx[0] >= s && wx[1] <= e) {
            if(ex[0] < 0) ex[0] = wx[0];
            if(ey[0] < 0) ey[0] = wy[0];
            ex[1] = wx[1]; ey[1] = wy[1];
            err += cl;
            if(hpc_len > 0) {
                assert(wy[0] >= ts && wy[1] <= te);
                if(hpc_mask_ff_region(qstr, ql, wx[0], wx[1], hpc_len, hpc_rr, 6, 0.51) || hpc_mask_ff_region(tstr, tl, wy[0] - ts, wy[1] - ts, hpc_len, hpc_rr, 6, 0.51)) {
                    herr += cl;
                }
            }
        }
    }

    if(hpc_len > 0) {
        // cp->qs = ex[0]; cp->qe = ex[1];
        // cp->ts = ey[0]; cp->te = ey[1];
        // cp->ts = s; cp->te = e;
        assert(cp->qn == err);
        cp->qs = s; cp->qe = e;
        cp->ts = ex[0]; cp->te = ex[1];
        cp->tn = zid; cp->qn = err;
        if((herr > 0) && (herr > (err*0.66))) cp->el = 0;
        else cp->el = 1;
        // (*rys) = ey[0]; (*rye) = ey[1];
        if(err <= exd_err_bd) {
            p = (s + e)/2; p -= exd_err_bd; if(p < 0) p = 0; if(p < ((int64_t)cp->qs)) cp->qs = p;
            p = (s + e)/2; p += exd_err_bd; if(p > ql) p = ql; if(p > ((int64_t)cp->qe))  cp->qe = p;
        }
    } 
    // else {
    //     assert(cp->qn == err);
    // }
    (*rys) = ey[0]; (*rye) = ey[1];

    return err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, %r12
movq %rcx, %rbp
movq %rdi, 0x68(%rsp)
movq 0x108(%rsp), %rax
movq 0x100(%rsp), %rcx
movq 0xa8(%rdx), %rdi
movq %rdi, 0x60(%rsp)
orq $-0x1, (%rax)
orq $-0x1, (%rcx)
leaq 0xa8(%rdx), %rax
movq %rax, 0x80(%rsp)
movl 0x4(%rsi), %r10d
movq %rsi, 0x70(%rsp)
movl 0x8(%rsi), %edi
movq (%rbp), %rcx
testq %rcx, %rcx
jle 0x3bf68
cmpq %r10, (%r8)
jl 0x3bf68
leaq -0x1(%rcx), %rsi
movq %rsi, (%rbp)
movq 0xb8(%rdx), %rax
movzwl -0x2(%rax,%rcx,2), %r9d
shrl $0xe, %r9d
movw %r9w, 0x12(%rsp)
cmpw $0x2, %r9w
je 0x3bf59
movzwl -0x2(%rax,%rcx,2), %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
subq %rcx, (%r8)
cmpw $0x3, %r9w
je 0x3bf0c
movq (%rbp), %rsi
movzwl (%rax,%rsi,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
subq %rax, (%r12)
jmp 0x3bf0c
movq %rdx, 0x78(%rsp)
movq %r10, 0x48(%rsp)
movq %rdi, 0x58(%rsp)
pushq $-0x1
popq %r14
movq $0x0, 0x38(%rsp)
movq %r14, %r15
movq %r14, 0x28(%rsp)
movq %r14, 0x20(%rsp)
movq $0x0, 0x40(%rsp)
movq %r8, 0x30(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, %rcx
movq %rcx, 0x50(%rsp)
movq (%rbp), %rsi
cmpq 0x60(%rsp), %rsi
jge 0x3c143
movq (%r8), %rbx
movq 0x58(%rsp), %r15
cmpq %r15, %rbx
jge 0x3c148
movq (%r12), %r13
movq 0x80(%rsp), %rdi
leaq 0x12(%rsp), %rdx
leaq 0x14(%rsp), %rcx
callq 0x53a7d
movq 0x30(%rsp), %r8
movl %eax, %eax
movq %rax, (%rbp)
movzwl 0x12(%rsp), %ecx
movl 0x14(%rsp), %eax
cmpw $0x2, %cx
je 0x3c010
movq (%r8), %r14
addq %rax, %r14
movq %r14, (%r8)
cmpw $0x3, %cx
je 0x3c025
movq (%r12), %r15
addq %rax, %r15
movq %r15, (%r12)
testw %cx, %cx
je 0x3bfac
movq (%r8), %r14
jmp 0x3c029
movq (%r12), %r15
cmpq 0x48(%rsp), %rbx
jl 0x3bfac
cmpq 0x58(%rsp), %r14
jg 0x3bfac
movq 0x20(%rsp), %rcx
testq %rcx, %rcx
cmovsq %rbx, %rcx
movq %rcx, 0x20(%rsp)
movq 0x28(%rsp), %rcx
testq %rcx, %rcx
cmovsq %r13, %rcx
movq %rcx, 0x28(%rsp)
addq %rax, 0x40(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, %rcx
cmpq $0x0, 0xf0(%rsp)
jle 0x3bfa7
subq 0xe0(%rsp), %r13
jl 0x3c3f0
cmpq 0xe8(%rsp), %r15
jg 0x3c3f0
movq $0x6, (%rsp)
movq 0xc0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq %rbx, %rdx
movq %r14, %rcx
movq 0xf0(%rsp), %r8
movq 0xf8(%rsp), %r9
movsd 0x1a74c9(%rip), %xmm0   # 0x1e3598
callq 0x28087
testb %al, %al
movq 0x30(%rsp), %r8
jne 0x3c135
movq %r15, %rcx
subq 0xe0(%rsp), %rcx
movq $0x6, (%rsp)
movq 0xd0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq %r13, %rdx
movq 0xf0(%rsp), %r8
movq 0xf8(%rsp), %r9
movsd 0x1a747d(%rip), %xmm0   # 0x1e3598
callq 0x28087
movq 0x30(%rsp), %r8
movq %r15, 0x18(%rsp)
movq %r14, %rcx
testb %al, %al
je 0x3bfa7
movl 0x14(%rsp), %eax
addq %rax, 0x38(%rsp)
jmp 0x3bf9f
movq 0x58(%rsp), %r15
cmpq 0x60(%rsp), %rsi
jge 0x3c2c0
movq (%r8), %rdx
cmpq %r15, %rdx
jg 0x3c2c0
movq (%r12), %r14
movq 0x78(%rsp), %rax
movq 0xb8(%rax), %rax
movzwl (%rax,%rsi,2), %ecx
shrl $0xe, %ecx
movw %cx, 0x12(%rsp)
movzwl (%rax,%rsi,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movl %eax, 0x14(%rsp)
cmpw $0x2, %cx
jne 0x3c2c0
leaq (%r14,%rax), %rcx
movq %rcx, (%r12)
incq (%rbp)
movq (%r8), %rbx
cmpq 0x48(%rsp), %rdx
setl %cl
cmpq %r15, %rbx
setg %sil
orb %cl, %sil
je 0x3c1bf
movq 0x50(%rsp), %rbx
jmp 0x3c2b2
movq (%r12), %rcx
movq %rcx, 0x18(%rsp)
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
cmovsq %rdx, %rsi
movq 0x28(%rsp), %rcx
testq %rcx, %rcx
cmovsq %r14, %rcx
addq %rax, 0x40(%rsp)
cmpq $0x0, 0xf0(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
jle 0x3c2b2
subq 0xe0(%rsp), %r14
jl 0x3c3b2
movq 0x18(%rsp), %rax
cmpq 0xe8(%rsp), %rax
jg 0x3c3b2
movq $0x6, (%rsp)
movsd 0x1a7369(%rip), %xmm0   # 0x1e3598
movq 0xc0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq %rbx, %rcx
movq 0xf0(%rsp), %r8
movq 0xf8(%rsp), %r9
callq 0x28087
testb %al, %al
jne 0x3c2a4
movq 0x18(%rsp), %rcx
subq 0xe0(%rsp), %rcx
movq $0x6, (%rsp)
movsd 0x1a7320(%rip), %xmm0   # 0x1e3598
movq 0xd0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq %r14, %rdx
movq 0xf0(%rsp), %r8
movq 0xf8(%rsp), %r9
callq 0x28087
testb %al, %al
je 0x3c2ad
movl 0x14(%rsp), %eax
addq %rax, 0x38(%rsp)
movq 0x30(%rsp), %r8
movq (%rbp), %rsi
movq %rbx, 0x50(%rsp)
jmp 0x3c148
cmpq $0x0, 0xf0(%rsp)
movq 0x70(%rsp), %rsi
movq 0x110(%rsp), %rdi
movq 0x48(%rsp), %r8
movq 0x40(%rsp), %r9
jle 0x3c37d
movl (%rsi), %eax
cmpq %rax, %r9
jne 0x3c3d1
movl %r8d, 0x4(%rsi)
movl %r15d, 0x8(%rsi)
movq 0x20(%rsp), %rax
movl %eax, 0x10(%rsi)
movq 0x50(%rsp), %rax
movl %eax, 0x14(%rsi)
movq 0x68(%rsp), %rax
movl %eax, 0xc(%rsi)
movl %r9d, (%rsi)
movq 0x38(%rsp), %rax
testq %rax, %rax
jle 0x3c33c
cvtsi2sd %rax, %xmm0
cvtsi2sd %r9, %xmm1
mulsd 0x1a7270(%rip), %xmm1   # 0x1e35a0
ucomisd %xmm1, %xmm0
jbe 0x3c33c
andb $-0x41, 0x1b(%rsi)
jmp 0x3c340
orb $0x40, 0x1b(%rsi)
cmpq %rdi, %r9
jg 0x3c37d
leaq (%r15,%r8), %rax
shrq %rax
movq %rax, %rdx
subq %rdi, %rdx
xorl %ecx, %ecx
testq %rdx, %rdx
cmovgq %rdx, %rcx
cmpq %r8, %rcx
jae 0x3c363
movl %ecx, 0x4(%rsi)
addq %rdi, %rax
movq 0xc8(%rsp), %rcx
cmpq %rcx, %rax
cmovgeq %rcx, %rax
cmpq %r15, %rax
jle 0x3c37d
movl %eax, 0x8(%rsi)
movq 0x100(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movq 0x108(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, (%rax)
movq %r9, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a99cc(%rip), %rdi    # 0x1e5d85
leaq 0x1a72e3(%rip), %rsi    # 0x1e36a3
leaq 0x1a99d9(%rip), %rcx    # 0x1e5da0
movl $0x4977, %edx           # imm = 0x4977
callq 0x2110
leaq 0x1a9a8b(%rip), %rdi    # 0x1e5e63
leaq 0x1a72c4(%rip), %rsi    # 0x1e36a3
leaq 0x1a99ba(%rip), %rcx    # 0x1e5da0
movl $0x4983, %edx           # imm = 0x4983
callq 0x2110
leaq 0x1a998e(%rip), %rdi    # 0x1e5d85
leaq 0x1a72a5(%rip), %rsi    # 0x1e36a3
leaq 0x1a999b(%rip), %rcx    # 0x1e5da0
movl $0x4962, %edx           # imm = 0x4962
callq 0x2110