// File: /workspace/github_cpp_makefile_star2_O2/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, 0x8(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl 0x18(%rdx), %eax
cmpl %esi, %eax
jne 0x1bbc63
movq %r8, %r14
movq %rdx, %rbx
movq 0x1d0(%rdi), %rax
movq %rax, 0x18(%rsp)
movl %esi, %eax
movq %rax, 0x10(%rsp)
movl 0x1c(%rdx), %ecx
imulq $0x28, %rcx, %rdx
xorl %eax, %eax
cmpq %rax, %rdx
je 0x1bb974
movq (%rbx), %rdi
testb $0x40, 0x24(%rdi,%rax)
jne 0x1bb916
testq %rax, %rax
je 0x1bb910
movl (%rdi,%rax), %esi
cmpl -0x28(%rdi,%rax), %esi
jb 0x1bb916
addq $0x28, %rax
jmp 0x1bb8f3
movq 0x8(%rbx), %rax
andl $0x0, 0x1c(%rbx)
xorl %ecx, %ecx
movq %rdi, %rdx
movq %rax, %rsi
subq $0x1, %rsi
jb 0x1bb943
testb $0x40, 0x24(%rdx)
jne 0x1bb939
incl %ecx
movl %ecx, 0x1c(%rbx)
jmp 0x1bb93d
orb $-0x80, 0x3(%rdx)
addq $0x28, %rdx
jmp 0x1bb926
imulq $0x28, %rax, %rsi
addq %rdi, %rsi
callq 0x1a4565
movq 0x8(%rbx), %rax
xorl %ecx, %ecx
subq $0x1, %rax
jb 0x1bb9a3
movq (%rbx), %rdx
movl (%rdx,%rcx), %esi
testl %esi, %esi
jns 0x1bb96e
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl %esi, (%rdx,%rcx)
addq $0x28, %rcx
jmp 0x1bb955
movq 0x8(%rbx), %rax
movq %rcx, %rsi
cmpq %rax, %rsi
jae 0x1bb9a3
movq (%rbx), %rdi
testb $0x40, 0x24(%rdi,%rdx)
je 0x1bb91a
cmpq %rcx, %rsi
jbe 0x1bb99a
movl (%rdi,%rdx), %r8d
cmpl -0x28(%rdi,%rdx), %r8d
jb 0x1bb91a
incq %rsi
addq $0x28, %rdx
jmp 0x1bb97b
andq $0x0, 0x90(%r14)
xorl %r12d, %r12d
xorl %r13d, %r13d
movl 0x1c(%rbx), %eax
cmpq %rax, %r13
jae 0x1bba85
movq 0x90(%r14), %rdx
movq 0xa0(%r14), %rax
cmpq 0x98(%r14), %rdx
jne 0x1bba03
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x98(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0xa0(%r14)
movq 0x90(%r14), %rdx
movq (%rbx), %rcx
movl 0x14(%rcx,%r12), %esi
addl %esi, %esi
leaq 0x1(%rdx), %rdi
movq %rdi, 0x90(%r14)
movq %rsi, (%rax,%rdx,8)
movq 0x90(%r14), %rdx
cmpq 0x98(%r14), %rdx
jne 0x1bba5e
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x98(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0xa0(%r14)
movq (%rbx), %rcx
movq 0x90(%r14), %rdx
movl 0x18(%rcx,%r12), %ecx
leal 0x1(,%rcx,2), %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x90(%r14)
movq %rcx, (%rax,%rdx,8)
incq %r13
addq $0x28, %r12
jmp 0x1bb9b1
movq 0x90(%r14), %rax
movq 0xa0(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movq 0x90(%r14), %r12
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
cmpq %r13, %r12
je 0x1bbb76
movq 0xa0(%r14), %rax
movq (%rax,%r13,8), %r15
movl %r15d, %ebp
andl $0x1, %ebp
negq %rbp
orq $0x1, %rbp
addq %rcx, %rbp
cmpq %rdi, %rcx
setge %dl
cmpq %rdi, %rbp
setl %sil
orb %dl, %sil
je 0x1bbb65
cmpq %rdi, %rbp
setl %dl
cmpq %rdi, %rcx
setge %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x1bbb6b
movq 0x90(%r14), %rcx
cmpq 0x98(%r14), %rcx
jne 0x1bbb47
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x98(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r8, 0x20(%rsp)
callq 0x2370
movq 0x20(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %rax, 0xa0(%r14)
movq 0x90(%r14), %rcx
shrq %r15
movq %r8, %rdx
shlq $0x20, %rdx
orq %r15, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x90(%r14)
movq %rdx, (%rax,%rcx,8)
jmp 0x1bbb6b
shrq %r15
movq %r15, %r8
incq %r13
movq %rbp, %rcx
jmp 0x1bbab0
movq 0x90(%r14), %rax
cmpq %r12, %rax
jne 0x1bbbc9
orb $-0x80, 0x1b(%rbx)
pushq $0x24
popq %rax
xorl %ecx, %ecx
movl 0x1c(%rbx), %edx
cmpq %rdx, %rcx
jae 0x1bbba3
movq (%rbx), %rdx
orb $0x40, (%rdx,%rax)
incq %rcx
addq $0x28, %rax
jmp 0x1bbb8b
testq %rdx, %rdx
je 0x1bbbc0
movq 0x8(%rbx), %rax
cmpq %rdx, %rax
jbe 0x1bbbc0
movq (%rbx), %rdi
imulq $0x28, %rax, %rsi
addq %rdi, %rsi
callq 0x1a4565
andl $0x0, 0x1c(%rbx)
jmp 0x1bbc54
movq 0x10(%rsp), %rdx
shlq $0x5, %rdx
movq 0x18(%rsp), %rcx
leaq (%rcx,%rdx), %rdi
addq $0x18, %rdi
xorl %ecx, %ecx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
pushq $0x1
popq %r9
xorl %esi, %esi
xorl %edx, %edx
cmpq %rax, %r12
jae 0x1bbc26
movq 0xa0(%r14), %r10
movq (%r10,%r12,8), %r10
movq %r10, %r11
shrq $0x20, %r11
movl (%rdi), %ebp
andl %r8d, %ebp
cmpl %r10d, %ebp
cmovel %r9d, %esi
cmovnel %r9d, %edx
testq %r11, %r11
cmovel %r9d, %ecx
cmovnel %r9d, %edx
incq %r12
jmp 0x1bbbef
movl %ecx, %eax
orl %esi, %eax
setne %al
testl %ecx, %ecx
sete %cl
testl %esi, %esi
sete %sil
orb %cl, %sil
testl %edx, %edx
sete %cl
orb %sil, %cl
cmpb $0x1, %cl
jne 0x1bbb82
testb %al, %al
je 0x1bbb82
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3daf2(%rip), %rdi     # 0x1f975c
leaq 0x3b868(%rip), %rsi     # 0x1f74d9
leaq 0x3daf1(%rip), %rcx     # 0x1f9769
movl $0x1b69, %edx           # imm = 0x1B69
callq 0x2110