// File: /workspace/github_cpp_makefile_star2_O3/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 0x20809e
movq %r8, %r14
movq %rdx, %rbx
movl %esi, %r15d
movq 0x1d0(%rdi), %rax
movq %rax, 0x18(%rsp)
movl 0x1c(%rdx), %ecx
testq %rcx, %rcx
je 0x207d12
movq (%rbx), %rdi
leaq (,%rcx,8), %rax
leaq (%rax,%rax,4), %rax
xorl %edx, %edx
testb $0x40, 0x24(%rdi,%rdx)
jne 0x207d4f
testq %rdx, %rdx
je 0x207d09
movl (%rdi,%rdx), %esi
cmpl -0x28(%rdi,%rdx), %esi
jb 0x207d4f
addq $0x28, %rdx
cmpq %rdx, %rax
jne 0x207cf4
movq 0x8(%rbx), %rax
cmpq %rcx, %rax
jbe 0x207db1
movq (%rbx), %rdi
leaq (%rcx,%rcx,4), %rdx
leaq (%rdi,%rdx,8), %rdx
movq %rcx, %rsi
testb $0x40, 0x24(%rdx)
je 0x207d53
cmpq %rcx, %rsi
jbe 0x207d41
movl (%rdx), %r8d
cmpl -0x28(%rdx), %r8d
jb 0x207d53
incq %rsi
addq $0x28, %rdx
cmpq %rsi, %rax
jne 0x207d2d
jmp 0x207db1
movq 0x8(%rbx), %rax
movl $0x0, 0x1c(%rbx)
testq %rax, %rax
je 0x207d81
xorl %ecx, %ecx
movq %rdi, %rdx
movq %rax, %rsi
testb $0x40, 0x24(%rdx)
jne 0x207d74
incl %ecx
movl %ecx, 0x1c(%rbx)
jmp 0x207d78
orb $-0x80, 0x3(%rdx)
addq $0x28, %rdx
decq %rsi
jne 0x207d67
leaq (%rax,%rax,4), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1eed55
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x207db1
movq (%rbx), %rcx
movl (%rcx), %edx
testl %edx, %edx
jns 0x207da8
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %edx, (%rcx)
addq $0x28, %rcx
decq %rax
jne 0x207d9a
movq $0x0, 0x90(%r14)
cmpl $0x0, 0x1c(%rbx)
je 0x207ea3
movq 0xa0(%r14), %rdi
xorl %r12d, %r12d
movl $0x2, %r13d
xorl %ebp, %ebp
movq 0x90(%r14), %rcx
cmpq 0x98(%r14), %rcx
jne 0x207e14
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r13, %rsi
movq %rsi, 0x98(%r14)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0xa0(%r14)
movq 0x90(%r14), %rcx
movq (%rbx), %rax
movl 0x14(%rax,%r12), %edx
addl %edx, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x90(%r14)
movq %rdx, (%rdi,%rcx,8)
movq 0x90(%r14), %rcx
cmpq 0x98(%r14), %rcx
jne 0x207e6c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r13, %rsi
movq %rsi, 0x98(%r14)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0xa0(%r14)
movq (%rbx), %rax
movq 0x90(%r14), %rcx
movl 0x18(%rax,%r12), %eax
leal 0x1(,%rax,2), %eax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x90(%r14)
movq %rax, (%rdi,%rcx,8)
incq %rbp
movl 0x1c(%rbx), %eax
addq $0x28, %r12
cmpq %rax, %rbp
jb 0x207dd8
movq 0x90(%r14), %rax
jmp 0x207eac
movq 0xa0(%r14), %rdi
xorl %eax, %eax
leaq (%rdi,%rax,8), %rsi
callq 0x1ee586
movq 0x90(%r14), %r13
testq %r13, %r13
je 0x207fb3
movl %r15d, %eax
movq %rax, 0x10(%rsp)
movq 0xa0(%r14), %rax
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %ebp, %ebp
movq 0x8(%rsp), %rdi
movq %rbx, 0x20(%rsp)
movq %rbp, %rcx
movq (%rax,%r12,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 0x207f95
cmpq %rdi, %rbp
setl %dl
cmpq %rdi, %rcx
setge %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x207f9b
movq 0x90(%r14), %rcx
cmpq 0x98(%r14), %rcx
jne 0x207f77
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x98(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r8, %rbx
callq 0x2370
movq %rbx, %r8
movq 0x20(%rsp), %rbx
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 0x207f9b
shrq %r15
movq %r15, %r8
incq %r12
cmpq %r12, %r13
jne 0x207ee6
movq 0x90(%r14), %rdx
cmpq %r13, %rdx
jne 0x20800e
orb $-0x80, 0x1b(%rbx)
cmpl $0x0, 0x1c(%rbx)
je 0x207ff8
movl $0x24, %eax
xorl %ecx, %ecx
movq (%rbx), %rdx
orb $0x40, (%rdx,%rax)
incq %rcx
movl 0x1c(%rbx), %edx
addq $0x28, %rax
cmpq %rdx, %rcx
jb 0x207fc4
testq %rdx, %rdx
je 0x207ff8
movq 0x8(%rbx), %rax
cmpq %rdx, %rax
jbe 0x207ff8
movq (%rbx), %rdi
leaq (%rax,%rax,4), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1eed55
movl $0x0, 0x1c(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jbe 0x20806c
movq 0xa0(%r14), %rsi
movq 0x10(%rsp), %rcx
shlq $0x5, %rcx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rax
andl 0x18(%rax,%rcx), %edi
xorl %r8d, %r8d
movl $0x1, %r9d
xorl %eax, %eax
xorl %ecx, %ecx
movq (%rsi,%r13,8), %r10
movq %r10, %r11
shrq $0x20, %r11
cmpl %r10d, %edi
cmovel %r9d, %eax
cmovnel %r9d, %r8d
testq %r11, %r11
cmovel %r9d, %ecx
cmovnel %r9d, %r8d
incq %r13
cmpq %r13, %rdx
jne 0x20803b
testl %r8d, %r8d
sete %dl
jmp 0x208072
movb $0x1, %dl
xorl %ecx, %ecx
xorl %eax, %eax
movl %ecx, %esi
orl %eax, %esi
setne %sil
testl %ecx, %ecx
sete %cl
testl %eax, %eax
sete %al
orb %cl, %al
orb %dl, %al
testb $0x1, %al
je 0x207fb3
testb %sil, %sil
jne 0x207fff
jmp 0x207fb3
leaq 0x3f6f8(%rip), %rdi     # 0x24779d
leaq 0x3d55d(%rip), %rsi     # 0x245609
leaq 0x3f6f7(%rip), %rcx     # 0x2477aa
movl $0x1b69, %edx           # imm = 0x1B69
callq 0x2110