// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: chain_origin_trans_uid_c_bubble(unsigned int, buf_t*, buf_t*, ma_ug_t*, hap_cov_t*)
void chain_origin_trans_uid_c_bubble(uint32_t query, buf_t *target, buf_t *idx, ma_ug_t *ug, hap_cov_t *cov)
{
    if(target->b.n == 0) return;
    uint32_t qs, qe, ts, te, i, v, ovlp;
    qs = idx->a[query].d; qe = qs + ug->g->seq[query>>1].len;
    uint64_t qlen = ug->g->seq[query>>1].len, tlen;
    cov->u_buffer.a.n = cov->tailIndex.a.n = 0;

    ///uint32_t i_n = cov->t_ch->k_trans.n;
    for (i = 0; i < target->b.n; ++i)
    {
        v = target->b.a[i];
        if(v < query) continue; //avoid dup
        ts = idx->a[v].d; te = ts + ug->g->seq[v>>1].len; tlen = ug->g->seq[v>>1].len;
        ovlp = ((MIN(qe, te) > MAX(qs, ts))? (MIN(qe, te) - MAX(qs, ts)) : 0);
        if(ovlp == 0) continue;
        chain_origin_trans_uid_by_distance(cov, cov->read_g, &query, 1, MAX(qs, ts) - qs, &qlen, 
                                                    &v, 1, MAX(qs, ts) - ts, &tlen, ug, RC_0, -1024, __func__);
    }
    // fprintf(stderr, "\nocc: %u\n", (uint32_t)(cov->t_ch->k_trans.n - i_n));
    // for (i = i_n; i < cov->t_ch->k_trans.n; i++)
    // {
    //     fprintf(stderr, "s-utg%.6ul\t%u\t%u\td-utg%.6ul\t%u\t%u\trev(%u)\n", 
    //     cov->t_ch->k_trans.a[i].qn+1, cov->t_ch->k_trans.a[i].qs, cov->t_ch->k_trans.a[i].qe, 
    //     cov->t_ch->k_trans.a[i].tn+1, cov->t_ch->k_trans.a[i].ts, cov->t_ch->k_trans.a[i].te, 
    //     cov->t_ch->k_trans.a[i].rev);
    // }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edi, 0x8(%rsp)
cmpq $0x0, 0x38(%rsi)
je 0xe1463
movq %r8, %r14
movq %rcx, %r11
movq %rsi, %r12
movq (%rdx), %rax
movl %edi, %ecx
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rcx, %rsi
movl 0x4(%rax,%rsi), %ebx
movq 0x18(%r11), %rax
movq 0x20(%rax), %rax
shrl %edi
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
andl (%rax,%rdi,8), %r13d
movl (%rax,%rdi,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x20(%rsp)
xorl %eax, %eax
movq %rax, 0x60(%r8)
movq %rax, 0x40(%r8)
cmpq %rax, 0x38(%r12)
je 0xe1463
addl %ebx, %r13d
movl $0x1, %r15d
xorl %eax, %eax
movq %rdx, 0x10(%rsp)
movq 0x48(%r12), %rcx
movl (%rcx,%rax,4), %ecx
movl %ecx, 0xc(%rsp)
cmpl 0x8(%rsp), %ecx
jb 0xe1452
movq (%rdx), %rax
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rcx, %rsi
movl 0x4(%rax,%rsi), %eax
movq 0x18(%r11), %rsi
movq 0x20(%rsi), %rsi
shrl %ecx
movl (%rsi,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leal (%rcx,%rax), %esi
movq %rcx, 0x18(%rsp)
movl %r13d, %ecx
cmpl %esi, %r13d
jb 0xe13f0
movl %esi, %ecx
movl %ebx, %r10d
cmpl %eax, %ebx
ja 0xe13fa
movl %eax, %r10d
cmpl %r10d, %ecx
jbe 0xe1452
movq 0x30(%r14), %rsi
movl %r10d, %r8d
subl %ebx, %r8d
subl %eax, %r10d
subq $0x10, %rsp
movq %r14, %rdi
leaq 0x18(%rsp), %rdx
movl $0x1, %ecx
leaq 0x30(%rsp), %r9
movsd 0x148736(%rip), %xmm0   # 0x229b60
pushq $0x0
pushq %r11
leaq 0x38(%rsp), %rax
pushq %rax
pushq %r10
pushq $0x1
leaq 0x44(%rsp), %rax
pushq %rax
movq %r11, %rbp
callq 0xbca10
movq %rbp, %r11
movq 0x50(%rsp), %rdx
addq $0x40, %rsp
movl %r15d, %eax
incl %r15d
cmpq %rax, 0x38(%r12)
ja 0xe13a3
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq