// File: /workspace/github_c_makefile_star2_O1/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, %rbp
movq (%rdi), %rax
movq 0x8(%rax), %rbx
cmpl $0x0, 0x38(%rdi)
jne 0x16224
cmpl $0x0, 0x20(%rbx)
jle 0x1621d
movq 0x50(%rbp), %rax
xorl %ecx, %ecx
movl $0x1, (%rax,%rcx,4)
incq %rcx
movslq 0x20(%rbx), %rdx
cmpq %rdx, %rcx
jl 0x1620a
movl $0x1, 0x38(%rbp)
movl $0x0, 0x34(%rbp)
movslq 0x20(%rbx), %rax
testq %rax, %rax
jle 0x1624b
movq 0x50(%rbp), %rcx
xorl %edx, %edx
cmpl $0x0, (%rcx,%rdx,4)
je 0x16243
incl 0x34(%rbp)
incq %rdx
cmpq %rdx, %rax
jne 0x1623a
movq 0x40(%rbp), %rdi
movslq 0x34(%rbp), %rsi
shlq $0x2, %rsi
callq 0x2330
movq %rax, 0x40(%rbp)
movq 0x48(%rbp), %rdi
movslq 0x34(%rbp), %rsi
shlq $0x2, %rsi
callq 0x2330
movq %rax, 0x48(%rbp)
movl $0x0, 0x34(%rbp)
cmpl $0x0, 0x20(%rbx)
jle 0x162b1
movq 0x50(%rbp), %rcx
xorl %edx, %edx
movl (%rcx,%rdx,4), %esi
testl %esi, %esi
je 0x162a5
movslq 0x34(%rbp), %rdi
movl %esi, (%rax,%rdi,4)
movq 0x40(%rbp), %rsi
leal 0x1(%rdi), %r8d
movl %r8d, 0x34(%rbp)
movl %edx, (%rsi,%rdi,4)
incq %rdx
movslq 0x20(%rbx), %rsi
cmpq %rsi, %rdx
jl 0x16288
movq 0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x162bf
callq 0xb84a
callq 0xb807
movq %rax, 0x58(%rbp)
movq (%rbp), %rax
movq 0x10(%rax), %rax
movq 0x48(%rax), %r13
movslq (%rax), %r15
leaq 0x1(%r15), %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, %r14
movq %rax, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x2280
movq %r14, %rax
movb $0x0, (%r14,%r15)
cmpb $0x1, -0x1(%r14,%r15)
sbbq $0x0, %r15
cmpl $0x0, 0x34(%rbp)
movq %rbp, (%rsp)
jle 0x164bf
leaq 0x8(%r15), %rdx
cmpq %r12, %rdx
jb 0x16390
movq %rdx, %rcx
shrq %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x2, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x4, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x8, %rdx
orq %rcx, %rdx
movq %rdx, %r12
shrq $0x10, %r12
orq %rdx, %r12
incq %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0x2330
movl $0x54414d52, 0x3(%rax,%r15) # imm = 0x54414D52
movl $0x524f4609, (%rax,%r15) # imm = 0x524F4609
movb $0x0, 0x7(%rax,%r15)
addq $0x7, %r15
cmpl $0x0, 0x34(%rbp)
jle 0x164bf
movq %rbx, 0x10(%rsp)
xorl %ebx, %ebx
leaq 0x1(%r15), %r14
cmpq %r12, %r14
jb 0x16404
movq %r14, %rdx
shrq %rdx
orq %r14, %rdx
movq %rdx, %rcx
shrq $0x2, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x4, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x8, %rcx
orq %rdx, %rcx
movq %rcx, %r12
shrq $0x10, %r12
orq %rcx, %r12
incq %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0x2330
movq %rax, 0x8(%rsp)
movw $0x9, (%rax,%r15)
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x40(%rbp), %rcx
movslq (%rcx,%rbx,4), %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %rbp
movq %rbp, %rdi
callq 0x2170
movslq %eax, %r13
leaq (%r15,%r13), %rax
addq $0x2, %rax
cmpq %r12, %rax
jae 0x16448
movq 0x8(%rsp), %rax
jmp 0x16489
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
leaq (%rax,%r14), %rdi
movq %rbp, %rsi
movq %rax, %r15
movq %r13, %rdx
callq 0x2280
movq %r15, %rax
addq %r14, %r13
movb $0x0, (%r15,%r13)
incq %rbx
movq (%rsp), %rbp
movslq 0x34(%rbp), %rcx
movq %r13, %r15
cmpq %rcx, %rbx
jl 0x163bc
jmp 0x164c2
movq %r15, %r13
movq 0x58(%rbp), %rdi
movq %rax, 0x48(%rdi)
incl %r13d
movl %r13d, (%rdi)
callq 0xb655
movslq 0x34(%rbp), %rbx
leal (%rbx,%rbx), %ebp
leaq (,%rbx,8), %rdi
callq 0x22e0
movq %rax, %r14
testq %rbx, %rbx
jle 0x1653b
movq (%rsp), %rax
movq 0x40(%rax), %rax
addq %rbx, %rbx
xorl %ecx, %ecx
movabsq $0x200000000, %rdx      # imm = 0x200000000
xorl %esi, %esi
movl (%rax,%rsi,2), %edi
addl %edi, %edi
movq %rcx, %r8
sarq $0x1e, %r8
movl %edi, (%r14,%r8)
movl (%rax,%rsi,2), %edi
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 0x1650a
movq (%rsp), %rbx
movq 0x8(%rbx), %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x15a66
movq %r14, %rdi
callq 0x2050
movq 0x18(%rbx), %rax
movq $0x0, 0x18(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq