// File: /workspace/github_c_makefile_star2_O2/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, %r13
movl %edx, %r15d
movq %rsi, %r14
leaq 0x9a62(%rip), %rdx      # 0x182c0
xorl %esi, %esi
callq 0xcee5
testl %eax, %eax
js 0xebba
movl %eax, %r12d
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0xc692
movzbl 0x14(%r14), %eax
imulq $0x18, %rax, %rax
xorl %ebx, %ebx
movq %r14, %rcx
cmpq %rbx, %rax
je 0xeb9d
movq 0x80(%rcx), %r14
cmpl %r12d, (%r14,%rbx)
je 0xe8a5
addq $0x18, %rbx
jmp 0xe889
movq %rcx, -0x68(%rbp)
cmpl $0x2, 0x4(%r14,%rbx)
jne 0xebd3
movq 0x10(%r14,%rbx), %rax
movzbl (%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl 0x30(%r13), %ecx
shll $0x1f, %eax
orl %eax, %ecx
movl %ecx, 0x30(%r13)
movl %r15d, -0x2c(%rbp)
movslq %r15d, %rdi
movq %rsp, %r15
movq %r15, -0x48(%rbp)
leaq 0xf(,%rdi,4), %r12
andq $-0x10, %r12
subq %r12, %r15
movq %r15, %rsp
negq %r12
movq %rdi, -0x50(%rbp)
movq %r13, %rsi
callq 0xdc03
movq %r15, %r11
movq -0x48(%rbp), %rax
andl $0x0, (%rax,%r12)
pushq $0x1
popq %r15
xorl %r12d, %r12d
movq %r13, -0x38(%rbp)
movq %r13, %rdi
movl $0x0, -0x30(%rbp)
movq %r11, -0x40(%rbp)
cmpq -0x50(%rbp), %r15
jge 0xe960
leaq 0x40(%rdi), %r13
movq %r13, %rsi
callq 0xf1a5
movq -0x40(%rbp), %r11
testl %eax, %eax
movl %r15d, %eax
cmovel %r12d, %eax
cmpl %r12d, %eax
pushq $0x1
popq %rcx
movl -0x30(%rbp), %edx
cmovel %ecx, %edx
movl %edx, -0x30(%rbp)
movl %eax, (%r11,%r15,4)
incq %r15
movq %r13, %rdi
movl %eax, %r12d
jmp 0xe923
movq -0x68(%rbp), %rsi
movzwl 0x12(%rsi), %eax
leal 0xf(,%rax,4), %eax
andl $-0x10, %eax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
andl $0x0, (%r13)
xorl %edx, %edx
movl -0x2c(%rbp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, -0x58(%rbp)
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
addq $0x24, %rax
movq %rax, -0x70(%rbp)
pushq $0x2
popq %r15
cmpq -0x58(%rbp), %rdx
je 0xeafb
movl (%r11,%rdx,4), %eax
movq %rdx, -0x60(%rbp)
cmpq %rax, %rdx
jne 0xe9d7
movq -0x60(%rbp), %r12
shlq $0x6, %r12
addq -0x38(%rbp), %r12
movl 0x30(%r12), %ecx
btl $0x1e, %ecx
jb 0xe9e0
movq -0x60(%rbp), %rdx
incq %rdx
jmp 0xe9a9
andl $0xdfffffff, %ecx       # imm = 0xDFFFFFFF
movl %ecx, 0x30(%r12)
movq 0x10(%rsi), %rcx
movl %ecx, %edx
shrl $0x10, %edx
pushq $0x1
popq %rsi
cmpq %rdx, %rsi
jae 0xea07
andl $0x0, (%r13,%rsi,4)
incq %rsi
jmp 0xe9f7
movq -0x70(%rbp), %rdx
xorl %esi, %esi
cmpq %rsi, -0x48(%rbp)
je 0xea5e
movl 0xc(%rdx), %edi
btl $0x1e, %edi
jae 0xea55
pushq $0x1
popq %r8
cmpl %eax, (%r11,%rsi,4)
jne 0xea33
andl $0x1fffffff, %edi       # imm = 0x1FFFFFFF
movl %r8d, (%r13,%rdi,4)
jmp 0xea55
movl (%rdx), %r9d
movl 0x24(%r12), %r10d
movl 0x28(%r12), %r8d
addl %r10d, %r8d
cmpl %r8d, %r9d
jge 0xea55
addl 0x4(%rdx), %r9d
pushq $0x3
popq %r8
cmpl %r9d, %r10d
jl 0xea26
incq %rsi
addq $0x40, %rdx
jmp 0xea0d
movq 0x38(%r12), %rdi
shrq $0x28, %rcx
movslq 0x4(%r14,%rbx), %rsi
imulq %rcx, %rsi
callq 0x2340
movq -0x40(%rbp), %r11
movq %rax, 0x38(%r12)
andl $0x0, 0x2c(%r12)
xorl %eax, %eax
xorl %ecx, %ecx
movq -0x68(%rbp), %rsi
movq 0x10(%rsi), %rdx
shrq $0x28, %rdx
cmpl %edx, %ecx
jae 0xe9d7
movslq %eax, %rdx
xorl %esi, %esi
movslq 0x4(%r14,%rbx), %rdi
cmpq %rdi, %rsi
jge 0xeaf3
movq 0x10(%r14,%rbx), %rdi
addq %rdx, %rdi
movzbl (%rsi,%rdi), %r8d
movl %r15d, %edi
cmpq $0x2, %r8
jb 0xeacc
shrl %r8d
decl %r8d
movl (%r13,%r8,4), %edi
movq 0x38(%r12), %r8
movslq 0x2c(%r12), %r9
leal 0x1(%r9), %r10d
movl %r10d, 0x2c(%r12)
movb %dil, (%r8,%r9)
cmpl $0x3, %edi
jne 0xeaee
orb $0x20, 0x33(%r12)
incq %rsi
jmp 0xeaa1
incl %ecx
addl %eax, %edi
movl %edi, %eax
jmp 0xea88
cmpl $0x0, -0x30(%rbp)
je 0xeb8b
movq -0x50(%rbp), %rdx
shlq $0x6, %rdx
movq %rsp, %r15
subq %rdx, %r15
movq %r15, %rsp
movq %r15, %rdi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x2280
movq -0x40(%rbp), %r9
movl -0x2c(%rbp), %ecx
decl %ecx
xorl %r8d, %r8d
xorl %eax, %eax
cmpq %r8, -0x58(%rbp)
je 0xeb8e
movl (%r9,%r8,4), %edx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rdx, %r8
sete %sil
setne %dil
movl %eax, %edx
cmovnel %ecx, %edx
subl %edi, %ecx
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(%rbx,%rdx)
movups %xmm2, 0x20(%rbx,%rdx)
movups %xmm1, 0x10(%rbx,%rdx)
movups %xmm0, (%rbx,%rdx)
incq %r8
addq $0x40, %r15
jmp 0xeb33
movl -0x2c(%rbp), %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa9bd(%rip), %rdi      # 0x19561
leaq 0xa88d(%rip), %rsi      # 0x19438
leaq 0xa96b(%rip), %rcx      # 0x1951d
pushq $0x19
popq %rdx
callq 0x21b0
leaq 0xa951(%rip), %rdi      # 0x19512
leaq 0xa870(%rip), %rsi      # 0x19438
leaq 0xa94e(%rip), %rcx      # 0x1951d
pushq $0x15
jmp 0xebb4
leaq 0xa994(%rip), %rdi      # 0x1956e
leaq 0xa857(%rip), %rsi      # 0x19438
leaq 0xa935(%rip), %rcx      # 0x1951d
pushq $0x1b
jmp 0xebb4