// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rax
movq %rsi, %rbx
movq 0x40(%rsp), %r14
movq $0x0, (%r14)
cmpq $0x0, 0x30(%rsi)
je 0x17b5f8
movq $0x0, 0x30(%rbx)
jmp 0x17b7e9
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, %r13
testq %r8, %r8
movq %r9, (%rsp)
je 0x17b6d0
movq 0x8(%rbx), %rax
movq 0x20(%rbx), %rcx
cmpq (%rax), %rcx
jae 0x17b6c1
movq (%rbx), %rdx
movq 0x10(%rax), %rsi
testq %r9, %r9
sete %r8b
cmpq %r15, %r12
setne %dil
orb %r8b, %dil
movq %rcx, %r8
movq (%rsi,%rcx,8), %r9
leaq (%r9,%r9,8), %r10
leaq (%r10,%r10,2), %r11
addq %r9, %r11
movl (%rdx,%r11), %ebp
movl 0xc(%rdx,%r11), %r10d
imulq $0xa8, %r10, %r10
movq 0x58(%r13,%r10), %r14
shlq $0x5, %rbp
movslq (%r14,%rbp), %r10
movl 0x18(%rdx,%r11), %r11d
andl $0x3fffffff, %r11d      # imm = 0x3FFFFFFF
addq %r11, %r10
movl 0x4(%r14,%rbp), %ebp
subl %r11d, %ebp
incl %ebp
movslq %ebp, %r11
cmpq %r12, %r10
movq %r12, %r14
cmovgq %r10, %r14
cmpq %r15, %r11
movq %r15, %rbp
cmovlq %r11, %rbp
cmpq %r14, %rbp
jg 0x17b6ac
cmpq %r12, %r10
setg %r10b
orb %dil, %r10b
jne 0x17b6b3
cmpq %r12, %r11
jl 0x17b6b3
movq %r9, (%rsi,%r8,8)
incq %r8
incq %rcx
cmpq (%rax), %rcx
jb 0x17b63a
jmp 0x17b6c4
movq %rcx, %r8
movq %r8, (%rax)
movq 0x40(%rsp), %r14
movq (%rsp), %r9
movq 0x10(%rbx), %rcx
cmpq 0x20(%rbx), %rcx
jge 0x17b7e9
testq %r9, %r9
sete %al
cmpq %r15, %r12
setne %bpl
orb %al, %bpl
testq %r9, %r9
setne %dl
movq (%rbx), %r11
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rdi
movl (%rdi,%rcx,8), %ecx
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %r10
addq %rcx, %r10
movl (%r11,%r10), %esi
movl 0xc(%r11,%r10), %ecx
imulq $0xa8, %rcx, %rcx
movq 0x58(%r13,%rcx), %r8
shlq $0x5, %rsi
movslq (%r8,%rsi), %rcx
movl 0x18(%r11,%r10), %r11d
andl $0x3fffffff, %r11d      # imm = 0x3FFFFFFF
addq %r11, %rcx
cmpq %r15, %rcx
setl %r10b
jg 0x17b7e9
orb %r10b, %dl
je 0x17b7e9
movl 0x4(%r8,%rsi), %edx
subl %r11d, %edx
incl %edx
movslq %edx, %rdx
cmpq %r12, %rcx
movq %r12, %rsi
cmovgq %rcx, %rsi
cmpq %r15, %rdx
movq %r15, %r8
cmovlq %rdx, %r8
cmpq %rsi, %r8
jg 0x17b785
cmpq %r12, %rcx
setg %cl
orb %bpl, %cl
jne 0x17b7d4
cmpq %r12, %rdx
jl 0x17b7d4
movq 0x8(%rax), %rcx
cmpq %rcx, (%rax)
jne 0x17b7b7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rax)
shlq $0x3, %rsi
callq 0x2370
movq (%rsp), %r9
movq 0x8(%rbx), %rcx
movq %rax, 0x10(%rcx)
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
movq (%rax), %rdx
movq 0x10(%rax), %rsi
movl (%rsi,%rcx,8), %ecx
leaq 0x1(%rdx), %rdi
movq %rdi, (%rax)
movq %rcx, (%rsi,%rdx,8)
movq 0x10(%rbx), %rcx
incq %rcx
movq %rcx, 0x10(%rbx)
cmpq 0x20(%rbx), %rcx
jl 0x17b6ee
movq 0x8(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq (,%rcx,8), %rdx
addq 0x10(%rax), %rdx
movq %rdx, (%r14)
movq 0x8(%rbx), %rax
movq (%rax), %rax
subq %rcx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq