// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x21e0
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x1f481
movl $0x0, -0x4(%rbp)
jmp 0x1fad3
movq -0x10(%rbp), %rax
movl $0x0, 0x4(%rax)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1f4db
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1cce0
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x34(%rax), %ecx
movq -0x10(%rbp), %rax
addl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1f493
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
shll $0x2, %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
shll $0x2, %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
shll $0x3, %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1f68b
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
movslq -0x14(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpl 0x34(%rcx), %eax
jge 0x1f67b
movslq -0x14(%rbp), %rdx
shlq $0x20, %rdx
movq -0x48(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x18(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
orq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x1f5e7
movq -0x48(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x54(%rbp)
jmp 0x1f5f1
movl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1f5f1
movl -0x54(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
movq -0x48(%rbp), %rcx
movq 0x40(%rcx), %rcx
movslq -0x18(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0x1f647
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
movq -0x48(%rbp), %rcx
movq 0x40(%rcx), %rcx
movslq -0x18(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x58(%rbp)
jmp 0x1f651
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x1c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x1c(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1f582
jmp 0x1f67d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1f558
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x1f6a0
movq -0x10(%rbp), %rax
movl $0x1, 0x8(%rax)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x1f6e2
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
cmpl $0x1, (%rax,%rcx,4)
jg 0x1f6d5
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1f6d7
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1f6ae
cmpl $0x0, -0x1c(%rbp)
jne 0x1f6f5
movq -0x10(%rbp), %rax
movl 0xc(%rax), %ecx
orl $0x2, %ecx
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
leaq 0x8e3a(%rip), %rdi      # 0x2854c
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x8e40(%rip), %rdi      # 0x28562
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x8e7e(%rip), %rdi      # 0x285b0
leaq -0x38(%rbp), %rsi
callq 0x1d160
movl $0x1, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jg 0x1f789
movl -0x14(%rbp), %edx
movl -0x14(%rbp), %ecx
leaq -0x38(%rbp), %rdi
leaq 0x8e9b(%rip), %rsi      # 0x285fa
movb $0x0, %al
callq 0x1fae0
movl -0x14(%rbp), %edx
movl -0x14(%rbp), %ecx
leaq -0x38(%rbp), %rdi
leaq 0x8ee7(%rip), %rsi      # 0x2865e
movb $0x0, %al
callq 0x1fae0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1f742
leaq 0x8f2e(%rip), %rdi      # 0x286be
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x8f63(%rip), %rdi      # 0x28703
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x8f91(%rip), %rdi      # 0x28741
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x8faa(%rip), %rdi      # 0x2876a
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x8fc1(%rip), %rdi      # 0x28791
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x8fdb(%rip), %rdi      # 0x287bb
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x8ff3(%rip), %rdi      # 0x287e3
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x900b(%rip), %rdi      # 0x2880b
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x9033(%rip), %rdi      # 0x28843
leaq -0x38(%rbp), %rsi
callq 0x1d160
leaq 0x905f(%rip), %rdi      # 0x2887f
leaq -0x38(%rbp), %rsi
callq 0x1d160
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x1f888
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
leaq -0x38(%rbp), %rdi
leaq 0x9046(%rip), %rsi      # 0x288bc
movb $0x0, %al
callq 0x1fae0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1f830
leaq 0x9049(%rip), %rdi      # 0x288d8
leaq -0x38(%rbp), %rsi
callq 0x1d160
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x1f97a
leaq 0x8be4(%rip), %rdi      # 0x28496
leaq -0x38(%rbp), %rsi
callq 0x1d160
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1f978
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0x34(%rcx), %eax
jge 0x1f968
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x1c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x1c(%rbp)
movslq %ecx, %rcx
cmpl $0x1, (%rax,%rcx,4)
jle 0x1f91d
jmp 0x1f95d
movl $0x9, %edi
leaq -0x38(%rbp), %rsi
callq 0x1d1a0
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0x28(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x40(%rcx), %rcx
movslq -0x18(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1d160
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1f8f3
jmp 0x1f96a
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1f8c9
jmp 0x1f97a
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x1f992
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
callq 0xfe40
callq 0xfde0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
callq 0xfb00
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
shll %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
shll %eax
movslq %eax, %rsi
callq 0x2340
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0xa0(%rax)
je 0x1facc
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x1fa6d
movq -0x10(%rbp), %rax
movslq 0x4(%rax), %rdi
movl $0x4, %esi
callq 0x2230
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x1faa0
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
shll %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x2230
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movl 0x4(%rax), %eax
shll %eax
movl %eax, %eax
movl %eax, %edi
movl $0x30, %esi
callq 0x2230
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x98(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)