// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movq 0x18(%rdi), %rax
movq %rax, 0x20(%rsp)
movq 0x1d0(%rdi), %r8
movl %edx, %r15d
movq %r15, %rax
shlq $0x5, %rax
movl $0x0, 0x1c(%rcx)
movq $0x0, 0x8(%rcx)
movl %edx, 0x2c(%rsp)
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %edx, 0x18(%rcx)
testl $0x7ffffffe, 0x18(%r8,%rax) # imm = 0x7FFFFFFE
je 0x1fb4a5
movq %rcx, 0x30(%rsp)
addq %rax, %r8
movq %rsi, 0x8(%rsp)
movq $0x0, 0x30(%rsi)
testl $0x7fffffff, 0x18(%r8)  # imm = 0x7FFFFFFF
movq %rdi, 0x10(%rsp)
je 0x1fb1ff
xorl %edx, %edx
imulq $0x58, %r15, %rax
movq %rax, 0x40(%rsp)
movq %r8, 0x38(%rsp)
movq 0x30(%rdi), %rax
movq 0x48(%rax), %rax
movq 0x40(%rsp), %rcx
movq 0x30(%rax,%rcx), %rax
movq 0x10(%r8), %rcx
movq %rdx, 0x18(%rsp)
movq (%rcx,%rdx,8), %r8
movq %r8, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,8), %rdx
movl (%rax,%rdx,4), %ecx
movl 0x20(%rax,%rdx,4), %esi
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rcx,2), %esi
cmpl %r8d, %esi
jne 0x1fb4f5
movq 0x1a0(%rdi), %rsi
movq %r8, 0x58(%rsp)
shrl %r8d
movq (%rsi,%r8,8), %r12
movq %r8, 0x60(%rsp)
movq 0x8(%rsi,%r8,8), %rbp
cmpq %r12, %rbp
je 0x1fb1e0
leaq (%rax,%rdx,4), %rax
movq 0x1b8(%rdi), %rbx
movl 0xc(%rax), %edi
movl 0x10(%rax), %esi
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
movq 0x20(%rsp), %rax
addq 0x10(%rax), %rdx
callq 0x1f2d50
movq 0x10(%rsp), %rdi
movq %rax, 0x50(%rsp)
movl %eax, %eax
movq %rax, 0x48(%rsp)
movq (%rbx,%r12,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
cmpq %r15, %rcx
je 0x1fb1d4
movq 0x1d0(%rdi), %rdx
shlq $0x5, %rcx
testl $0x7ffffffe, 0x18(%rdx,%rcx) # imm = 0x7FFFFFFE
je 0x1fb1d4
movq %r15, %r14
addq %rcx, %rdx
movq 0x10(%rdx), %rcx
movl %eax, %eax
movl (%rcx,%rax,8), %r15d
movl %r15d, %eax
shrl %eax
cmpl %eax, 0x60(%rsp)
jne 0x1fb4b7
movq 0x8(%rsp), %r13
movq 0x38(%r13), %rax
cmpq %rax, 0x30(%r13)
jne 0x1fb0dc
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x38(%r13)
movq 0x40(%r13), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x10(%rsp), %rdi
movq %rax, 0x40(%r13)
movq 0x30(%r13), %rax
movq 0x40(%r13), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x30(%r13)
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %r13
movq 0x58(%rsp), %rdx
movl %edx, 0x4(%r13)
movq (%rbx,%r12,8), %rax
movl %eax, (%r13)
movl %r15d, %ecx
xorl %edx, %ecx
andl $0x1, %ecx
je 0x1fb136
movq 0x1d0(%rdi), %rdx
movq %rax, %rsi
shrq $0x1b, %rsi
andq $-0x20, %rsi
movl 0x18(%rdx,%rsi), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
movl %eax, %esi
notl %esi
addl %edx, %esi
movl %esi, (%r13)
shrq $0x1f, %rax
andl $-0x2, %eax
orl %ecx, %eax
shlq $0x20, %rax
addq 0x18(%rsp), %rax
movq %rax, 0x10(%r13)
movq 0x50(%rsp), %rax
movl %eax, 0x8(%r13)
movq 0x30(%rdi), %rax
movq 0x1d0(%rdi), %rcx
movq 0x48(%rax), %rax
movq (%rbx,%r12,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
leaq (%rcx,%rcx,8), %rdx
movl (%rax,%rdx,4), %ecx
movl 0x20(%rax,%rdx,4), %esi
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rcx,2), %esi
cmpl %r15d, %esi
jne 0x1fb4d6
leaq (%rax,%rdx,4), %rax
movl 0xc(%rax), %edi
movl 0x10(%rax), %esi
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
movq 0x20(%rsp), %rax
addq 0x10(%rax), %rdx
callq 0x1f2d50
cmpq 0x48(%rsp), %rax
movq 0x10(%rsp), %rdi
movq %r14, %r15
jae 0x1fb1d4
movl %eax, 0x8(%r13)
incq %r12
cmpq %r12, %rbp
jne 0x1fb053
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x38(%rsp), %r8
movl 0x18(%r8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %rdx
jb 0x1fafb2
movq 0x8(%rsp), %r14
movq 0x30(%r14), %rax
movq 0x40(%r14), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e4585
movq 0x10(%rsp), %r10
movq 0x30(%r14), %rbx
movq $0x0, 0xc0(%r14)
testq %rbx, %rbx
je 0x1fb3b6
leaq 0x190(%r10), %r12
movl $0x1, %r13d
movl $0x28, %ebp
xorl %ecx, %ecx
leaq 0x68(%rsp), %r14
movq %r12, 0x18(%rsp)
cmpq %r13, %rbx
je 0x1fb279
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rax
leaq (%rcx,%rcx,2), %rdx
movq (%rax,%rbp), %rsi
xorq 0x10(%rax,%rdx,8), %rsi
shrq $0x20, %rsi
je 0x1fb3a6
leaq (%rcx,%rcx,2), %rsi
shlq $0x3, %rsi
movq 0x8(%rsp), %r8
addq 0x40(%r8), %rsi
movq %r13, %rdx
subq %rcx, %rdx
subq $0x8, %rsp
movl 0x34(%rsp), %edi
movq 0x28(%rsp), %r9
pushq %r14
pushq 0x30(%r10)
pushq %r12
callq 0x1f436a
movq 0x30(%rsp), %r10
addq $0x20, %rsp
movl 0x70(%rsp), %edx
cmpl $-0x1, %edx
setne %sil
andb %al, %sil
movq %r13, %rcx
cmpb $0x1, %sil
jne 0x1fb3a6
movq %r14, %r12
movq 0x8(%rsp), %r14
movq 0xc0(%r14), %rax
testq %rax, %rax
je 0x1fb31a
movq 0xd0(%r14), %rcx
leaq (%rax,%rax,4), %rsi
xorl -0x20(%rcx,%rsi,8), %edx
cmpl $0x1, %edx
ja 0x1fb31a
leaq (%rcx,%rsi,8), %rax
movq -0x18(%rax), %rdx
movq %r13, %rcx
cmpq 0x78(%rsp), %rdx
movq %r12, %r14
movq 0x18(%rsp), %r12
jae 0x1fb3a6
addq $-0x28, %rax
jmp 0x1fb386
movq 0xc8(%r14), %rcx
cmpq %rcx, %rax
jne 0x1fb35d
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rax
movq %rax, 0xc8(%r14)
movq 0xd0(%r14), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,4), %rsi
callq 0x2370
movq 0x10(%rsp), %r10
movq %rax, 0xd0(%r14)
movq 0xc0(%r14), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0xc0(%r14)
leaq (%rax,%rax,4), %rax
shlq $0x3, %rax
addq 0xd0(%r14), %rax
movq %r12, %r14
movq 0x18(%rsp), %r12
movq 0x88(%rsp), %rcx
movq %rcx, 0x20(%rax)
movups 0x68(%rsp), %xmm0
movups 0x78(%rsp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq %r13, %rcx
incq %r13
addq $0x18, %rbp
cmpq %rbx, %r13
jbe 0x1fb254
movq 0x30(%rsp), %rbp
movq $0x0, 0x8(%rbp)
movq 0x8(%rsp), %rax
cmpq $0x0, 0xc0(%rax)
je 0x1fb49f
movq %r15, %r12
xorl %ebx, %ebx
leaq 0x90(%rsp), %r15
movl $0x2, %r14d
xorl %r13d, %r13d
movq 0x30(%r10), %rdi
movq 0x1d0(%r10), %rdx
movq 0x40(%rax), %rcx
movq 0xd0(%rax), %r8
addq %rbx, %r8
movq %r12, %r9
pushq %r15
pushq %rax
callq 0x1f61bd
addq $0x10, %rsp
testq %rax, %rax
je 0x1fb481
movq 0x10(%rbp), %rax
cmpq %rax, 0x8(%rbp)
jne 0x1fb447
leaq (%rax,%rax), %rcx
testq %rax, %rax
cmoveq %r14, %rcx
movq %rcx, 0x10(%rbp)
movq (%rbp), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
callq 0x2370
movq %rax, (%rbp)
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbp)
leaq (%rcx,%rcx,4), %rcx
movq 0xb0(%rsp), %rdx
movq %rdx, 0x20(%rax,%rcx,8)
movups 0x90(%rsp), %xmm0
movups 0xa0(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx,8)
movups %xmm0, (%rax,%rcx,8)
incq %r13
addq $0x28, %rbx
movq 0x8(%rsp), %rax
cmpq 0xc0(%rax), %r13
movq 0x10(%rsp), %r10
jb 0x1fb3ec
movl 0x8(%rbp), %eax
movl %eax, 0x1c(%rbp)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3fdbc(%rip), %rdi     # 0x23b27a
leaq 0x3de60(%rip), %rsi     # 0x239325
leaq 0x3fdeb(%rip), %rcx     # 0x23b2b7
movl $0x19dc, %edx           # imm = 0x19DC
callq 0x2110
leaq 0x3fdb0(%rip), %rdi     # 0x23b28d
leaq 0x3de41(%rip), %rsi     # 0x239325
leaq 0x3fdcc(%rip), %rcx     # 0x23b2b7
movl $0x19e5, %edx           # imm = 0x19E5
callq 0x2110
leaq 0x3fcf8(%rip), %rdi     # 0x23b1f4
leaq 0x3de22(%rip), %rsi     # 0x239325
leaq 0x3fdad(%rip), %rcx     # 0x23b2b7
movl $0x19ce, %edx           # imm = 0x19CE
callq 0x2110