// File: /workspace/github_cpp_makefile_star2_O3/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 $0xb8, %rsp
movq 0x100(%rsp), %r11
movq 0xf8(%rsp), %rbx
movq 0xf0(%rsp), %r14
movl (%r11), %r8d
movl 0x18(%r11), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r10
shlq $0x5, %r8
movslq (%r10,%r8), %r12
leaq (%r12,%rax), %r15
movslq 0x4(%r10,%r8), %r13
subq %rax, %r13
incq %r13
cmpq %r14, %r15
cmovgq %r15, %r14
cmpq %rbx, %r13
cmovlq %r13, %rbx
movq $-0x1, %rax
movq %r14, 0x48(%rsp)
movq %rbx, 0x68(%rsp)
cmpq %rbx, %r14
jge 0x6d26b
addq %r8, %r10
movl 0x1c(%r10), %ebx
testq %rbx, %rbx
je 0x6d26b
movq %r13, 0x58(%rsp)
movq %r15, 0x80(%rsp)
movq %rsi, 0x88(%rsp)
movl 0x4(%r11), %esi
movl 0x10(%r11), %r15d
movl 0x8(%r11), %r8d
movq %rdi, 0x90(%rsp)
movq 0x70(%rdi), %rax
movl 0x18(%r10), %r13d
xorl %ebp, %ebp
movq %rbx, 0x70(%rsp)
cmpl %ebx, %r8d
movq %r8, 0x40(%rsp)
cmovbeq %r8, %rbp
movslq 0x8(%r10), %r14
cmovbeq %r15, %r14
movq %rsi, 0x60(%rsp)
cmovbeq %rsi, %r12
testq %rbp, %rbp
movq %rdx, 0x20(%rsp)
je 0x6cc41
cmpq 0x48(%rsp), %r12
jle 0x6cc41
leaq (%rax,%r13,2), %rdx
addq $-0x2, %rdx
xorl %esi, %esi
movq 0x40(%rsp), %rbp
movq %rbp, %rdi
movzwl (%rdx,%rbp,2), %r8d
movl %r8d, %ebx
shrl $0xe, %ebx
andl $0x3fff, %r8d           # imm = 0x3FFF
cmpl $0x2, %ebx
movl %r8d, %r10d
cmovel %esi, %r10d
decq %rbp
subq %r10, %r12
cmpl $0x3, %ebx
movl $0x0, %r10d
cmovneq %r8, %r10
subq %r10, %r14
cmpq $0x2, %rdi
jb 0x6cc41
cmpq 0x48(%rsp), %r12
jg 0x6cbff
cmpq $0x0, 0x108(%rsp)
je 0x6cc71
movl $0x0, 0x14(%r11)
movq $-0x1, 0x18(%rsp)
movl %r12d, %edx
movq %rdx, 0x60(%rsp)
movq %r14, %r15
movl %ebp, %edx
movq %rdx, 0x40(%rsp)
jmp 0x6cc9c
cmpq 0x60(%rsp), %r12
jne 0x6d27d
cmpq %r15, %r14
jne 0x6d286
cmpq 0x40(%rsp), %rbp
jne 0x6d28f
movl 0x14(%r11), %edx
addq %r15, %rdx
movq %rdx, 0x18(%rsp)
cmpq 0x70(%rsp), %rbp
jge 0x6d1f9
cmpq 0x68(%rsp), %r12
jge 0x6d1f9
movq 0x110(%rsp), %rsi
leaq (%rax,%r13,2), %rax
movq %rax, 0xb0(%rsp)
subq 0x48(%rsp), %rsi
movq $0x0, 0x10(%rsp)
movq 0x20(%rsp), %r10
movq %r9, 0x78(%rsp)
movq %rcx, 0xa8(%rsp)
movq %rsi, 0x38(%rsp)
movq %r15, 0x8(%rsp)
incq %rbp
movq %rbp, %rcx
movq %r12, %r15
movq %r14, %rax
movq 0x68(%rsp), %r9
movq %rcx, %rbp
movq 0xb0(%rsp), %rcx
movzwl -0x2(%rcx,%rbp,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), %r12
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 0x6cd74
movq 0x48(%rsp), %rdx
cmpq %r15, %rdx
movq %r15, %r13
cmovgq %rdx, %r13
cmpq %r12, %r9
movq %r12, %rdx
cmovlq %r9, %rdx
cmpq %r13, %rdx
jg 0x6cd97
cmpq 0x70(%rsp), %rbp
jge 0x6d1f4
leaq 0x1(%rbp), %rcx
movq %r12, %r15
movq %r14, %rax
cmpq %r9, %r12
jl 0x6cd01
jmp 0x6d1f4
cmpq $0x0, 0x108(%rsp)
je 0x6ce03
cmpl $0x1, %esi
jne 0x6d1db
movq 0xf8(%rsp), %rsi
movq 0x58(%rsp), %rax
cmpq %rax, %rsi
cmovlq %rsi, %rax
movl %ecx, %ecx
addq %rcx, %r15
cmpq %r15, %rax
cmovlq %rax, %r15
movq 0x38(%rsp), %rsi
movl $0x7f, %edi
movb (%rsi,%r13), %al
leal 0x1(%rax), %ecx
cmpb $0x7f, %al
movzbl %cl, %eax
cmovael %edi, %eax
movb %al, (%rsi,%r13)
incq %r13
cmpq %r13, %r15
jne 0x6cdd5
movq %r14, %rax
subq %r12, %rax
addq %rdx, %rax
movq %rax, 0x18(%rsp)
jmp 0x6d1db
movzwl %di, %edx
cmpl $0x3fff, %edx           # imm = 0x3FFF
ja 0x6cf3c
movq 0xf8(%rsp), %rdx
movq 0x58(%rsp), %rsi
cmpq %rsi, %rdx
movq %rsi, %rdi
cmovlq %rdx, %rdi
movl %ecx, %ecx
addq %r15, %rcx
cmpq %rcx, %rdi
cmovgeq %rcx, %rdi
movq 0xf0(%rsp), %rdx
cmpq %rdx, %r15
cmovleq %rdx, %r15
movq 0x80(%rsp), %rdx
cmpq %rdx, %r15
cmovleq %rdx, %r15
addl %eax, %r15d
addl %ebx, %r15d
subl %ecx, %r15d
movq 0x38(%rsp), %rsi
movq %rdi, 0x28(%rsp)
movb (%rsi,%r13), %al
testb %al, %al
je 0x6cf28
cmpb $0x3, %al
sete %bl
movq 0xa8(%rsp), %rax
movb (%rax,%r13), %dl
movl 0xc(%r11), %esi
movl 0x10(%r10), %ecx
leal 0x1(%rcx), %eax
cmpl 0x14(%r10), %eax
jbe 0x6cef7
movl %ecx, %eax
shrl %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x2, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x4, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x8, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x10, %eax
orl %ecx, %eax
incl %eax
movl %eax, 0x14(%r10)
movq (%r10), %rdi
shlq $0x3, %rax
movl %esi, 0x50(%rsp)
leaq (%rax,%rax,2), %rsi
movb %dl, 0x30(%rsp)
callq 0x2370
movl 0x50(%rsp), %esi
movb 0x30(%rsp), %dl
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %r10
movq 0x100(%rsp), %r11
movq %rax, (%r10)
movl 0x10(%r10), %ecx
jmp 0x6cefa
movq (%r10), %rax
addb %bl, %bl
movl %ecx, %ecx
leaq (%rcx,%rcx,2), %rcx
movl %r13d, (%rax,%rcx,8)
movl %esi, 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(%r10)
movq 0x38(%rsp), %rsi
incq %r13
incl %r15d
cmpq %r13, %rdi
jne 0x6ce66
jmp 0x6d1db
cmpl $0x1, %esi
jne 0x6d1db
addq %rax, %rbx
movq 0xf0(%rsp), %rax
cmpq %rax, %r15
movq %rax, %rdi
cmovgq %r15, %rdi
movq 0x80(%rsp), %rax
cmpq %rax, %rdi
cmovleq %rax, %rdi
movq 0x120(%rsp), %rax
addq %r12, %rax
addq %rdi, %rbx
movq %rbx, %rdx
subq %r15, %rdx
movl %ecx, %ecx
subq %rcx, %rdx
movq %rdx, 0x28(%rsp)
movq 0xf8(%rsp), %rdx
movq 0x58(%rsp), %rsi
cmpq %rsi, %rdx
cmovlq %rdx, %rsi
addq %rcx, %r15
cmpq %r15, %rsi
cmovgeq %r15, %rsi
subq %rdi, %rsi
movq %rsi, 0x50(%rsp)
movq %rbx, %rcx
subq %r15, %rcx
movq %rcx, 0x98(%rsp)
movq 0x38(%rsp), %rcx
movq %rdi, 0xa0(%rsp)
addq %rdi, %rcx
movq %rcx, 0x30(%rsp)
subq %rax, %rbx
xorl %r13d, %r13d
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rax
movb (%rax,%r13), %al
testb %al, %al
je 0x6d1c6
testq %rdi, %rdi
jne 0x6d081
leaq (%rbx,%r13), %r15
testq %r15, %r15
movl $0x0, %eax
cmovleq %rax, %r15
movq 0x78(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rdx
subq %r15, %rdx
cmpq %rdx, 0x10(%rax)
jge 0x6d041
movq %rdx, %rsi
movq %rdx, 0x8(%rsp)
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, %rdi
movq 0x78(%rsp), %rax
movq %rdi, (%rax)
movq %rdx, 0x10(%rax)
movq 0x90(%rsp), %rax
movzbl 0x1c(%rax), %ecx
movl 0x10(%rax), %r9d
movq %rdi, 0x10(%rsp)
movq %r15, %rsi
movq 0x88(%rsp), %r8
callq 0x32ef8
movq 0x30(%rsp), %rax
movb (%rax,%r13), %al
movq 0x100(%rsp), %r11
movq 0x20(%rsp), %r10
movq 0x10(%rsp), %rdi
cmpb $0x3, %al
sete %r8b
cmpq $0x0, 0x120(%rsp)
movq %rdi, 0x10(%rsp)
je 0x6d0ed
cmpb $0x3, %al
je 0x6d0ed
movq 0x18(%rsp), %rsi
subq %r15, %rsi
movq 0x28(%rsp), %rax
leaq (%rax,%r13), %rdx
subq %r15, %rdx
subq $0x8, %rsp
movl $0x4, %r8d
movq 0x128(%rsp), %rcx
xorl %r9d, %r9d
pushq $0x2
movq $-0x1, %rax
pushq %rax
pushq %rax
callq 0x56a35
movq 0x30(%rsp), %rdi
movq 0x40(%rsp), %r10
movq 0x120(%rsp), %r11
addq $0x20, %rsp
movl %eax, %r8d
movq %rdi, %rax
movq %r15, 0x8(%rsp)
subq %r15, %rax
addq 0x28(%rsp), %rax
movb (%r13,%rax), %dil
movl 0xc(%r11), %r15d
movl 0x10(%r10), %ecx
leal 0x1(%rcx), %eax
cmpl 0x14(%r10), %eax
jbe 0x6d175
movl %ecx, %eax
shrl %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x2, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x4, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x8, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x10, %eax
orl %ecx, %eax
incl %eax
movl %eax, 0x14(%r10)
movb %dil, 0x7(%rsp)
movq (%r10), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
movb %r8b, 0x6(%rsp)
callq 0x2370
movb 0x7(%rsp), %dil
movb 0x6(%rsp), %r8b
movq 0x20(%rsp), %r10
movq 0x100(%rsp), %r11
movq %rax, (%r10)
movl 0x10(%r10), %ecx
jmp 0x6d178
movq (%r10), %rax
movq 0xa0(%rsp), %rdx
addq %r13, %rdx
movq 0x98(%rsp), %rsi
addq %r13, %rsi
addb %r8b, %r8b
incb %r8b
movl %ecx, %ecx
leaq (%rcx,%rcx,2), %rcx
movl %edx, (%rax,%rcx,8)
movl %r15d, 0x4(%rax,%rcx,8)
movl %esi, 0x8(%rax,%rcx,8)
movb %r8b, 0xc(%rax,%rcx,8)
movl $0x1, 0x10(%rax,%rcx,8)
movb %dil, 0x14(%rax,%rcx,8)
incl 0x10(%r10)
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %rdi
incq %r13
cmpq %r13, 0x50(%rsp)
jne 0x6cfe0
movq %rdi, 0x10(%rsp)
jmp 0x6d1e0
movq 0x8(%rsp), %r15
cmpq 0x70(%rsp), %rbp
jge 0x6d1f9
cmpq 0x68(%rsp), %r12
jl 0x6cceb
jmp 0x6d1f9
movq 0x8(%rsp), %r15
cmpq $0x0, 0x108(%rsp)
je 0x6d254
movq 0x60(%rsp), %rax
movl %eax, 0x4(%r11)
movl %r15d, 0x10(%r11)
movq 0x40(%rsp), %rax
movl %eax, 0x8(%r11)
movq 0x18(%rsp), %rdx
cmpq $-0x1, %rdx
je 0x6d260
addq 0x120(%rsp), %rdx
movq 0x88(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x90(%rsp), %rcx
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl %r15d, %edx
jmp 0x6d262
movl %r12d, 0x4(%r11)
movl %r14d, 0x10(%r11)
movl %ebp, 0x8(%r11)
xorl %edx, %edx
movl %edx, 0x14(%r11)
movl $0x1, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c6ada(%rip), %rdi    # 0x233d5e
jmp 0x6d296
leaq 0x1c6b94(%rip), %rdi    # 0x233e21
jmp 0x6d296
leaq 0x1c6b95(%rip), %rdi    # 0x233e2b
leaq 0x1c44b6(%rip), %rsi    # 0x231753
leaq 0x1c6ac4(%rip), %rcx    # 0x233d68
movl $0x488d, %edx           # imm = 0x488D
callq 0x2110