// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: integer_gen_ovlp(ul_resolve_t*, integer_t*, unsigned int, ul2ul_item_t*, unsigned int)
void integer_gen_ovlp(ul_resolve_t *uidx, integer_t *buf, uint32_t qid, ul2ul_item_t *o, uint32_t ug_offset)
{
    ul_str_idx_t *str_idx = &(uidx->pstr); ma_ug_t *ug = uidx->l1_ug; ul2ul_t res;
    ul_str_t *str = &(str_idx->str.a[qid]); integer_aln_t *p; ul_chain_t sc;
    uint64_t k, z, *hid_a, hid_n, sck, b_n, m/**, ovn = 0**/; uint32_t vk, vz; uc_block_t *xi;
    o->n = o->cn = 0; o->id = qid; o->is_del = 0; ///o->is_consist = 0;
    // if(qid == 95 || qid == 36) print_integer_seq(ug, str_idx->str.a, qid, 1);
    if(str->cn < 2) return;
    for (k = 0, buf->b.n = 0; k < str->cn; k++) {
        xi = &(uidx->idx->a[qid].bb.a[str->a[k]>>32]);
        assert(((xi->hid<<1)+xi->rev)==((uint32_t)str->a[k]));
        sck = ug_occ_w(xi->ts, xi->te, &(ug->u.a[xi->hid]));

        vk = (uint32_t)str->a[k];
        hid_a = str_idx->occ.a + str_idx->idx.a[vk>>1];
        hid_n = str_idx->idx.a[(vk>>1)+1] - str_idx->idx.a[vk>>1];
        for (z = 0; z < hid_n; z++) {
            // if(qid == 95 || qid == 36) {
            //     fprintf(stderr,"[M::%s::qid->%u::tid->%lu] k->%lu, tid_occ->%u\n", 
            //                 __func__, qid, (hid_a[z]>>32), k, str_idx->str.a[hid_a[z]>>32].cn);
            // }
            if((hid_a[z]>>32) == qid) continue;
            if(str_idx->str.a[hid_a[z]>>32].cn < 2) continue;
            vz = (uint32_t)(str_idx->str.a[hid_a[z]>>32].a[(uint32_t)hid_a[z]]);
            assert((vk>>1) == (vz>>1));
            kv_pushp(integer_aln_t, buf->b, &p);
            p->vq = vk; p->tk = (uint32_t)hid_a[z]; 
            if((vk^vz)&1) p->tk = str_idx->str.a[hid_a[z]>>32].cn - p->tk - 1;///rev
            p->tn_rev_qk = (hid_a[z]>>32); p->tn_rev_qk <<= 1; p->tn_rev_qk |= ((vk^vz)&1);
            p->tn_rev_qk <<= 32; p->tn_rev_qk += k; 
            ///set score of this pair
            p->sc = sck;
            xi = &(uidx->idx->a[hid_a[z]>>32].bb.a[(str_idx->str.a[hid_a[z]>>32].a[(uint32_t)hid_a[z]])>>32]);
            assert(((xi->hid<<1)+xi->rev)==vz); m = ug_occ_w(xi->ts, xi->te, &(ug->u.a[xi->hid]));
            if(p->sc > m) p->sc = m;
        }
    }

    radix_sort_integer_aln_t_srt(buf->b.a, buf->b.a + buf->b.n); 

    // b_n = buf->b.n;
    // for (k = 1, z = ovn = 0; k <= b_n; k++) {
    //     if(k == b_n || (buf->b.a[z].tn_rev_qk>>33) != (buf->b.a[k].tn_rev_qk>>33)) {
    //         ovn++; z = k;
    //     }
    // }

    b_n = buf->b.n; buf->sc.n = 0;
    for (k = 1, z = 0; k <= b_n; k++) {
        if(k == b_n || (buf->b.a[z].tn_rev_qk>>32) != (buf->b.a[k].tn_rev_qk>>32)) {
            // if(qid == 95 || qid == 36) {
            //     fprintf(stderr,"[M::%s::qid->%u::tid->%lu]\n", __func__, qid, buf->b.a[z].tn_rev_qk>>33);
            // }
            if(integer_chain(qid, buf->b.a + z, k - z, z, buf, ug, str_idx, uidx->idx, &sc) && sc.v != (uint32_t)-1) {
                if((buf->sc.n > 0) && ((buf->sc.a[buf->sc.n-1].v>>1) == (sc.v>>1))) {
                    if(buf->sc.a[buf->sc.n-1].sc < sc.sc) {
                        buf->sc.a[buf->sc.n-1] = sc;
                    }
                } else {
                    kv_push(ul_chain_t, buf->sc, sc);
                }
            }
            z = k;
        }
    }

    for (k = o->n = 0; k < buf->sc.n; k++) {
        if(update_exact_ul_ovlps(uidx->idx, ug, str_idx->str.a, buf->b.a, &(buf->sc.a[k]), qid, buf, &res)) {                                                
            kv_push(ul2ul_t, *o, res);
        }
    }
    // if(str->cn > 0) {
    //     for (k = 0, buf->b.n = 0; k < str->cn; k++) {
    //         xi = &(uidx->idx->a[qid].bb.a[str->a[k]>>32]);

    //         // if(xi->ts)
    //     }
    //     // uint32_t v, nv; asg_arc_t *av; asg_t *g = ug->g; ul2ul_t *r;
    //     // v = ((uint32_t)str->a[0])^1;
    //     // nv = asg_arc_n(g, v); av = asg_arc_a(g, v);
    //     // for (k = 0; k < nv; k++) {
    //     //     if(av[k].del) continue;
    //     //     kv_pushp(ul2ul_t, *o, &r);
    //     //     r->hid = (av[k].v>>1) + ug_offset; 
    //     //     r->is_rev = (av[k].v^v)&1;
    //     //     r->qs = 0; r->qe = av[k].ol;

    //     // }
    //     // v = ((uint32_t)str->a[str->cn-1]);
    //     // nv = asg_arc_n(g, v); av = asg_arc_a(g, v);
    // }
    o->cn = o->n; 
    // if(ovn == o->cn) o->is_consist = 1;
    if(o->n <= 0) return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %rbp
movq 0x18(%rdi), %rax
movq %rax, 0x10(%rsp)
movq 0x1d0(%rdi), %rsi
movl %edx, %r14d
movq %r14, %rax
shlq $0x5, %rax
andl $0x0, 0x1c(%rcx)
andq $0x0, 0x8(%rcx)
movl %edx, 0x18(%rsp)
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %edx, 0x18(%rcx)
testl $0x7ffffffe, 0x18(%rsi,%rax) # imm = 0x7FFFFFFE
je 0x1ba9af
movq %rcx, 0x48(%rsp)
leaq 0x190(%rdi), %rcx
movq %rcx, 0x50(%rsp)
addq %rax, %rsi
andq $0x0, 0x30(%rbp)
xorl %r13d, %r13d
imulq $0x58, %r14, %rax
movq %rax, 0x60(%rsp)
xorl %edx, %edx
movq %rdi, 0x8(%rsp)
movq %rbp, 0x20(%rsp)
movq %rsi, 0x58(%rsp)
movl 0x18(%rsi), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %rdx
jae 0x1ba77d
movq 0x30(%rdi), %rax
movq 0x48(%rax), %rax
movq 0x60(%rsp), %rcx
movq 0x30(%rax,%rcx), %rax
movq 0x10(%rsi), %rcx
movq %rdx, 0x40(%rsp)
movq (%rcx,%rdx,8), %rdx
movq %rdx, %rcx
shrq $0x20, %rcx
imulq $0x24, %rcx, %rcx
movl (%rax,%rcx), %r8d
movl 0x20(%rax,%rcx), %esi
shrl $0x5, %esi
andl $0x1, %esi
movq %rsi, 0x38(%rsp)
leal (%rsi,%r8,2), %esi
movl %esi, 0x1c(%rsp)
cmpl %edx, %esi
jne 0x1ba9ff
movq %rdi, %rbx
addq %rcx, %rax
movl 0xc(%rax), %edi
movl 0x10(%rax), %esi
imulq $0x28, %r8, %rdx
movq 0x10(%rsp), %rax
addq 0x10(%rax), %rdx
movq %r8, %r15
callq 0x1b2312
movq %r15, %rsi
movq 0x1a0(%rbx), %rcx
movq 0x1b8(%rbx), %r12
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq (%rcx,%rsi,8), %r15
movq 0x8(%rcx,%rsi,8), %r8
movq %rax, 0x70(%rsp)
movl %eax, %eax
movq %rax, 0x68(%rsp)
movq %rbx, %rdi
movq %rsi, 0x30(%rsp)
movq %r8, 0x28(%rsp)
cmpq %r15, %r8
je 0x1ba76b
movq (%r12,%r15,8), %rax
movq %rax, %rdx
shrq $0x20, %rdx
cmpq %r14, %rdx
je 0x1ba763
movq 0x1d0(%rdi), %rcx
shlq $0x5, %rdx
testl $0x7ffffffe, 0x18(%rcx,%rdx) # imm = 0x7FFFFFFE
je 0x1ba763
movq %r14, %rbx
addq %rcx, %rdx
movq 0x10(%rdx), %rdx
movl %eax, %eax
movl (%rdx,%rax,8), %r14d
movl %r14d, %eax
shrl %eax
cmpl %eax, %esi
jne 0x1ba9c1
movq 0x40(%rbp), %rax
cmpq 0x38(%rbp), %r13
jne 0x1ba66e
leaq (,%r13,2), %rcx
testq %r13, %r13
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x38(%rbp)
imulq $0x18, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x40(%rbp)
movq 0x30(%rbp), %r13
movq 0x1d0(%rdi), %rcx
movq 0x38(%rsp), %r9
imulq $0x18, %r13, %rsi
incq %r13
movq %r13, 0x30(%rbp)
leaq (%rax,%rsi), %rbp
movl 0x1c(%rsp), %edx
movl %edx, 0x4(%rax,%rsi)
movq (%r12,%r15,8), %rdx
movl %edx, (%rax,%rsi)
movl %r14d, %eax
andl $0x1, %eax
cmpl %r9d, %eax
je 0x1ba6c1
movq %rdx, %rsi
shrq $0x1b, %rsi
andq $-0x20, %rsi
movl 0x18(%rcx,%rsi), %esi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %esi
movl %edx, %r8d
notl %r8d
addl %esi, %r8d
movl %r8d, (%rbp)
xorl %r9d, %eax
shrq $0x1f, %rdx
andl $-0x2, %edx
orl %eax, %edx
shlq $0x20, %rdx
addq 0x40(%rsp), %rdx
movq %rdx, 0x10(%rbp)
movq 0x70(%rsp), %rax
movl %eax, 0x8(%rbp)
movq 0x30(%rdi), %rax
movq 0x48(%rax), %rax
movq (%r12,%r15,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
imulq $0x58, %rdx, %rdi
movq 0x30(%rax,%rdi), %rax
shlq $0x5, %rdx
movq 0x10(%rcx,%rdx), %rcx
movl 0x4(%rcx,%rsi,8), %ecx
imulq $0x24, %rcx, %rdx
movl (%rax,%rdx), %ecx
movl 0x20(%rax,%rdx), %esi
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rcx,2), %esi
cmpl %r14d, %esi
jne 0x1ba9e0
addq %rdx, %rax
movl 0xc(%rax), %edi
movl 0x10(%rax), %esi
imulq $0x28, %rcx, %rdx
movq 0x10(%rsp), %rax
addq 0x10(%rax), %rdx
callq 0x1b2312
cmpq 0x68(%rsp), %rax
jae 0x1ba74c
movl %eax, 0x8(%rbp)
movq 0x20(%rsp), %rbp
movq 0x8(%rsp), %rdi
movq %rbx, %r14
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %r8
incq %r15
jmp 0x1ba5db
movq 0x40(%rsp), %rdx
incq %rdx
movq 0x58(%rsp), %rsi
jmp 0x1ba523
movq 0x40(%rbp), %rdi
imulq $0x18, %r13, %rsi
addq %rdi, %rsi
callq 0x1a47a8
movq 0x30(%rbp), %rbx
andq $0x0, 0xc0(%rbp)
pushq $0x1
popq %r15
pushq $0x28
popq %r12
xorl %ecx, %ecx
leaq 0x78(%rsp), %r13
cmpq %rbx, %r15
ja 0x1ba8d6
movq 0x40(%rbp), %rsi
je 0x1ba7ce
imulq $0x18, %rcx, %rax
movq (%rsi,%r12), %rdx
xorq 0x10(%rsi,%rax), %rdx
shrq $0x20, %rdx
je 0x1ba8ca
imulq $0x18, %rcx, %rax
addq %rax, %rsi
movq %r15, %rdx
subq %rcx, %rdx
subq $0x8, %rsp
movl 0x20(%rsp), %edi
movq %rbp, %r8
movq 0x18(%rsp), %r9
pushq %r13
movq 0x18(%rsp), %rax
pushq 0x30(%rax)
pushq 0x68(%rsp)
callq 0x1b379d
addq $0x20, %rsp
movl 0x80(%rsp), %edx
cmpl $-0x1, %edx
setne %sil
andb %al, %sil
movq %r15, %rcx
cmpb $0x1, %sil
jne 0x1ba8ca
movq 0xc0(%rbp), %rcx
movq 0xd0(%rbp), %rax
testq %rcx, %rcx
je 0x1ba85d
imulq $0x28, %rcx, %rsi
xorl -0x20(%rax,%rsi), %edx
cmpl $0x1, %edx
ja 0x1ba85d
addq %rsi, %rax
movq -0x18(%rax), %rdx
movq %r15, %rcx
cmpq 0x88(%rsp), %rdx
jae 0x1ba8ca
addq $-0x28, %rax
movq %rax, %rcx
jmp 0x1ba8a7
cmpq 0xc8(%rbp), %rcx
jne 0x1ba895
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0xc8(%rbp)
imulq $0x28, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0xd0(%rbp)
movq 0xc0(%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0xc0(%rbp)
imulq $0x28, %rcx, %rcx
addq %rax, %rcx
movq 0x98(%rsp), %rax
movq %rax, 0x20(%rcx)
movups 0x78(%rsp), %xmm0
movups 0x88(%rsp), %xmm1
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
movq %r15, %rcx
incq %r15
addq $0x18, %r12
jmp 0x1ba7a8
movq %r14, %r13
movq 0x48(%rsp), %r12
andq $0x0, 0x8(%r12)
xorl %ebx, %ebx
leaq 0xa0(%rsp), %r14
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
cmpq 0xc0(%rbp), %r15
jae 0x1ba9a5
movq 0x30(%rax), %rdi
movq 0x1d0(%rax), %rdx
movq 0x40(%rbp), %rcx
movq 0xd0(%rbp), %r8
addq %rbx, %r8
movq %r13, %r9
pushq %r14
pushq %rbp
callq 0x1b5a69
popq %rcx
popq %rdx
testq %rax, %rax
je 0x1ba999
movq (%r12), %rax
movq 0x8(%r12), %rcx
cmpq 0x10(%r12), %rcx
jne 0x1ba966
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r12)
imulq $0x28, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r12)
movq 0x8(%r12), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r12)
imulq $0x28, %rcx, %rcx
movq 0xc0(%rsp), %rdx
movq %rdx, 0x20(%rax,%rcx)
movups 0xa0(%rsp), %xmm0
movups 0xb0(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq %r15
addq $0x28, %rbx
jmp 0x1ba8f1
movl 0x8(%r12), %eax
movl %eax, 0x1c(%r12)
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3eb55(%rip), %rdi     # 0x1f951d
leaq 0x3cb0a(%rip), %rsi     # 0x1f74d9
leaq 0x3eb84(%rip), %rcx     # 0x1f955a
movl $0x19dc, %edx           # imm = 0x19DC
callq 0x2110
leaq 0x3eb49(%rip), %rdi     # 0x1f9530
leaq 0x3caeb(%rip), %rsi     # 0x1f74d9
leaq 0x3eb65(%rip), %rcx     # 0x1f955a
movl $0x19e5, %edx           # imm = 0x19E5
callq 0x2110
leaq 0x3ea91(%rip), %rdi     # 0x1f9497
leaq 0x3cacc(%rip), %rsi     # 0x1f74d9
leaq 0x3eb46(%rip), %rcx     # 0x1f955a
movl $0x19ce, %edx           # imm = 0x19CE
callq 0x2110