// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_commen_sketch(All_reads*, ul_idx_t const*, overlap_region_alloc*, unsigned long*, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long*, unsigned long, char*, char*, kv_ul_ov_t*, asg64_v*, long, double, long)
uint64_t gen_commen_sketch(All_reads *rref, const ul_idx_t *uref, overlap_region_alloc* ol, uint64_t *id_a, uint64_t id_n, uint64_t s, uint64_t e, uint64_t ql, uint64_t wl,
uint64_t *buf, uint64_t dp, char *str0, char *str1, kv_ul_ov_t *aln, asg64_v *trace,
int64_t max_lgap, double sgap_rate, int64_t sgap)///[s, e)
{
    if(!id_n) return id_n;
    uint64_t i, m, k, rm_n = 0, buf_n = 0, qs, qe, wid, co, occ; char *qstring, *tstring;
    overlap_region *z; uint64_t ws, we; int64_t r_y[2], r_err, p_y[2], p_err, rxl, ryl, pxl;
    ///shrink [qs, qe)
    qs = (s/wl)*wl; if(qs < s) qs += wl; if(qs >= ql) return id_n;
    qe = (e/wl)*wl; if(qe >= ql) qe = ql;
    if(qs >= qe) return id_n;
    //idx_a[] is sorted by aln[].qs
    for (k = 0; k < id_n; k++) {
        if(aln->a[id_a[k]].qs<=qs && aln->a[id_a[k]].qe>=qe) {
            buf[buf_n] = ol->list[aln->a[id_a[k]].qn].align_length; buf[buf_n] <<= 32; buf[buf_n] |= id_a[k];
            buf_n++;
        }
        if(aln->a[id_a[k]].qe < e) rm_n++;
    }
    assert(buf_n == dp && buf_n > 1);

    if(buf_n > 0) {
        ///fs = fe = (uint64_t)-1;
        co = 1; trace->n = occ = 0;
        for (k = qs; k < qe; k += wl) {
            ws = k; we = ws + wl; if(we > qe) we = qe;//[ws, we)
            ///first overlap
            z = &(ol->list[aln->a[(uint32_t)buf[0]].qn]);
            wid = get_win_id_by_s(z, ws, wl, NULL);
            if(!get_win_aln(z, wid, &(r_y[0]), &(r_y[1]), &r_err)) continue;
            if(!ovlp_win_check(z, z->align_length, wid, max_lgap, sgap_rate, sgap)) continue;///not co-linear
            rxl = z->w_list.a[wid].x_end+1-z->w_list.a[wid].x_start;
            ryl = r_y[1]-r_y[0];
            // fprintf(stderr, "+[M::%s::] buf_n::%lu, q::[%lu, %lu), w::[%lu, %lu), rxl::%ld, ryl::%ld, x::[%d, %d), y::[%ld, %ld)\n", 
            //         __func__, buf_n, qs, qe, ws, we, rxl, ryl, z->w_list.a[wid].x_start, z->w_list.a[wid].x_end+1, r_y[0], r_y[1]);
            if(rxl > 1 && ryl > 1) continue;///length of window should be longer than 1
            // if(rxl <= 1 || ryl <= 1) continue;///length of window should be longer than 1
            // fprintf(stderr, "-[M::%s::] buf_n::%lu, q::[%lu, %lu), w::[%lu, %lu), rxl::%ld, ryl::%ld, x::[%d, %d), y::[%ld, %ld)\n", 
            //         __func__, buf_n, qs, qe, ws, we, rxl, ryl, z->w_list.a[wid].x_start, z->w_list.a[wid].x_end+1, r_y[0], r_y[1]);
            qstring = tstring = NULL;
            for (i = 1; i < buf_n; i++) {
                z = &(ol->list[aln->a[(uint32_t)buf[i]].qn]);
                wid = get_win_id_by_s(z, ws, wl, NULL);
                if(!get_win_aln(z, wid, &(p_y[0]), &(p_y[1]), &p_err)) break;
                if(!ovlp_win_check(z, z->align_length, wid, max_lgap, sgap_rate, sgap)) break;///not co-linear
                pxl = z->w_list.a[wid].x_end+1-z->w_list.a[wid].x_start;
                if(pxl != rxl) break;
                if(((r_y[1]-r_y[0]) != (p_y[1]-p_y[0])) || (r_err != p_err)) break;
                if(r_err == 0) continue;
                if(!qstring) {
                    qstring = retrive_str_piece_exz(rref, uref, str0, r_y[0], r_y[1]-r_y[0], 
                                                ol->list[aln->a[(uint32_t)buf[0]].qn].y_pos_strand, ol->list[aln->a[(uint32_t)buf[0]].qn].y_id);
                }
                tstring = retrive_str_piece_exz(rref, uref, str1, p_y[0], p_y[1]-p_y[0], z->y_pos_strand, z->y_id);
                if(memcmp(qstring, tstring, (p_y[1]-p_y[0]))) {
                    // fprintf(stderr, "[M::%s::] qs::%ld, qe::%lu, ts::%ld, te::%lu, err::%ld, rts::%ld, rte::%lu, err::%ld\n", 
                    // __func__, ws, we, p_y[0], p_y[1], r_err, r_y[0], r_y[1], p_err);
                    // fprintf(stderr, "str0::%.*s\n", (int32_t)(r_y[1]-r_y[0]), str0);
                    // fprintf(stderr, "str1::%.*s\n", (int32_t)(p_y[1]-p_y[0]), str1);
                    break;
                }
            }
            if(i < buf_n) continue;
            if(co) {
                for (i = 0; i < buf_n; i++) {
                    z = &(ol->list[aln->a[(uint32_t)buf[i]].qn]);
                    wid = get_win_id_by_s(z, ws, wl, NULL);
                    co = ovlp_win_check(z, buf[i]>>32, wid, max_lgap, sgap_rate, sgap);
                    m = wid; m <<= 32; m |= (uint32_t)buf[i]; buf[i] = m;
                }
                if(co) occ++;
            }
            kv_push(uint64_t, *trace, ws);
        }

        if(trace->n) {
            if(!co) kv_resize(uint64_t, *trace, trace->n<<1);
            trace->n = dp_commen_sketch(aln, ol, buf, buf_n, trace->a, trace->n, trace->a + trace->n, occ, wl, dp, max_lgap, sgap_rate, sgap);
        }
    }
    if(rm_n) {
        for (i = m = 0; i < id_n; i++) {
            if(aln->a[id_a[i]].qe < e) continue;
            id_a[m++] = id_a[i];
        }
        id_n = m;
    }
    return id_n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movsd %xmm0, 0x8(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, 0x50(%rsp)
testq %r8, %r8
je 0x479cb
movq %rdx, %rbx
movq 0xe0(%rsp), %rbp
movq 0xd8(%rsp), %r12
movq %r9, %rax
xorl %edx, %edx
divq %rbp
leaq (%r9,%rbp), %r13
subq %rdx, %r13
testq %rdx, %rdx
cmoveq %r9, %r13
cmpq %r12, %r13
jae 0x47f0a
movq 0xd0(%rsp), %r11
movq %r11, %rax
xorl %edx, %edx
divq %rbp
movq %r11, %rax
subq %rdx, %rax
cmpq %r12, %rax
cmovbq %rax, %r12
cmpq %r13, %rax
jbe 0x47f0a
movq 0x110(%rsp), %r14
movq 0x108(%rsp), %rax
movq (%rax), %r10
xorl %edx, %edx
xorl %r15d, %r15d
xorl %eax, %eax
cmpq %rdx, %r8
je 0x479d3
movq (%rcx,%rdx,8), %rsi
imulq $0x1c, %rsi, %rdi
movl 0x4(%r10,%rdi), %r9d
cmpq %r9, %r13
jb 0x479b6
addq %r10, %rdi
movl 0x8(%rdi), %r9d
cmpq %r9, %r12
ja 0x479b6
movq (%rbx), %rsi
movl (%rdi), %edi
imulq $0xa8, %rdi, %rdi
movl 0x28(%rsi,%rdi), %esi
shlq $0x20, %rsi
movq 0xe8(%rsp), %rdi
movq %rsi, (%rdi,%rax,8)
orq (%rcx,%rdx,8), %rsi
movq %rsi, (%rdi,%rax,8)
incq %rax
movq (%rcx,%rdx,8), %rsi
imulq $0x1c, %rsi, %rsi
movl 0x8(%r10,%rsi), %esi
cmpq %r11, %rsi
adcq $0x0, %r15
incq %rdx
jmp 0x47964
xorl %r8d, %r8d
jmp 0x47f0a
movq %r12, 0x80(%rsp)
movq %r8, 0x78(%rsp)
cmpq 0xf0(%rsp), %rax
jne 0x47f1f
cmpq $0x1, %rax
jbe 0x47f1f
movq %r13, 0x60(%rsp)
movq %r15, 0x68(%rsp)
movq %rcx, 0x70(%rsp)
andq $0x0, (%r14)
pushq $0x1
popq %rax
movq $0x0, 0x48(%rsp)
movq %rax, 0x28(%rsp)
movq %rbx, (%rsp)
movq 0x60(%rsp), %r14
cmpq 0x80(%rsp), %r14
jae 0x47ded
leaq (%r14,%rbp), %rax
movq %rax, 0x60(%rsp)
movq (%rbx), %rax
movq 0x108(%rsp), %rcx
movq (%rcx), %rcx
movq 0xe8(%rsp), %rdx
movl (%rdx), %edx
imulq $0x1c, %rdx, %rdx
movl (%rcx,%rdx), %ecx
imulq $0xa8, %rcx, %rcx
leaq (%rax,%rcx), %r15
movl 0x4(%rax,%rcx), %ecx
movq %rcx, %rax
xorl %edx, %edx
idivq %rbp
leaq (%rdx,%r14), %rax
subq %rcx, %rax
cqto
idivq %rbp
movq %rax, %r13
movq %r15, %rdi
movq %rax, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x90(%rsp), %r8
callq 0x62eca
testq %rax, %rax
je 0x47a20
movl 0x28(%r15), %esi
movq %r15, %rdi
movl %r13d, %edx
movq 0x118(%rsp), %rcx
movsd 0x8(%rsp), %xmm0
movq 0x120(%rsp), %r8
callq 0x62d83
testl %eax, %eax
je 0x47a20
movq 0x58(%r15), %rax
shlq $0x5, %r13
movl 0x4(%rax,%r13), %r12d
subl (%rax,%r13), %r12d
incl %r12d
cmpl $0x2, %r12d
jl 0x47b02
movq 0x38(%rsp), %rax
subq 0x30(%rsp), %rax
cmpq $0x1, %rax
jg 0x47a20
xorl %r15d, %r15d
pushq $0x1
popq %rbp
cmpq %rbp, 0xf0(%rsp)
je 0x47ce8
movq (%rbx), %rax
movq 0x108(%rsp), %rcx
movq (%rcx), %rcx
movq 0xe8(%rsp), %rdx
movl (%rdx,%rbp,8), %edx
imulq $0x1c, %rdx, %rdx
movl (%rcx,%rdx), %ecx
imulq $0xa8, %rcx, %rcx
leaq (%rax,%rcx), %r13
movl 0x4(%rax,%rcx), %ecx
movq %rcx, %rax
xorl %edx, %edx
movq 0xe0(%rsp), %rsi
idivq %rsi
leaq (%rdx,%r14), %rax
subq %rcx, %rax
cqto
idivq %rsi
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
leaq 0x10(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x88(%rsp), %r8
callq 0x62eca
testq %rax, %rax
je 0x47cca
movl 0x28(%r13), %esi
movq %r13, %rdi
movl %ebx, %edx
movq 0x118(%rsp), %rcx
movsd 0x8(%rsp), %xmm0
movq 0x120(%rsp), %r8
callq 0x62d83
testl %eax, %eax
je 0x47cca
movq 0x58(%r13), %rax
shlq $0x5, %rbx
movl 0x4(%rax,%rbx), %ecx
subl (%rax,%rbx), %ecx
incl %ecx
cmpl %r12d, %ecx
jne 0x47cca
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r8
subq %rcx, %r8
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %r10
subq %rax, %r10
cmpq %r10, %r8
movq (%rsp), %rbx
jne 0x47cdb
movq 0x90(%rsp), %rdx
cmpq 0x88(%rsp), %rdx
jne 0x47cdb
testq %rdx, %rdx
je 0x47cc2
testq %r15, %r15
jne 0x47c7a
movq (%rbx), %rax
movq 0x108(%rsp), %rdx
movq (%rdx), %rdx
movq 0xe8(%rsp), %rsi
movl (%rsi), %esi
imulq $0x1c, %rsi, %rsi
movl (%rdx,%rsi), %edx
imulq $0xa8, %rdx, %rdx
movl 0x10(%rax,%rdx), %r10d
movl 0x1c(%rax,%rdx), %r9d
subq $0x8, %rsp
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x100(%rsp), %rdx
pushq %r10
callq 0x319f3
popq %rcx
popq %rdx
movq %rax, %r15
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %r10
subq %rax, %r10
movl 0x10(%r13), %r11d
movl 0x1c(%r13), %r9d
subq $0x8, %rsp
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x108(%rsp), %rdx
movq %rax, %rcx
movq %r10, %r8
pushq %r11
callq 0x319f3
addq $0x10, %rsp
movq 0x18(%rsp), %rdx
subq 0x10(%rsp), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x21a0
testl %eax, %eax
jne 0x47cdb
incq %rbp
jmp 0x47b08
movq 0xe0(%rsp), %rbp
movq (%rsp), %rbx
jmp 0x47a20
movq 0xe0(%rsp), %rbp
jmp 0x47a20
movq 0x28(%rsp), %rdx
testq %rdx, %rdx
je 0x47d94
xorl %r15d, %r15d
movq 0xe0(%rsp), %rbp
cmpq %r15, 0xf0(%rsp)
je 0x47d88
movq (%rbx), %rax
movq 0x108(%rsp), %rcx
movq (%rcx), %rcx
movq 0xe8(%rsp), %r12
movq (%r12,%r15,8), %rsi
movl %esi, %edx
imulq $0x1c, %rdx, %rdx
movl (%rcx,%rdx), %ecx
imulq $0xa8, %rcx, %rcx
leaq (%rax,%rcx), %rdi
movl 0x4(%rax,%rcx), %ecx
movq %rcx, %rax
xorl %edx, %edx
idivq %rbp
leaq (%rdx,%r14), %rax
subq %rcx, %rax
cqto
idivq %rbp
movq %rax, %rbx
shrq $0x20, %rsi
movl %ebx, %edx
movq 0x118(%rsp), %rcx
movsd 0x8(%rsp), %xmm0
movq 0x120(%rsp), %r8
callq 0x62d83
movl %eax, %edx
movl %ebx, 0x4(%r12,%r15,8)
movq (%rsp), %rbx
incq %r15
jmp 0x47d01
cmpq $0x1, %rdx
sbbq $-0x1, 0x48(%rsp)
jmp 0x47d9e
xorl %edx, %edx
movq 0xe0(%rsp), %rbp
movq 0x110(%rsp), %r15
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
movq %rdx, 0x28(%rsp)
jne 0x47ddd
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r15)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movq %r14, (%rax,%rcx,8)
jmp 0x47a20
movq 0x110(%rsp), %r14
movq (%r14), %r9
testq %r9, %r9
movq 0x70(%rsp), %rbx
movq 0x68(%rsp), %r15
je 0x47ec4
cmpq $0x0, 0x28(%rsp)
je 0x47e19
movq 0x10(%r14), %r8
jmp 0x47e7d
leaq (%r9,%r9), %rax
movq 0x10(%r14), %r8
cmpq %rax, 0x8(%r14)
jae 0x47e7d
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r14)
leaq 0x8(,%rcx,8), %rsi
movq %r8, %rdi
callq 0x2370
movq %rax, %r8
movq %rax, 0x10(%r14)
movq (%r14), %r9
leaq (%r8,%r9,8), %rax
movq 0x108(%rsp), %rdi
movq (%rsp), %rsi
movq 0xe8(%rsp), %rdx
movq 0xf0(%rsp), %rcx
movsd 0x8(%rsp), %xmm0
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq %rcx
pushq %rbp
pushq 0x68(%rsp)
pushq %rax
callq 0x4758b
addq $0x30, %rsp
movq %rax, (%r14)
testq %r15, %r15
movq 0x78(%rsp), %r8
je 0x47f0a
movq 0x108(%rsp), %rax
movq (%rax), %rcx
xorl %eax, %eax
xorl %edx, %edx
movq 0xd0(%rsp), %r9
cmpq %rdx, %r8
je 0x47f07
movq (%rbx,%rdx,8), %rsi
imulq $0x1c, %rsi, %rdi
movl 0x8(%rcx,%rdi), %edi
cmpq %r9, %rdi
jb 0x47f02
movq %rsi, (%rbx,%rax,8)
incq %rax
incq %rdx
jmp 0x47ee5
movq %rax, %r8
movq %r8, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19eccd(%rip), %rdi    # 0x1e6bf3
leaq 0x19b776(%rip), %rsi    # 0x1e36a3
leaq 0x19ecd8(%rip), %rcx    # 0x1e6c0c
movl $0x571d, %edx           # imm = 0x571D
callq 0x2110