// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: qry_bub_sc(ul_idx_t const*, ma_ug_t*, scaf_res_t*, bubble_type*, unsigned long, unsigned long, unsigned long, ul_vec_t*, unsigned int, kv_ul_ov_t*)
uint64_t qry_bub_sc(const ul_idx_t *uref, ma_ug_t *gfa, scaf_res_t *ref_sc, bubble_type *bub, uint64_t bid, uint64_t qsidx, uint64_t qeidx, ul_vec_t *qstr, uint32_t avoid_id, kv_ul_ov_t *res)
{
	// fprintf(stderr, "qsidx::%lu(utg%.6u%c), qeidx::%lu(utg%.6u%c)\n", qsidx, (qstr->bb.a[qsidx].hid)+1, "lc"[gfa->u.a[qstr->bb.a[qsidx].hid].circ], qeidx, (qstr->bb.a[qeidx].hid)+1, "lc"[gfa->u.a[qstr->bb.a[qeidx].hid].circ]);
	uint32_t beg, sink, v, w, z, si, ei, qrev = (uint32_t)-1, qv, qw, trev, nn = 0, tn;
	get_bubbles(bub, bid, &beg, &sink, NULL, NULL, NULL);
	if((beg == ((uint32_t)-1)) || (sink == ((uint32_t)-1))) return 0;
	utg_rid_dt *a; uint64_t a_n, i, k; ul_vec_t *q; ul_ov_t p;
	qv = qstr->bb.a[qsidx].hid; qv <<= 1; qv |= ((uint32_t)qstr->bb.a[qsidx].rev);
	qw = qstr->bb.a[qeidx].hid; qw <<= 1; qw |= ((uint32_t)qstr->bb.a[qeidx].rev);
	if((qv == beg) && (qw == (sink^1))) qrev = 0;
	if((qv == sink) && (qw == (beg^1))) qrev = 1;
	assert(qrev != (uint32_t)-1);
	memset(&p, 0, sizeof(p));

	v = beg; w = sink^1; trev = 0;
	a = get_r_ug_region(uref->r_ug, &a_n, v>>1);
	// fprintf(stderr, "+a_n::%lu\n", a_n);
	for (i = 0; i < a_n; i++) {
		// fprintf(stderr, "+(0)i::%lu\n", i);
		if((a[i].u&1) != (v&1)) continue;
		if((a[i].u>>1) == avoid_id) continue;
		q = &(ref_sc->a[a[i].u>>1]); si = ei = a[i].off; 
		assert((q->bb.a[si].hid == (v>>1)) && (q->bb.a[si].rev == (v&1)));
		// fprintf(stderr, "+(1)i::%lu\n", i);
		for (k = tn = 0; k < q->bb.n; k++) tn += q->bb.a[k].te - q->bb.a[k].ts;
		for (k = si + 1; (k < tn) && (q->bb.a[k].aidx == q->bb.a[si].aidx); k++) {
			z = (((uint32_t)q->bb.a[k].hid)<<1)|((uint32_t)q->bb.a[k].rev);
			if(z == w) {ei = k; break;}
			if(bub->index[z>>1] != bid) break;
		}
		
		if(ei > si) {////found a bubble
			// fprintf(stderr, "+(2)i::%lu, si::%u, ei::%u, ref_id::%u\nn", i, si, ei, a[i].u>>1);
			p.qn = q->bb.a[si].aidx; p.qs = qsidx; p.qe = qeidx; 
        	p.tn = a[i].u>>1; p.ts = si; p.te = ei; 
        	p.rev = (qrev == trev?0:1); 
			kv_push(ul_ov_t, *res, p);
			nn++;
		}
	}


	v = sink; w = beg^1; trev = 1;
	a = get_r_ug_region(uref->r_ug, &a_n, v>>1);
	// fprintf(stderr, "-a_n::%lu\n", a_n);
	for (i = 0; i < a_n; i++) {
		if((a[i].u&1) != (v&1)) continue;
		if((a[i].u>>1) == avoid_id) continue;
		q = &(ref_sc->a[a[i].u>>1]); si = ei = a[i].off; 
		assert((q->bb.a[si].hid == (v>>1)) && (q->bb.a[si].rev == (v&1)));
		for (k = tn = 0; k < q->bb.n; k++) tn += q->bb.a[k].te - q->bb.a[k].ts;
		for (k = si + 1; (k < tn) && (q->bb.a[k].aidx == q->bb.a[si].aidx); k++) {
			z = (((uint32_t)q->bb.a[k].hid)<<1)|((uint32_t)q->bb.a[k].rev);
			if(z == w) {ei = k; break;}
			if(bub->index[z>>1] != bid) break;
		}

		if(ei > si) {////found a bubble
			// fprintf(stderr, "-(2)i::%lu, si::%u, ei::%u, ref_id::%u\nn", i, si, ei, a[i].u>>1);
			p.qn = q->bb.a[si].aidx; p.qs = qsidx; p.qe = qeidx; 
        	p.tn = a[i].u>>1; p.ts = si; p.te = ei; 
        	p.rev = (qrev == trev?0:1); 
			kv_push(ul_ov_t, *res, p);
			nn++;
		}
	}
	
	return nn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %r14
movq %r8, %rsi
movq %rdx, 0x58(%rsp)
movq %rdi, %r15
movq $0x0, (%rsp)
xorl %ebp, %ebp
leaq 0x6c(%rsp), %rbx
leaq 0x68(%rsp), %r13
movq %rcx, 0x48(%rsp)
movq %rcx, %rdi
movq %r8, 0x50(%rsp)
movq %rbx, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x13df01
movl (%rbx), %r11d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r11
sete %al
movl (%r13), %edi
cmpl $-0x1, %edi
sete %cl
orb %al, %cl
jne 0x1d77ac
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %r8
movq 0x30(%rax), %rax
leaq (%r14,%r14,8), %rcx
movl (%rax,%rcx,4), %edx
movl 0x20(%rax,%rcx,4), %ecx
shrl $0x5, %ecx
andl $0x1, %ecx
leal (%rcx,%rdx,2), %esi
leaq (%r8,%r8,8), %rcx
movl (%rax,%rcx,4), %edx
movl 0x20(%rax,%rcx,4), %eax
shrl $0x5, %eax
andl $0x1, %eax
leal (%rax,%rdx,2), %eax
movl %esi, %edx
xorl %r11d, %edx
movl %edi, %r8d
movl %eax, %edi
xorl %r8d, %edi
xorl $0x1, %edi
xorl %ecx, %ecx
orl %edx, %edi
setne %dl
movl %r8d, 0x20(%rsp)
xorl %r8d, %esi
xorl %r11d, %eax
xorl $0x1, %eax
xorl %ebx, %ebx
orl %esi, %eax
setne %bpl
je 0x1d72b0
testb %dl, %dl
jne 0x1d77fc
movb %dl, 0x8(%rsp)
movl 0xc0(%rsp), %r13d
movq 0x28(%r15), %rax
movl %r11d, %r12d
shrl %r12d
movq (%rax), %rsi
movl (%rsi,%r12,4), %edi
movl 0x4(%rsi,%r12,4), %edx
cmpl %edi, %edx
movq 0x50(%rsp), %r8
movq 0x48(%rsp), %r9
movq 0x58(%rsp), %r10
movq %r14, 0x70(%rsp)
jne 0x1d7304
movl $0x0, 0x2c(%rsp)
movq $0x0, 0x30(%rsp)
movl 0x20(%rsp), %edi
jmp 0x1d755c
movq %r15, 0x40(%rsp)
movl %ebp, %esi
leaq (%rdi,%rdi,2), %rbp
shlq $0x2, %rbp
addq 0x18(%rax), %rbp
subl %edi, %edx
andl $0x1, %r11d
movb 0x8(%rsp), %al
movb %al, %cl
shll $0x1f, %ecx
movb %sil, 0x17(%rsp)
testb %sil, %sil
movl $0x80000000, %eax       # imm = 0x80000000
cmovnel %ecx, %eax
movl %eax, 0x64(%rsp)
cmpl $0x1, %edx
adcl $0x0, %edx
movq $0x0, 0x30(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x2c(%rsp)
movq %r12, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
leaq (%r14,%r14,2), %rax
movl (%rbp,%rax,4), %r15d
movl %r15d, %ecx
andl $0x1, %ecx
cmpl %r11d, %ecx
jne 0x1d7534
shrl %r15d
cmpl %r13d, %r15d
je 0x1d7534
leaq (,%rax,4), %rax
addq %rbp, %rax
movq (%r10), %rcx
movl 0x4(%rax), %ebx
imulq $0x58, %r15, %rsi
movq 0x30(%rcx,%rsi), %rax
leaq (%rbx,%rbx,8), %rdx
cmpl %r12d, (%rax,%rdx,4)
jne 0x1d77be
leaq (%rax,%rdx,4), %rdi
btl $0x5, 0x20(%rdi)
movl %r11d, %edi
sbbl $0x0, %edi
jne 0x1d77be
addq %rsi, %rcx
addq $0x20, %rcx
movq (%rcx), %rsi
testq %rsi, %rsi
je 0x1d752f
leaq 0x10(%rax), %rdi
xorl %ecx, %ecx
addl (%rdi), %ecx
subl -0x4(%rdi), %ecx
addq $0x24, %rdi
decq %rsi
jne 0x1d73d8
leal 0x1(%rbx), %esi
cmpl %ecx, %esi
movl 0x20(%rsp), %edi
jae 0x1d752f
movl %ecx, %ecx
movl %esi, %r13d
movl 0x1c(%rax,%rdx,4), %r12d
leaq (,%r13,8), %rdx
addq %r13, %rdx
leaq (%rax,%rdx,4), %rax
addq $0x20, %rax
cmpl %r12d, -0x4(%rax)
jne 0x1d7522
movl -0x20(%rax), %edx
movl (%rax), %esi
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rdx,2), %esi
xorl %edi, %esi
cmpl $0x1, %esi
je 0x1d7457
movq (%r9), %rsi
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl (%rsi,%rdx,4), %edx
cmpq %r8, %rdx
jne 0x1d7522
incq %r13
addq $0x24, %rax
cmpq %rcx, %r13
jb 0x1d7412
jmp 0x1d7522
cmpl %r13d, %ebx
jae 0x1d7522
movq 0xc8(%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
cmpq 0x10(%rdx), %rcx
jne 0x1d74c9
movq %rdx, %rsi
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rsi)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
movq %r11, 0x38(%rsp)
callq 0x2370
movq 0xc8(%rsp), %rdi
movq 0x38(%rsp), %r11
movq 0x58(%rsp), %r10
movq 0x48(%rsp), %r9
movq 0x50(%rsp), %r8
movq %rax, (%rdi)
movq 0x8(%rdi), %rcx
jmp 0x1d74cc
movq %rdx, %rdi
movq 0x30(%rsp), %rsi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
orl 0x64(%rsp), %esi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rdi)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl %r12d, (%rax,%rdx)
movq 0x70(%rsp), %rcx
movl %ecx, 0x4(%rax,%rdx)
movq 0xb0(%rsp), %rcx
movl %ecx, 0x8(%rax,%rdx)
movl %r15d, 0xc(%rax,%rdx)
movl %ebx, 0x10(%rax,%rdx)
movl %r13d, 0x14(%rax,%rdx)
movq %rsi, 0x30(%rsp)
movl %esi, 0x18(%rax,%rdx)
incl 0x2c(%rsp)
movl 0xc0(%rsp), %r13d
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %rdx
incq %r14
cmpq %rdx, %r14
jne 0x1d735e
movl 0x68(%rsp), %edi
movl 0x6c(%rsp), %r11d
movq 0x40(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rsi
xorl %ebx, %ebx
movb 0x17(%rsp), %bpl
movl %edi, %edx
shrl %edx
movl (%rsi,%rdx,4), %ecx
movq %rdx, 0x18(%rsp)
movl 0x4(%rsi,%rdx,4), %r12d
cmpl %ecx, %r12d
je 0x1d77a8
subl %ecx, %r12d
leaq (%rcx,%rcx,2), %rdx
shlq $0x2, %rdx
addq 0x18(%rax), %rdx
andl $0x1, %edi
movb %bpl, %bl
shll $0x1f, %ebx
movl %ebx, 0x40(%rsp)
cmpl $0x1, %r12d
adcl $0x0, %r12d
xorl %r14d, %r14d
movq 0x18(%rsp), %rbp
movq %r12, 0x20(%rsp)
movq %rdx, 0x8(%rsp)
leaq (%r14,%r14,2), %rax
movl (%rdx,%rax,4), %r15d
movl %r15d, %ecx
andl $0x1, %ecx
cmpl %edi, %ecx
jne 0x1d779c
shrl %r15d
cmpl %r13d, %r15d
je 0x1d779c
leaq (%rdx,%rax,4), %rax
movq (%r10), %rcx
movl 0x4(%rax), %ebx
imulq $0x58, %r15, %rsi
movq 0x30(%rcx,%rsi), %rax
leaq (%rbx,%rbx,8), %rdx
cmpl %ebp, (%rax,%rdx,4)
jne 0x1d77dd
movl %edi, %r12d
leaq (%rax,%rdx,4), %rdi
btl $0x5, 0x20(%rdi)
movl %r12d, %edi
sbbl $0x0, %edi
jne 0x1d77dd
addq %rsi, %rcx
addq $0x20, %rcx
movq (%rcx), %rsi
testq %rsi, %rsi
je 0x1d7694
leaq 0x10(%rax), %rdi
xorl %ecx, %ecx
addl (%rdi), %ecx
subl -0x4(%rdi), %ecx
addq $0x24, %rdi
decq %rsi
jne 0x1d761b
leal 0x1(%rbx), %esi
cmpl %ecx, %esi
jae 0x1d7694
movl %ecx, %ecx
movl %esi, %r13d
movl 0x1c(%rax,%rdx,4), %ebp
leaq (,%r13,8), %rdx
addq %r13, %rdx
leaq (%rax,%rdx,4), %rax
addq $0x20, %rax
movl %r12d, %edi
cmpl %ebp, -0x4(%rax)
jne 0x1d7785
movl -0x20(%rax), %edx
movl (%rax), %esi
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rdx,2), %esi
xorl %r11d, %esi
cmpl $0x1, %esi
je 0x1d769c
movq (%r9), %rsi
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl (%rsi,%rdx,4), %edx
cmpq %r8, %rdx
jne 0x1d7785
incq %r13
addq $0x24, %rax
cmpq %rcx, %r13
jb 0x1d764f
jmp 0x1d7785
movl %r12d, %edi
jmp 0x1d7792
cmpl %r13d, %ebx
jae 0x1d7785
movq 0xc8(%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %r10
cmpq 0x10(%rdx), %r10
jne 0x1d770a
movq %rdx, %rsi
leaq (%r10,%r10), %rdx
testq %r10, %r10
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rsi)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
movq %r11, 0x38(%rsp)
callq 0x2370
movl %r12d, %edi
movq 0x38(%rsp), %r11
movq 0x48(%rsp), %r9
movq 0x50(%rsp), %r8
movq 0xc8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x8(%rcx), %r10
movq 0x20(%rsp), %r12
movq 0x30(%rsp), %rsi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
orl 0x40(%rsp), %esi
leaq 0x1(%r10), %rdx
movq 0xc8(%rsp), %rcx
movq %rdx, 0x8(%rcx)
leaq (%r10,%r10,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r10, %rdx
movl %ebp, (%rax,%rdx)
movq 0x70(%rsp), %rcx
movl %ecx, 0x4(%rax,%rdx)
movq 0xb0(%rsp), %rcx
movl %ecx, 0x8(%rax,%rdx)
movl %r15d, 0xc(%rax,%rdx)
movl %ebx, 0x10(%rax,%rdx)
movl %r13d, 0x14(%rax,%rdx)
movq %rsi, 0x30(%rsp)
movl %esi, 0x18(%rax,%rdx)
incl 0x2c(%rsp)
movl 0xc0(%rsp), %r13d
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %rdx
movq 0x58(%rsp), %r10
jmp 0x1d779c
movl 0xc0(%rsp), %r13d
movq 0x18(%rsp), %rbp
movq 0x20(%rsp), %r12
movq 0x8(%rsp), %rdx
incq %r14
cmpq %r12, %r14
jne 0x1d75ac
movl 0x2c(%rsp), %ebp
movq %rbp, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6bdcc(%rip), %rdi     # 0x243591
leaq 0x6848f(%rip), %rsi     # 0x23fc5b
leaq 0x6bd2e(%rip), %rcx     # 0x243501
movl $0x4108, %edx           # imm = 0x4108
callq 0x2110
leaq 0x6bdad(%rip), %rdi     # 0x243591
leaq 0x68470(%rip), %rsi     # 0x23fc5b
leaq 0x6bd0f(%rip), %rcx     # 0x243501
movl $0x4123, %edx           # imm = 0x4123
callq 0x2110
leaq 0x6bce9(%rip), %rdi     # 0x2434ec
leaq 0x68451(%rip), %rsi     # 0x23fc5b
leaq 0x6bcf0(%rip), %rcx     # 0x243501
movl $0x40fd, %edx           # imm = 0x40FD
callq 0x2110