// File: /workspace/github_cpp_makefile_star2_O3/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, 0x98(%rsp)
movq %r8, 0xb8(%rsp)
movq 0x1d8(%rsp), %r9
movq 0x58(%rdi), %r8
shlq $0x5, %rdx
movq %rcx, 0xb0(%rsp)
movq 0x18(%rcx), %rax
movl 0x10(%rdi), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x58(%rsp)
movq 0x8(%r9), %rax
movq %rax, 0x80(%rsp)
movl 0x18(%r8,%rdx), %r12d
addq %r12, %r12
movq %rdi, 0x90(%rsp)
addq 0x70(%rdi), %r12
movq %r12, 0x178(%rsp)
movl 0x1c(%r8,%rdx), %r15d
movq %r15, 0x170(%rsp)
movq %r15, 0x168(%rsp)
movq (%r8,%rdx), %rax
movq %rax, 0xdc(%rsp)
movq 0x8(%r8,%rdx), %rcx
movq %rcx, 0xd0(%rsp)
movq %rdx, 0xa8(%rsp)
movswl 0x14(%r8,%rdx), %edx
movl %edx, 0xec(%rsp)
movl %edx, 0xe8(%rsp)
testq %r15, %r15
je 0x6e300
movq %rsi, 0x88(%rsp)
movq %rax, %xmm0
movq %rcx, %xmm1
movd %xmm1, %eax
movslq %eax, %r14
movd %xmm0, %eax
movslq %eax, %rbx
movq 0x80(%rsp), %rcx
leal 0x1(%rcx), %eax
movq %rax, 0x78(%rsp)
movl $0x1, 0x14(%rsp)
movq $-0x1, %rbp
movq $-0x1, 0x20(%rsp)
xorl %r13d, %r13d
movq $-0x1, 0x18(%rsp)
movq $-0x1, %r10
movq %r10, 0x48(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r13, %rax
movq 0x20(%rsp), %rdx
movq %rdx, 0x50(%rsp)
movq %rbp, 0x28(%rsp)
movq %r14, %r11
cmpq %r15, %r13
jge 0x6deca
movzwl (%r12,%r13,2), %edx
movl %edx, %esi
shrl $0xe, %esi
movl %edx, %ebp
andl $0x3fff, %ebp           # imm = 0x3FFF
movq %rbx, %r9
cmpw $0x2, %si
je 0x6dc8c
leaq (%r9,%rbp), %rbx
movq %r11, %r14
cmpw $0x3, %si
je 0x6dc90
leaq (%r11,%rbp), %r14
incq %r13
movzwl %dx, %edx
cmpq %r15, %r13
jge 0x6dce5
cmpl $0x4000, %edx           # imm = 0x4000
setb %dil
movzwl (%r12,%r13,2), %esi
cmpq $0x4000, %rsi           # imm = 0x4000
setae %r8b
cmpb %r8b, %dil
je 0x6dce5
movl %esi, %edi
shrl $0xe, %edi
andl $0x3fff, %esi           # imm = 0x3FFF
cmpw $0x2, %di
je 0x6dcd4
addq %rsi, %rbx
cmpw $0x3, %di
je 0x6dcd7
addq %rsi, %r14
addq %rsi, %rbp
incq %r13
cmpq %r13, %r15
jne 0x6dc9b
movq %r15, %r13
cmpl $0x4000, %edx           # imm = 0x4000
jb 0x6dc58
subq %rbp, %r9
movl $0x0, %edx
cmovlq %rdx, %r9
movq %r9, 0x18(%rsp)
leaq (%rbx,%rbp), %r10
movq 0x98(%rsp), %rax
cmpq %rax, %r10
cmovgeq %rax, %r10
subq %rbp, %r11
cmovlq %rdx, %r11
movq %r11, 0x20(%rsp)
addq %r14, %rbp
movq 0x58(%rsp), %rax
cmpq %rax, %rbp
cmovgeq %rax, %rbp
cmpq $0x0, 0x8(%rsp)
movq 0x28(%rsp), %r8
movq 0x50(%rsp), %r9
movq 0x48(%rsp), %r11
js 0x6ddd6
testq %r11, %r11
js 0x6ddd6
testq %r9, %r9
js 0x6ddd6
testq %r8, %r8
js 0x6ddd6
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rdx
cmpq %rax, %rdx
cmovgq %rdx, %rax
cmpq %r11, %r10
movq %r11, %rdx
cmovlq %r10, %rdx
movq 0x20(%rsp), %rdi
cmpq %r9, %rdi
movq %r9, %rsi
cmovgq %rdi, %rsi
cmpq %r8, %rbp
movq %r8, %rdi
cmovlq %rbp, %rdi
cmpq %rsi, %rdi
jle 0x6ddd6
cmpq %rax, %rdx
jle 0x6ddd6
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rdx
cmpq %rax, %rdx
cmovgeq %rax, %rdx
movq %rdx, 0x18(%rsp)
cmpq %r11, %r10
cmovleq %r11, %r10
movq 0x20(%rsp), %rax
cmpq %r9, %rax
cmovgeq %r9, %rax
movq %rax, 0x20(%rsp)
cmpq %r8, %rbp
cmovleq %r8, %rbp
jmp 0x6dc37
cmpq $0x0, 0x8(%rsp)
js 0x6dc37
testq %r11, %r11
js 0x6dc37
testq %r9, %r9
js 0x6dc37
testq %r8, %r8
js 0x6dc37
movq 0x1d8(%rsp), %r9
movq (%r9), %rax
cmpq 0x10(%r9), %rcx
jne 0x6de59
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r9)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %r10, 0xa0(%rsp)
movq %rax, %rdi
callq 0x2370
movq 0xa0(%rsp), %r10
movq 0x48(%rsp), %r11
movq 0x1d8(%rsp), %r9
movq %rax, (%r9)
movq 0x8(%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r9)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movq 0x8(%rsp), %rsi
movl %esi, 0x4(%rax,%rdx)
movl %r11d, 0x8(%rax,%rdx)
movq 0x50(%rsp), %rcx
movl %ecx, 0x10(%rax,%rdx)
movq 0x28(%rsp), %rcx
movl %ecx, 0x14(%rax,%rdx)
movl $0x0, 0x18(%rax,%rdx)
movq 0x8(%r9), %rcx
cmpq 0x78(%rsp), %rcx
jbe 0x6dc37
movq (%r9), %rax
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
cmpl %esi, -0x34(%rax,%rdx)
movl 0x14(%rsp), %eax
movl $0x0, %edx
cmoval %edx, %eax
movl %eax, 0x14(%rsp)
jmp 0x6dc37
movq %rax, 0x60(%rsp)
movq 0x50(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movq 0x28(%rsp), %rsi
movq %rsi, 0x38(%rsp)
movq %rbx, 0x70(%rsp)
movq %r11, 0x68(%rsp)
movq 0x8(%rsp), %r12
testq %r12, %r12
js 0x6e34d
movq 0x48(%rsp), %r8
testq %r8, %r8
movq 0x1e0(%rsp), %r13
movq 0x1d8(%rsp), %r14
js 0x6dfc9
testq %rdi, %rdi
js 0x6dfc9
testq %rsi, %rsi
js 0x6dfc9
movq (%r14), %rax
cmpq 0x10(%r14), %rcx
jne 0x6df71
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmovneq %rdx, %rcx
movq %rcx, 0x10(%r14)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x48(%rsp), %r8
movq 0x50(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %rax, (%r14)
movq 0x8(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r14)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl %r12d, 0x4(%rax,%rdx)
movl %r8d, 0x8(%rax,%rdx)
movl %edi, 0x10(%rax,%rdx)
movl %esi, 0x14(%rax,%rdx)
movl $0x0, 0x18(%rax,%rdx)
movq 0x8(%r14), %rcx
cmpq 0x78(%rsp), %rcx
jbe 0x6dfc9
movq (%r14), %rax
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
xorl %esi, %esi
cmpl %r12d, -0x34(%rax,%rdx)
movl 0x14(%rsp), %eax
cmoval %esi, %eax
movl %eax, 0x14(%rsp)
movl 0x80(%rsp), %r12d
cmpq %r12, %rcx
jbe 0x6e30d
movq (%r14), %rax
leaq (%r12,%r12,8), %rbx
cmpl $0x0, 0x14(%rsp)
jne 0x6e00c
leaq (%rbx,%rbx,2), %rdi
addq %r12, %rdi
addq %rax, %rdi
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rcx, %rsi
addq %rax, %rsi
callq 0x46b25
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %r12, %rcx
testq %rcx, %rcx
jle 0x6e309
leaq (%rbx,%rbx,2), %rdx
addq %r12, %rdx
leaq (%rax,%rdx), %rbp
addq %rdx, %rax
addq $0x14, %rax
xorl %r15d, %r15d
testq %r15, %r15
jle 0x6e080
leaq (%r15,%r15,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %r15, %rsi
leaq (%rsi,%rbp), %rdx
movl -0x14(%rbp,%rsi), %esi
cmpl %esi, -0x10(%rax)
jae 0x6e06b
movl -0xc(%rax), %edi
cmpl %esi, %edi
jbe 0x6e054
movl %edi, -0x14(%rdx)
movl -0x4(%rax), %esi
cmpl -0xc(%rdx), %esi
jae 0x6e05f
movl %esi, -0xc(%rdx)
movl (%rax), %esi
cmpl -0x8(%rdx), %esi
jbe 0x6e0a4
movl %esi, -0x8(%rdx)
jmp 0x6e0a4
subl -0x18(%rdx), %esi
cmpq %r13, %rsi
jae 0x6e080
movl -0x8(%rdx), %esi
subl -0xc(%rdx), %esi
cmpq %r13, %rsi
sbbq $0x0, %r15
leaq (%r15,%r15,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r15, %rdx
incq %r15
movdqu -0x14(%rax), %xmm0
movdqu -0x8(%rax), %xmm1
movdqu %xmm1, 0xc(%rbp,%rdx)
movdqu %xmm0, (%rbp,%rdx)
addq $0x1c, %rax
decq %rcx
jne 0x6e02d
testq %r15, %r15
jle 0x6e0db
leaq (%r15,%r15,8), %rax
leaq (%rax,%rax,2), %rax
addq %r15, %rax
movl -0x14(%rbp,%rax), %ecx
subl -0x18(%rbp,%rax), %ecx
cmpq %r13, %rcx
jae 0x6e0e4
addq %rbp, %rax
movl -0x8(%rax), %ecx
subl -0xc(%rax), %ecx
cmpq %r13, %rcx
jae 0x6e0e4
decq %r15
testq %r15, %r15
je 0x6e309
movq %r12, 0x28(%rsp)
movq 0x90(%rsp), %rax
movq 0x58(%rax), %rax
movq 0xa8(%rsp), %rdx
movslq (%rax,%rdx), %rcx
movq %rcx, 0x70(%rsp)
movslq 0x8(%rax,%rdx), %rax
movq %rax, 0x68(%rsp)
movq $0x0, 0x60(%rsp)
testq %r15, %r15
jle 0x6e311
leaq (,%r13,2), %rax
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
movq %rbp, %r12
movq $0x0, 0x8(%rsp)
movq 0x88(%rsp), %rbx
movq %rbp, 0x20(%rsp)
subq $0x8, %rsp
movq %rbx, %rdi
movq %r12, %rsi
leaq 0xc8(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x78(%rsp), %r8
leaq 0x70(%rsp), %r9
movq $-0x1, %r10
pushq %r10
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
pushq %r10
pushq %r10
pushq %r10
pushq %r10
pushq %r10
pushq %r14
pushq %r10
pushq %r14
callq 0x6d59b
addq $0x60, %rsp
movl %eax, (%r12)
movl %eax, %eax
cmpq %r13, %rax
jb 0x6e2f1
movq 0x30(%rsp), %r13
testq %r13, %r13
js 0x6e362
movq 0x38(%rsp), %rbp
testq %rbp, %rbp
js 0x6e362
cmpq %r13, %rbp
jl 0x6e362
movq 0x1e8(%rsp), %rax
subq %rax, %r13
testq %r13, %r13
cmovleq %r14, %r13
movq %r13, 0x30(%rsp)
addq %rax, %rbp
movq 0x58(%rsp), %rax
cmpq %rax, %rbp
cmovgeq %rax, %rbp
movq %rbp, 0x38(%rsp)
movq 0x1d0(%rsp), %r14
movq (%r14), %rdi
movq %rbp, %rbx
subq %r13, %rbx
cmpq %rbx, 0x10(%r14)
jge 0x6e222
movq %rbx, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, (%r14)
movq %rbx, 0x10(%r14)
movq 0x90(%rsp), %rax
movzbl 0x1c(%rax), %ecx
movl 0x10(%rax), %r9d
movq %r13, %rsi
movq %rbx, %rdx
movq 0xb0(%rsp), %r8
callq 0x32ef8
subq $0x8, %rsp
movq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
leaq 0xc8(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x78(%rsp), %r8
leaq 0x70(%rsp), %r9
pushq 0x20(%rsp)
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
pushq 0x210(%rsp)
pushq 0x210(%rsp)
pushq %rbp
pushq %r13
pushq 0x98(%rsp)
pushq (%r14)
pushq 0xe8(%rsp)
pushq 0x110(%rsp)
callq 0x6d59b
addq $0x60, %rsp
movq 0x8(%rsp), %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
incq %rcx
movdqu (%r12), %xmm0
movdqu 0xc(%r12), %xmm1
movq 0x20(%rsp), %rbp
movdqu %xmm0, (%rbp,%rax)
movdqu %xmm1, 0xc(%rbp,%rax)
movq %rcx, 0x8(%rsp)
movq 0x1e0(%rsp), %r13
xorl %r14d, %r14d
addq $0x1c, %r12
decq %r15
jne 0x6e14b
jmp 0x6e31a
movq $-0x1, %rax
jmp 0x6e33b
movq %r12, 0x8(%r14)
xorl %eax, %eax
jmp 0x6e33b
movq $0x0, 0x8(%rsp)
movq 0x28(%rsp), %rcx
movq 0x8(%rsp), %rdx
addq %rdx, %rcx
movq 0x1d8(%rsp), %rax
movq %rcx, 0x8(%rax)
xorl %eax, %eax
testq %rdx, %rdx
setg %al
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1e0(%rsp), %r13
movq 0x1d8(%rsp), %r14
jmp 0x6dfc9
leaq 0x1c5bb8(%rip), %rdi    # 0x233f21
leaq 0x1c33e3(%rip), %rsi    # 0x231753
leaq 0x1c5bd5(%rip), %rcx    # 0x233f4c
movl $0x49f1, %edx           # imm = 0x49F1
callq 0x2110