// File: /workspace/github_cpp_makefile_star2_O1/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 0x1c24d2
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 0x1c2534
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 0x1c25fa
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 0x1c25a5
cmpl -0x1c(%rcx,%r15,2), %edx
jne 0x1c25a5
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 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1c25d3
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r13, (%rcx,%rax,8)
movq 0x8(%r14), %rax
addq $0x2, %r12
addq $0x10, %r15
cmpq %rax, %rbp
jb 0x1c254c
movq (%rbx), %rax
movq 0x10(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x194fef
cmpq $0x0, (%rbx)
je 0x1c27fb
xorl %r15d, %r15d
movl $0x2, %r12d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x10(%rbx), %rax
movl (%rax,%r15,8), %r13d
movq 0x28(%rbx), %rax
cmpq %rax, 0x20(%rbx)
jne 0x1c265b
leaq (%rax,%rax), %rcx
testq %rax, %rax
cmoveq %r12, %rcx
movq %rcx, 0x28(%rbx)
movq 0x18(%rbx), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
shrl %r13d
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x20(%rbx)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdx, %rcx
movq (%r14), %rsi
movq %r13, %rdx
shlq $0x5, %rdx
movl (%rsi,%rdx), %edi
movl %edi, 0xc(%rax,%rcx)
movl 0x8(%rsi,%rdx), %edi
movl %edi, (%rax,%rcx)
movl 0xc(%rsi,%rdx), %edi
movl %edi, 0x4(%rax,%rcx)
movl 0x10(%rsi,%rdx), %edi
movl %edi, 0x8(%rax,%rcx)
movl 0x14(%rsi,%rdx), %edi
movl %edi, 0x10(%rax,%rcx)
movl 0x18(%rsi,%rdx), %edi
movl %edi, 0x14(%rax,%rcx)
movl 0x1c(%rsi,%rdx), %esi
andl $-0x80, %esi
shll $0x18, %esi
movl 0x18(%rax,%rcx), %edi
andl %ebp, %edi
orl %esi, %edi
movl %edi, 0x18(%rax,%rcx)
movq (%r14), %r8
movl 0x1c(%r8,%rdx), %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,%rdx), %edx
movl %edx, %edi
andl $0x1f, %edi
shrl $0x5, %edx
andl $0x1, %edx
leal (%rdx,%rdi,2), %edx
orl %esi, %edx
orl %r8d, %edx
movl %edx, 0x18(%rax,%rcx)
movq 0x10(%rbx), %rax
testb $0x1, (%rax,%r15,8)
je 0x1c27ef
movq 0x28(%rbx), %rax
cmpq %rax, 0x20(%rbx)
jne 0x1c2743
leaq (%rax,%rax), %rcx
testq %rax, %rax
cmoveq %r12, %rcx
movq %rcx, 0x28(%rbx)
movq 0x18(%rbx), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
incq %r13
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rdx
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)
movl 0x8(%rdx,%r13), %esi
movl %esi, (%rax,%rcx)
movl 0xc(%rdx,%r13), %esi
movl %esi, 0x4(%rax,%rcx)
movl 0x10(%rdx,%r13), %esi
movl %esi, 0x8(%rax,%rcx)
movl 0x14(%rdx,%r13), %esi
movl %esi, 0x10(%rax,%rcx)
movl 0x18(%rdx,%r13), %esi
movl %esi, 0x14(%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 0x1c2622
cmpq $0x0, 0x8(%r14)
je 0x1c28d7
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)
movl -0x14(%rsi,%rcx), %edi
movl %edi, -0x10(%r8,%rax)
movl -0x10(%rsi,%rcx), %edi
movl %edi, -0xc(%r8,%rax)
movl -0x8(%rsi,%rcx), %edi
movl %edi, -0x8(%r8,%rax)
movl -0x4(%rsi,%rcx), %edi
movl %edi, -0x4(%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 0x1c2812
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq