// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
xorl %esi, %esi
leaq 0xfdb2(%rip), %rdx      # 0x272c0
callq 0x14b90
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jl 0x1751e
jmp 0x1753d
leaq 0x10ed5(%rip), %rdi     # 0x283fa
leaq 0x10df4(%rip), %rsi     # 0x28320
movl $0x15, %edx
leaq 0x10ecd(%rip), %rcx     # 0x28405
callq 0x21b0
movq -0x10(%rbp), %rdi
movl $0x8, %esi
callq 0x13d00
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
shrq $0x20, %rcx
andq $0xff, %rcx
cmpl %ecx, %eax
jge 0x17598
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x3c(%rbp), %eax
jne 0x1758b
jmp 0x17598
jmp 0x1758d
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x17552
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
shrq $0x20, %rcx
andq $0xff, %rcx
cmpl %ecx, %eax
jge 0x175b4
jmp 0x175d3
leaq 0x10e8e(%rip), %rdi     # 0x28449
leaq 0x10d5e(%rip), %rsi     # 0x28320
movl $0x19, %edx
leaq 0x10e37(%rip), %rcx     # 0x28405
callq 0x21b0
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpl $0x2, 0x4(%rax)
jne 0x175f9
jmp 0x17618
leaq 0x10e56(%rip), %rdi     # 0x28456
leaq 0x10d19(%rip), %rsi     # 0x28320
movl $0x1b, %edx
leaq 0x10df2(%rip), %rcx     # 0x28405
callq 0x21b0
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movzbl (%rax), %edx
movq -0x20(%rbp), %rax
movl 0x30(%rax), %ecx
shll $0x1f, %edx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl %edx, %ecx
movl %ecx, 0x30(%rax)
movslq -0x14(%rbp), %rax
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x38(%rbp)
movslq -0x14(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x161e0
movl $0x1, -0x24(%rbp)
movq -0x38(%rbp), %rax
movl $0x0, (%rax)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x17708
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x6, %rax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movslq -0x24(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
callq 0x186e0
cmpl $0x0, %eax
je 0x176b3
movl -0x24(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x176c6
movq -0x38(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %edx
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x38(%rbp), %rcx
movl -0x24(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x176f8
movl $0x1, -0x4c(%rbp)
jmp 0x176fa
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x17673
movq -0x10(%rbp), %rax
movzwl 0x12(%rax), %ecx
movl %ecx, %eax
leal 0xf(,%rax,4), %eax
andl $-0x10, %eax
movl %eax, %ecx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x17a05
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x2c(%rbp), %eax
je 0x17770
jmp 0x179f7
movq -0x68(%rbp), %rax
movl 0x30(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x17787
jmp 0x179f7
movq -0x68(%rbp), %rax
movl 0x30(%rax), %ecx
andl $0xdfffffff, %ecx       # imm = 0xDFFFFFFF
orl $0x0, %ecx
movl %ecx, 0x30(%rax)
movl $0x1, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
shrq $0x10, %rcx
andq $0xffff, %rcx           # imm = 0xFFFF
cmpl %ecx, %eax
jge 0x177d5
movq -0x48(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x177a1
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x178d3
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x30(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x1780a
jmp 0x178c5
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x38(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x17849
movq -0x48(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq -0x24(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
movl 0x30(%rcx), %ecx
andl $0x1fffffff, %ecx       # imm = 0x1FFFFFFF
movl %ecx, %ecx
movl $0x1, (%rax,%rcx,4)
jmp 0x178c3
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x24(%rax), %eax
movq -0x68(%rbp), %rcx
movl 0x24(%rcx), %ecx
movq -0x68(%rbp), %rdx
addl 0x28(%rdx), %ecx
cmpl %ecx, %eax
jge 0x178c1
movq -0x68(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x20(%rbp), %rcx
movslq -0x24(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
movl 0x24(%rcx), %ecx
movq -0x20(%rbp), %rdx
movslq -0x24(%rbp), %rsi
shlq $0x6, %rsi
addq %rsi, %rdx
addl 0x28(%rdx), %ecx
cmpl %ecx, %eax
jge 0x178c1
movq -0x48(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq -0x24(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rcx
movl 0x30(%rcx), %ecx
andl $0x1fffffff, %ecx       # imm = 0x1FFFFFFF
movl %ecx, %ecx
movl $0x3, (%rax,%rcx,4)
jmp 0x178c3
jmp 0x178c5
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x177dc
movq -0x68(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
shrq $0x28, %rax
movq -0x58(%rbp), %rcx
imull 0x4(%rcx), %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x68(%rbp), %rax
movl $0x0, 0x2c(%rax)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
shrq $0x28, %rcx
cmpl %ecx, %eax
jge 0x179f5
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x58(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x179d8
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x5c(%rbp), %ecx
addl -0x28(%rbp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
sarl %eax
subl $0x1, %eax
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jge 0x17976
movl $0x2, %eax
movl %eax, -0x80(%rbp)
jmp 0x17984
movq -0x48(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movb %al, %dl
movq -0x68(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x68(%rbp), %rsi
movl 0x2c(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x2c(%rsi)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
cmpl $0x3, -0x6c(%rbp)
jne 0x179c8
movq -0x68(%rbp), %rax
movl 0x30(%rax), %ecx
andl $0xdfffffff, %ecx       # imm = 0xDFFFFFFF
orl $0x20000000, %ecx       # imm = 0x20000000
movl %ecx, 0x30(%rax)
jmp 0x179ca
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x17939
jmp 0x179da
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x58(%rbp), %rax
movl 0x4(%rax), %eax
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x1791b
jmp 0x179f7
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1773c
cmpl $0x0, -0x4c(%rbp)
je 0x17ae6
movslq -0x14(%rbp), %rcx
shlq $0x6, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq -0x20(%rbp), %rsi
movslq -0x14(%rbp), %rdx
shlq $0x6, %rdx
callq 0x2290
movl $0x0, -0x28(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x17ae0
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x24(%rbp), %eax
jne 0x17a9f
movq -0x20(%rbp), %rdi
movl -0x28(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x28(%rbp)
cltq
shlq $0x6, %rax
addq %rax, %rdi
movq -0x78(%rbp), %rsi
movslq -0x24(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
movl $0x40, %edx
callq 0x2290
jmp 0x17ad0
movq -0x20(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
shlq $0x6, %rax
addq %rax, %rdi
movq -0x78(%rbp), %rsi
movslq -0x24(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
movl $0x40, %edx
callq 0x2290
jmp 0x17ad2
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x17a50
movl -0x28(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nop