// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq 0x60(%rbp), %rax
movq 0x58(%rbp), %rax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rax, -0x78(%rbp)
movq $-0x1, -0x48(%rbp)
movq $-0x1, -0x50(%rbp)
movq $-0x1, -0x58(%rbp)
movq $-0x1, -0x60(%rbp)
movq 0x58(%rbp), %rax
movq $-0x1, (%rax)
movq 0x50(%rbp), %rax
movq $-0x1, (%rax)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, (%rcx)
movb %al, -0xb1(%rbp)
jle 0x6f3f0
movq -0x28(%rbp), %rax
movq (%rax), %rax
cmpq -0x68(%rbp), %rax
setge %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x6f3ff
jmp 0x6f4a0
movq -0x20(%rbp), %rax
movq (%rax), %rcx
addq $-0x1, %rcx
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
movw %ax, -0xaa(%rbp)
movzwl -0xaa(%rbp), %eax
cmpl $0x2, %eax
je 0x6f464
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movslq %eax, %rdx
movq -0x28(%rbp), %rax
movq (%rax), %rcx
subq %rdx, %rcx
movq %rcx, (%rax)
movzwl -0xaa(%rbp), %eax
cmpl $0x3, %eax
je 0x6f49b
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movslq %eax, %rdx
movq -0x30(%rbp), %rax
movq (%rax), %rcx
subq %rdx, %rcx
movq %rcx, (%rax)
jmp 0x6f3ca
jmp 0x6f4a2
movq -0x20(%rbp), %rax
movq (%rax), %rcx
xorl %eax, %eax
cmpq -0x78(%rbp), %rcx
movb %al, -0xb2(%rbp)
jge 0x6f4cb
movq -0x28(%rbp), %rax
movq (%rax), %rax
cmpq -0x70(%rbp), %rax
setl %al
movb %al, -0xb2(%rbp)
movb -0xb2(%rbp), %al
testb $0x1, %al
jne 0x6f4da
jmp 0x6f6e9
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rdi
addq $0xa8, %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl %eax, %esi
leaq -0xaa(%rbp), %rdx
leaq -0xb0(%rbp), %rcx
callq 0xa1ee0
movl %eax, %eax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movzwl -0xaa(%rbp), %eax
cmpl $0x2, %eax
je 0x6f546
movl -0xb0(%rbp), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movzwl -0xaa(%rbp), %eax
cmpl $0x3, %eax
je 0x6f564
movl -0xb0(%rbp), %eax
movl %eax, %ecx
movq -0x30(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movzwl -0xaa(%rbp), %eax
cmpl $0x0, %eax
jne 0x6f591
jmp 0x6f4a2
movq -0x90(%rbp), %rax
cmpq -0x68(%rbp), %rax
jl 0x6f6e4
movq -0x88(%rbp), %rax
cmpq -0x70(%rbp), %rax
jg 0x6f6e4
cmpq $0x0, -0x50(%rbp)
jge 0x6f5c5
movq -0x90(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x60(%rbp)
jge 0x6f5d7
movq -0xa0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0xb0(%rbp), %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, 0x40(%rbp)
jle 0x6f6e2
movq -0xa0(%rbp), %rcx
xorl %eax, %eax
cmpq 0x30(%rbp), %rcx
movb %al, -0xb3(%rbp)
jl 0x6f62f
movq -0x98(%rbp), %rax
cmpq 0x38(%rbp), %rax
setle %al
movb %al, -0xb3(%rbp)
movb -0xb3(%rbp), %al
testb $0x1, %al
jne 0x6f63b
jmp 0x6f63d
jmp 0x6f65c
leaq 0x3153b9(%rip), %rdi    # 0x3849fd
leaq 0x312cd0(%rip), %rsi    # 0x38231b
movl $0x4962, %edx           # imm = 0x4962
leaq 0x3153c1(%rip), %rcx    # 0x384a18
callq 0x2130
movq 0x10(%rbp), %rdi
movq 0x18(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x88(%rbp), %rcx
movq 0x40(%rbp), %r8
movq 0x48(%rbp), %r9
movsd 0x312c36(%rip), %xmm0   # 0x3822b8
movq $0x6, (%rsp)
callq 0x4a180
cmpb $0x0, %al
jne 0x6f6d2
movq 0x20(%rbp), %rdi
movq 0x28(%rbp), %rsi
movq -0xa0(%rbp), %rdx
subq 0x30(%rbp), %rdx
movq -0x98(%rbp), %rcx
subq 0x30(%rbp), %rcx
movq 0x40(%rbp), %r8
movq 0x48(%rbp), %r9
movsd 0x312bf7(%rip), %xmm0   # 0x3822b8
movq $0x6, (%rsp)
callq 0x4a180
cmpb $0x0, %al
je 0x6f6e0
movl -0xb0(%rbp), %eax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x6f6e2
jmp 0x6f6e4
jmp 0x6f4a2
jmp 0x6f6eb
movq -0x20(%rbp), %rax
movq (%rax), %rcx
xorl %eax, %eax
cmpq -0x78(%rbp), %rcx
movb %al, -0xb4(%rbp)
jge 0x6f714
movq -0x28(%rbp), %rax
movq (%rax), %rax
cmpq -0x70(%rbp), %rax
setle %al
movb %al, -0xb4(%rbp)
movb -0xb4(%rbp), %al
testb $0x1, %al
jne 0x6f723
jmp 0x6f93b
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
movw %ax, -0xaa(%rbp)
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movl %eax, -0xb0(%rbp)
movzwl -0xaa(%rbp), %eax
cmpl $0x2, %eax
je 0x6f791
jmp 0x6f93b
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movslq %eax, %rcx
movq -0x30(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x68(%rbp), %rax
jl 0x6f936
movq -0x88(%rbp), %rax
cmpq -0x70(%rbp), %rax
jg 0x6f936
cmpq $0x0, -0x50(%rbp)
jge 0x6f817
movq -0x90(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x60(%rbp)
jge 0x6f829
movq -0xa0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0xb0(%rbp), %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, 0x40(%rbp)
jle 0x6f934
movq -0xa0(%rbp), %rcx
xorl %eax, %eax
cmpq 0x30(%rbp), %rcx
movb %al, -0xb5(%rbp)
jl 0x6f881
movq -0x98(%rbp), %rax
cmpq 0x38(%rbp), %rax
setle %al
movb %al, -0xb5(%rbp)
movb -0xb5(%rbp), %al
testb $0x1, %al
jne 0x6f88d
jmp 0x6f88f
jmp 0x6f8ae
leaq 0x315167(%rip), %rdi    # 0x3849fd
leaq 0x312a7e(%rip), %rsi    # 0x38231b
movl $0x4977, %edx           # imm = 0x4977
leaq 0x31516f(%rip), %rcx    # 0x384a18
callq 0x2130
movq 0x10(%rbp), %rdi
movq 0x18(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x88(%rbp), %rcx
movq 0x40(%rbp), %r8
movq 0x48(%rbp), %r9
movsd 0x3129e4(%rip), %xmm0   # 0x3822b8
movq $0x6, (%rsp)
callq 0x4a180
cmpb $0x0, %al
jne 0x6f924
movq 0x20(%rbp), %rdi
movq 0x28(%rbp), %rsi
movq -0xa0(%rbp), %rdx
subq 0x30(%rbp), %rdx
movq -0x98(%rbp), %rcx
subq 0x30(%rbp), %rcx
movq 0x40(%rbp), %r8
movq 0x48(%rbp), %r9
movsd 0x3129a5(%rip), %xmm0   # 0x3822b8
movq $0x6, (%rsp)
callq 0x4a180
cmpb $0x0, %al
je 0x6f932
movl -0xb0(%rbp), %eax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x6f934
jmp 0x6f936
jmp 0x6f6eb
cmpq $0x0, 0x40(%rbp)
jle 0x6faee
movq -0x10(%rbp), %rax
movl (%rax), %eax
cmpq -0x38(%rbp), %rax
jne 0x6f954
jmp 0x6f973
leaq 0x315180(%rip), %rdi    # 0x384adb
leaq 0x3129b9(%rip), %rsi    # 0x38231b
movl $0x4983, %edx           # imm = 0x4983
leaq 0x3150aa(%rip), %rcx    # 0x384a18
callq 0x2130
movq -0x68(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x70(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x8(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x38(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x40(%rbp)
jle 0x6f9fa
cvtsi2sdq -0x40(%rbp), %xmm0
cvtsi2sdq -0x38(%rbp), %xmm1
movsd 0x3128e5(%rip), %xmm2   # 0x3822c0
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6f9fa
movq -0x10(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0xbfffffff, %ecx       # imm = 0xBFFFFFFF
orl $0x0, %ecx
movl %ecx, 0x18(%rax)
jmp 0x6fa10
movq -0x10(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0xbfffffff, %ecx       # imm = 0xBFFFFFFF
orl $0x40000000, %ecx       # imm = 0x40000000
movl %ecx, 0x18(%rax)
movq -0x38(%rbp), %rax
cmpq 0x60(%rbp), %rax
jg 0x6faec
movq -0x68(%rbp), %rax
addq -0x70(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, -0xa8(%rbp)
movq 0x60(%rbp), %rcx
movq -0xa8(%rbp), %rax
subq %rcx, %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jge 0x6fa61
movq $0x0, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jge 0x6fa84
movq -0xa8(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x68(%rbp), %rax
addq -0x70(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, -0xa8(%rbp)
movq 0x60(%rbp), %rax
addq -0xa8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
cmpq 0x18(%rbp), %rax
jle 0x6fac7
movq 0x18(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jle 0x6faea
movq -0xa8(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0x6faec
jmp 0x6faee
movq -0x60(%rbp), %rcx
movq 0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x58(%rbp), %rcx
movq 0x58(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)