// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: srt_kv_emask_t(kv_emask_t*, mask_ul_ov_t*, unsigned int, ma_ug_t*)
void srt_kv_emask_t(kv_emask_t *res, mask_ul_ov_t *mk, uint32_t rid, ma_ug_t *ug)
{
	uint64_t k, m, z; ul_ov_t *p;
	mk->srt.n = mk->idx.n = 0;
	kv_resize(uint64_t, mk->idx, res->n);
	kv_resize(ul_ov_t, mk->srt, res->n);
	
	for (k = 0; k < res->n; k++) {
		m = k + 1; z = (((uint64_t)res->a[k].tn)<<32)|(k<<1);
		if((m < res->n) && (res->a[k].tn == res->a[m].tn) && (res->a[k].rev == res->a[m].rev) && 
		(res->a[k].pe) && (res->a[m].pe) && (res->a[k].dir == 0 && res->a[m].dir == 1)) {
			z |= 1;
		}
		kv_push(uint64_t, mk->idx, z);
	}
	radix_sort_gfa64(mk->idx.a, mk->idx.a+mk->idx.n);
	for (z = 0; z < mk->idx.n; z++) {
		k = ((uint32_t)mk->idx.a[z])>>1; 
		kv_pushp(ul_ov_t, mk->srt, &p);
		p->tn = res->a[k].tn; p->qn = res->a[k].el;
		p->qs = res->a[k].qs; p->qe = res->a[k].qe; 
		p->ts = res->a[k].ts; p->te = res->a[k].te; 
		p->rev = res->a[k].rev; p->el = res->a[k].full; 
		m = res->a[k].dir; m <<= 1; m |= (uint32_t)res->a[k].pe; p->sec = m;

		if(mk->idx.a[z]&1) {
			k++; 
			kv_pushp(ul_ov_t, mk->srt, &p);
			p->tn = res->a[k].tn; p->qn = res->a[k].el;
			p->qs = res->a[k].qs; p->qe = res->a[k].qe; 
			p->ts = res->a[k].ts; p->te = res->a[k].te; 
			p->rev = res->a[k].rev; p->el = res->a[k].full;
			m = res->a[k].dir; m <<= 1; m |= (uint32_t)res->a[k].pe; p->sec = m;
		}
	}

	for (k = 0; k < res->n; k++) {
		p = &(mk->srt.a[k]);
		res->a[k].tn = p->tn; res->a[k].el = p->qn;
		res->a[k].qs = p->qs; res->a[k].qe = p->qe; 
		res->a[k].ts = p->ts; res->a[k].te = p->te; 
		res->a[k].rev = p->rev; res->a[k].full = p->el; res->a[k].rn = (uint32_t)-1;
		m = p->sec; res->a[k].pe = m&1; res->a[k].dir = m>>1;
	}

	// for (k = 0; k < res->n; k++) {
	// 	int64_t fqs, fqe, fts, fte, ql = ug->g->seq[rid].len, tl = ug->g->seq[res->a[k].tn].len;
	// 	p = &(mk->srt.a[k]); extend_batch(p, ql, tl, &fqs, &fqe, &fts, &fte);
	// 	///if(res->a[k].full == 0) 
	// 	{
	// 		// fprintf(stderr, "+[M::%s]\tutg%.6ul\tq::[%u,\t%u)[%ld,\t%ld)\t%c\tutg%.6ul\tt::[%u,\t%u)[%ld,\t%ld)\tel::%u\tfu::%u\n", __func__, 
	// 		// rid+1, res->a[k].qs, res->a[k].qe, fqs, fqe, "+-"[res->a[k].rev], res->a[k].tn+1, res->a[k].ts, res->a[k].te, fts, fte, res->a[k].el, res->a[k].full);
	// 		// fprintf(stderr, "utg%.6ul\tq::[%u,\t%u)[%ld,\t%ld)\t%c\tutg%.6ul\tt::[%u,\t%u)[%ld,\t%ld)\tel::%u\tfu::%u\n", 
	// 		// rid+1, res->a[k].qs, res->a[k].qe, fqs, fqe, "+-"[res->a[k].rev], res->a[k].tn+1, res->a[k].ts, res->a[k].te, fts, fte, res->a[k].el, res->a[k].full);		
	// 	}
	// }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
andq $0x0, (%rsi)
andq $0x0, 0x20(%rsi)
movq %rdi, %r14
movq 0x8(%rdi), %rax
cmpq %rax, 0x8(%rsi)
jae 0x1809b6
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(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq 0x8(%r14), %rax
cmpq %rax, 0x28(%rbx)
jae 0x180a08
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
incq %rcx
movq %rcx, 0x28(%rbx)
movq 0x18(%rbx), %rdi
imulq $0x1c, %rcx, %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
pushq $0x1e
popq %r12
pushq $0x1
popq %r13
xorl %ebp, %ebp
leaq -0x1(%r13), %rax
movq 0x8(%r14), %rcx
cmpq %rcx, %rax
jae 0x180ac5
movq (%r14), %rax
movl -0x3c(%rax,%r12,2), %edx
movq %rdx, %r15
shlq $0x20, %r15
orq %rbp, %r15
cmpq %rcx, %r13
jae 0x180a78
cmpl -0x1c(%rax,%r12,2), %edx
jne 0x180a78
movb -0x20(%rax,%r12,2), %cl
movb (%rax,%r12,2), %al
movl %eax, %edx
xorb %cl, %dl
setns %dl
movl %ecx, %esi
andb %al, %sil
andb $0x20, %sil
shrb $0x5, %sil
testb $0x1f, %cl
sete %cl
andb %dl, %cl
andb %sil, %cl
andb $0x1f, %al
cmpb $0x1, %al
sete %al
andb %cl, %al
movzbl %al, %eax
orq %rax, %r15
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x180aaa
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r15, (%rax,%rcx,8)
addq $0x10, %r12
incq %r13
addq $0x2, %rbp
jmp 0x180a12
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x156b0d
xorl %r12d, %r12d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
cmpq (%rbx), %r12
jae 0x180c87
movq 0x10(%rbx), %rcx
movq 0x18(%rbx), %rax
movl (%rcx,%r12,8), %r13d
movq 0x20(%rbx), %rcx
cmpq 0x28(%rbx), %rcx
jne 0x180b22
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x28(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x20(%rbx), %rcx
shrl %r13d
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
imulq $0x1c, %rcx, %rcx
movq (%r14), %rdx
movq %r13, %rsi
shlq $0x5, %rsi
movl (%rdx,%rsi), %edi
movl %edi, 0xc(%rax,%rcx)
movq 0x8(%rdx,%rsi), %rdi
movq %rdi, (%rax,%rcx)
movl 0x10(%rdx,%rsi), %edi
movl %edi, 0x8(%rax,%rcx)
movq 0x14(%rdx,%rsi), %rdi
movq %rdi, 0x10(%rax,%rcx)
movl 0x1c(%rdx,%rsi), %edx
andl $-0x80, %edx
shll $0x18, %edx
movl 0x18(%rax,%rcx), %edi
andl %ebp, %edi
orl %edx, %edi
movl %edi, 0x18(%rax,%rcx)
movq (%r14), %r8
movl 0x1c(%r8,%rsi), %r8d
andl $0x40, %r8d
shll $0x18, %r8d
andl $0xbfffffff, %edi       # imm = 0xBFFFFFFF
orl %r8d, %edi
movl %edi, 0x18(%rax,%rcx)
movq (%r14), %rdi
movzbl 0x1c(%rdi,%rsi), %esi
movl %esi, %edi
andl $0x1f, %edi
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rdi,2), %esi
orl %edx, %esi
orl %r8d, %esi
movl %esi, 0x18(%rax,%rcx)
movq 0x10(%rbx), %rax
testb $0x1, (%rax,%r12,8)
je 0x180c7f
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
cmpq 0x28(%rbx), %rcx
jne 0x180bf2
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x28(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x20(%rbx), %rcx
incq %r13
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
imulq $0x1c, %rcx, %rcx
movq (%r14), %rdx
shlq $0x5, %r13
movl (%rdx,%r13), %esi
movl %esi, 0xc(%rax,%rcx)
movq 0x8(%rdx,%r13), %rsi
movq %rsi, (%rax,%rcx)
movl 0x10(%rdx,%r13), %esi
movl %esi, 0x8(%rax,%rcx)
movq 0x14(%rdx,%r13), %rsi
movq %rsi, 0x10(%rax,%rcx)
movl 0x1c(%rdx,%r13), %edx
andl $-0x80, %edx
shll $0x18, %edx
movl 0x18(%rax,%rcx), %esi
andl %ebp, %esi
orl %edx, %esi
movl %esi, 0x18(%rax,%rcx)
movq (%r14), %rdi
movl 0x1c(%rdi,%r13), %edi
andl $0x40, %edi
shll $0x18, %edi
andl $0xbfffffff, %esi       # imm = 0xBFFFFFFF
orl %edi, %esi
movl %esi, 0x18(%rax,%rcx)
movq (%r14), %rsi
movzbl 0x1c(%rsi,%r13), %esi
movl %esi, %r8d
andl $0x1f, %r8d
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%r8,2), %esi
orl %edx, %esi
orl %edi, %esi
movl %esi, 0x18(%rax,%rcx)
incq %r12
jmp 0x180add
pushq $0x1c
popq %rax
pushq $0x18
popq %rcx
xorl %edx, %edx
cmpq 0x8(%r14), %rdx
jae 0x180d47
movq 0x18(%rbx), %rsi
movl -0xc(%rsi,%rcx), %edi
movq (%r14), %r8
movl %edi, -0x1c(%r8,%rax)
movl -0x18(%rsi,%rcx), %edi
movl %edi, -0x14(%r8,%rax)
movsd -0x8(%rsi,%rcx), %xmm0
movsd -0x14(%rsi,%rcx), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movups %xmm1, -0x10(%r8,%rax)
movb (%r8,%rax), %dil
movb 0x3(%rsi,%rcx), %r9b
movl %r9d, %r10d
andb $-0x80, %r10b
andb $0x7f, %dil
orb %r10b, %dil
movb %dil, (%r8,%rax)
movq (%r14), %rdi
movb (%rdi,%rax), %r8b
andb $0x40, %r9b
andb $-0x41, %r8b
orb %r9b, %r8b
movb %r8b, (%rdi,%rax)
movq (%r14), %rdi
orl $-0x1, -0x18(%rdi,%rax)
movb (%rsi,%rcx), %sil
movb (%rdi,%rax), %r8b
movl %esi, %r9d
shlb $0x5, %r9b
andb $0x20, %r9b
andb $-0x21, %r8b
orb %r9b, %r8b
movb %r8b, (%rdi,%rax)
shrb %sil
movq (%r14), %rdi
movb (%rdi,%rax), %r8b
andb $0x1f, %sil
andb $-0x20, %r8b
orb %sil, %r8b
movb %r8b, (%rdi,%rax)
incq %rdx
addq $0x20, %rax
addq $0x1c, %rcx
jmp 0x180c8f
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq