// File: /workspace/github_c_makefile_star2_O2/lh3[P]bgt/bgt.c
// Function: bgt_prepare
void bgt_prepare(bgt_t *bgt)
{
	int i, *t;
	const fmf_t *f = bgt->f->f;
	kstring_t str = {0,0,0};

	if (bgt->n_groups == 0) bgt_add_group_core(bgt, BGT_SET_ALL_SAMPLES, 0, 0);
	for (i = 0, bgt->n_out = 0; i < f->n_rows; ++i)
		if (bgt->gtag[i] > 0) ++bgt->n_out;
	bgt->out = (int*)realloc(bgt->out, bgt->n_out * sizeof(int));
	bgt->group = (uint32_t*)realloc(bgt->group, bgt->n_out * 4);
	for (i = 0, bgt->n_out = 0; i < f->n_rows; ++i)
		if (bgt->gtag[i] > 0)
			bgt->group[bgt->n_out] = bgt->gtag[i], bgt->out[bgt->n_out++] = i;

	// build ->h_out VCF header
	if (bgt->h_out) bcf_hdr_destroy(bgt->h_out);
	bgt->h_out = bcf_hdr_init();
	kputsn(bgt->f->h0->text, bgt->f->h0->l_text, &str);
	if (str.s[str.l-1] == 0) --str.l;
	if (bgt->n_out > 0) {
		kputs("\tFORMAT", &str);
		for (i = 0; i < bgt->n_out; ++i) {
			kputc('\t', &str);
			kputs(f->rows[bgt->out[i]].name, &str);
		}
	}
	bgt->h_out->text = str.s;
	bgt->h_out->l_text = str.l + 1; // including the last NULL
	bcf_hdr_parse(bgt->h_out);

	// subsetting pbf
	t = (int*)malloc(bgt->n_out * 2 * sizeof(int));
	for (i = 0; i < bgt->n_out; ++i)
		t[i<<1|0] = bgt->out[i]<<1|0, t[i<<1|1] = bgt->out[i]<<1|1;
	pbf_subset(bgt->pb, bgt->n_out<<1, t);
	free(t);

	bgt->b0->shared.l = 0; // mark b0 unread
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdi, %rbx
movq (%rdi), %rax
movq 0x8(%rax), %r15
andq $0x0, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
cmpl $0x0, 0x38(%rdi)
jne 0x118ba
pushq $-0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x115b0
andl $0x0, 0x34(%rbx)
movl 0x20(%r15), %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x118e4
movq 0x50(%rbx), %rsi
cmpl $0x0, (%rsi,%rdx,4)
je 0x118df
incl %eax
movl %eax, 0x34(%rbx)
incq %rdx
jmp 0x118cb
movq 0x40(%rbx), %rdi
movslq %eax, %rsi
shlq $0x2, %rsi
callq 0x2340
movq %rax, 0x40(%rbx)
movq 0x48(%rbx), %rdi
movslq 0x34(%rbx), %rsi
shlq $0x2, %rsi
callq 0x2340
movq %rax, 0x48(%rbx)
andl $0x0, 0x34(%rbx)
xorl %ecx, %ecx
movslq 0x20(%r15), %rdx
cmpq %rdx, %rcx
jge 0x11940
movq 0x50(%rbx), %rdx
movl (%rdx,%rcx,4), %edx
testl %edx, %edx
je 0x1193b
movslq 0x34(%rbx), %rsi
movl %edx, (%rax,%rsi,4)
movq 0x40(%rbx), %rdx
leal 0x1(%rsi), %edi
movl %edi, 0x34(%rbx)
movl %ecx, (%rdx,%rsi,4)
incq %rcx
jmp 0x11913
movq 0x58(%rbx), %rdi
testq %rdi, %rdi
je 0x1194e
callq 0xa500
callq 0xa4b6
movq %rax, 0x58(%rbx)
movq (%rbx), %rax
movq 0x10(%rax), %rax
movq 0x48(%rax), %rdi
movl (%rax), %esi
movq %rsp, %r14
movq %r14, %rdx
callq 0x11a85
movq (%r14), %rax
movq 0x10(%r14), %rcx
cmpb $0x0, -0x1(%rcx,%rax)
jne 0x11984
decq %rax
movq %rax, (%rsp)
cmpl $0x0, 0x34(%rbx)
jle 0x119df
leaq 0x7e34(%rip), %rdi      # 0x197c5
movq %rsp, %r14
movq %r14, %rsi
callq 0x11b19
xorl %r12d, %r12d
pushq $0x9
popq %rbp
movslq 0x34(%rbx), %rax
cmpq %rax, %r12
jge 0x119d6
movl %ebp, %edi
movq %r14, %rsi
callq 0x11b3c
movq 0x28(%r15), %rax
movq 0x40(%rbx), %rcx
movslq (%rcx,%r12,4), %rcx
imulq $0x18, %rcx, %rcx
movq (%rax,%rcx), %rdi
movq %r14, %rsi
callq 0x11b19
incq %r12
jmp 0x119a2
movq (%rsp), %rax
movq 0x10(%rsp), %rcx
movq 0x58(%rbx), %rdi
movq %rcx, 0x48(%rdi)
incl %eax
movl %eax, (%rdi)
callq 0xa30d
movslq 0x34(%rbx), %r14
leal (%r14,%r14), %ebp
movq %r14, %rdi
shlq $0x3, %rdi
callq 0x22e0
xorl %ecx, %ecx
testl %r14d, %r14d
movl $0x0, %edx
cmovgl %r14d, %edx
movq %rax, %r14
addl %edx, %edx
movabsq $0x200000000, %rax      # imm = 0x200000000
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x11a59
movq 0x40(%rbx), %rdi
movl (%rdi,%rsi,2), %edi
leal (%rdi,%rdi), %r8d
movq %rcx, %r9
sarq $0x1e, %r9
movl %r8d, (%r14,%r9)
leal 0x1(,%rdi,2), %edi
leal 0x1(%rsi), %r8d
movslq %r8d, %r8
movl %edi, (%r14,%r8,4)
addq $0x2, %rsi
addq %rax, %rcx
jmp 0x11a23
movq 0x8(%rbx), %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x11149
movq %r14, %rdi
callq 0x2050
movq 0x18(%rbx), %rax
andq $0x0, 0x18(%rax)
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq