// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movq %rsi, %rbx
leaq 0xb7ac(%rip), %rdx      # 0x1e2c0
xorl %esi, %esi
callq 0x106de
testl %eax, %eax
js 0x12f24
movl %eax, %r12d
movq %rbx, %rdi
movl $0x8, %esi
callq 0xf0a4
movq 0x10(%rbx), %rdx
shrq $0x20, %rdx
testb %dl, %dl
setne %cl
movq -0x38(%rbp), %rsi
je 0x12f05
movq 0x80(%rbx), %rax
cmpl %r12d, (%rax)
jne 0x12b5a
xorl %eax, %eax
jmp 0x12b87
movzbl %dl, %ecx
addq $0x18, %rax
leaq -0x1(%rcx), %r8
xorl %edx, %edx
cmpq %rdx, %r8
je 0x12f05
leaq 0x18(%rax), %rdi
incq %rdx
cmpl %r12d, (%rax)
movq %rdi, %rax
jne 0x12b67
cmpq %rcx, %rdx
setb %cl
movl %edx, %eax
testb %cl, %cl
je 0x12f05
movq %rbx, -0x48(%rbp)
movq 0x80(%rbx), %rcx
leaq (%rax,%rax,2), %rax
cmpl $0x2, 0x4(%rcx,%rax,8)
jne 0x12f43
leaq (%rcx,%rax,8), %r12
movq 0x10(%r12), %rax
movzbl (%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl 0x30(%rsi), %ecx
shll $0x1f, %eax
orl %eax, %ecx
movl %ecx, 0x30(%rsi)
movslq %r14d, %r15
movq %rsp, %rbx
movl %r14d, -0x30(%rbp)
leaq 0xf(,%r15,4), %r14
andq $-0x10, %r14
movq %rbx, %r13
subq %r14, %r13
movq %r13, %rsp
negq %r14
movq %r15, %rdi
callq 0x11998
movq %r13, %r9
movq -0x38(%rbp), %rsi
movl $0x0, (%rbx,%r14)
movl -0x30(%rbp), %ecx
movb $0x1, %al
movq %r15, -0x58(%rbp)
cmpl $0x2, %r15d
movq -0x48(%rbp), %r8
movq %r13, -0x40(%rbp)
jl 0x12c94
movl %ecx, %ebx
leaq 0x68(%rsi), %r15
xorl %r14d, %r14d
movl $0x1, %r13d
movl $0x1, %ecx
movl -0x48(%r15), %eax
subl -0x8(%r15), %eax
jne 0x12c64
movl -0x44(%r15), %eax
subl -0x4(%r15), %eax
jne 0x12c64
movl -0x40(%r15), %eax
subl (%r15), %eax
jne 0x12c64
movq -0x50(%r15), %rdi
movq -0x10(%r15), %rsi
callq 0x2230
movl $0x1, %ecx
movq -0x40(%rbp), %r9
movq -0x48(%rbp), %r8
movq -0x38(%rbp), %rsi
testl %eax, %eax
je 0x12c6d
movl %r13d, %eax
jmp 0x12c72
movl -0x4(%r9,%r13,4), %eax
movl %eax, (%r9,%r13,4)
cmpl -0x4(%r9,%r13,4), %eax
cmovel %ecx, %r14d
incq %r13
addq $0x40, %r15
cmpq %r13, %rbx
jne 0x12c29
testl %r14d, %r14d
sete %al
movl -0x30(%rbp), %ecx
movb %al, -0x29(%rbp)
movzwl 0x12(%r8), %eax
leal 0xf(,%rax,4), %eax
andl $-0x10, %eax
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
movl $0x0, (%rbx)
testl %ecx, %ecx
jle 0x12e58
leaq 0x4(%rbx), %rax
movq %rax, -0x60(%rbp)
movl -0x30(%rbp), %r10d
leaq 0x24(%rsi), %rax
movq %rax, -0x70(%rbp)
xorl %r13d, %r13d
movq %r10, -0x50(%rbp)
movl (%r9,%r13,4), %r14d
cmpq %r14, %r13
jne 0x12e4c
movq %r13, %r15
shlq $0x6, %r15
addq %rsi, %r15
movl 0x30(%r15), %eax
btl $0x1e, %eax
jae 0x12e4c
andl $0xdfffffff, %eax       # imm = 0xDFFFFFFF
movl %eax, 0x30(%r15)
movq 0x10(%r8), %r11
cmpl $0x20000, %r11d         # imm = 0x20000
jb 0x12d45
movl %r11d, %edx
shrl $0x10, %edx
cmpl $0x3, %edx
movl $0x2, %eax
cmovbl %eax, %edx
decl %edx
shlq $0x2, %rdx
movq -0x60(%rbp), %rdi
xorl %esi, %esi
movq %r11, -0x68(%rbp)
callq 0x21e0
movq -0x68(%rbp), %r11
movq -0x50(%rbp), %r10
movq -0x40(%rbp), %r9
movq -0x70(%rbp), %rax
xorl %ecx, %ecx
movl 0xc(%rax), %edx
btl $0x1e, %edx
jae 0x12d88
movl $0x1, %esi
cmpl %r14d, (%r9,%rcx,4)
jne 0x12d6a
andl $0x1fffffff, %edx       # imm = 0x1FFFFFFF
movl %esi, (%rbx,%rdx,4)
jmp 0x12d88
movl (%rax), %edi
movl 0x24(%r15), %r8d
movl 0x28(%r15), %esi
addl %r8d, %esi
cmpl %esi, %edi
jge 0x12d88
addl 0x4(%rax), %edi
movl $0x3, %esi
cmpl %edi, %r8d
jl 0x12d5f
incq %rcx
addq $0x40, %rax
cmpq %rcx, %r10
jne 0x12d4b
movq 0x38(%r15), %rdi
shrq $0x28, %r11
movslq 0x4(%r12), %rsi
imulq %r11, %rsi
callq 0x2330
movq -0x50(%rbp), %r10
movq -0x40(%rbp), %r9
movq %rax, 0x38(%r15)
movl $0x0, 0x2c(%r15)
movq -0x48(%rbp), %r8
movq 0x10(%r8), %rax
shrq $0x28, %rax
movq -0x38(%rbp), %rsi
je 0x12e4c
xorl %eax, %eax
xorl %ecx, %ecx
movl 0x4(%r12), %edi
testl %edi, %edi
jle 0x12e3c
movslq %ecx, %rdx
xorl %r11d, %r11d
movq 0x10(%r12), %rdi
addq %rdx, %rdi
movzbl (%r11,%rdi), %r8d
movl $0x2, %edi
cmpq $0x2, %r8
jb 0x12e05
shrl %r8d
decl %r8d
movl (%rbx,%r8,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 0x12e23
orb $0x20, 0x33(%r15)
incq %r11
movslq 0x4(%r12), %rdi
cmpq %rdi, %r11
jl 0x12de3
movq -0x48(%rbp), %r8
movq -0x40(%rbp), %r9
movq -0x50(%rbp), %r10
incl %eax
addl %edi, %ecx
movq 0x10(%r8), %rdx
shrq $0x28, %rdx
cmpl %edx, %eax
jb 0x12dd4
incq %r13
cmpq %r10, %r13
jne 0x12cd8
cmpb $0x0, -0x29(%rbp)
je 0x12e66
movl -0x30(%rbp), %eax
jmp 0x12ef6
movq -0x58(%rbp), %rdx
shlq $0x6, %rdx
movq %rsp, %r15
subq %rdx, %r15
movq %r15, %rsp
movq %r15, %rdi
callq 0x2280
movl -0x30(%rbp), %ecx
testl %ecx, %ecx
jle 0x12ef4
movl %ecx, %r10d
decl %ecx
xorl %edx, %edx
xorl %eax, %eax
movq -0x38(%rbp), %r9
movq -0x40(%rbp), %r11
movl (%r11,%rdx,4), %esi
xorl %edi, %edi
xorl %r8d, %r8d
cmpq %rsi, %rdx
sete %dil
movl %eax, %esi
cmovnel %ecx, %esi
setne %r8b
subl %r8d, %ecx
addl %edi, %eax
movslq %esi, %rsi
shlq $0x6, %rsi
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups 0x30(%r15), %xmm3
movups %xmm3, 0x30(%r9,%rsi)
movups %xmm2, 0x20(%r9,%rsi)
movups %xmm1, 0x10(%r9,%rsi)
movups %xmm0, (%r9,%rsi)
incq %rdx
addq $0x40, %r15
cmpq %rdx, %r10
jne 0x12e97
jmp 0x12ef6
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc68c(%rip), %rdi      # 0x1f598
leaq 0xc55f(%rip), %rsi      # 0x1f472
leaq 0xc63a(%rip), %rcx      # 0x1f554
movl $0x19, %edx
callq 0x21b0
leaq 0xc61e(%rip), %rdi      # 0x1f549
leaq 0xc540(%rip), %rsi      # 0x1f472
leaq 0xc61b(%rip), %rcx      # 0x1f554
movl $0x15, %edx
callq 0x21b0
leaq 0xc65b(%rip), %rdi      # 0x1f5a5
leaq 0xc521(%rip), %rsi      # 0x1f472
leaq 0xc5fc(%rip), %rcx      # 0x1f554
movl $0x1b, %edx
callq 0x21b0