// File: /workspace/github_c_makefile_star2_O2/lh3[P]bgt/bgt.c
// Function: bgtm_prepare
int bgtm_prepare(bgtm_t *bm)
{
	int i, j, m;
	kstring_t h = {0,0,0};
	bcf_hdr_t *h0;

	if (bm->n_bgt == 0) return 0;

	// prepare group and sample_idx
	for (i = bm->n_out = 0; i < bm->n_bgt; ++i) {
		bgt_prepare(bm->bgt[i]);
		bm->n_out += bm->bgt[i]->n_out;
	}
	bm->mgs = (int32_t*)realloc(bm->mgs, bm->n_out * 4);
	bm->group = (uint32_t*)realloc(bm->group, bm->n_out * 4);
	bm->sample_idx = (uint64_t*)realloc(bm->sample_idx, bm->n_out * 8);
	for (i = m = 0; i < bm->n_bgt; ++i) {
		bgt_t *bgt = bm->bgt[i];
		for (j = 0; j < bm->bgt[i]->n_out; ++j) {
			bm->sample_idx[m] = (uint64_t)i<<32 | bgt->out[j];
			bm->group[m] = bm->n_groups? bgt->group[j] : 1;
			bm->mgs[m++] = bgt->f->mgs[bgt->out[j]] >= 0? bgt->f->mgs[bgt->out[j]] : bm->mgs_def;
		}
	}
	if (bm->n_groups == 0) bm->n_groups = 1;
	for (i = m = 0; i < bm->n_out; ++i)
		if (bm->mgs[i] <= 1) ++m;
	if (m == 0) bm->flag |= BGT_F_NO_GT;

	// prepare header
	h0 = bm->bgt[0]->f->h0; // FIXME: test if headers are consistent
	kputs("##fileformat=VCFv4.1\n", &h);
	kputs("##INFO=<ID=AC,Number=A,Type=String,Description=\"Count of alternate alleles\">\n", &h);
	kputs("##INFO=<ID=AN,Number=A,Type=String,Description=\"Count of total alleles\">\n", &h);
	for (i = 1; i <= bm->n_groups; ++i) {
		ksprintf(&h, "##INFO=<ID=AC%d,Number=A,Type=String,Description=\"Count of alternate alleles for sample group %d\">\n", i, i);
		ksprintf(&h, "##INFO=<ID=AN%d,Number=A,Type=String,Description=\"Count of total alleles for sample group %d\">\n", i, i);
	}
	kputs("##INFO=<ID=END,Number=1,Type=Integer,Description=\"Ending position\">\n", &h);
	kputs("##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">\n", &h);
	kputs("##ALT=<ID=M,Description=\"Multi-allele\">\n", &h);
	kputs("##ALT=<ID=DEL,Description=\"Deletion\">\n", &h);
	kputs("##ALT=<ID=DUP,Description=\"Duplication\">\n", &h);
	kputs("##ALT=<ID=INS,Description=\"Insertion\">\n", &h);
	kputs("##ALT=<ID=INV,Description=\"Inversion\">\n", &h);
	kputs("##ALT=<ID=DUP:TANDEM,Description=\"Tandem duplication\">\n", &h);
	kputs("##ALT=<ID=DEL:ME,Description=\"Deletion of mobile element\">\n", &h);
	kputs("##ALT=<ID=INS:ME,Description=\"Insertion of mobile element\">\n", &h);
	for (i = 0; i < h0->n[BCF_DT_CTG]; ++i)
		ksprintf(&h, "##contig=<ID=%s,length=%d>\n", h0->id[BCF_DT_CTG][i].key, h0->id[BCF_DT_CTG][i].val->info[0]);
	kputs("#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO", &h);
	if (!(bm->flag & BGT_F_NO_GT)) {
		kputs("\tFORMAT", &h);
		for (i = m = 0; i < bm->n_bgt; ++i) {
			bgt_t *bgt = bm->bgt[i];
			for (j = 0; j < bgt->n_out; ++j) {
				if (bm->mgs[m++] > 1) continue;
				kputc('\t', &h);
				kputs(bgt->f->f->rows[bgt->out[j]].name, &h);
			}
		}
	}
	if (bm->h_out) bcf_hdr_destroy(bm->h_out);
	bm->h_out = bcf_hdr_init();
	bm->h_out->l_text = h.l + 1, bm->h_out->m_text = h.m, bm->h_out->text = h.s;
	bcf_hdr_parse(bm->h_out);

	// prepare the haplotype arrays
	bm->a[0] = (uint8_t*)realloc(bm->a[0], bm->n_out<<1);
	bm->a[1] = (uint8_t*)realloc(bm->a[1], bm->n_out<<1);

	if (bm->h_al != 0) {
		if (bm->flag&BGT_F_CNT_AL)
			bm->alcnt = (int*)calloc(bm->n_out, sizeof(int));
		if (bm->flag&BGT_F_CNT_HAP)
			bm->hap = (uint64_t*)calloc(bm->n_out<<1, 8);
		bm->aal = (bgt_allele_t*)calloc(kh_size((khash_t(str)*)bm->h_al) * 2, sizeof(bgt_allele_t));
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
andq $0x0, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movl (%rdi), %ecx
testl %ecx, %ecx
je 0x135a2
movq %rdi, %rbx
andl $0x0, 0x4(%rdi)
xorl %eax, %eax
xorl %r14d, %r14d
movslq %ecx, %rcx
cmpq %rcx, %r14
jge 0x131d1
movq 0x38(%rbx), %rax
movq (%rax,%r14,8), %rdi
callq 0x11882
movq 0x38(%rbx), %rax
movq (%rax,%r14,8), %rdx
movl (%rbx), %ecx
movl 0x4(%rbx), %eax
addl 0x34(%rdx), %eax
movl %eax, 0x4(%rbx)
incq %r14
jmp 0x131a4
movq 0x28(%rbx), %rdi
shll $0x2, %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, 0x28(%rbx)
movq 0x20(%rbx), %rdi
movslq 0x4(%rbx), %rsi
shlq $0x2, %rsi
callq 0x2340
movq %rax, 0x20(%rbx)
movq 0x18(%rbx), %rdi
movslq 0x4(%rbx), %rsi
shlq $0x3, %rsi
callq 0x2340
movq %rax, 0x18(%rbx)
xorl %ecx, %ecx
pushq $0x1
popq %rdx
xorl %esi, %esi
movslq (%rbx), %rdi
cmpq %rdi, %rcx
jge 0x132a6
movq 0x38(%rbx), %rdi
movq (%rdi,%rcx,8), %rdi
movq %rcx, %r8
shlq $0x20, %r8
movslq %esi, %rsi
leaq (%rax,%rsi,8), %r10
leaq (,%rsi,4), %r11
xorl %r9d, %r9d
movslq 0x34(%rdi), %r14
cmpq %r14, %r9
jge 0x1329b
movq 0x40(%rdi), %r14
movslq (%r14,%r9,4), %r15
orq %r8, %r15
movq %r15, (%r10,%r9,8)
cmpl $0x0, 0x8(%rbx)
movl %edx, %ebp
je 0x1326a
movq 0x48(%rdi), %r15
movl (%r15,%r9,4), %ebp
movq 0x20(%rbx), %r15
addq %r11, %r15
movl %ebp, (%r15,%r9,4)
movq (%rdi), %r15
movq 0x20(%r15), %r15
movslq (%r14,%r9,4), %r14
movl (%r15,%r14,4), %ebp
testl %ebp, %ebp
jns 0x1328b
movl 0x30(%rbx), %ebp
movq 0x28(%rbx), %r14
addq %r11, %r14
movl %ebp, (%r14,%r9,4)
incq %r9
jmp 0x13242
incq %rcx
addq %r9, %rsi
jmp 0x13215
cmpl $0x0, 0x8(%rbx)
jne 0x132b3
movl $0x1, 0x8(%rbx)
movl 0x4(%rbx), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
xorl %edx, %edx
cmpq %rcx, %rax
je 0x132d9
movq 0x28(%rbx), %rsi
xorl %edi, %edi
cmpl $0x2, (%rsi,%rcx,4)
setl %dil
addl %edi, %edx
incq %rcx
jmp 0x132bf
testl %edx, %edx
jne 0x132e1
orb $0x2, 0xc(%rbx)
movq 0x38(%rbx), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x10(%rax), %r13
leaq 0x6585(%rip), %rdi      # 0x1987b
movq %rsp, %r14
movq %r14, %rsi
callq 0x11b19
leaq 0x6589(%rip), %rdi      # 0x19891
movq %r14, %rsi
callq 0x11b19
leaq 0x65c8(%rip), %rdi      # 0x198df
movq %r14, %rsi
callq 0x11b19
pushq $0x1
popq %rbp
leaq 0x6600(%rip), %r15      # 0x19929
leaq 0x665d(%rip), %r12      # 0x1998d
cmpl 0x8(%rbx), %ebp
jg 0x1335b
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x135b3
movq %r14, %rdi
movq %r12, %rsi
movl %ebp, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x135b3
incl %ebp
jmp 0x13330
leaq 0x668b(%rip), %rdi      # 0x199ed
movq %rsp, %r14
movq %r14, %rsi
callq 0x11b19
leaq 0x66be(%rip), %rdi      # 0x19a32
movq %r14, %rsi
callq 0x11b19
leaq 0x66ed(%rip), %rdi      # 0x19a70
movq %r14, %rsi
callq 0x11b19
leaq 0x6707(%rip), %rdi      # 0x19a99
movq %r14, %rsi
callq 0x11b19
leaq 0x671f(%rip), %rdi      # 0x19ac0
movq %r14, %rsi
callq 0x11b19
leaq 0x673a(%rip), %rdi      # 0x19aea
movq %r14, %rsi
callq 0x11b19
leaq 0x6753(%rip), %rdi      # 0x19b12
movq %r14, %rsi
callq 0x11b19
leaq 0x676c(%rip), %rdi      # 0x19b3a
movq %r14, %rsi
callq 0x11b19
leaq 0x6795(%rip), %rdi      # 0x19b72
movq %r14, %rsi
callq 0x11b19
leaq 0x67c2(%rip), %rdi      # 0x19bae
movq %r14, %rsi
callq 0x11b19
pushq $0x8
popq %r12
leaq 0x67ec(%rip), %r15      # 0x19beb
xorl %ebp, %ebp
movslq 0xc(%r13), %rax
cmpq %rax, %rbp
jge 0x1342f
movq 0x20(%r13), %rax
movq -0x8(%rax,%r12), %rdx
movq (%rax,%r12), %rax
movl (%rax), %ecx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x135b3
incq %rbp
addq $0x10, %r12
jmp 0x13401
leaq 0x67d1(%rip), %rdi      # 0x19c07
movq %rsp, %rsi
callq 0x11b19
testb $0x2, 0xc(%rbx)
jne 0x134e8
leaq 0x6376(%rip), %rdi      # 0x197c5
movq %rsp, %r14
movq %r14, %rsi
callq 0x11b19
xorl %ecx, %ecx
pushq $0x9
popq %rbp
xorl %edx, %edx
movslq (%rbx), %rax
cmpq %rax, %rcx
jge 0x134e8
movq 0x38(%rbx), %rax
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %r13
movslq %edx, %rdx
movq %rdx, 0x18(%rsp)
leaq (,%rdx,4), %r12
xorl %r15d, %r15d
movslq 0x34(%r13), %rax
cmpq %rax, %r15
jge 0x134d3
movq 0x28(%rbx), %rax
addq %r12, %rax
cmpl $0x1, (%rax,%r15,4)
jg 0x134ce
movl %ebp, %edi
movq %r14, %rsi
callq 0x11b3c
movq (%r13), %rax
movq 0x40(%r13), %rcx
movq 0x8(%rax), %rax
movq 0x28(%rax), %rax
movslq (%rcx,%r15,4), %rcx
imulq $0x18, %rcx, %rcx
movq (%rax,%rcx), %rdi
movq %r14, %rsi
callq 0x11b19
incq %r15
jmp 0x13489
movq 0x20(%rsp), %rcx
incq %rcx
movq 0x18(%rsp), %rdx
addq %r15, %rdx
jmp 0x13461
movq 0x50(%rbx), %rdi
testq %rdi, %rdi
je 0x134f6
callq 0xa500
callq 0xa4b6
movq %rax, 0x50(%rbx)
movl (%rsp), %ecx
incl %ecx
movl %ecx, (%rax)
movl 0x8(%rsp), %ecx
movl %ecx, 0x4(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x48(%rax)
movq %rax, %rdi
callq 0xa30d
movq 0x58(%rbx), %rdi
movl 0x4(%rbx), %eax
addl %eax, %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, 0x58(%rbx)
movq 0x60(%rbx), %rdi
movl 0x4(%rbx), %eax
addl %eax, %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, 0x60(%rbx)
movq 0xa0(%rbx), %r14
testq %r14, %r14
je 0x135a2
movl 0xc(%rbx), %ebp
testb $0x4, %bpl
je 0x13570
movslq 0x4(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0x2220
movq %rax, 0xa8(%rbx)
testb $0x8, %bpl
je 0x1358d
movl 0x4(%rbx), %eax
addl %eax, %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x2220
movq %rax, 0xb0(%rbx)
movl 0x4(%r14), %edi
addl %edi, %edi
pushq $0x30
popq %rsi
callq 0x2220
movq %rax, 0x98(%rbx)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq