// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %r9, %r13
movq %r8, %rbp
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, 0x58(%rsp)
movq 0xe8(%rsp), %rax
movq 0xe0(%rsp), %rcx
movq 0xa8(%rdx), %rdx
movq %rdx, 0x48(%rsp)
movq $-0x1, %rdx
movq %rdx, (%rax)
movq $-0x1, 0x20(%rsp)
movq %rdx, (%rcx)
movl 0x4(%rsi), %edi
movq %rsi, 0x60(%rsp)
movl 0x8(%rsi), %eax
movq %rax, 0x8(%rsp)
movq (%r14), %rcx
testq %rcx, %rcx
jle 0x6db97
cmpq %rdi, (%rbp)
jl 0x6db97
leaq -0x1(%rcx), %rax
movq %rax, (%r14)
movq 0xb8(%r15), %rax
movzwl -0x2(%rax,%rcx,2), %ecx
movl %ecx, %edx
shrl $0xe, %edx
cmpw $0x2, %dx
je 0x6db85
andl $0x3fff, %ecx           # imm = 0x3FFF
subq %rcx, (%rbp)
cmpw $0x3, %dx
je 0x6db49
movq (%r14), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
subq %rax, (%r13)
jmp 0x6db49
movq $0x0, 0x38(%rsp)
movq $-0x1, 0x10(%rsp)
movq $-0x1, 0x28(%rsp)
movq $-0x1, %rcx
movq $0x0, 0x40(%rsp)
movq %rdi, 0x30(%rsp)
movq %rcx, 0x18(%rsp)
movq 0x18(%rsp), %rcx
movq (%r14), %rsi
cmpq 0x48(%rsp), %rsi
jge 0x6dd89
movq (%rbp), %rdx
cmpq 0x8(%rsp), %rdx
jge 0x6dd89
movq 0xa8(%r15), %r9
movq 0xb8(%r15), %r10
movl %esi, %eax
movzwl (%r10,%rax,2), %eax
movl %eax, %edi
shrl $0xe, %edi
movl %eax, %r8d
andl $0x3fff, %r8d           # imm = 0x3FFF
incl %esi
cmpq %rsi, %r9
jbe 0x6dc34
movzwl (%r10,%rsi,2), %r11d
movl %r11d, %ebx
shrl $0xe, %ebx
cmpl %edi, %ebx
jne 0x6dc34
andl $0x3fff, %r11d          # imm = 0x3FFF
addl %r11d, %r8d
jmp 0x6dc12
movq (%r13), %r12
movl %esi, %esi
movq %rsi, (%r14)
movl %r8d, %ebx
cmpw $0x2, %di
je 0x6dc50
addq %rbx, (%rbp)
cmpw $0x3, %di
je 0x6dc54
addq %rbx, (%r13)
movzwl %ax, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x6dbd1
movq (%rbp), %rcx
cmpq 0x30(%rsp), %rdx
setl %al
cmpq 0x8(%rsp), %rcx
setg %sil
orb %al, %sil
jne 0x6dbcc
movq 0x20(%rsp), %rax
testq %rax, %rax
cmovsq %rdx, %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rax
testq %rax, %rax
cmovsq %r12, %rax
movq %rax, 0x28(%rsp)
movq (%r13), %rax
movq %rax, 0x10(%rsp)
addq %rbx, 0x40(%rsp)
movq %rcx, 0x18(%rsp)
cmpq $0x0, 0xd0(%rsp)
jle 0x6dbcc
subq 0xc0(%rsp), %r12
jl 0x6e03a
movq 0xc8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
jg 0x6e03a
movq $0x6, (%rsp)
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq %rcx, 0x50(%rsp)
movq 0xd0(%rsp), %r8
movq 0xd8(%rsp), %r9
movsd 0x1b782e(%rip), %xmm0   # 0x225548
callq 0x57976
movq 0x50(%rsp), %rcx
testb %al, %al
jne 0x6dd7f
movq 0x10(%rsp), %rcx
subq 0xc0(%rsp), %rcx
movq $0x6, (%rsp)
movq 0xb0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq %r12, %rdx
movq 0xd0(%rsp), %r8
movq 0xd8(%rsp), %r9
movsd 0x1b77e0(%rip), %xmm0   # 0x225548
callq 0x57976
movq 0x50(%rsp), %rcx
movq %rcx, 0x18(%rsp)
testb %al, %al
je 0x6dbcc
addq %rbx, 0x38(%rsp)
jmp 0x6dbc7
movq (%r14), %rax
cmpq 0x48(%rsp), %rax
jge 0x6defe
movq (%rbp), %rdx
cmpq 0x8(%rsp), %rdx
jg 0x6defe
movq 0xb8(%r15), %rsi
movzwl (%rsi,%rax,2), %eax
movl %eax, %esi
andl $0xc000, %esi           # imm = 0xC000
negw %si
jno 0x6defe
movq (%r13), %r12
andl $0x3fff, %eax           # imm = 0x3FFF
leaq (%r12,%rax), %rsi
movq %rsi, (%r13)
incq (%r14)
movq (%rbp), %rbx
cmpq 0x30(%rsp), %rdx
setl %sil
cmpq 0x8(%rsp), %rbx
setg %dil
orb %sil, %dil
je 0x6ddf9
movq %rcx, %rbx
jmp 0x6deeb
movq 0x20(%rsp), %rcx
testq %rcx, %rcx
cmovsq %rdx, %rcx
movq %rcx, 0x20(%rsp)
movq 0x28(%rsp), %rcx
testq %rcx, %rcx
cmovsq %r12, %rcx
movq %rcx, 0x28(%rsp)
movq (%r13), %rcx
movzwl %ax, %eax
addq %rax, 0x40(%rsp)
cmpq $0x0, 0xd0(%rsp)
movq %rcx, 0x10(%rsp)
jle 0x6deeb
movq %rax, 0x18(%rsp)
subq 0xc0(%rsp), %r12
jl 0x6dffc
cmpq 0xc8(%rsp), %rcx
jg 0x6dffc
movq $0x6, (%rsp)
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq %rbx, %rcx
movq 0xd0(%rsp), %r8
movq 0xd8(%rsp), %r9
movsd 0x1b76b9(%rip), %xmm0   # 0x225548
callq 0x57976
testb %al, %al
jne 0x6dee1
movq 0x10(%rsp), %rcx
subq 0xc0(%rsp), %rcx
movq $0x6, (%rsp)
movq 0xb0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq %r12, %rdx
movq 0xd0(%rsp), %r8
movq 0xd8(%rsp), %r9
movsd 0x1b7670(%rip), %xmm0   # 0x225548
callq 0x57976
testb %al, %al
je 0x6deeb
movq 0x18(%rsp), %rax
addq %rax, 0x38(%rsp)
movq (%r14), %rax
movq %rbx, %rcx
cmpq 0x48(%rsp), %rax
jl 0x6dd97
jmp 0x6df01
movq %rcx, %rbx
cmpq $0x0, 0xd0(%rsp)
movq 0x60(%rsp), %rsi
movq 0xf0(%rsp), %r8
movq 0x40(%rsp), %r9
jle 0x6dfca
movl (%rsi), %eax
cmpq %rax, %r9
jne 0x6e01b
movq 0x30(%rsp), %rax
movl %eax, 0x4(%rsi)
movq 0x8(%rsp), %rax
movl %eax, 0x8(%rsi)
movq 0x20(%rsp), %rax
movl %eax, 0x10(%rsi)
movl %ebx, 0x14(%rsi)
movq 0x58(%rsp), %rax
movl %eax, 0xc(%rsi)
movl %r9d, (%rsi)
movq 0x38(%rsp), %rax
testq %rax, %rax
jle 0x6df7e
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
cvtsi2sd %r9, %xmm1
mulsd 0x1b75de(%rip), %xmm1   # 0x225550
ucomisd %xmm1, %xmm0
jbe 0x6df7e
andb $-0x41, 0x1b(%rsi)
jmp 0x6df82
orb $0x40, 0x1b(%rsi)
cmpq %r8, %r9
jg 0x6dfca
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %rdi
addq %rdi, %rax
shrq %rax
movq %rax, %rdx
subq %r8, %rdx
xorl %ecx, %ecx
testq %rdx, %rdx
cmovgq %rdx, %rcx
cmpq %rdi, %rcx
jae 0x6dfae
movl %ecx, 0x4(%rsi)
addq %r8, %rax
movq 0xa8(%rsp), %rcx
cmpq %rcx, %rax
cmovgeq %rcx, %rax
cmpq 0x8(%rsp), %rax
jle 0x6dfca
movl %eax, 0x8(%rsi)
movq 0xe0(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movq 0xe8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movq %r9, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b9d22(%rip), %rdi    # 0x227d25
leaq 0x1b7639(%rip), %rsi    # 0x225643
leaq 0x1b9d2f(%rip), %rcx    # 0x227d40
movl $0x4977, %edx           # imm = 0x4977
callq 0x2110
leaq 0x1b9de1(%rip), %rdi    # 0x227e03
leaq 0x1b761a(%rip), %rsi    # 0x225643
leaq 0x1b9d10(%rip), %rcx    # 0x227d40
movl $0x4983, %edx           # imm = 0x4983
callq 0x2110
leaq 0x1b9ce4(%rip), %rdi    # 0x227d25
leaq 0x1b75fb(%rip), %rsi    # 0x225643
leaq 0x1b9cf1(%rip), %rcx    # 0x227d40
movl $0x4962, %edx           # imm = 0x4962
callq 0x2110