// File: /workspace/github_cpp_makefile_star2_O1/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 0x1bbc53
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 0x1bbcf5
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), %ecx
cmpq %rdi, %rcx
jne 0x1bbcf5
movl -0xc(%rax,%rbp), %r15d
movl (%rax,%rbp), %r12d
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x1bbcc7
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rax, 0x10(%r14)
shrl $0x1f, %r12d
shlq $0x21, %r15
shlq $0x20, %r12
orq %r15, %r12
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movq %r12, (%rcx,%rax,8)
decq %r13
addq $-0x1c, %rbp
cmpq $0x1, %r13
jg 0x1bbc7b
movq (%r14), %rax
testq %rax, %rax
je 0x1bbf0e
addq 0x10(%rbx), %rax
cmpq %rax, 0x8(%r14)
jae 0x1bbd5c
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 %rax, 0x10(%r14)
cmpq $0x0, 0x10(%rbx)
je 0x1bbddb
movl $0x1c, %r15d
xorl %r12d, %r12d
movq (%rbx), %rax
movl -0xc(%rax,%r15), %r13d
movl (%rax,%r15), %ebp
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x1bbda6
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r14)
shlq $0x21, %r13
shlq $0x20, %rbp
orq %r13, %rbp
orq %r12, %rbp
btsq $0x1f, %rbp
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movq %rbp, (%rcx,%rax,8)
incq %r12
addq $0xa8, %r15
cmpq 0x10(%rbx), %r12
jb 0x1bbd6c
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x194fef
movq (%r14), %rax
testq %rax, %rax
je 0x1bbf0e
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 0x1bbe28
movq 0x10(%r14), %r8
movq (%r8,%rdx,8), %r9
xorq (%r8,%rdi,8), %r9
shrq $0x20, %r9
je 0x1bbe83
movq %rdx, %r8
subq %rdi, %r8
cmpq $0x2, %r8
jb 0x1bbe80
movq 0x10(%r14), %r8
testb $-0x80, 0x3(%r8,%rdi,8)
sete %r9b
cmpq %rdi, %rdx
seta %r10b
andb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x1bbe80
movq (%r8,%rdi,8), %r9
testl %r9d, %r9d
jns 0x1bbe78
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 0x1bbe52
movq %rdx, %rdi
incq %rdx
cmpq %rcx, %rdx
jne 0x1bbe11
testq %rsi, %rsi
jle 0x1bbf0e
cmpq $0x0, 0x10(%rbx)
je 0x1bbf08
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %ebp, %ebp
movq (%rbx), %r15
cmpl $-0x1, 0x10(%r15,%r13)
je 0x1bbef6
cmpq %rbp, %r14
je 0x1bbef3
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
incq %rbp
incq %r14
addq $0xa8, %r13
cmpq 0x10(%rbx), %r14
jb 0x1bbe9f
jmp 0x1bbf0a
xorl %ebp, %ebp
movq %rbp, 0x10(%rbx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq