// File: /workspace/github_cpp_makefile_star2_O2/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 0x17a5ba
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), %rbp
andq $0x0, (%r14)
imulq $0x1c, %rbp, %r13
addq $-0x4, %r13
testq %rbp, %rbp
jle 0x17a643
movq (%rsi), %rax
movl -0x18(%rax,%r13), %edx
movq (%r14), %rcx
cmpq %rdi, %rdx
jne 0x17a646
movl -0xc(%rax,%r13), %r15d
movl (%rax,%r13), %r12d
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x17a620
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
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)
addq $-0x1c, %r13
decq %rbp
jmp 0x17a5ca
movq (%r14), %rcx
testq %rcx, %rcx
je 0x17a818
addq 0x10(%rbx), %rcx
cmpq %rcx, 0x8(%r14)
jae 0x17a6aa
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)
pushq $0x1c
popq %r15
xorl %r12d, %r12d
cmpq 0x10(%rbx), %r12
jae 0x17a71f
movq (%rbx), %rax
movl -0xc(%rax,%r15), %ebp
movl (%rax,%r15), %r13d
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x17a6f5
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
shlq $0x21, %rbp
shlq $0x20, %r13
orq %rbp, %r13
orq %r12, %r13
btsq $0x1f, %r13
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %r13, (%rax,%rcx,8)
incq %r12
addq $0xa8, %r15
jmp 0x17a6b1
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
movq (%r14), %rax
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %esi, %esi
cmpq %rax, %rdx
ja 0x17a79e
je 0x17a752
movq 0x10(%r14), %rdi
movq (%rdi,%rdx,8), %r8
xorq (%rdi,%rsi,8), %r8
shrq $0x20, %r8
je 0x17a799
movq %rdx, %rdi
subq %rsi, %rdi
cmpq $0x2, %rdi
jb 0x17a796
movq 0x10(%r14), %rdi
testb $-0x80, 0x3(%rdi,%rsi,8)
jne 0x17a796
cmpq %rdx, %rsi
jae 0x17a796
movq (%rdi,%rsi,8), %r8
testl %r8d, %r8d
jns 0x17a791
movq (%rbx), %r9
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
imulq $0xa8, %r8, %r8
orl $-0x1, 0x10(%r9,%r8)
incq %rcx
incq %rsi
jmp 0x17a769
movq %rdx, %rsi
incq %rdx
jmp 0x17a739
testq %rcx, %rcx
jle 0x17a818
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpq 0x10(%rbx), %r14
jae 0x17a814
movq (%rbx), %r15
cmpl $-0x1, 0x10(%r15,%r13)
je 0x17a808
cmpq %rbp, %r14
je 0x17a805
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
jmp 0x17a7ab
movq %rbp, 0x10(%rbx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq