// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: clip_integer_chimeric(ul_resolve_t*, unsigned int, ul2ul_item_t*, ul2ul_idx_t*, integer_t*, long)
void clip_integer_chimeric(ul_resolve_t *uidx, uint32_t qid, ul2ul_item_t *o, ul2ul_idx_t *ul2, integer_t *buf, int64_t min_dp)
{
    uint64_t k, is_srt = 0, is_del = 0/**, rid, zs, ze**/; assert(o->id == qid);
    ul_str_t *str = &(uidx->pstr.str.a[qid]); ///int64_t z, z_n;
    for (k = 0; k < o->cn; k++) {
        if(o->a[k].is_del) break;
        if(k > 0 && o->a[k].hid < o->a[k-1].hid) break;
    }
    if(k < o->cn) {
        is_srt = 1;
    } else {
        for (; k < o->n; k++) {
            if(!(o->a[k].is_del)) break;
            if(k > o->cn && o->a[k].hid < o->a[k-1].hid) break;
        }
        if(k < o->n) is_srt = 1;
    }

    if(is_srt) {
        for (k = o->cn = 0; k < o->n; k++) {
            if(o->a[k].is_del) o->a[k].hid |= ((uint32_t)(0x80000000));
            else o->cn++;
        }

        radix_sort_ul2ul_srt(o->a, o->a + o->n);

        for (k = 0; k < o->n; k++) {
            if(o->a[k].hid&((uint32_t)(0x80000000))) {
                o->a[k].hid -= ((uint32_t)(0x80000000));
            }
        }
    }

    for (k = 0, buf->u.n = 0; k < o->cn; k++) {
        // kv_push(uint64_t, buf->u, (o->a[k].qs<<1));
        kv_push(uint64_t, buf->u, (o->a[k].qs_k<<1));
        // kv_push(uint64_t, buf->u, (o->a[k].qe<<1)|1);
        kv_push(uint64_t, buf->u, (o->a[k].qe_k<<1)|1);
    }
    radix_sort_srt64(buf->u.a, buf->u.a + buf->u.n);

    int64_t dp, old_dp; uint64_t start, end, b_n = buf->u.n;
    for (k = 0, dp = 0, start = 0; k < b_n; ++k) {
        old_dp = dp;
        ///if a[j] is qe
        if (buf->u.a[k]&1) --dp;
        else ++dp;

        if (old_dp < min_dp && dp >= min_dp) {///old_dp < dp, b.a[j] is qs
            start = buf->u.a[k]>>1;
        } else if (old_dp >= min_dp && dp < min_dp) {///old_dp > min_dp, b.a[j] is qe
            end = buf->u.a[k]>>1;
            kv_push(uint64_t, buf->u, ((start<<32)|(end)));
        }
    }

    is_del = 0;
    if(buf->u.n == b_n) {
        is_del = 1;
    } else {
        uint32_t is_left = 0, is_right = 0, is_middle = 0;
        for (k = b_n; k < buf->u.n; ++k) {
            start = buf->u.a[k]>>32; end = (uint32_t)buf->u.a[k];
            if(start == 0) is_left = 1;
            else is_middle = 1;
            if(end == str->cn/**uidx->idx->a[qid].rlen**/) is_right = 1;
            else is_middle = 1;
        }
        if(is_left == 0 && is_right == 0) is_del = 1;
        if(is_left && is_right && is_middle) is_del = 1;
    }

    // if(is_del && str->cn > 1 && o->is_consist && dd_path_connect(uidx->l1_ug->g, str)) {
    //     is_del = 0;
    // }

    // fprintf(stderr, "[M::%s] qid::%u, o->is_consist::%u, str->cn::%u, is_connect::%lu\n", 
    //     __func__, qid, o->is_consist, str->cn, dd_path_connect(uidx->l1_ug->g, str));

    /**
    if(!is_del) {
        for (k = 0; k < str->cn; k++) {
            rid = (((uint32_t)str->a[k])>>1);
            if(!IF_HOM(rid, *(uidx->bub))) break;
        }

        if(k < str->cn) {///at least a het node covered
            for (k = 0, buf->u.n = 0; k < o->cn; k++) {
                z = o->a[k].qs_k; z_n = o->a[k].qe_k;
                for (; z < z_n; z++) {
                    rid = (((uint32_t)str->a[z])>>1);
                    if(!IF_HOM(rid, *(uidx->bub))) break;
                }
                if(z >= z_n) continue;
                zs = z;

                for (z = z_n - 1; z >= (int64_t)zs; z--) {
                    rid = (((uint32_t)str->a[z])>>1);
                    if(!IF_HOM(rid, *(uidx->bub))) break;
                }
                ze = z + 1;
                assert(zs < ze);

                kv_push(uint64_t, buf->u, (zs<<1));
                kv_push(uint64_t, buf->u, (ze<<1)|1);
                
            }
            radix_sort_srt64(buf->u.a, buf->u.a + buf->u.n);

            b_n = buf->u.n;
            for (k = 0, dp = 0, start = 0; k < b_n; ++k) {
                old_dp = dp;
                ///if a[j] is qe
                if (buf->u.a[k]&1) --dp;
                else ++dp;

                if (old_dp < min_dp && dp >= min_dp) {///old_dp < dp, b.a[j] is qs
                    start = buf->u.a[k]>>1;
                } else if (old_dp >= min_dp && dp < min_dp) {///old_dp > min_dp, b.a[j] is qe
                    end = buf->u.a[k]>>1;
                    kv_push(uint64_t, buf->u, ((start<<32)|(end)));
                }
            }

            is_del = 0;
            if(buf->u.n == b_n) {
                is_del = 1;
            } else {
                uint32_t is_left = 0, is_right = 0, is_middle = 0;
                for (k = b_n; k < buf->u.n; ++k) {
                    start = buf->u.a[k]>>32; end = (uint32_t)buf->u.a[k];
                    if(start == 0) is_left = 1;
                    else is_middle = 1;
                    if(end == str->cn) is_right = 1;
                    else is_middle = 1;
                }
                if(is_left == 0 && is_right == 0) is_del = 1;
                if(is_left && is_right && is_middle) is_del = 1;
            }
        }
    }
    **/

    if(is_del) {
        o->is_del = 1;
        for (k = 0; k < o->cn; k++) o->a[k].is_del = 1;
        if(o->cn && o->n > o->cn) radix_sort_ul2ul_srt(o->a, o->a + o->n);
        o->cn = 0;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, 0x10(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl 0x18(%rdx), %eax
cmpl %esi, %eax
jne 0x1fc905
movq %r8, %r14
movq %rdx, %rbx
movq 0x1d0(%rdi), %r10
movl 0x1c(%rdx), %eax
testq %rax, %rax
setne %dl
movl %esi, 0x4(%rsp)
movq %r10, 0x18(%rsp)
je 0x1fc536
movq (%rbx), %rsi
testb $0x40, 0x24(%rsi)
jne 0x1fc53a
leaq -0x1(%rax), %rdi
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x1fc51c
movl (%rsi), %r8d
cmpl -0x28(%rsi), %r8d
jb 0x1fc53c
cmpq %rcx, %rdi
je 0x1fc547
incq %rcx
cmpq %rax, %rcx
setb %dl
testb $0x40, 0x4c(%rsi)
leaq 0x28(%rsi), %rsi
je 0x1fc50e
jmp 0x1fc53c
xorl %ecx, %ecx
jmp 0x1fc54a
xorl %ecx, %ecx
leaq 0x1c(%rbx), %r15
testb $0x1, %dl
je 0x1fc54e
jmp 0x1fc5a4
movq %rax, %rcx
leaq 0x1c(%rbx), %r15
movq 0x8(%rbx), %rdx
cmpq %rdx, %rcx
setb %sil
jae 0x1fc605
movq (%rbx), %rdi
leaq (%rcx,%rcx,4), %r8
testb $0x40, 0x24(%rdi,%r8,8)
je 0x1fc59e
leaq (%rdi,%r8,8), %r8
leaq -0x1(%rdx), %rdi
cmpq %rax, %rcx
jbe 0x1fc584
movl (%r8), %r9d
cmpl -0x28(%r8), %r9d
jb 0x1fc59e
cmpq %rcx, %rdi
je 0x1fc605
incq %rcx
cmpq %rdx, %rcx
setb %sil
testb $0x40, 0x4c(%r8)
leaq 0x28(%r8), %r8
jne 0x1fc576
testb $0x1, %sil
je 0x1fc605
movl $0x0, 0x1c(%rbx)
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1fc5d2
movq (%rbx), %rcx
movq %rax, %rdx
testb $0x40, 0x24(%rcx)
jne 0x1fc5c5
incl (%r15)
jmp 0x1fc5c9
orb $-0x80, 0x3(%rcx)
addq $0x28, %rcx
decq %rdx
jne 0x1fc5ba
movq (%rbx), %rdi
leaq (%rax,%rax,4), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e433f
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1fc605
movq (%rbx), %rcx
movl (%rcx), %edx
testl %edx, %edx
jns 0x1fc5fc
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %edx, (%rcx)
addq $0x28, %rcx
decq %rax
jne 0x1fc5ee
movq $0x0, 0x90(%r14)
movq %r15, 0x8(%rsp)
cmpl $0x0, (%r15)
je 0x1fc6fc
xorl %r12d, %r12d
movl $0x2, %r15d
xorl %r13d, %r13d
movq 0x98(%r14), %rax
cmpq %rax, 0x90(%r14)
jne 0x1fc664
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r15, %rsi
movq %rsi, 0x98(%r14)
movq 0xa0(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0xa0(%r14)
movq (%rbx), %rax
movl 0x14(%rax,%r12), %eax
addl %eax, %eax
movq 0x90(%r14), %rcx
movq 0xa0(%r14), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x90(%r14)
movq %rax, (%rdi,%rcx,8)
movq 0x98(%r14), %rax
cmpq %rax, 0x90(%r14)
jne 0x1fc6bd
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r15, %rsi
movq %rsi, 0x98(%r14)
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0xa0(%r14)
movq (%rbx), %rax
movl 0x18(%rax,%r12), %eax
leal 0x1(,%rax,2), %eax
movq 0x90(%r14), %rcx
movq 0xa0(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x90(%r14)
movq %rax, (%rdx,%rcx,8)
incq %r13
movl 0x1c(%rbx), %eax
addq $0x28, %r12
cmpq %rax, %r13
jb 0x1fc62b
movq %rbx, 0x20(%rsp)
movq 0x90(%r14), %rax
movq 0xa0(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
movq 0x90(%r14), %r13
testq %r13, %r13
movq 0x10(%rsp), %r8
je 0x1fc7ef
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %r15, %rax
movq 0xa0(%r14), %rdi
movq (%rdi,%rbp,8), %r12
movl %r12d, %r15d
andl $0x1, %r15d
negq %r15
orq $0x1, %r15
addq %rax, %r15
cmpq %r8, %rax
setge %cl
cmpq %r8, %r15
setl %dl
orb %cl, %dl
je 0x1fc7dd
cmpq %r8, %r15
setl %cl
cmpq %r8, %rax
setge %al
andb %cl, %al
cmpb $0x1, %al
jne 0x1fc7e3
movq 0x98(%r14), %rax
cmpq %rax, 0x90(%r14)
jne 0x1fc7b1
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x98(%r14)
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r8
movq %rax, 0xa0(%r14)
shrq %r12
movq %rbx, %rax
shlq $0x20, %rax
orq %r12, %rax
movq 0x90(%r14), %rcx
movq 0xa0(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x90(%r14)
movq %rax, (%rdx,%rcx,8)
jmp 0x1fc7e3
shrq %r12
movq %r12, %rbx
incq %rbp
cmpq %rbp, %r13
jne 0x1fc734
movq 0x90(%r14), %rax
cmpq %r13, %rax
movq 0x20(%rsp), %rbx
jne 0x1fc83a
orb $-0x80, 0x1b(%rbx)
movl 0x1c(%rbx), %eax
testq %rax, %rax
je 0x1fc89b
movl $0x24, %ecx
xorl %edx, %edx
movq 0x8(%rsp), %r14
movq (%rbx), %rax
orb $0x40, (%rax,%rcx)
incq %rdx
movl 0x1c(%rbx), %eax
addq $0x28, %rcx
cmpq %rax, %rdx
jb 0x1fc81c
testq %rax, %rax
sete %cl
jmp 0x1fc8a2
jbe 0x1fc8c8
movl 0x4(%rsp), %ecx
movq 0xa0(%r14), %rsi
shlq $0x5, %rcx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rdx
andl 0x18(%rdx,%rcx), %edi
xorl %r8d, %r8d
movl $0x1, %r9d
xorl %ecx, %ecx
xorl %edx, %edx
movq (%rsi,%r13,8), %r10
movq %r10, %r11
shrq $0x20, %r11
cmpl %r10d, %edi
cmovel %r9d, %ecx
cmovnel %r9d, %r8d
testq %r11, %r11
cmovel %r9d, %edx
cmovnel %r9d, %r8d
incq %r13
cmpq %r13, %rax
jne 0x1fc86a
testl %r8d, %r8d
sete %al
jmp 0x1fc8ce
movb $0x1, %cl
movq 0x8(%rsp), %r14
testb %cl, %cl
jne 0x1fc8bf
movq 0x8(%rbx), %rcx
cmpq %rax, %rcx
jbe 0x1fc8bf
movq (%rbx), %rdi
leaq (%rcx,%rcx,4), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e433f
movl $0x0, (%r14)
jmp 0x1fc8f6
movb $0x1, %al
xorl %edx, %edx
xorl %ecx, %ecx
movl %edx, %esi
orl %ecx, %esi
setne %sil
testl %edx, %edx
sete %dl
testl %ecx, %ecx
sete %cl
orb %dl, %cl
orb %al, %cl
testb $0x1, %cl
je 0x1fc800
testb %sil, %sil
je 0x1fc800
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3ebad(%rip), %rdi     # 0x23b4b9
leaq 0x3ca12(%rip), %rsi     # 0x239325
leaq 0x3ebac(%rip), %rcx     # 0x23b4c6
movl $0x1b69, %edx           # imm = 0x1B69
callq 0x2110