// File: /workspace/github_c_makefile_star2_O3/lh3[P]bgt/atomic.c
// Function: bcf_atom_gen_at
static int bcf_atom_gen_at(const bcf_hdr_t *h, bcf1_t *b, int n, bcf_atom_t *a)
{
	int i, j, k, *eq, id_GT, *tr, has_dup = 0;
	bcf_fmt_t *gt;

	id_GT = bcf_id2int(h, BCF_DT_ID, "GT");
	assert(id_GT >= 0);
	bcf_unpack(b, BCF_UN_FMT);
	for (i = 0; i < b->n_fmt; ++i)
		if (b->d.fmt[i].id == id_GT) break;
	assert(i < b->n_fmt);
	gt = &b->d.fmt[i];
	assert(gt->n == 2);
	a->phased = gt->p[0]&1;

	eq = (int*)alloca(n * sizeof(int));
	ks_introsort(atom, n, a);
	for (i = 1, eq[0] = 0; i < n; ++i) {
		eq[i] = bcf_atom_cmp(&a[i-1], &a[i])? i : eq[i-1]; // eq[k] points to the smallest allele that is the same as allele k
		if (eq[i] == eq[i-1]) has_dup = 1;
	}

	tr = (int*)alloca(b->n_allele * sizeof(int));
	tr[0] = 0;
	for (k = 0; k < n; ++k) {
		int m;
		bcf_atom_t *ak = &a[k];
		if (eq[k] != k) continue; // duplicated atom
		if (!ak->from_new) continue;
		ak->has_multi = 0;
		for (i = 1; i < b->n_allele; ++i) tr[i] = 0; // tr[] translates allele number in BCF to the 4-value allele number
		for (i = 0; i < n; ++i) { // WARNING: quadratic in the number of atoms
			if (!a[i].from_new) continue;
			if (eq[i] == eq[k]) // identical allele
				tr[a[i].anum] = 1;
			else if (a[i].pos < ak->pos + ak->rlen && ak->pos < a[i].pos + a[i].rlen) // overlap
				tr[a[i].anum] = 3;
		}
		ak->gt = (uint8_t*)realloc(ak->gt, b->n_sample * gt->n);
		ak->n_gt = 0;
		for (i = 0, m = 0; i < b->n_sample; ++i, m += gt->n) {
			for (j = 0; j < gt->n; ++j) {
				int c = (int)(gt->p[m+j] >> 1) - 1;
				c = c < 0? 2 : tr[c];
				ak->gt[ak->n_gt++] = c;
				if (c == 3) ak->has_multi = 1;
			}
		}
	}

	if (has_dup) {
		bcf_atom_t *swap;
		swap = (bcf_atom_t*)alloca(n * sizeof(bcf_atom_t));
		memcpy(swap, a, n * sizeof(bcf_atom_t));
		for (i = j = 0, k = n - 1; i < n; ++i) {
			if (eq[i] == i) a[j++] = swap[i];
			else a[k--] = swap[i];
		}
		n = j;
	}
	return n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, -0x38(%rbp)
movl %edx, %r13d
movq %rsi, %rbx
leaq 0xb3d4(%rip), %rdx      # 0x1e2c0
xorl %esi, %esi
callq 0x1088a
testl %eax, %eax
js 0x132f3
movl %eax, %r12d
movq %rbx, %rdi
movl $0x8, %esi
callq 0xf116
movq 0x10(%rbx), %rcx
shrq $0x20, %rcx
testb %cl, %cl
movq -0x38(%rbp), %rsi
je 0x12f3b
movq 0x80(%rbx), %rax
movzbl %cl, %edx
xorl %ecx, %ecx
movq %rax, %rdi
cmpl %r12d, (%rdi)
je 0x12f5a
incq %rcx
addq $0x18, %rdi
cmpq %rcx, %rdx
jne 0x12f2a
leaq 0xc661(%rip), %rdi      # 0x1f5a3
leaq 0xc534(%rip), %rsi      # 0x1f47d
leaq 0xc60f(%rip), %rcx      # 0x1f55f
movl $0x19, %edx
callq 0x21b0
movq %rbx, -0x40(%rbp)
movl %ecx, %ecx
leaq (%rcx,%rcx,2), %rcx
cmpl $0x2, 0x4(%rax,%rcx,8)
jne 0x13312
leaq (%rax,%rcx,8), %r14
movq 0x10(%r14), %rax
movzbl (%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl 0x30(%rsi), %ecx
shll $0x1f, %eax
orl %eax, %ecx
movl %ecx, 0x30(%rsi)
movslq %r13d, %r12
movq %rsp, %rbx
leaq 0xf(,%r12,4), %r15
andq $-0x10, %r15
movq %rbx, %rax
subq %r15, %rax
movq %rax, -0x50(%rbp)
movq %rax, %rsp
negq %r15
movq %r12, %rdi
callq 0x11b24
movl $0x0, (%rbx,%r15)
movl %r13d, %r10d
movq %r12, -0x60(%rbp)
cmpl $0x2, %r12d
movq %r10, -0x48(%rbp)
movl %r13d, -0x30(%rbp)
jl 0x1307c
movq -0x38(%rbp), %rsi
movl 0x20(%rsi), %edx
leaq 0x68(%rsi), %rbx
movl $0x1, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
movq -0x40(%rbp), %r9
movl %edx, %eax
movl -0x8(%rbx), %edx
subl %edx, %eax
jne 0x13027
movl -0x44(%rbx), %eax
subl -0x4(%rbx), %eax
jne 0x13027
movl -0x40(%rbx), %eax
subl (%rbx), %eax
jne 0x13027
movq -0x50(%rbx), %rdi
movq -0x10(%rbx), %rsi
movl %edx, -0x58(%rbp)
callq 0x2230
movl -0x58(%rbp), %edx
movq -0x48(%rbp), %r10
movq -0x40(%rbp), %r9
movq -0x38(%rbp), %rsi
testl %eax, %eax
movl %r15d, %ecx
cmovel %r13d, %ecx
movq -0x50(%rbp), %rax
movl %ecx, (%rax,%r15,4)
movl $0x1, %eax
cmpl %r13d, %ecx
je 0x13045
movl %r12d, %eax
incq %r15
addq $0x40, %rbx
movl %ecx, %r13d
movl %eax, %r12d
cmpq %r15, %r10
jne 0x12ff0
testl %eax, %eax
sete %cl
movzwl 0x12(%r9), %eax
leal 0xf(,%rax,4), %eax
andl $-0x10, %eax
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
movl $0x0, (%rbx)
jmp 0x130ae
movq -0x40(%rbp), %r9
movzwl 0x12(%r9), %eax
leal 0xf(,%rax,4), %eax
andl $-0x10, %eax
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
movl $0x0, (%rbx)
movb $0x1, %cl
cmpl $0x1, %r13d
movq -0x38(%rbp), %rsi
jne 0x13251
movb %cl, -0x29(%rbp)
leaq 0x4(%rbx), %rax
movq %rax, -0x68(%rbp)
leaq 0x30(%rsi), %rax
movq %rax, -0x58(%rbp)
xorl %r12d, %r12d
movq -0x50(%rbp), %rax
movl (%rax,%r12,4), %r13d
cmpq %r13, %r12
jne 0x1323f
movq %r12, %r15
shlq $0x6, %r15
addq %rsi, %r15
movl 0x30(%r15), %eax
btl $0x1e, %eax
jae 0x1323f
andl $0xdfffffff, %eax       # imm = 0xDFFFFFFF
movl %eax, 0x30(%r15)
movq 0x10(%r9), %r11
cmpl $0x20000, %r11d         # imm = 0x20000
jb 0x13126
movl %r11d, %edx
shrl $0x10, %edx
decl %edx
shlq $0x2, %rdx
movq -0x68(%rbp), %rdi
xorl %esi, %esi
movq %r11, -0x70(%rbp)
callq 0x21e0
movq -0x70(%rbp), %r11
movq -0x48(%rbp), %r10
movq -0x58(%rbp), %rax
xorl %ecx, %ecx
movq -0x50(%rbp), %r9
movl (%rax), %edx
btl $0x1e, %edx
jae 0x1316d
movl $0x1, %esi
cmpl %r13d, (%r9,%rcx,4)
jne 0x1314e
andl $0x1fffffff, %edx       # imm = 0x1FFFFFFF
movl %esi, (%rbx,%rdx,4)
jmp 0x1316d
movl -0xc(%rax), %edi
movl 0x24(%r15), %r8d
movl 0x28(%r15), %esi
addl %r8d, %esi
cmpl %esi, %edi
jge 0x1316d
addl -0x8(%rax), %edi
movl $0x3, %esi
cmpl %edi, %r8d
jl 0x13143
incq %rcx
addq $0x40, %rax
cmpq %rcx, %r10
jne 0x13130
movq 0x38(%r15), %rdi
shrq $0x28, %r11
movslq 0x4(%r14), %rsi
imulq %r11, %rsi
callq 0x2330
movq -0x48(%rbp), %r10
movq %rax, 0x38(%r15)
movl $0x0, 0x2c(%r15)
movq -0x40(%rbp), %r9
movq 0x10(%r9), %rdx
movq %rdx, %rax
shrq $0x28, %rax
movq -0x38(%rbp), %rsi
je 0x1323f
movl 0x4(%r14), %edi
xorl %eax, %eax
xorl %ecx, %ecx
testl %edi, %edi
jle 0x1322f
movslq %ecx, %rdx
xorl %r11d, %r11d
movq 0x10(%r14), %rdi
addq %rdx, %rdi
movzbl (%r11,%rdi), %edi
cmpq $0x1, %rdi
ja 0x131f2
movq 0x38(%r15), %rdi
movslq 0x2c(%r15), %r8
leal 0x1(%r8), %r9d
movl %r9d, 0x2c(%r15)
movb $0x2, (%rdi,%r8)
jmp 0x13217
shrl %edi
decl %edi
movl (%rbx,%rdi,4), %edi
movq 0x38(%r15), %r8
movslq 0x2c(%r15), %r9
leal 0x1(%r9), %r10d
movl %r10d, 0x2c(%r15)
movb %dil, (%r8,%r9)
cmpl $0x3, %edi
jne 0x13217
orb $0x20, 0x33(%r15)
incq %r11
movslq 0x4(%r14), %rdi
cmpq %rdi, %r11
jl 0x131c9
movq -0x40(%rbp), %r9
movq 0x10(%r9), %rdx
movq -0x48(%rbp), %r10
incl %eax
addl %edi, %ecx
movq %rdx, %r8
shrq $0x28, %r8
cmpl %r8d, %eax
jb 0x131bf
incq %r12
cmpq %r10, %r12
jne 0x130c4
cmpb $0x0, -0x29(%rbp)
je 0x13259
movl -0x30(%rbp), %eax
jmp 0x132e4
movq -0x60(%rbp), %rdx
shlq $0x6, %rdx
movq %rsp, %r15
subq %rdx, %r15
movq %r15, %rsp
movq %r15, %rdi
callq 0x2280
movq -0x48(%rbp), %r11
movq -0x38(%rbp), %r9
movl -0x30(%rbp), %r8d
decl %r8d
xorl %ecx, %ecx
xorl %eax, %eax
movq -0x50(%rbp), %r10
movl (%r10,%rcx,4), %edx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rdx, %rcx
sete %sil
movl %eax, %edx
cmovnel %r8d, %edx
setne %dil
subl %edi, %r8d
addl %esi, %eax
movslq %edx, %rdx
shlq $0x6, %rdx
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups 0x30(%r15), %xmm3
movups %xmm3, 0x30(%r9,%rdx)
movups %xmm2, 0x20(%r9,%rdx)
movups %xmm1, 0x10(%r9,%rdx)
movups %xmm0, (%r9,%rdx)
incq %rcx
addq $0x40, %r15
cmpq %rcx, %r11
jne 0x13289
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc25a(%rip), %rdi      # 0x1f554
leaq 0xc17c(%rip), %rsi      # 0x1f47d
leaq 0xc257(%rip), %rcx      # 0x1f55f
movl $0x15, %edx
callq 0x21b0
leaq 0xc297(%rip), %rdi      # 0x1f5b0
leaq 0xc15d(%rip), %rsi      # 0x1f47d
leaq 0xc238(%rip), %rcx      # 0x1f55f
movl $0x1b, %edx
callq 0x21b0