// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl -0xc(%rbp), %eax
jne 0x339c65
jmp 0x339c84
leaq 0x5e821(%rip), %rdi     # 0x39848d
leaq 0x5c546(%rip), %rsi     # 0x3961b9
movl $0x1b69, %edx           # imm = 0x1B69
leaq 0x5e81b(%rip), %rcx     # 0x39849a
callq 0x2130
movq -0x8(%rbp), %rax
movq 0x1d0(%rax), %rax
movl -0xc(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x1c(%rcx), %ecx
cmpq %rcx, %rax
jae 0x339d16
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movb 0x24(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
je 0x339cd2
jmp 0x339d16
cmpq $0x0, -0x38(%rbp)
jbe 0x339d06
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jae 0x339d06
jmp 0x339d16
jmp 0x339d08
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x339ca5
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x1c(%rcx), %ecx
cmpq %rcx, %rax
jae 0x339d33
movq $0x1, -0x40(%rbp)
jmp 0x339dc5
jmp 0x339d35
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x339dad
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movb 0x24(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
jne 0x339d60
jmp 0x339dad
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x1c(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x339d9d
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jae 0x339d9d
jmp 0x339dad
jmp 0x339d9f
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x339d35
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x339dc3
movq $0x1, -0x40(%rbp)
jmp 0x339dc5
cmpq $0x0, -0x40(%rbp)
je 0x339ebf
movq -0x18(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x339e44
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movb 0x24(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
je 0x339e27
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %ecx
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, (%rax)
jmp 0x339e34
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1c(%rax)
jmp 0x339e36
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x339de3
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
imulq $0x28, 0x8(%rax), %rax
addq %rax, %rsi
callq 0x30fc30
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x339ebd
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x339ead
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %ecx
subl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, (%rax)
jmp 0x339eaf
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x339e6b
jmp 0x339ebf
movq $0x0, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, 0x90(%rax)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x1c(%rcx), %ecx
cmpq %rcx, %rax
jae 0x33a098
jmp 0x339eec
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jne 0x339f76
movq -0x28(%rbp), %rax
cmpq $0x0, 0x98(%rax)
je 0x339f29
movq -0x28(%rbp), %rax
movq 0x98(%rax), %rax
shlq %rax
movq %rax, -0x90(%rbp)
jmp 0x339f37
movl $0x2, %eax
movq %rax, -0x90(%rbp)
jmp 0x339f37
movq -0x90(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x98(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
shll %eax
movl %eax, %eax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x90(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x90(%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x339fb8
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jne 0x33a042
movq -0x28(%rbp), %rax
cmpq $0x0, 0x98(%rax)
je 0x339ff5
movq -0x28(%rbp), %rax
movq 0x98(%rax), %rax
shlq %rax
movq %rax, -0x98(%rbp)
jmp 0x33a003
movl $0x2, %eax
movq %rax, -0x98(%rbp)
jmp 0x33a003
movq -0x98(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x98(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
shll %eax
orl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x90(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x90(%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x33a087
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x339ed6
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rsi
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x30ed90
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x78(%rbp), %rax
jae 0x33a277
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x33a12d
movq -0x58(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x33a139
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x33a16c
movq -0x58(%rbp), %rax
cmpq -0x30(%rbp), %rax
jl 0x33a16c
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq %rax
movq %rax, -0x68(%rbp)
jmp 0x33a264
movq -0x60(%rbp), %rax
cmpq -0x30(%rbp), %rax
jl 0x33a262
movq -0x58(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x33a262
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq %rax
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jne 0x33a22c
movq -0x28(%rbp), %rax
cmpq $0x0, 0x98(%rax)
je 0x33a1df
movq -0x28(%rbp), %rax
movq 0x98(%rax), %rax
shlq %rax
movq %rax, -0xa0(%rbp)
jmp 0x33a1ed
movl $0x2, %eax
movq %rax, -0xa0(%rbp)
jmp 0x33a1ed
movq -0xa0(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x98(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x68(%rbp), %rdx
shlq $0x20, %rdx
orq -0x70(%rbp), %rdx
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x28(%rbp), %rsi
movq 0x90(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x90(%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x33a262
jmp 0x33a264
jmp 0x33a266
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x33a0ec
movq $0x0, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq 0x90(%rax), %rax
cmpq -0x78(%rbp), %rax
jne 0x33a29d
movq $0x1, -0x48(%rbp)
jmp 0x33a391
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x90(%rcx), %rax
jae 0x33a35e
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x33a316
movl $0x1, -0x7c(%rbp)
jmp 0x33a320
movl $0x1, -0x84(%rbp)
movq -0x70(%rbp), %rax
movq -0x50(%rbp), %rcx
movl 0x18(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
cmpq %rcx, %rax
jne 0x33a341
movl $0x1, -0x80(%rbp)
jmp 0x33a34b
movl $0x1, -0x84(%rbp)
jmp 0x33a34d
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x33a2bd
cmpl $0x0, -0x7c(%rbp)
jne 0x33a372
cmpl $0x0, -0x80(%rbp)
jne 0x33a372
movq $0x1, -0x48(%rbp)
cmpl $0x0, -0x7c(%rbp)
je 0x33a38f
cmpl $0x0, -0x80(%rbp)
je 0x33a38f
cmpl $0x0, -0x84(%rbp)
je 0x33a38f
movq $0x1, -0x48(%rbp)
jmp 0x33a391
cmpq $0x0, -0x48(%rbp)
je 0x33a43b
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0x18(%rax)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x1c(%rcx), %ecx
cmpq %rcx, %rax
jae 0x33a3f3
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movb 0x24(%rax), %cl
andb $-0x41, %cl
orb $0x40, %cl
movb %cl, 0x24(%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x33a3ba
movq -0x18(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x33a430
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x1c(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x33a430
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
imulq $0x28, 0x8(%rax), %rax
addq %rax, %rsi
callq 0x30fc30
movq -0x18(%rbp), %rax
movl $0x0, 0x1c(%rax)
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)