// File: /workspace/github_c_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq (%rdi), %rax
movq 0x8(%rax), %r15
cmpl $0x0, 0x38(%rdi)
movl 0x20(%r15), %eax
jne 0x1685c
testl %eax, %eax
jle 0x16855
movq 0x50(%rbx), %rcx
xorl %edx, %edx
movl $0x1, (%rcx,%rdx,4)
incq %rdx
movslq 0x20(%r15), %rax
cmpq %rax, %rdx
jl 0x16842
movl $0x1, 0x38(%rbx)
movl $0x0, 0x34(%rbx)
xorl %esi, %esi
testl %eax, %eax
jle 0x1688b
movq 0x50(%rbx), %rcx
movl %eax, %eax
xorl %edx, %edx
cmpl $0x0, (%rcx,%rdx,4)
je 0x1687c
incl %esi
movl %esi, 0x34(%rbx)
incq %rdx
cmpq %rdx, %rax
jne 0x16871
movslq %esi, %rsi
shlq $0x2, %rsi
movq 0x40(%rbx), %rdi
callq 0x2330
movq %rax, 0x40(%rbx)
movq 0x48(%rbx), %rdi
movslq 0x34(%rbx), %rsi
shlq $0x2, %rsi
callq 0x2330
movq %rax, 0x48(%rbx)
movl $0x0, 0x34(%rbx)
movl 0x20(%r15), %esi
testl %esi, %esi
jle 0x168ee
movq 0x50(%rbx), %rcx
xorl %edx, %edx
movl (%rcx,%rdx,4), %edi
testl %edi, %edi
je 0x168e3
movslq 0x34(%rbx), %rsi
movl %edi, (%rax,%rsi,4)
movq 0x40(%rbx), %rdi
leal 0x1(%rsi), %r8d
movl %r8d, 0x34(%rbx)
movl %edx, (%rdi,%rsi,4)
movl 0x20(%r15), %esi
incq %rdx
movslq %esi, %rdi
cmpq %rdi, %rdx
jl 0x168c2
movq %rbx, (%rsp)
movq 0x58(%rbx), %rdi
testq %rdi, %rdi
je 0x16900
callq 0xb980
callq 0xb93d
movq %rax, %r13
movq (%rsp), %rbx
movq %rax, 0x58(%rbx)
movq (%rbx), %rax
movq 0x10(%rax), %rax
movq 0x48(%rax), %rcx
movq %rcx, 0x8(%rsp)
movslq (%rax), %r14
leaq 0x1(%r14), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %r12
shrq $0x10, %r12
orq %rax, %r12
incq %r12
movq %r12, %rdi
callq 0x22e0
movq %rax, %rbp
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x2280
movq %rbp, %rdi
movb $0x0, (%rbp,%r14)
cmpb $0x1, -0x1(%rbp,%r14)
sbbq $0x0, %r14
cmpl $0x0, 0x34(%rbx)
jle 0x16b0e
leaq 0x8(%r14), %rax
cmpq %r12, %rax
movq %r15, 0x10(%rsp)
jb 0x169e0
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %r12
shrq $0x10, %r12
orq %rax, %r12
incq %r12
movq %r12, %rsi
callq 0x2330
movq %rax, %rdi
movq (%rsp), %r13
movl $0x54414d52, 0x3(%rdi,%r14) # imm = 0x54414D52
movl $0x524f4609, (%rdi,%r14) # imm = 0x524F4609
movb $0x0, 0x7(%rdi,%r14)
addq $0x7, %r14
xorl %ebp, %ebp
movq %r14, %r15
leaq 0x1(%r15), %rbx
cmpq %r12, %rbx
jb 0x16a4c
movq %rbx, %rax
shrq %rax
orq %rbx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq %r12, %rsi
callq 0x2330
movq %rax, %rdi
movq %rdi, 0x8(%rsp)
movw $0x9, (%rdi,%r15)
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x40(%r13), %rcx
movslq (%rcx,%rbp,4), %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %r13
movq %r13, %rdi
callq 0x2170
movslq %eax, %r14
leaq (%r15,%r14), %rax
addq $0x2, %rax
cmpq %r12, %rax
jae 0x16a90
movq 0x8(%rsp), %rax
jmp 0x16ad1
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %r12
shrq $0x10, %r12
orq %rax, %r12
incq %r12
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x2330
movq %rax, %r15
leaq (%rax,%rbx), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x2280
movq %r15, %rdi
addq %rbx, %r14
movb $0x0, (%r15,%r14)
incq %rbp
movq (%rsp), %r13
movslq 0x34(%r13), %rax
movq %r14, %r15
cmpq %rax, %rbp
jl 0x16a04
movq %r13, %r15
movq 0x58(%r13), %r13
jmp 0x16b12
movq (%rsp), %r15
movq %rdi, 0x48(%r13)
incl %r14d
movl %r14d, (%r13)
movq %r13, %rdi
callq 0xb797
movslq 0x34(%r15), %rbx
leal (%rbx,%rbx), %ebp
leaq (,%rbx,8), %rdi
callq 0x22e0
movq %rax, %r14
testq %rbx, %rbx
jle 0x16b86
movq 0x40(%r15), %rax
addq %rbx, %rbx
xorl %ecx, %ecx
movabsq $0x200000000, %rdx      # imm = 0x200000000
xorl %esi, %esi
movl (%rax,%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 %rdx, %rcx
cmpq %rsi, %rbx
jne 0x16b56
movq 0x8(%r15), %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x15f12
movq %r14, %rdi
callq 0x2050
movq 0x18(%r15), %rax
movq $0x0, 0x18(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq