// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdi, %r14
xorl %eax, %eax
movq %rax, (%rsi)
movq %rax, 0x20(%rsi)
movq 0x8(%rdi), %rax
cmpq %rax, 0x8(%rsi)
jae 0x1cc4ee
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 0x1cc550
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, 0x28(%rbx)
movq 0x18(%rbx), %rdi
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax), %rsi
incq %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x1cc613
movl $0x1e, %r15d
xorl %r12d, %r12d
xorl %ebp, %ebp
incq %rbp
movq (%r14), %rcx
movl -0x3c(%rcx,%r15,2), %edx
movq %rdx, %r13
shlq $0x20, %r13
orq %r12, %r13
cmpq %rax, %rbp
jae 0x1cc5bd
cmpl -0x1c(%rcx,%r15,2), %edx
jne 0x1cc5bd
movb -0x20(%rcx,%r15,2), %al
movb (%rcx,%r15,2), %cl
movl %ecx, %edx
xorb %al, %dl
setns %dl
movl %eax, %esi
andb %cl, %sil
andb $0x20, %sil
shrb $0x5, %sil
testb $0x1f, %al
sete %al
andb %dl, %al
andb %sil, %al
andb $0x1f, %cl
cmpb $0x1, %cl
sete %cl
andb %al, %cl
movzbl %cl, %eax
orq %rax, %r13
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
cmpq 0x8(%rbx), %rax
jne 0x1cc5f1
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rbx)
movq %r13, (%rdi,%rax,8)
movq 0x8(%r14), %rax
addq $0x2, %r12
addq $0x10, %r15
cmpq %rax, %rbp
jb 0x1cc564
jmp 0x1cc617
movq 0x10(%rbx), %rdi
movq (%rbx), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x19dd99
cmpq $0x0, (%rbx)
je 0x1cc7f6
xorl %r15d, %r15d
movl $0x2, %r12d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x10(%rbx), %rcx
movq 0x18(%rbx), %rax
movl (%rcx,%r15,8), %r13d
movq 0x20(%rbx), %rdx
cmpq 0x28(%rbx), %rdx
jne 0x1cc67b
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
cmoveq %r12, %rcx
movq %rcx, 0x28(%rbx)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x20(%rbx), %rdx
shrl %r13d
leaq 0x1(%rdx), %rcx
movq %rcx, 0x20(%rbx)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdx, %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,%r15,8)
je 0x1cc7ea
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rdx
cmpq 0x28(%rbx), %rdx
jne 0x1cc756
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
cmoveq %r12, %rcx
movq %rcx, 0x28(%rbx)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x20(%rbx), %rdx
incq %r13
leaq 0x1(%rdx), %rcx
movq %rcx, 0x20(%rbx)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdx, %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 %r15
cmpq (%rbx), %r15
jb 0x1cc63b
cmpq $0x0, 0x8(%r14)
je 0x1cc8c3
movl $0x1c, %eax
movl $0x18, %ecx
xorl %edx, %edx
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
movl $0xffffffff, -0x18(%rdi,%rax) # imm = 0xFFFFFFFF
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
cmpq 0x8(%r14), %rdx
jb 0x1cc80d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq