// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movq %rdx, %r10
movq %rdi, %r12
movq 0x120(%rsp), %rdi
movq 0x118(%rsp), %r11
movq 0x110(%rsp), %rbx
movl (%rdi), %edx
movl 0x18(%rdi), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%r12), %r8
shlq $0x5, %rdx
movslq (%r8,%rdx), %r13
leaq (%rax,%r13), %r14
movslq 0x4(%r8,%rdx), %r15
subq %rax, %r15
incq %r15
cmpq %rbx, %r14
cmovgq %r14, %rbx
cmpq %r11, %r15
movq %r11, %rbp
cmovlq %r15, %rbp
pushq $-0x1
popq %rax
movq %rbp, 0x98(%rsp)
cmpq %rbp, %rbx
jge 0x3bbd8
movq %rbx, %r11
addq %rdx, %r8
movl 0x1c(%r8), %ebx
testq %rbx, %rbx
je 0x3bbd8
movq %r14, 0x78(%rsp)
movq %r9, 0x70(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x10(%rdi), %ebp
movl 0x8(%rdi), %r9d
movq 0x70(%r12), %rax
movl 0x18(%r8), %edx
leaq (%rax,%rdx,2), %rax
movq %rax, 0xa0(%rsp)
xorl %eax, %eax
movq %rbx, 0xd0(%rsp)
cmpl %ebx, %r9d
movl $0x0, %ebx
movq %r9, 0x58(%rsp)
cmovbeq %r9, %rbx
movslq 0x8(%r8), %r14
cmovbeq %rbp, %r14
movl 0x4(%rdi), %edx
movq %rdx, 0x60(%rsp)
cmovbeq %rdx, %r13
testq %rbx, %rbx
jle 0x3b680
cmpq %r11, %r13
jle 0x3b680
movq 0xa0(%rsp), %rdx
movzwl -0x2(%rdx,%rbx,2), %r9d
decq %rbx
movl %r9d, %edx
shrl $0xe, %edx
andl $0x3fff, %r9d           # imm = 0x3FFF
cmpl $0x2, %edx
movl %r9d, %r8d
cmovel %eax, %r8d
subq %r8, %r13
cmpl $0x3, %edx
movl $0x0, %edx
cmovneq %r9, %rdx
subq %rdx, %r14
jmp 0x3b63a
cmpq $0x0, 0x128(%rsp)
movq %r15, 0x48(%rsp)
je 0x3b6b0
andl $0x0, 0x14(%rdi)
pushq $-0x1
popq %rax
movq %rax, 0x18(%rsp)
movl %r13d, %eax
movq %rax, 0x60(%rsp)
movq %r14, %rbp
movl %ebx, %eax
movq %rax, 0x58(%rsp)
jmp 0x3b6da
cmpq 0x60(%rsp), %r13
jne 0x3bbea
cmpq %rbp, %r14
jne 0x3bbf3
cmpq %rbx, 0x58(%rsp)
jne 0x3bbfc
movl 0x14(%rdi), %eax
addq %rbp, %rax
movq %rax, 0x18(%rsp)
movq %rsi, 0x88(%rsp)
subq %r11, 0x28(%rsp)
xorl %r15d, %r15d
movq %r11, 0x90(%rsp)
movq %r12, 0x80(%rsp)
movq %r15, 0x50(%rsp)
movq %rbp, 0x10(%rsp)
movq %rbx, %rsi
movq %rsi, %rbx
movq %r14, %rax
movq %r13, %r15
cmpq 0xd0(%rsp), %rbx
jge 0x3bb62
cmpq 0x98(%rsp), %r15
jge 0x3bb62
movq 0xa0(%rsp), %rdx
movzwl (%rdx,%rbx,2), %edi
movl %edi, %esi
shrl $0xe, %esi
movl %edi, %edx
andl $0x3fff, %edx           # imm = 0x3FFF
cmpl $0x2, %esi
movl %edx, %r9d
movl $0x0, %r8d
cmovel %r8d, %r9d
leaq (%r15,%r9), %r13
cmpl $0x3, %esi
movl $0x0, %ebp
cmovneq %rdx, %rbp
leaq (%rax,%rbp), %r14
cmpw $0x4000, %di            # imm = 0x4000
setb %dl
cmpl $0x1, %esi
sete %r8b
orb %dl, %r8b
je 0x3b79f
cmpq %r15, %r11
movq %r15, %r12
cmovgq %r11, %r12
movq 0x98(%rsp), %r8
movq %r8, %rdx
cmpq %r13, %r8
jl 0x3b79a
movq %r13, %rdx
cmpq %r12, %rdx
jg 0x3b7a7
incq %rbx
jmp 0x3b70a
leaq 0x1(%rbx), %r8
movq %r8, 0x8(%rsp)
movl %r9d, %r8d
cmpq $0x0, 0x128(%rsp)
je 0x3b7d1
cmpl $0x1, %esi
movq 0x8(%rsp), %rsi
jne 0x3b707
jmp 0x3b8b4
cmpw $0x3fff, %di            # imm = 0x3FFF
ja 0x3b8a4
movq 0x118(%rsp), %rdx
movq 0x48(%rsp), %rsi
cmpq %rsi, %rdx
cmovlq %rdx, %rsi
addq %r15, %r8
cmpq %r8, %rsi
cmovgeq %r8, %rsi
movq %rsi, 0x20(%rsp)
movq 0x110(%rsp), %rdx
cmpq %rdx, %r15
cmovleq %rdx, %r15
movq 0x78(%rsp), %rdx
cmpq %rdx, %r15
cmovleq %rdx, %r15
addl %eax, %r15d
addl %ebp, %r15d
subl %r8d, %r15d
movq 0x8(%rsp), %rsi
cmpq %r12, 0x20(%rsp)
je 0x3b707
movq 0x28(%rsp), %rax
movb (%rax,%r12), %al
testb %al, %al
je 0x3b89c
cmpb $0x3, %al
sete %al
movb (%rcx,%r12), %dl
movb %dl, 0x44(%rsp)
movq 0x120(%rsp), %rdx
movl 0xc(%rdx), %edx
movl %edx, 0x34(%rsp)
movl %r12d, 0x30(%rsp)
movl %r15d, 0x38(%rsp)
addb %al, %al
movb %al, 0x3c(%rsp)
movl $0x1, 0x40(%rsp)
movq %r10, %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
movq %rcx, %rbp
movq %r10, %rbx
callq 0x532ee
movq 0x8(%rsp), %rsi
movq 0x90(%rsp), %r11
movq %rbx, %r10
movq %rbp, %rcx
incq %r12
incl %r15d
jmp 0x3b828
cmpl $0x1, %esi
movq 0x8(%rsp), %rsi
jne 0x3b707
jmp 0x3b910
incq %rbx
movq 0x118(%rsp), %rsi
movq 0x48(%rsp), %rax
cmpq %rax, %rsi
cmovlq %rsi, %rax
addq %r8, %r15
cmpq %r15, %rax
cmovlq %rax, %r15
pushq $0x7f
popq %r8
cmpq %r12, %r15
je 0x3b8fd
movq 0x28(%rsp), %rdi
movb (%rdi,%r12), %al
leal 0x1(%rax), %esi
cmpb $0x7f, %al
movzbl %sil, %eax
cmovael %r8d, %eax
movb %al, (%rdi,%r12)
incq %r12
jmp 0x3b8d9
movq %r14, %rax
subq %r13, %rax
addq %rdx, %rax
movq %rax, 0x18(%rsp)
jmp 0x3b704
incq %rbx
movq 0x118(%rsp), %rdx
movq 0x48(%rsp), %rsi
cmpq %rsi, %rdx
movq %rsi, %rdi
cmovlq %rdx, %rdi
leaq (%r15,%r8), %rdx
cmpq %rdx, %rdi
cmovgeq %rdx, %rdi
movq 0x110(%rsp), %rsi
cmpq %rsi, %r15
movq %rsi, %r9
cmovgq %r15, %r9
movq 0x78(%rsp), %rsi
cmpq %rsi, %r9
cmovleq %rsi, %r9
addq %rax, %rbp
addq %r9, %rbp
movq %rbp, %rsi
subq %r15, %rsi
movq 0x140(%rsp), %rax
addq %r13, %rax
subq %r9, %rdi
movq %rdi, 0xc8(%rsp)
subq %r8, %rsi
movq %rsi, 0xb0(%rsp)
movq %rbp, %rsi
subq %rax, %rsi
movq %rsi, 0xa8(%rsp)
movq 0x28(%rsp), %rax
movq %r9, 0xb8(%rsp)
addq %r9, %rax
movq %rax, 0x8(%rsp)
subq %rdx, %rbp
xorl %edi, %edi
movq 0x80(%rsp), %r12
movq 0x50(%rsp), %r15
movq %rcx, 0xc0(%rsp)
movq %r10, 0x68(%rsp)
cmpq %rdi, 0xc8(%rsp)
je 0x3bb58
movq 0x8(%rsp), %rax
movb (%rax,%rdi), %dl
testb %dl, %dl
je 0x3bb50
testq %r15, %r15
movq %rdi, 0x20(%rsp)
jne 0x3ba72
movq 0xa8(%rsp), %rax
addq %rdi, %rax
testq %rax, %rax
movl $0x0, %ecx
cmovleq %rcx, %rax
movq 0x70(%rsp), %rcx
movq (%rcx), %r15
movq 0x18(%rsp), %rdx
movq %rax, 0x10(%rsp)
subq %rax, %rdx
cmpq %rdx, 0x10(%rcx)
jge 0x3ba40
movq %r15, %rdi
movq %rdx, %rsi
movq %rdx, %r15
callq 0x2370
movq %r15, %rdx
movq %rax, %r15
movq 0x70(%rsp), %rax
movq %r15, (%rax)
movq %rdx, 0x10(%rax)
movzbl 0x1c(%r12), %ecx
movl 0x10(%r12), %r9d
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x88(%rsp), %r8
callq 0x6c9e
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %rdi
movb (%rax,%rdi), %dl
movq 0x68(%rsp), %r10
cmpb $0x3, %dl
sete %al
cmpq $0x0, 0x140(%rsp)
je 0x3bad6
cmpb $0x3, %dl
je 0x3bad6
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rax
subq %rax, %rsi
movq 0xb0(%rsp), %rcx
leaq (%rcx,%rdi), %rdx
subq %rax, %rdx
subq $0x8, %rsp
movq %r15, %rdi
movq 0x148(%rsp), %rcx
pushq $0x4
popq %r8
xorl %r9d, %r9d
pushq $0x2
pushq $-0x1
pushq $-0x1
callq 0x27ce8
movq 0x40(%rsp), %rdi
movq 0x88(%rsp), %r10
addq $0x20, %rsp
movq %r15, %rcx
subq 0x10(%rsp), %rcx
addq %rbp, %rcx
movb (%rdi,%rcx), %cl
movb %cl, 0x44(%rsp)
movq 0x120(%rsp), %rcx
movl 0xc(%rcx), %ecx
movl %ecx, 0x34(%rsp)
movq 0xb8(%rsp), %rcx
addl %edi, %ecx
movl %ecx, 0x30(%rsp)
leal (%rdi,%rbp), %ecx
movl %ecx, 0x38(%rsp)
addb %al, %al
incb %al
movb %al, 0x3c(%rsp)
movl $0x1, 0x40(%rsp)
movq %r10, %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
movq %r15, 0x50(%rsp)
movq %r10, %r15
callq 0x532ee
movq 0x20(%rsp), %rdi
movq %r15, %r10
movq 0x50(%rsp), %r15
movq 0xc0(%rsp), %rcx
movq 0x90(%rsp), %r11
incq %rdi
jmp 0x3b9c2
movq 0x10(%rsp), %rbp
jmp 0x3b6fa
movq 0x120(%rsp), %rdx
cmpq $0x0, 0x128(%rsp)
je 0x3bbc6
movq 0x60(%rsp), %rax
movl %eax, 0x4(%rdx)
movq 0x10(%rsp), %rsi
movl %esi, 0x10(%rdx)
movq 0x58(%rsp), %rax
movl %eax, 0x8(%rdx)
movq 0x18(%rsp), %rdi
cmpq $-0x1, %rdi
je 0x3bbd0
addq 0x140(%rsp), %rdi
movq 0x88(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x80(%rsp), %rcx
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
cmpq %rax, %rdi
cmovgeq %rax, %rdi
subl %esi, %edi
jmp 0x3bbd2
movl %r15d, 0x4(%rdx)
movl %eax, 0x10(%rdx)
movl %ebx, 0x8(%rdx)
xorl %edi, %edi
movl %edi, 0x14(%rdx)
pushq $0x1
popq %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1aa0bd(%rip), %rdi    # 0x1e5cae
jmp 0x3bc03
leaq 0x1aa177(%rip), %rdi    # 0x1e5d71
jmp 0x3bc03
leaq 0x1aa178(%rip), %rdi    # 0x1e5d7b
leaq 0x1a7a99(%rip), %rsi    # 0x1e36a3
leaq 0x1aa0a7(%rip), %rcx    # 0x1e5cb8
movl $0x488d, %edx           # imm = 0x488D
callq 0x2110