// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: extract_sub_cigar_sv(overlap_region*, long, long, All_reads*, char*, long, UC_Read*, kv_ul_ov_t*, unsigned long, long, long)
int64_t extract_sub_cigar_sv(overlap_region *z, int64_t zid, int64_t wk, All_reads *rref, char *qstr, int64_t ql, UC_Read* tu, kv_ul_ov_t *rr, uint64_t min_err, int64_t hpc_len, int64_t hpc_rr)
{
    int64_t xk = z->w_list.a[wk].x_start, yk = z->w_list.a[wk].y_start; ul_ov_t *cp, *ra; bit_extz_t ez;
    int64_t ck = 0, ex[2], ey[2], el0, tl = Get_READ_LENGTH((*rref), z->y_id), ox[2], oy[2], os, oe, ff, rn, rn1, k; uint32_t cl, rr_n0 = rr->n, is_srt = 1;
    set_bit_extz_t(ez, (*z), wk);
    if(!ez.cigar.n) return -1;
    int64_t cn = ez.cigar.n, wx[2], wy[2]; uint16_t op, op0;
    ex[0] = ex[1] = ey[0] = ey[1] = -1; 
    
    while (ck < cn) {//[s, e)
        wx[0] = xk; wy[0] = yk; el0 = 0;
        op = (ez.cigar.a[ck]>>14); 
        cl = (ez.cigar.a[ck]&(0x3fff));
        if(op!=2) xk += cl; if(op!=3) yk += cl; 
        op0 = op; el0 += cl;
        for (ck++; ck < cn; ck++) {
            op = (ez.cigar.a[ck]>>14); 
            if((!!op) != (!!op0)) break;
            cl = (ez.cigar.a[ck]&(0x3fff));
            if(op!=2) xk += cl; if(op!=3) yk += cl;
            el0 += cl;
        }
        wx[1] = xk; wy[1] = yk; 
        if(op0 == 0) continue;
        ff = 1;


        ox[0] = ((wx[0]>=el0)?(wx[0]-el0):(0)); ox[1] = ((wx[1]+el0<=ql)?(wx[1]+el0):(ql));
        oy[0] = ((wy[0]>=el0)?(wy[0]-el0):(0)); oy[1] = ((wy[1]+el0<=tl)?(wy[1]+el0):(tl));

        if(ex[0] < 0 || ex[1] < 0 || ey[0] < 0 || ey[1] < 0) ff = 0;

        if(ff) {
            os = MAX(ox[0], ex[0]); oe = MIN(ox[1], ex[1]);
            if(oe <= os) ff = 0;

            os = MAX(oy[0], ey[0]); oe = MIN(oy[1], ey[1]);
            if(oe <= os) ff = 0;
        }

        if(ff) {///extend
            ex[0] = MIN(ox[0], ex[0]); ex[1] = MAX(ox[1], ex[1]);
            ey[0] = MIN(oy[0], ey[0]); ey[1] = MAX(oy[1], ey[1]);
        } else {
            if(ex[0] >= 0 && ex[1] >= 0 && ey[0] >= 0 && ey[1] >= 0) {
                kv_pushp(ul_ov_t, *rr, &cp);
                cp->qs = ex[0]; cp->qe = ex[1];
                cp->ts = ey[0]; cp->te = ey[1];
                cp->sec = 0; cp->el = 0; cp->rev = 0;
                if((rr->n > rr_n0 + 1) && (cp->qs < rr->a[rr->n-2].qs)) is_srt = 0;
            } 
            ex[0] = ox[0]; ex[1] = ox[1];
            ey[0] = oy[0]; ey[1] = oy[1];
        }
    }

    if(ex[0] >= 0 && ex[1] >= 0 && ey[0] >= 0 && ey[1] >= 0) {
        kv_pushp(ul_ov_t, *rr, &cp);
        cp->qs = ex[0]; cp->qe = ex[1];
        cp->ts = ey[0]; cp->te = ey[1];
        cp->sec = 0; cp->el = 0; cp->rev = 0;
        if((rr->n > rr_n0 + 1) && (cp->qs < rr->a[rr->n-2].qs)) is_srt = 0;
    } 

    if(rr->n <= rr_n0) return 0;
    if(!is_srt) radix_sort_ul_ov_srt_qs1(rr->a + rr_n0, rr->a + rr->n);

    ra = rr->a + rr_n0; rn = rr->n - rr_n0;
    for (k = ck = 0; k < rn; k++) {
        if(ck > 0 && ra[k].qs < ra[ck - 1].qe) {
            if(ra[k].qe > ra[ck - 1].qe) ra[ck - 1].qe = ra[k].qe;
            if(ra[k].ts < ra[ck - 1].ts) ra[ck - 1].ts = ra[k].ts;
            if(ra[k].te > ra[ck - 1].te) ra[ck - 1].te = ra[k].te;
        } else {
            if((ck > 0) && (ra[ck - 1].qe - ra[ck - 1].qs < min_err) && (ra[ck - 1].te - ra[ck - 1].ts < min_err)) {
                ck--;
            }
            ra[ck++] = ra[k];
        }
    }
    if((ck > 0) && (ra[ck - 1].qe - ra[ck - 1].qs < min_err) && (ra[ck - 1].te - ra[ck - 1].ts < min_err)) ck--;
    if(ck == 0) {
        rr->n = rr_n0;
        return 0;
    }
    rn = ck; ck = 0; xk = z->w_list.a[wk].x_start; yk = z->w_list.a[wk].y_start;
    for (k = ck = rn1 = 0; k < rn; k++) {
        cp = &(ra[k]); 
        cp->qn = iter_sub_cigar_sv(zid, cp, &ez, &ck, &xk, &yk, NULL, -1, NULL, -1, -1, -1, -1, -1, &(ey[0]), &(ey[1]), -1);
        if(cp->qn < min_err) continue;
        assert(ey[0] >= 0 && ey[1] >= 0 && ey[1] >= ey[0]);
        ey[0] -= hpc_len; if(ey[0] < 0) ey[0] = 0;
        ey[1] += hpc_len; if(ey[1] > tl) ey[1] = tl;

        UC_Read_resize(*tu, (ey[1] - ey[0])); ///ystr = tu->seq; 
        recover_UC_Read_sub_region(tu->seq, ey[0], (ey[1] - ey[0]), z->y_pos_strand, rref, z->y_id);
        iter_sub_cigar_sv(zid, cp, &ez, &ck, &xk, &yk, qstr, ql, tu->seq, tl, ey[0], ey[1], hpc_len, hpc_rr, &(ey[0]), &(ey[1]), (min_err<<1));

        ra[rn1++] = *cp;
        // fprintf(stderr, "-[M::%s] rid::%u\t%.*s\tq0::[%u,%u)\tql::%ld\tq1::[%u,%u)\t\terr::%u\tel::%u\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, cp->qn, cp->el);
    }

    // fprintf(stderr, "\n");
    
    rr->n = rr_n0 + rn1;
    if(rn1 > 0) return 1;
    else return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %r9, 0x88(%rsp)
movq %r8, 0xb8(%rsp)
movq 0x1d8(%rsp), %r8
movq 0x58(%rdi), %rax
shlq $0x5, %rdx
movslq (%rax,%rdx), %r15
movslq 0x8(%rax,%rdx), %r14
movq %rcx, 0xb0(%rsp)
movq 0x18(%rcx), %rcx
movl 0x10(%rdi), %r9d
movq (%rcx,%r9,8), %rcx
movq %rcx, 0x60(%rsp)
movq 0x8(%r8), %rcx
movq %rcx, 0x78(%rsp)
movl 0x18(%rax,%rdx), %r12d
addq %r12, %r12
movq %rdi, 0x80(%rsp)
addq 0x70(%rdi), %r12
movq %r12, 0x178(%rsp)
movl 0x1c(%rax,%rdx), %r13d
movq %r13, 0x170(%rsp)
movq %r13, 0x168(%rsp)
movl %r15d, 0xdc(%rsp)
movl 0x4(%rax,%rdx), %ecx
movl %ecx, 0xe0(%rsp)
movl %r14d, 0xd0(%rsp)
movl 0xc(%rax,%rdx), %ecx
movl %ecx, 0xd4(%rsp)
movq %rdx, 0xa8(%rsp)
movswl 0x14(%rax,%rdx), %eax
movl %eax, 0xec(%rsp)
movl %eax, 0xe8(%rsp)
testq %r13, %r13
je 0x3c94f
movq %rsi, 0x58(%rsp)
movq 0x78(%rsp), %rcx
leal 0x1(%rcx), %eax
movq %rax, 0x70(%rsp)
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
pushq $-0x1
popq %rbp
movq %rbp, 0x20(%rsp)
xorl %ebx, %ebx
movq %rbp, 0x10(%rsp)
movq %rbp, %rsi
movq %rsi, 0x18(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r15, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %rbp, 0x50(%rsp)
cmpq %r13, %rbx
jge 0x3c7aa
movq %r14, %r11
movzwl (%r12,%rbx,2), %edx
movl %edx, %esi
shrl $0xe, %esi
movl %edx, %ebp
andl $0x3fff, %ebp           # imm = 0x3FFF
movq %r15, %rax
cmpw $0x2, %si
je 0x3c568
leaq (%rax,%rbp), %r15
movq %r11, %r14
cmpw $0x3, %si
je 0x3c56c
leaq (%r11,%rbp), %r14
movq %rbx, %rsi
incq %rsi
cmpq %r13, %rsi
movq %r13, %rbx
cmovgq %rsi, %rbx
cmpq %r13, %rsi
jge 0x3c5cb
cmpw $0x4000, %dx            # imm = 0x4000
setb %r8b
movzwl (%r12,%rsi,2), %edi
cmpq $0x4000, %rdi           # imm = 0x4000
setae %r9b
cmpb %r9b, %r8b
je 0x3c5c8
movl %edi, %r8d
shrl $0xe, %r8d
andl $0x3fff, %edi           # imm = 0x3FFF
cmpw $0x2, %r8w
je 0x3c5bd
addq %rdi, %r15
cmpw $0x3, %r8w
je 0x3c5c0
addq %rdi, %r14
addq %rdi, %rbp
incq %rsi
jmp 0x3c57c
movq %rsi, %rbx
cmpw $0x4000, %dx            # imm = 0x4000
jb 0x3c534
subq %rbp, %rax
movl $0x0, %edx
cmovlq %rdx, %rax
movq %rax, 0x10(%rsp)
leaq (%r15,%rbp), %rsi
movq 0x88(%rsp), %rax
cmpq %rax, %rsi
cmovgeq %rax, %rsi
subq %rbp, %r11
cmovlq %rdx, %r11
movq %r11, 0x20(%rsp)
addq %r14, %rbp
movq 0x60(%rsp), %rax
cmpq %rax, %rbp
cmovgeq %rax, %rbp
movq 0x28(%rsp), %r8
testq %r8, %r8
movq 0x50(%rsp), %r10
movq 0x48(%rsp), %r11
js 0x3c6c5
cmpq $0x0, 0x18(%rsp)
js 0x3c6c5
testq %r11, %r11
js 0x3c6c5
testq %r10, %r10
js 0x3c6c5
movq 0x10(%rsp), %rdx
cmpq %r8, %rdx
movq %r8, %rax
cmovgq %rdx, %rax
movq 0x18(%rsp), %rdx
cmpq %rdx, %rsi
cmovlq %rsi, %rdx
movq 0x20(%rsp), %rdi
cmpq %r11, %rdi
movq %rsi, %r9
movq %r11, %rsi
cmovgq %rdi, %rsi
cmpq %r10, %rbp
movq %r10, %rdi
cmovlq %rbp, %rdi
cmpq %rsi, %rdi
movq %r9, %rsi
jle 0x3c6c5
cmpq %rax, %rdx
jle 0x3c6c5
movq 0x10(%rsp), %rax
cmpq %r8, %rax
cmovgeq %r8, %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
cmpq %rax, %rsi
cmovleq %rax, %rsi
movq 0x20(%rsp), %rax
cmpq %r11, %rax
cmovgeq %r11, %rax
movq %rax, 0x20(%rsp)
cmpq %r10, %rbp
cmovleq %r10, %rbp
jmp 0x3c511
testq %r8, %r8
js 0x3c511
cmpq $0x0, 0x18(%rsp)
js 0x3c511
testq %r11, %r11
js 0x3c511
testq %r10, %r10
js 0x3c511
movq 0x1d8(%rsp), %r9
movq (%r9), %rax
cmpq 0x10(%r9), %rcx
jne 0x3c749
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r9)
movq %rsi, 0xa0(%rsp)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x1d8(%rsp), %r9
movq 0xa0(%rsp), %rsi
movq 0x48(%rsp), %r11
movq 0x50(%rsp), %r10
movq 0x28(%rsp), %r8
movq %rax, (%r9)
movq 0x8(%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r9)
imulq $0x1c, %rcx, %rcx
movl %r8d, 0x4(%rax,%rcx)
movq 0x18(%rsp), %rdx
movl %edx, 0x8(%rax,%rcx)
movl %r11d, 0x10(%rax,%rcx)
movl %r10d, 0x14(%rax,%rcx)
andl $0x0, 0x18(%rax,%rcx)
movq 0x8(%r9), %rcx
cmpq 0x70(%rsp), %rcx
jbe 0x3c511
movq 0x1d8(%rsp), %rax
movq (%rax), %rax
imulq $0x1c, %rcx, %rdx
cmpl %r8d, -0x34(%rax,%rdx)
movl 0xc(%rsp), %eax
movl $0x0, %edx
cmoval %edx, %eax
movl %eax, 0xc(%rsp)
jmp 0x3c511
movq 0x10(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x48(%rsp), %r8
movq %r8, 0x30(%rsp)
movq 0x50(%rsp), %rsi
movq %rsi, 0x38(%rsp)
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
movq 0x1e0(%rsp), %r14
js 0x3cbd7
cmpq $0x0, 0x18(%rsp)
movq 0x1d8(%rsp), %rbx
js 0x3c88b
testq %r8, %r8
js 0x3c88b
testq %rsi, %rsi
js 0x3c88b
movq (%rbx), %rax
cmpq 0x10(%rbx), %rcx
jne 0x3c841
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x48(%rsp), %r8
movq 0x50(%rsp), %rsi
movq 0x28(%rsp), %rdi
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movl %edi, 0x4(%rax,%rcx)
movq 0x18(%rsp), %rdx
movl %edx, 0x8(%rax,%rcx)
movl %r8d, 0x10(%rax,%rcx)
movl %esi, 0x14(%rax,%rcx)
andl $0x0, 0x18(%rax,%rcx)
movq 0x8(%rbx), %rcx
cmpq 0x70(%rsp), %rcx
jbe 0x3c88b
movq (%rbx), %rax
imulq $0x1c, %rcx, %rdx
xorl %esi, %esi
cmpl %edi, -0x34(%rax,%rdx)
movl 0xc(%rsp), %eax
cmoval %esi, %eax
movl %eax, 0xc(%rsp)
movl 0x78(%rsp), %r12d
cmpq %r12, %rcx
jbe 0x3cbc3
movq (%rbx), %rax
cmpl $0x0, 0xc(%rsp)
jne 0x3c8bd
imulq $0x1c, %r12, %rdi
addq %rax, %rdi
imulq $0x1c, %rcx, %rsi
addq %rax, %rsi
callq 0x1a7a0
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq 0x58(%rsp), %rbx
imulq $0x1c, %r12, %rdx
leaq (%rax,%rdx), %rbp
subq %r12, %rcx
xorl %r15d, %r15d
testq %rcx, %rcx
cmovleq %r15, %rcx
addq %rdx, %rax
addq $0x14, %rax
subq $0x1, %rcx
jb 0x3c957
testq %r15, %r15
jle 0x3c930
imulq $0x1c, %r15, %rsi
leaq (%rsi,%rbp), %rdx
movl -0x14(%rbp,%rsi), %esi
cmpl %esi, -0x10(%rax)
jae 0x3c91b
movl -0xc(%rax), %edi
cmpl %esi, %edi
jbe 0x3c904
movl %edi, -0x14(%rdx)
movl -0x4(%rax), %esi
cmpl -0xc(%rdx), %esi
jae 0x3c90f
movl %esi, -0xc(%rdx)
movl (%rax), %esi
cmpl -0x8(%rdx), %esi
jbe 0x3c949
movl %esi, -0x8(%rdx)
jmp 0x3c949
subl -0x18(%rdx), %esi
cmpq %r14, %rsi
jae 0x3c930
movl -0x8(%rdx), %esi
subl -0xc(%rdx), %esi
cmpq %r14, %rsi
sbbq $0x0, %r15
imulq $0x1c, %r15, %rdx
incq %r15
movups -0x14(%rax), %xmm0
movups -0x8(%rax), %xmm1
movups %xmm1, 0xc(%rbp,%rdx)
movups %xmm0, (%rbp,%rdx)
addq $0x1c, %rax
jmp 0x3c8de
pushq $-0x1
popq %rax
jmp 0x3cbc5
testq %r15, %r15
jle 0x3c97e
imulq $0x1c, %r15, %rax
movl -0x14(%rbp,%rax), %ecx
subl -0x18(%rbp,%rax), %ecx
cmpq %r14, %rcx
jae 0x3c987
addq %rbp, %rax
movl -0x8(%rax), %ecx
subl -0xc(%rax), %ecx
cmpq %r14, %rcx
jae 0x3c987
decq %r15
testq %r15, %r15
je 0x3cbb7
movq %r12, 0x28(%rsp)
movq 0x80(%rsp), %rax
movq 0x58(%rax), %rax
movq 0xa8(%rsp), %rsi
movslq (%rax,%rsi), %rcx
leaq 0x68(%rsp), %rdx
movq %rcx, (%rdx)
movslq 0x8(%rax,%rsi), %rax
leaq 0x98(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x90(%rsp), %rax
andq $0x0, (%rax)
leaq (%r14,%r14), %rax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
testq %r15, %r15
cmovleq %rax, %r15
movq %rbp, %r13
xorl %r12d, %r12d
movq %rbp, 0x20(%rsp)
subq $0x1, %r15
jb 0x3cb99
subq $0x8, %rsp
movq %rbx, %rdi
movq %r13, %rsi
leaq 0xc8(%rsp), %rdx
leaq 0x98(%rsp), %rcx
leaq 0x70(%rsp), %r8
leaq 0xa0(%rsp), %r9
pushq $-0x1
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
pushq $-0x1
pushq $-0x1
pushq $-0x1
pushq $-0x1
pushq $-0x1
pushq $0x0
pushq $-0x1
pushq $0x0
callq 0x3beb1
addq $0x60, %rsp
movl %eax, (%r13)
movl %eax, %eax
cmpq %r14, %rax
jb 0x3cb90
movq %r12, 0x18(%rsp)
movq 0x30(%rsp), %r12
testq %r12, %r12
js 0x3cbe4
movq 0x38(%rsp), %r14
testq %r14, %r14
js 0x3cbe4
cmpq %r12, %r14
jl 0x3cbe4
movq 0x1e8(%rsp), %rax
subq %rax, %r12
testq %r12, %r12
movl $0x0, %ecx
cmovleq %rcx, %r12
movq %r12, 0x30(%rsp)
addq %rax, %r14
movq 0x60(%rsp), %rax
cmpq %rax, %r14
cmovgeq %rax, %r14
movq %r14, 0x38(%rsp)
movq 0x1d0(%rsp), %rbp
movq (%rbp), %rdi
movq %r14, %rbx
subq %r12, %rbx
cmpq %rbx, 0x10(%rbp)
jge 0x3cad1
movq %rbx, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, (%rbp)
movq %rbx, 0x10(%rbp)
movq 0x80(%rsp), %rax
movzbl 0x1c(%rax), %ecx
movl 0x10(%rax), %r9d
movq %r12, %rsi
movq %rbx, %rdx
movq 0xb0(%rsp), %r8
callq 0x6c9e
subq $0x8, %rsp
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0xc8(%rsp), %rdx
leaq 0x98(%rsp), %rcx
leaq 0x70(%rsp), %r8
leaq 0xa0(%rsp), %r9
pushq 0x18(%rsp)
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
pushq 0x210(%rsp)
pushq 0x210(%rsp)
pushq %r14
pushq %r12
pushq 0xa0(%rsp)
pushq (%rbp)
pushq 0xd8(%rsp)
pushq 0x110(%rsp)
callq 0x3beb1
addq $0x60, %rsp
movq 0x18(%rsp), %r12
imulq $0x1c, %r12, %rax
incq %r12
movups (%r13), %xmm0
movups 0xc(%r13), %xmm1
movq 0x20(%rsp), %rbp
movups %xmm0, (%rbp,%rax)
movups %xmm1, 0xc(%rbp,%rax)
movq 0x1e0(%rsp), %r14
addq $0x1c, %r13
jmp 0x3c9e5
movq 0x28(%rsp), %rcx
addq %r12, %rcx
movq 0x1d8(%rsp), %rax
movq %rcx, 0x8(%rax)
xorl %eax, %eax
testq %r12, %r12
setg %al
jmp 0x3cbc5
movq 0x1d8(%rsp), %rax
movq %r12, 0x8(%rax)
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1d8(%rsp), %rbx
jmp 0x3c88b
leaq 0x1a9286(%rip), %rdi    # 0x1e5e71
leaq 0x1a6ab1(%rip), %rsi    # 0x1e36a3
leaq 0x1a92a3(%rip), %rcx    # 0x1e5e9c
movl $0x49f1, %edx           # imm = 0x49F1
callq 0x2110