// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: backward_dedup_ol(unsigned long, kv_ul_ov_t*, st_mt_t*, overlap_region_alloc*)
void backward_dedup_ol(uint64_t rid, kv_ul_ov_t *bck, st_mt_t *sp, overlap_region_alloc* ol)
{
	int64_t i, m; uint64_t z, k, l; overlap_region t; 

	kv_resize(uint64_t, *sp, ol->length);
	for (i = ((int64_t)bck->n)-1, sp->n = 0; i >= 0 && bck->a[i].qn == rid; i--) {
		z = bck->a[i].tn; z <<= 1; z |= bck->a[i].rev; z <<= 32; 
		kv_push(uint64_t, *sp, z);
	}
	if(!(sp->n)) return;///no bck overlap

	kv_resize(uint64_t, *sp, (ol->length)+sp->n);
	for (k = 0; k < ol->length; k++) {
		z = ol->list[k].y_id; z <<= 1; z |= ol->list[k].y_pos_strand; 
		z <<= 32; z |= k; z |= ((uint64_t)0x80000000);
		kv_push(uint64_t, *sp, z);
	}

	radix_sort_gfa64(sp->a, sp->a + sp->n); 
	for (k = 1, l = 0, m = 0; k <= sp->n; k++) {
		if(k == sp->n || (sp->a[l]>>32)!=(sp->a[k]>>32)) {
			if((k - l > 1) && (!(sp->a[l]&((uint64_t)0x80000000)))) {///overlap within bck
				for (z = l; z < k; z++) {
					if(sp->a[z]&((uint64_t)0x80000000)) {
						ol->list[(uint32_t)(sp->a[z]-((uint64_t)0x80000000))].y_id = (uint32_t)-1;
						m++;
					}
				}
			}
			l = k;
		}
	}
	if(m > 0) {
		for (k = z = 0; k < ol->length; k++) {
			if(ol->list[k].y_id == (uint32_t)-1) continue;
			if(z != k) {
				t = ol->list[z];
				ol->list[z] = ol->list[k];
				ol->list[k] = t;
			}
			z++;
		}
		ol->length = z;
	}
	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq 0x10(%rcx), %rax
cmpq %rax, 0x8(%rdx)
movq %rsi, 0x8(%rsp)
movq %rdi, (%rsp)
jae 0x1c5aba
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rax, 0x10(%r14)
movq 0x8(%rsi), %rax
movq $0x0, (%r14)
testq %rax, %rax
jle 0x1c5d7d
leaq 0x1(%rax), %r13
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx), %rbp
addq $-0x4, %rbp
movq (%rsi), %rax
movl -0x18(%rax,%rbp), %edx
movq (%r14), %rcx
cmpq %rdi, %rdx
jne 0x1c5b5e
movl -0xc(%rax,%rbp), %r15d
movl (%rax,%rbp), %r12d
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x1c5b34
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rax, 0x10(%r14)
movq (%r14), %rcx
shrl $0x1f, %r12d
shlq $0x21, %r15
shlq $0x20, %r12
orq %r15, %r12
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r12, (%rax,%rcx,8)
decq %r13
addq $-0x1c, %rbp
cmpq $0x1, %r13
ja 0x1c5ae2
movq (%r14), %rcx
testq %rcx, %rcx
je 0x1c5d7d
movq 0x10(%rbx), %rax
addq %rax, %rcx
cmpq %rcx, 0x8(%r14)
jae 0x1c5bc9
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq 0x10(%rbx), %rax
movq 0x10(%r14), %rdi
testq %rax, %rax
je 0x1c5c45
movl $0x1c, %r15d
xorl %r12d, %r12d
movq (%rbx), %rax
movl -0xc(%rax,%r15), %r13d
movl (%rax,%r15), %ebp
movq (%r14), %rax
cmpq 0x8(%r14), %rax
jne 0x1c5c17
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%r14)
movq (%r14), %rax
shlq $0x21, %r13
shlq $0x20, %rbp
orq %r13, %rbp
orq %r12, %rbp
btsq $0x1f, %rbp
leaq 0x1(%rax), %rcx
movq %rcx, (%r14)
movq %rbp, (%rdi,%rax,8)
incq %r12
addq $0xa8, %r15
cmpq 0x10(%rbx), %r12
jb 0x1c5bdb
movq (%r14), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
movq (%r14), %rax
testq %rax, %rax
je 0x1c5d7d
leaq 0x1(%rax), %rdx
cmpq $0x3, %rdx
movl $0x2, %ecx
cmovaeq %rdx, %rcx
movl $0x1, %edx
xorl %edi, %edi
xorl %esi, %esi
cmpq %rax, %rdx
je 0x1c5c8e
movq 0x10(%r14), %r8
movq (%r8,%rdx,8), %r9
xorq (%r8,%rdi,8), %r9
shrq $0x20, %r9
je 0x1c5ce9
movq %rdx, %r8
subq %rdi, %r8
cmpq $0x2, %r8
jb 0x1c5ce6
movq 0x10(%r14), %r8
testb $-0x80, 0x3(%r8,%rdi,8)
sete %r9b
cmpq %rdi, %rdx
seta %r10b
andb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x1c5ce6
movq (%r8,%rdi,8), %r9
testl %r9d, %r9d
jns 0x1c5cde
movq (%rbx), %r10
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
imulq $0xa8, %r9, %r9
movl $0xffffffff, 0x10(%r10,%r9) # imm = 0xFFFFFFFF
incq %rsi
incq %rdi
cmpq %rdi, %rdx
jne 0x1c5cb8
movq %rdx, %rdi
incq %rdx
cmpq %rcx, %rdx
jne 0x1c5c77
testq %rsi, %rsi
jle 0x1c5d7d
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x1c5d77
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %ebp, %ebp
movq (%rbx), %r15
cmpl $-0x1, 0x10(%r15,%r13)
je 0x1c5d66
cmpq %rbp, %r14
je 0x1c5d63
imulq $0xa8, %rbp, %r12
addq %r15, %r12
movl $0xa8, %edx
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x2160
addq %r13, %r15
movl $0xa8, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2160
movq (%rbx), %rdi
addq %r13, %rdi
movl $0xa8, %edx
leaq 0x10(%rsp), %rsi
callq 0x2160
movq 0x10(%rbx), %rax
incq %rbp
incq %r14
addq $0xa8, %r13
cmpq %rax, %r14
jb 0x1c5d0b
jmp 0x1c5d79
xorl %ebp, %ebp
movq %rbp, 0x10(%rbx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq