// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %r9, %r13
movq %r8, %rbp
movq %rcx, %r14
movq %rdi, 0x68(%rsp)
movq 0xf8(%rsp), %rax
movq 0xf0(%rsp), %rcx
movq %rdx, 0x50(%rsp)
movq 0xa8(%rdx), %rdx
movq %rdx, 0x60(%rsp)
movq $-0x1, %rdx
movq %rdx, (%rax)
movq $-0x1, 0x20(%rsp)
movq %rdx, (%rcx)
movl 0x4(%rsi), %edi
movq %rsi, 0x70(%rsp)
movl 0x8(%rsi), %eax
movq %rax, 0x10(%rsp)
movq (%r14), %rcx
testq %rcx, %rcx
jle 0x6d655
cmpq %rdi, (%rbp)
jl 0x6d655
leaq -0x1(%rcx), %rax
movq %rax, (%r14)
movq 0x50(%rsp), %rax
movq 0xb8(%rax), %rax
movzwl -0x2(%rax,%rcx,2), %ecx
movl %ecx, %edx
shrl $0xe, %edx
cmpw $0x2, %dx
je 0x6d649
movzwl %cx, %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
subq %rcx, (%rbp)
cmpw $0x3, %dx
je 0x6d5fe
movq (%r14), %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
subq %rcx, (%r13)
jmp 0x6d5fe
movq %rdi, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movq $-0x1, %rbx
movq $-0x1, 0x28(%rsp)
movq $-0x1, %rcx
movq $0x0, 0x48(%rsp)
movq %rbx, 0x18(%rsp)
movq %rcx, 0x40(%rsp)
movq 0x40(%rsp), %rcx
movq (%r14), %rax
cmpq 0x60(%rsp), %rax
jge 0x6d9bd
movq (%rbp), %rdx
cmpq 0x10(%rsp), %rdx
jge 0x6d9ae
movq 0x50(%rsp), %rsi
movq 0xa8(%rsi), %r9
movq 0xb8(%rsi), %r10
movl %eax, %esi
movzwl (%r10,%rsi,2), %esi
movl %esi, %edi
shrl $0xe, %edi
movl %esi, %r8d
andl $0x3fff, %r8d           # imm = 0x3FFF
incl %eax
cmpq %rax, %r9
jbe 0x6d6fc
movzwl (%r10,%rax,2), %r11d
movl %r11d, %ebx
shrl $0xe, %ebx
cmpl %edi, %ebx
jne 0x6d6fa
andl $0x3fff, %r11d          # imm = 0x3FFF
addl %r11d, %r8d
jmp 0x6d6d8
movl %eax, %eax
movq (%r13), %r12
movq %rax, (%r14)
movl %r8d, %r15d
cmpw $0x2, %di
je 0x6d731
addq %r15, (%rbp)
movq (%r13), %rbx
cmpw $0x3, %di
je 0x6d73c
addq %r15, %rbx
movq %rbx, (%r13)
movzwl %si, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x6d692
jmp 0x6d73c
movq (%r13), %rbx
addq %r15, %rbx
movq %rbx, (%r13)
movq (%rbp), %rcx
cmpq 0x30(%rsp), %rdx
setl %al
cmpq 0x10(%rsp), %rcx
setg %sil
orb %al, %sil
jne 0x6d68d
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)
addq %r15, 0x48(%rsp)
movq %rbx, 0x18(%rsp)
movq %rcx, 0x40(%rsp)
cmpq $0x0, 0xe0(%rsp)
jle 0x6d68d
subq 0xd0(%rsp), %r12
jl 0x6daf9
cmpq 0xd8(%rsp), %rbx
jg 0x6daf9
movq $0x6, (%rsp)
movq 0xb0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq %rcx, 0x58(%rsp)
movq 0xe0(%rsp), %r8
movq 0xe8(%rsp), %r9
movsd 0x1c3e5d(%rip), %xmm0   # 0x231648
callq 0x56de5
movq 0x58(%rsp), %rcx
testb %al, %al
jne 0x6d853
movq %rbx, %rcx
subq 0xd0(%rsp), %rcx
movq $0x6, (%rsp)
movq 0xc0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq %r12, %rdx
movq 0xe0(%rsp), %r8
movq 0xe8(%rsp), %r9
movsd 0x1c3e11(%rip), %xmm0   # 0x231648
callq 0x56de5
movq 0x58(%rsp), %rcx
movq %rbx, 0x18(%rsp)
movq %rcx, 0x40(%rsp)
testb %al, %al
je 0x6d68d
addq %r15, 0x38(%rsp)
jmp 0x6d683
movq 0x50(%rsp), %rsi
movq 0xb8(%rsi), %rsi
movzwl (%rsi,%rax,2), %r15d
movl %r15d, %eax
andl $0xc000, %eax           # imm = 0xC000
negw %ax
jno 0x6d9bd
movq (%r13), %r12
andl $0x3fff, %r15d          # imm = 0x3FFF
leaq (%r12,%r15), %rax
movq %rax, (%r13)
incq (%r14)
movq (%rbp), %rbx
cmpq 0x30(%rsp), %rdx
setl %al
cmpq 0x10(%rsp), %rbx
setg %sil
orb %al, %sil
je 0x6d8b7
movq %rcx, %rbx
jmp 0x6d9a1
movq (%r13), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
testq %rax, %rax
cmovsq %rdx, %rax
movq 0x28(%rsp), %rcx
testq %rcx, %rcx
cmovsq %r12, %rcx
movq %rcx, 0x28(%rsp)
addq %r15, 0x48(%rsp)
cmpq $0x0, 0xe0(%rsp)
movq %rax, 0x20(%rsp)
jle 0x6d9a1
subq 0xd0(%rsp), %r12
jl 0x6dabb
movq 0xd8(%rsp), %rax
cmpq %rax, 0x18(%rsp)
jg 0x6dabb
movq $0x6, (%rsp)
movq 0xb0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq %rbx, %rcx
movq 0xe0(%rsp), %r8
movq 0xe8(%rsp), %r9
movsd 0x1c3cfe(%rip), %xmm0   # 0x231648
callq 0x56de5
testb %al, %al
jne 0x6d99c
movq 0x18(%rsp), %rcx
subq 0xd0(%rsp), %rcx
movq $0x6, (%rsp)
movq 0xc0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq %r12, %rdx
movq 0xe0(%rsp), %r8
movq 0xe8(%rsp), %r9
movsd 0x1c3cb5(%rip), %xmm0   # 0x231648
callq 0x56de5
testb %al, %al
je 0x6d9a1
addq %r15, 0x38(%rsp)
movq (%r14), %rax
movq %rbx, %rcx
cmpq 0x60(%rsp), %rax
jge 0x6d9c0
movq (%rbp), %rdx
cmpq 0x10(%rsp), %rdx
jle 0x6d85d
movq %rcx, %rbx
cmpq $0x0, 0xe0(%rsp)
movq 0x70(%rsp), %rsi
movq 0x100(%rsp), %r8
movq 0x48(%rsp), %r9
jle 0x6da89
movl (%rsi), %eax
cmpq %rax, %r9
jne 0x6dada
movq 0x30(%rsp), %rax
movl %eax, 0x4(%rsi)
movq 0x10(%rsp), %rax
movl %eax, 0x8(%rsi)
movq 0x20(%rsp), %rax
movl %eax, 0x10(%rsi)
movl %ebx, 0x14(%rsi)
movq 0x68(%rsp), %rax
movl %eax, 0xc(%rsi)
movl %r9d, (%rsi)
movq 0x38(%rsp), %rax
testq %rax, %rax
jle 0x6da3d
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
cvtsi2sd %r9, %xmm1
mulsd 0x1c3c1f(%rip), %xmm1   # 0x231650
ucomisd %xmm1, %xmm0
jbe 0x6da3d
andb $-0x41, 0x1b(%rsi)
jmp 0x6da41
orb $0x40, 0x1b(%rsi)
cmpq %r8, %r9
jg 0x6da89
movq 0x10(%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 0x6da6d
movl %ecx, 0x4(%rsi)
addq %r8, %rax
movq 0xb8(%rsp), %rcx
cmpq %rcx, %rax
cmovgeq %rcx, %rax
cmpq 0x10(%rsp), %rax
jle 0x6da89
movl %eax, 0x8(%rsi)
movq 0xf0(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movq 0xf8(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, (%rax)
movq %r9, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c6373(%rip), %rdi    # 0x233e35
leaq 0x1c3c8a(%rip), %rsi    # 0x231753
leaq 0x1c6380(%rip), %rcx    # 0x233e50
movl $0x4977, %edx           # imm = 0x4977
callq 0x2110
leaq 0x1c6432(%rip), %rdi    # 0x233f13
leaq 0x1c3c6b(%rip), %rsi    # 0x231753
leaq 0x1c6361(%rip), %rcx    # 0x233e50
movl $0x4983, %edx           # imm = 0x4983
callq 0x2110
leaq 0x1c6335(%rip), %rdi    # 0x233e35
leaq 0x1c3c4c(%rip), %rsi    # 0x231753
leaq 0x1c6342(%rip), %rcx    # 0x233e50
movl $0x4962, %edx           # imm = 0x4962
callq 0x2110