// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: extract_sub_cigar_hc(overlap_region*, All_reads*, haplotype_evdience_alloc*, char*, unsigned long, UC_Read*, long, long, ul_ov_t*, long, unsigned char*, unsigned char, unsigned long)
int64_t extract_sub_cigar_hc(overlap_region *z, All_reads *rref, haplotype_evdience_alloc* hp, char *qstr, uint64_t ql, UC_Read* tu, int64_t s, int64_t e, ul_ov_t *p, int64_t set_f, uint8_t *f, uint8_t occ_thres, uint64_t hpc_len/**, uint8_t is_dbg**/)
{
    // fprintf(stderr, "\n[M::%s]\ts::%ld\te::%ld\tset_f::%ld\tovlp_id::%u\twid::%u\n", __func__, s, e, set_f, ovlp_id(*p), ovlp_cur_wid(*p));
    // if((!set_f) && (!ovlp_cur_ylen(*p))) return 1;///no potential informative site
    int64_t wk = ovlp_cur_wid(*p), xk = ovlp_cur_xoff(*p), yk = ovlp_cur_yoff(*p), ck = ovlp_cur_coff(*p), os, oe, t;
    bit_extz_t ez; int64_t bd = ovlp_bd(*p), s0, e0; char *ystr = NULL;
    s0 = ((int64_t)(z->w_list.a[wk].x_start)) + bd; 
    e0 = ((int64_t)(z->w_list.a[wk].x_end)) + 1 - bd;
    if(s < s0) s = s0; if(e > e0) e = e0;///exclude boundary
    if(s >= e) return -1;
    os = MAX(s, s0); oe = MIN(e, e0);
    if(oe <= os) return -1;

    set_bit_extz_t(ez, (*z), wk);
    if(!ez.cigar.n) return -1;
    int64_t cn = ez.cigar.n, op; int64_t ws, we, ovlp, xk0, yk0, ck0, yk1 = -1, /**xk1 = -1, ck1 = -1,**/ yl; haplotype_evdience ev; uint8_t om;
    xk0 = xk; yk0 = yk; ck0 = ck; ///for assertion
    if((ck < 0) || (ck > cn)) {//(*ck) == cn is allowed
        ck = 0; xk = ez.ts; yk = ez.ps;
    }
    
    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)); 
    }
    
    if(set_f) {
        ovlp_cur_ylen(*p) = 0;
        xk0 = xk; yk0 = yk; ck0 = ck; 
    } else {
        assert(xk0 == xk); assert(yk0 == yk); assert(ck0 == ck);
        yk1 = yk0 + ovlp_cur_ylen(*p); yl = Get_READ_LENGTH((*rref), z->y_id);

        // UC_Read_resize(*tu, ovlp_cur_ylen(*p)); ystr = tu->seq; 
        // if(is_dbg) fprintf(stderr, "[M::%s] set_f::%ld, yk0::%ld, ovlp_cur_ylen::%u, ylen::%lu\n", __func__, set_f, yk0, ovlp_cur_ylen(*p), Get_READ_LENGTH((*rref), z->y_id));
        // recover_UC_Read_sub_region(ystr, yk0, ovlp_cur_ylen(*p), z->y_pos_strand, rref, z->y_id);
    }

    // if(is_dbg) fprintf(stderr, "---0---[M::%s] set_f::%ld, yk::%ld\n", __func__, set_f, yk);
     //some cigar will span s or e
    while (ck < cn && xk < e) {//[s, e)
        ws = xk; 
        op = ez.cigar.a[ck]>>14;
        if(op!=2) xk += (ez.cigar.a[ck]&(0x3fff));
        if(op!=3) yk += (ez.cigar.a[ck]&(0x3fff)); 
        ck++; we = xk;
        if(op != 0 && op != 1) continue;///only collect match/snp

        os = MAX(s, ws); oe = MIN(e, we);
        ovlp = ((oe>os)? (oe-os):0);
        if(!ovlp) continue;

        if(set_f) {
            if(op == 1) {
                for (t = os; t < oe; t++) {
                    f[t-s] = ((f[t-s]<=126)?(f[t-s]+1):(127));
                }
                // if(!hpc_len) {
                //     yk1 = oe-xk+yk;
                // } else {
                //     yk1 = yk; xk1 = xk; ck1 = ck;
                // } 
                yk1 = oe-xk+yk;
            }
        } else {
            if(op == 0) {
                for (t = os; t < oe; t++) {
                    if(f[t-s]) {
                        om = ((f[t-s]==3)?1:0);
                        ev.misBase = qstr[t];
                        ev.overlapID = ovlp_id(*p);
                        ev.site = t;
                        ev.overlapSite = t-xk+yk;
                        ev.type = (om<<1);
                        ev.cov = 1;
                        addHaplotypeEvdience(hp, &ev, NULL);
                    }
                }
            } else if(op == 1) {
                for (t = os; t < oe; t++) {
                    if(f[t-s]) {
                        if(!ystr) {
                            // yk0 = ((hpc_len)?(detect_near_cc_tlen(&ez, ck, xk, yk, 1)):(t-xk+yk));
                            yk0 = t-xk+yk;
                            yk0 -= hpc_len; if(yk0 < 0) yk0 = 0;
                            UC_Read_resize(*tu, (yk1 - yk0)); ystr = tu->seq; 
                            recover_UC_Read_sub_region(ystr, yk0, (yk1 - yk0), z->y_pos_strand, rref, z->y_id);
                        }

                        om = ((f[t-s]==3)?1:0);
                        if((!om) && (hpc_len) && (hpc_mask_ff(ystr, yk1 - yk0, t-xk+yk-yk0, hpc_len, HPC_RR, NULL, -1, -1, HPC_CC))) om = 1;

                        ev.misBase = ystr[t-xk+yk-yk0];
                        ev.overlapID = ovlp_id(*p);
                        ev.site = t;
                        ev.overlapSite = t-xk+yk;
                        ev.type = (om<<1) + 1;
                        ev.cov = 1;
                        addHaplotypeEvdience(hp, &ev, NULL);
                        // if(ev.overlapID == 21 && ev.site == 17129) {
                        //     fprintf(stderr, "[M::%s]\ts::%ld\te::%ld\tck::%ld\txk::%ld\tyk::%ld\tset_f::%ld\n", __func__, s, e, ck, xk, yk, set_f);
                        // }
                    }

                }
            }
        }
    }
    // if(is_dbg) fprintf(stderr, "---1---[M::%s] set_f::%ld, yk::%ld\n", __func__, set_f, yk);

    if(set_f) {
        ovlp_cur_xoff(*p) = xk0; ovlp_cur_yoff(*p) = yk0; ovlp_cur_coff(*p) = ck0; 
        if(yk1 != -1) {
            // if((xk1 != -1) && (yk1 != -1)) {///detect nearby differences
            //     yk1 = detect_near_cc_tlen(&ez, ck1, xk1, yk1, 0);
            // }
            yk1 += hpc_len;
            yl = Get_READ_LENGTH((*rref), z->y_id);
            if(yk1 > yl) yk1 = yl;
            ovlp_cur_ylen(*p) = yk1 - yk0;
        } else {///no potential informative site; no second round
            ovlp_cur_ylen(*p) = 0;
        }
    } else {
        ovlp_cur_xoff(*p) = xk; ovlp_cur_yoff(*p) = yk; ovlp_cur_coff(*p) = ck; ovlp_cur_ylen(*p) = 0;
    }

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq 0x110(%rsp), %r11
movq 0x108(%rsp), %rbx
movq 0x100(%rsp), %r14
movl (%r11), %r8d
movl 0x18(%r11), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r10
shlq $0x5, %r8
movslq (%r10,%r8), %rbp
leaq (%rax,%rbp), %r15
movslq 0x4(%r10,%r8), %r12
subq %rax, %r12
incq %r12
cmpq %r14, %r15
cmovgq %r15, %r14
cmpq %rbx, %r12
cmovlq %r12, %rbx
movq $-0x1, %rax
movq %r14, 0x50(%rsp)
movq %rbx, 0x18(%rsp)
cmpq %rbx, %r14
jge 0x6d7a6
addq %r8, %r10
movl 0x1c(%r10), %r14d
testq %r14, %r14
je 0x6d7a6
movq %r12, 0x60(%rsp)
movq %r15, 0x88(%rsp)
movq %rsi, 0x98(%rsp)
movq 0x118(%rsp), %r12
movl 0x4(%r11), %esi
movl 0x10(%r11), %r15d
movl 0x8(%r11), %r8d
movq %rdi, 0xa0(%rsp)
movq 0x70(%rdi), %rax
movl 0x18(%r10), %ebx
xorl %r13d, %r13d
movq %r14, 0x78(%rsp)
cmpl %r14d, %r8d
movq %r8, 0x68(%rsp)
cmovbeq %r8, %r13
movslq 0x8(%r10), %r14
cmovbeq %r15, %r14
movq %rsi, 0x70(%rsp)
cmovbeq %rsi, %rbp
testq %r13, %r13
movq %r9, 0x90(%rsp)
movq %rdx, 0x28(%rsp)
je 0x6d15e
cmpq 0x50(%rsp), %rbp
jle 0x6d15e
leaq (%rax,%rbx,2), %rdx
addq $-0x2, %rdx
xorl %esi, %esi
movq %r13, %rdi
movzwl (%rdx,%r13,2), %r8d
movl %r8d, %r9d
shrl $0xe, %r9d
andl $0x3fff, %r8d           # imm = 0x3FFF
cmpl $0x2, %r9d
movl %r8d, %r10d
cmovel %esi, %r10d
decq %r13
subq %r10, %rbp
cmpl $0x3, %r9d
movl $0x0, %r9d
cmovneq %r8, %r9
subq %r9, %r14
cmpq $0x2, %rdi
jl 0x6d15e
cmpq 0x50(%rsp), %rbp
jg 0x6d119
testq %r12, %r12
je 0x6d18d
movl $0x0, 0x14(%r11)
movq $-0x1, 0x20(%rsp)
movl %ebp, %edx
movq %rdx, 0x70(%rsp)
movq %r14, %r15
movl %r13d, %edx
movq %rdx, 0x68(%rsp)
movq 0x18(%rsp), %r10
jmp 0x6d1bd
cmpq 0x70(%rsp), %rbp
movq 0x18(%rsp), %r10
jne 0x6d7b8
cmpq %r15, %r14
jne 0x6d7c1
cmpq 0x68(%rsp), %r13
jne 0x6d7ca
movl 0x14(%r11), %edx
addq %r15, %rdx
movq %rdx, 0x20(%rsp)
cmpq 0x78(%rsp), %r13
jge 0x6d73a
cmpq %r10, %rbp
jge 0x6d73a
movq 0x120(%rsp), %rdi
leaq (%rax,%rbx,2), %rax
movq %rax, 0xc0(%rsp)
subq 0x50(%rsp), %rdi
movq $0x0, 0x48(%rsp)
movq 0x28(%rsp), %r9
movq %rcx, 0xb8(%rsp)
movq %rdi, 0x38(%rsp)
movq %r15, 0x10(%rsp)
incq %r13
movq %r13, %rcx
movq %rbp, %r15
movq %r14, %rax
movq %rcx, %r13
movq 0xc0(%rsp), %rcx
movzwl -0x2(%rcx,%r13,2), %edi
movl %edi, %esi
shrl $0xe, %esi
movl %edi, %edx
andl $0x3fff, %edx           # imm = 0x3FFF
cmpl $0x2, %esi
movl %edx, %ecx
movl $0x0, %r8d
cmovel %r8d, %ecx
leaq (%rcx,%r15), %rbp
cmpl $0x3, %esi
movl $0x0, %ebx
cmovneq %rdx, %rbx
leaq (%rbx,%rax), %r14
cmpl $0x4000, %edi           # imm = 0x4000
setb %dl
cmpl $0x1, %esi
sete %r8b
orb %dl, %r8b
je 0x6d28a
movq 0x50(%rsp), %rdx
cmpq %r15, %rdx
movq %r15, %r12
cmovgq %rdx, %r12
cmpq %rbp, %r10
movq %rbp, %rdx
cmovlq %r10, %rdx
cmpq %r12, %rdx
jg 0x6d2ad
cmpq 0x78(%rsp), %r13
jge 0x6d72d
leaq 0x1(%r13), %rcx
movq %rbp, %r15
movq %r14, %rax
cmpq %r10, %rbp
jl 0x6d216
jmp 0x6d72d
cmpq $0x0, 0x118(%rsp)
je 0x6d320
cmpl $0x1, %esi
jne 0x6d70e
cmpq %rdx, %r12
movq 0x38(%rsp), %rdi
movl $0x7f, %r8d
jge 0x6d30d
movq 0x108(%rsp), %rsi
movq 0x60(%rsp), %rax
cmpq %rax, %rsi
cmovlq %rsi, %rax
movl %ecx, %ecx
addq %rcx, %r15
cmpq %r15, %rax
cmovlq %rax, %r15
movb (%rdi,%r12), %al
leal 0x1(%rax), %ecx
cmpb $0x7f, %al
movzbl %cl, %eax
cmovael %r8d, %eax
movb %al, (%rdi,%r12)
incq %r12
cmpq %r12, %r15
jne 0x6d2f1
movq %r14, %rax
subq %rbp, %rax
addq %rdx, %rax
movq %rax, 0x20(%rsp)
jmp 0x6d70e
movzwl %di, %edi
cmpl $0x3fff, %edi           # imm = 0x3FFF
ja 0x6d460
cmpq %rdx, %r12
jge 0x6d70e
movq 0x108(%rsp), %rdx
movq 0x60(%rsp), %rsi
cmpq %rsi, %rdx
cmovlq %rdx, %rsi
movl %ecx, %ecx
addq %r15, %rcx
cmpq %rcx, %rsi
cmovgeq %rcx, %rsi
movq 0x100(%rsp), %rdx
cmpq %rdx, %r15
cmovleq %rdx, %r15
movq 0x88(%rsp), %rdx
cmpq %rdx, %r15
cmovleq %rdx, %r15
addl %eax, %r15d
addl %ebx, %r15d
subl %ecx, %r15d
movq 0x38(%rsp), %rdi
movq %rsi, 0x30(%rsp)
movb (%rdi,%r12), %al
testb %al, %al
je 0x6d44c
cmpb $0x3, %al
sete %bl
movq 0xb8(%rsp), %rax
movb (%rax,%r12), %dl
movl 0xc(%r11), %edi
movl 0x10(%r9), %eax
leal 0x1(%rax), %ecx
cmpl 0x14(%r9), %ecx
jbe 0x6d419
movl %eax, %ecx
shrl %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x10, %ecx
orl %eax, %ecx
incl %ecx
movl %ecx, 0x14(%r9)
movl %edi, 0x58(%rsp)
movq (%r9), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
movb %dl, 0x40(%rsp)
callq 0x2370
movl 0x58(%rsp), %edi
movb 0x40(%rsp), %dl
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r9
movq 0x110(%rsp), %r11
movq %rax, (%r9)
addb %bl, %bl
movq (%r9), %rax
movl 0x10(%r9), %ecx
leaq (%rcx,%rcx,2), %rcx
movl %r12d, (%rax,%rcx,8)
movl %edi, 0x4(%rax,%rcx,8)
movl %r15d, 0x8(%rax,%rcx,8)
movb %bl, 0xc(%rax,%rcx,8)
movl $0x1, 0x10(%rax,%rcx,8)
movb %dl, 0x14(%rax,%rcx,8)
incl 0x10(%r9)
movq 0x38(%rsp), %rdi
incq %r12
incl %r15d
cmpq %r12, %rsi
jne 0x6d389
jmp 0x6d70e
cmpl $0x1, %esi
jne 0x6d70e
cmpq %rdx, %r12
jge 0x6d70e
addq %rax, %rbx
movq 0x100(%rsp), %rax
cmpq %rax, %r15
movq %rax, %rdi
cmovgq %r15, %rdi
movq 0x88(%rsp), %rax
cmpq %rax, %rdi
cmovleq %rax, %rdi
movq 0x130(%rsp), %rax
addq %rbp, %rax
addq %rdi, %rbx
movq %rbx, %rdx
subq %r15, %rdx
movl %ecx, %ecx
subq %rcx, %rdx
movq %rdx, 0x30(%rsp)
movq 0x108(%rsp), %rdx
movq 0x60(%rsp), %rsi
cmpq %rsi, %rdx
cmovlq %rdx, %rsi
addq %rcx, %r15
cmpq %r15, %rsi
cmovgeq %r15, %rsi
subq %rdi, %rsi
movq %rsi, 0x58(%rsp)
movq %rbx, %rcx
subq %r15, %rcx
movq %rcx, 0xa8(%rsp)
movq 0x38(%rsp), %rcx
movq %rdi, 0xb0(%rsp)
addq %rdi, %rcx
movq %rcx, 0x40(%rsp)
subq %rax, %rbx
xorl %r12d, %r12d
movq 0x10(%rsp), %r15
movq 0x40(%rsp), %rax
cmpb $0x0, (%rax,%r12)
je 0x6d6fe
cmpq $0x0, 0x48(%rsp)
jne 0x6d5a8
leaq (%rbx,%r12), %r15
testq %r15, %r15
movl $0x0, %eax
cmovleq %rax, %r15
movq 0x20(%rsp), %rdx
subq %r15, %rdx
movq 0x90(%rsp), %rcx
cmpq %rdx, 0x10(%rcx)
jge 0x6d56e
movq (%rcx), %rdi
movq %rdx, %rsi
movq %rdx, 0x10(%rsp)
callq 0x2370
movq 0x10(%rsp), %rdx
movq 0x90(%rsp), %rcx
movq %rax, (%rcx)
movq %rdx, 0x10(%rcx)
movq (%rcx), %rdi
movq 0xa0(%rsp), %rax
movzbl 0x1c(%rax), %ecx
movl 0x10(%rax), %r9d
movq %rdi, 0x48(%rsp)
movq %r15, %rsi
movq 0x98(%rsp), %r8
callq 0x337f7
movq 0x110(%rsp), %r11
movq 0x28(%rsp), %r9
movq 0x18(%rsp), %r10
movq 0x40(%rsp), %rax
movb (%rax,%r12), %al
cmpb $0x3, %al
sete %dil
cmpq $0x0, 0x130(%rsp)
je 0x6d61c
cmpb $0x3, %al
je 0x6d61c
movq 0x20(%rsp), %rsi
subq %r15, %rsi
movq 0x30(%rsp), %rax
leaq (%rax,%r12), %rdx
subq %r15, %rdx
subq $0x8, %rsp
movl $0x4, %r8d
movq 0x50(%rsp), %rdi
movq 0x138(%rsp), %rcx
xorl %r9d, %r9d
pushq $0x2
movq $-0x1, %rax
pushq %rax
pushq %rax
callq 0x575c6
movq 0x38(%rsp), %r10
movq 0x48(%rsp), %r9
movq 0x130(%rsp), %r11
addq $0x20, %rsp
movl %eax, %edi
movq 0x48(%rsp), %rax
movq %r15, 0x10(%rsp)
subq %r15, %rax
addq 0x30(%rsp), %rax
movb (%r12,%rax), %r8b
movl 0xc(%r11), %r15d
movl 0x10(%r9), %eax
leal 0x1(%rax), %ecx
cmpl 0x14(%r9), %ecx
jbe 0x6d6b0
movl %eax, %ecx
shrl %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x10, %ecx
orl %eax, %ecx
incl %ecx
movl %ecx, 0x14(%r9)
movb %dil, 0xf(%rsp)
movq (%r9), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
movl %r15d, 0x84(%rsp)
movl %r8d, %r15d
callq 0x2370
movl %r15d, %r8d
movl 0x84(%rsp), %r15d
movb 0xf(%rsp), %dil
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r9
movq 0x110(%rsp), %r11
movq %rax, (%r9)
movq 0xb0(%rsp), %rax
addq %r12, %rax
movq 0xa8(%rsp), %rcx
addq %r12, %rcx
addb %dil, %dil
incb %dil
movq (%r9), %rdx
movl 0x10(%r9), %esi
leaq (%rsi,%rsi,2), %rsi
movl %eax, (%rdx,%rsi,8)
movl %r15d, 0x4(%rdx,%rsi,8)
movl %ecx, 0x8(%rdx,%rsi,8)
movb %dil, 0xc(%rdx,%rsi,8)
movl $0x1, 0x10(%rdx,%rsi,8)
movb %r8b, 0x14(%rdx,%rsi,8)
incl 0x10(%r9)
movq 0x10(%rsp), %r15
incq %r12
cmpq %r12, 0x58(%rsp)
jne 0x6d508
jmp 0x6d713
movq 0x10(%rsp), %r15
cmpq 0x78(%rsp), %r13
movq 0x118(%rsp), %r12
jge 0x6d73a
cmpq %r10, %rbp
jl 0x6d205
jmp 0x6d73a
movq 0x10(%rsp), %r15
movq 0x118(%rsp), %r12
testq %r12, %r12
je 0x6d78f
movq 0x70(%rsp), %rax
movl %eax, 0x4(%r11)
movl %r15d, 0x10(%r11)
movq 0x68(%rsp), %rax
movl %eax, 0x8(%r11)
movq 0x20(%rsp), %rdx
cmpq $-0x1, %rdx
je 0x6d79b
addq 0x130(%rsp), %rdx
movq 0x98(%rsp), %rax
movq 0x18(%rax), %rax
movq 0xa0(%rsp), %rcx
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl %r15d, %edx
jmp 0x6d79d
movl %ebp, 0x4(%r11)
movl %r14d, 0x10(%r11)
movl %r13d, 0x8(%r11)
xorl %edx, %edx
movl %edx, 0x14(%r11)
movl $0x1, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ba48f(%rip), %rdi    # 0x227c4e
jmp 0x6d7d1
leaq 0x1ba549(%rip), %rdi    # 0x227d11
jmp 0x6d7d1
leaq 0x1ba54a(%rip), %rdi    # 0x227d1b
leaq 0x1b7e6b(%rip), %rsi    # 0x225643
leaq 0x1ba479(%rip), %rcx    # 0x227c58
movl $0x488d, %edx           # imm = 0x488D
callq 0x2110