// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq 0x18(%rdi), %rax
movq %rax, 0x28(%rsp)
movq 0x1d0(%rdi), %r9
movl %edx, %r14d
movq %r14, %rax
shlq $0x5, %rax
movl $0x0, 0x1c(%rcx)
movq $0x0, 0x8(%rcx)
movl %edx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl %esi, 0x18(%rcx)
testl $0x7ffffffe, 0x18(%r9,%rax) # imm = 0x7FFFFFFE
je 0x206ada
movl %edx, 0x14(%rsp)
movq %rcx, 0x38(%rsp)
addq %rax, %r9
movq $0x0, 0x30(%r13)
movl 0x18(%r9), %eax
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %r13, (%rsp)
movq %rdi, 0x8(%rsp)
je 0x20682f
movq 0x30(%rdi), %rcx
movq %rcx, 0x48(%rsp)
movq $0x0, 0x18(%rsp)
imulq $0x58, %r14, %r10
xorl %r11d, %r11d
movq %r9, 0x58(%rsp)
movq %r10, 0x50(%rsp)
movq 0x48(%rsp), %rcx
movq 0x48(%rcx), %rcx
movq 0x30(%rcx,%r10), %rdx
movq 0x10(%r9), %rcx
movq (%rcx,%r11,8), %r8
movq %r8, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,8), %rsi
movl (%rdx,%rsi,4), %ecx
movl 0x20(%rdx,%rsi,4), %ebx
shrl $0x5, %ebx
andl $0x1, %ebx
movq %rbx, 0x20(%rsp)
leal (%rbx,%rcx,2), %ebx
movl %ebx, 0x34(%rsp)
cmpl %r8d, %ebx
jne 0x206b2a
movq 0x1a0(%rdi), %r8
movl %ecx, %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq (%r8,%rbx,8), %r12
movq %rbx, 0x70(%rsp)
movq 0x8(%r8,%rbx,8), %rbp
cmpq %r12, %rbp
je 0x206819
movq %r11, 0x40(%rsp)
leaq (%rdx,%rsi,4), %rax
movq 0x1b8(%rdi), %rbx
movl 0xc(%rax), %edi
movl 0x10(%rax), %esi
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
movq 0x28(%rsp), %rax
addq 0x10(%rax), %rdx
callq 0x1fe266
movq 0x8(%rsp), %rdi
movq %rax, 0x68(%rsp)
movl %eax, %eax
movq %rax, 0x60(%rsp)
movq (%rbx,%r12,8), %rax
movq %rax, %rdx
shrq $0x20, %rdx
cmpq %r14, %rdx
je 0x2067fa
movq 0x1d0(%rdi), %rcx
shlq $0x5, %rdx
testl $0x7ffffffe, 0x18(%rcx,%rdx) # imm = 0x7FFFFFFE
je 0x2067fa
addq %rcx, %rdx
movq 0x10(%rdx), %rdx
movl %eax, %eax
movl (%rdx,%rax,8), %r15d
movl %r15d, %eax
shrl %eax
cmpl %eax, 0x70(%rsp)
jne 0x206aec
movq 0x40(%r13), %rax
movq 0x18(%rsp), %rdx
cmpq 0x38(%r13), %rdx
jne 0x206703
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x38(%r13)
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x40(%r13)
movq 0x30(%r13), %rdx
movq 0x1d0(%rdi), %rcx
leaq (%rdx,%rdx,2), %rsi
leaq 0x1(%rdx), %r8
movq %r8, 0x18(%rsp)
movq %r8, 0x30(%r13)
leaq (%rax,%rsi,8), %r13
movl 0x34(%rsp), %eax
movl %eax, 0x4(%r13)
movq (%rbx,%r12,8), %rax
movl %eax, (%r13)
movl %r15d, %edx
andl $0x1, %edx
cmpl 0x20(%rsp), %edx
je 0x206759
movq %rax, %rsi
shrq $0x1b, %rsi
andq $-0x20, %rsi
movl 0x18(%rcx,%rsi), %esi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %esi
movl %eax, %r8d
notl %r8d
addl %esi, %r8d
movl %r8d, (%r13)
xorl 0x20(%rsp), %edx
shrq $0x1f, %rax
andl $-0x2, %eax
orl %edx, %eax
shlq $0x20, %rax
addq 0x40(%rsp), %rax
movq %rax, 0x10(%r13)
movq 0x68(%rsp), %rax
movl %eax, 0x8(%r13)
movq 0x30(%rdi), %rax
movq %rax, 0x48(%rsp)
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 0x206b0b
leaq (%rax,%rdx,4), %rax
movl 0xc(%rax), %edi
movl 0x10(%rax), %esi
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
movq 0x28(%rsp), %rax
addq 0x10(%rax), %rdx
callq 0x1fe266
cmpq 0x60(%rsp), %rax
jae 0x2067f1
movl %eax, 0x8(%r13)
movq (%rsp), %r13
movq 0x8(%rsp), %rdi
incq %r12
cmpq %r12, %rbp
jne 0x206673
movq 0x58(%rsp), %r9
movl 0x18(%r9), %eax
movq 0x50(%rsp), %r10
movq 0x40(%rsp), %r11
incq %r11
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r11
jb 0x2065cd
jmp 0x206838
movq $0x0, 0x18(%rsp)
movq 0x40(%r13), %rdi
movq 0x18(%rsp), %rax
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1eef97
movq 0x30(%r13), %rbx
movq $0x0, 0xc0(%r13)
testq %rbx, %rbx
je 0x206ac7
movq %r14, 0x20(%rsp)
movq 0x8(%rsp), %r10
leaq 0x190(%r10), %r14
movl $0x1, %r15d
movl $0x28, %r12d
xorl %ecx, %ecx
leaq 0x78(%rsp), %r13
movl 0x14(%rsp), %ebp
movq (%rsp), %rax
movq 0x40(%rax), %rax
cmpq %r15, %rbx
je 0x2068b2
leaq (%rcx,%rcx,2), %rdx
movq (%rax,%r12), %rsi
xorq 0x10(%rax,%rdx,8), %rsi
shrq $0x20, %rsi
je 0x2069c8
leaq (%rcx,%rcx,2), %rdx
leaq (%rax,%rdx,8), %rsi
movq %r15, %rdx
subq %rcx, %rdx
subq $0x8, %rsp
movl %ebp, %edi
movq 0x8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq %r13
pushq 0x30(%r10)
pushq %r14
callq 0x1ff87d
movq 0x28(%rsp), %r10
addq $0x20, %rsp
movl 0x80(%rsp), %edx
cmpl $-0x1, %edx
setne %sil
andb %al, %sil
movq %r15, %rcx
cmpb $0x1, %sil
jne 0x2069c8
movq (%rsp), %rax
movq 0xc0(%rax), %rcx
movq 0xd0(%rax), %rax
testq %rcx, %rcx
je 0x206947
leaq (%rcx,%rcx,4), %rsi
xorl -0x20(%rax,%rsi,8), %edx
cmpl $0x1, %edx
ja 0x206947
leaq (%rax,%rsi,8), %rax
movq -0x18(%rax), %rdx
movq %r15, %rcx
cmpq 0x88(%rsp), %rdx
jae 0x2069c8
addq $-0x28, %rax
jmp 0x2069a5
movq (%rsp), %rbp
cmpq 0xc8(%rbp), %rcx
jne 0x20698e
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0xc8(%rbp)
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r10
movq %rax, 0xd0(%rbp)
movq 0xc0(%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0xc0(%rbp)
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rax
movl 0x14(%rsp), %ebp
movq 0x98(%rsp), %rcx
movq %rcx, 0x20(%rax)
movups 0x78(%rsp), %xmm0
movups 0x88(%rsp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq %r15, %rcx
incq %r15
addq $0x18, %r12
cmpq %rbx, %r15
jbe 0x20688e
movq (%rsp), %rax
cmpq $0x0, 0xc0(%rax)
movq 0x38(%rsp), %r13
movq $0x0, 0x8(%r13)
je 0x206ad4
xorl %ebx, %ebx
leaq 0xa0(%rsp), %r14
movl $0x2, %r15d
xorl %r12d, %r12d
movq (%rsp), %rax
movq 0x30(%r10), %rdi
movq 0x1d0(%r10), %rdx
movq 0x40(%rax), %rcx
movq 0xd0(%rax), %r8
addq %rbx, %r8
movq 0x20(%rsp), %r9
pushq %r14
pushq %rax
callq 0x201759
addq $0x10, %rsp
testq %rax, %rax
je 0x206aa4
movq (%r13), %rax
movq 0x8(%r13), %rcx
cmpq 0x10(%r13), %rcx
jne 0x206a72
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
cmoveq %r15, %rdx
movq %rdx, 0x10(%r13)
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r13)
movq 0x8(%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
leaq (%rcx,%rcx,4), %rcx
movq 0xc0(%rsp), %rdx
movq %rdx, 0x20(%rax,%rcx,8)
movups 0xa0(%rsp), %xmm0
movups 0xb0(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx,8)
movups %xmm0, (%rax,%rcx,8)
incq %r12
addq $0x28, %rbx
movq (%rsp), %rax
cmpq 0xc0(%rax), %r12
movq 0x8(%rsp), %r10
jb 0x206a0e
movl 0x8(%r13), %eax
jmp 0x206ad6
movq 0x38(%rsp), %r13
movq $0x0, 0x8(%r13)
xorl %eax, %eax
movl %eax, 0x1c(%r13)
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40a6b(%rip), %rdi     # 0x24755e
leaq 0x3eb0f(%rip), %rsi     # 0x245609
leaq 0x40a9a(%rip), %rcx     # 0x24759b
movl $0x19dc, %edx           # imm = 0x19DC
callq 0x2110
leaq 0x40a5f(%rip), %rdi     # 0x247571
leaq 0x3eaf0(%rip), %rsi     # 0x245609
leaq 0x40a7b(%rip), %rcx     # 0x24759b
movl $0x19e5, %edx           # imm = 0x19E5
callq 0x2110
leaq 0x409a7(%rip), %rdi     # 0x2474d8
leaq 0x3ead1(%rip), %rsi     # 0x245609
leaq 0x40a5c(%rip), %rcx     # 0x24759b
movl $0x19ce, %edx           # imm = 0x19CE
callq 0x2110