// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/ecovlp.cpp
// Function: iter_cc_idx_t(overlap_region*, cc_idx_t*, long, long, unsigned long, unsigned long, unsigned long**)
uint64_t iter_cc_idx_t(overlap_region* ol, cc_idx_t *z, int64_t s, int64_t e, uint64_t is_reduce, uint64_t is_insert, uint64_t **ra)
{
    int64_t rm_n, q[2], os, oe; ul_ov_t *cp; uint64_t m; *ra = NULL;

    // if(s == 15816 && e == 15819) {
    //     fprintf(stderr, "[M::%s] is_reduce::%lu\n", __func__, is_reduce);
    // }
    if(z->ru == 0) {
        if(is_reduce) {
            for (m = rm_n = z->srt_n; m < z->idx->n; m++) {
                cp = &(z->c_idx[z->idx->a[m]]);
                // if(s == 15816 && e == 15819) {
                //     fprintf(stderr, "-0-[M::%s] ii::%lu, ii0::%ld\n", __func__, z->idx->a[m], z->i0);
                // }
                
                q[0] = ol[ovlp_id(*cp)].w_list.a[ovlp_cur_wid(*cp)].x_start+ovlp_bd(*cp);
                q[1] = ol[ovlp_id(*cp)].w_list.a[ovlp_cur_wid(*cp)].x_end+1-ovlp_bd(*cp);
                os = MAX(q[0], s); oe = MIN(q[1], e);
                if((oe > os) || ((is_insert) && (s == e) && (s >= q[0]) && (s <= q[1]))) {
                    z->idx->a[rm_n++] = z->idx->a[m];
                }
            }
            z->idx->n = rm_n;
        }

        for (; z->i < z->srt_n; ++z->i) {
            cp = &(z->c_idx[(uint32_t)z->idx->a[z->i]]);
            // if(s == 15816 && e == 15819) {
            //     fprintf(stderr, "-1-[M::%s] ii::%u, ii0::%ld\n", __func__, (uint32_t)z->idx->a[z->i], z->i0);
            // }
            q[0] = ol[ovlp_id(*cp)].w_list.a[ovlp_cur_wid(*cp)].x_start+ovlp_bd(*cp);
            q[1] = ol[ovlp_id(*cp)].w_list.a[ovlp_cur_wid(*cp)].x_end+1-ovlp_bd(*cp);
            if(q[0] > e) break;
            if((!is_insert) && (q[0] >= e)) break;
            os = MAX(q[0], s); oe = MIN(q[1], e);
            if((oe > os) || ((is_insert) && (s == e) && (s >= q[0]) && (s <= q[1]))) {
                kv_push(uint64_t, *(z->idx), ((uint32_t)z->idx->a[z->i]));
            }
        }
    } else {
        z->ru = 0;
    }

    (*ra) = z->idx->a + z->srt_n; 
    return z->idx->n - z->srt_n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq 0x50(%rsp), %rax
movq $0x0, (%rax)
cmpq $0x0, 0x30(%rsi)
je 0x1822ba
movq $0x0, 0x30(%rbx)
movq 0x20(%rbx), %rsi
jmp 0x1824c1
movq %r9, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, %r13
testq %r8, %r8
movq %r9, 0x10(%rsp)
je 0x18239e
movq 0x8(%rbx), %rax
movq 0x20(%rbx), %rcx
movq (%rax), %rsi
cmpq %rsi, %rcx
jae 0x182393
movq (%rbx), %rdx
movq 0x10(%rax), %rdi
testq %r14, %r14
sete %r9b
cmpq %r15, %r12
setne %r8b
orb %r9b, %r8b
movb %r8b, 0xf(%rsp)
movq %rcx, %r9
movq (%rdi,%rcx,8), %r10
leaq (%r10,%r10,8), %r11
leaq (%r11,%r11,2), %rbp
addq %r10, %rbp
movl (%rdx,%rbp), %r14d
movl 0xc(%rdx,%rbp), %r11d
imulq $0xa8, %r11, %r11
movq 0x58(%r13,%r11), %r8
shlq $0x5, %r14
movslq (%r8,%r14), %r11
movl 0x18(%rdx,%rbp), %ebp
andl $0x3fffffff, %ebp       # imm = 0x3FFFFFFF
addq %rbp, %r11
movl 0x4(%r8,%r14), %r8d
subl %ebp, %r8d
incl %r8d
movslq %r8d, %rbp
cmpq %r12, %r11
movq %r12, %r8
cmovgq %r11, %r8
cmpq %r15, %rbp
movq %r15, %r14
cmovlq %rbp, %r14
cmpq %r8, %r14
jg 0x18237b
cmpq %r12, %r11
setg %r8b
orb 0xf(%rsp), %r8b
jne 0x182385
cmpq %r12, %rbp
jl 0x182385
movq %r10, (%rdi,%r9,8)
incq %r9
movq (%rax), %rsi
incq %rcx
cmpq %rsi, %rcx
jb 0x182308
jmp 0x182396
movq %rcx, %r9
movq %r9, (%rax)
movq 0x10(%rsp), %r14
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %rsi
cmpq %rsi, %rdx
jge 0x1824c1
testq %r14, %r14
sete %al
cmpq %r15, %r12
setne %cl
orb %al, %cl
movb %cl, 0xf(%rsp)
movq 0x8(%rbx), %rcx
testq %r14, %r14
setne %r8b
movq (%rbx), %r11
movq 0x10(%rcx), %rdi
movl (%rdi,%rdx,8), %eax
leaq (%rax,%rax,8), %r9
leaq (%r9,%r9,2), %r14
addq %rax, %r14
movl (%r11,%r14), %r9d
movl 0xc(%r11,%r14), %eax
imulq $0xa8, %rax, %rax
movq 0x58(%r13,%rax), %r10
shlq $0x5, %r9
movslq (%r10,%r9), %rax
movl 0x18(%r11,%r14), %r11d
andl $0x3fffffff, %r11d      # imm = 0x3FFFFFFF
addq %r11, %rax
cmpq %r15, %rax
setl %bpl
jg 0x1824c1
orb %bpl, %r8b
je 0x1824c1
movl 0x4(%r10,%r9), %r8d
subl %r11d, %r8d
incl %r8d
movslq %r8d, %r8
cmpq %r12, %rax
movq %r12, %r9
cmovgq %rax, %r9
cmpq %r15, %r8
movq %r15, %r10
cmovlq %r8, %r10
cmpq %r9, %r10
movq 0x10(%rsp), %r14
jg 0x182460
cmpq %r12, %rax
setg %al
orb 0xf(%rsp), %al
jne 0x1824b1
cmpq %r12, %r8
jl 0x1824b1
movq (%rcx), %rax
cmpq 0x8(%rcx), %rax
jne 0x182494
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rcx)
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rbx), %rcx
movq %rax, 0x10(%rcx)
movq 0x10(%rbx), %rdx
jmp 0x182498
movq 0x10(%rcx), %rax
movl (%rax,%rdx,8), %edx
movq (%rcx), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, (%rcx)
movq %rdx, (%rax,%rsi,8)
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %rsi
incq %rdx
movq %rdx, 0x10(%rbx)
cmpq %rsi, %rdx
jl 0x1823c5
movq 0x8(%rbx), %rax
leaq (,%rsi,8), %rcx
addq 0x10(%rax), %rcx
movq 0x50(%rsp), %rax
movq %rcx, (%rax)
movq 0x8(%rbx), %rax
movq (%rax), %rax
subq %rsi, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq