// File: /workspace/github_c_makefile_star2_O2/lh3[P]bgt/bgt.c
// Function: bgtm_read_core
int bgtm_read_core(bgtm_t *bm, bcf1_t *b)
{
	int i, j, off = 0, n_rest = 0, max_allele = 0, l_ref, al_ret = 0;
	const bcf1_t *b0 = 0;

	// fill the buffer
	for (i = n_rest = 0; i < bm->n_bgt; ++i) {
		if (bm->r[i].b0 == 0)
			bgt_read_rec(bm->bgt[i], &bm->r[i]);
		n_rest += (bm->r[i].b0 != 0);
		if (bm->r[i].b0) bm->n_gt_read += bm->bgt[i]->n_out;
	}
	if (n_rest == 0) return -1;
	// search for the smallest allele
	for (i = 0; i < bm->n_bgt; ++i) {
		bgt_rec_t *r = &bm->r[i];
		if (r->b0 == 0) continue;
		if (b0) {
			j = bcfcmp(b0, r->b0);
			if (j > 0) b0 = r->b0, max_allele = b0->n_allele;
			else if (j == 0)
				max_allele = r->b0->n_allele > max_allele? r->b0->n_allele : max_allele;
		} else b0 = r->b0, max_allele = b0->n_allele;
	}
	assert(b0 && max_allele >= 2);
	// fill bcf1_t up to INFO, excluding AC/AN/etc
	l_ref = bcfcpy_min(b, b0, max_allele > 2? "<M>" : 0);
	if (l_ref != b->rlen) {
		int32_t val = b->pos + b->rlen;
		bcf_append_info_ints(bm->h_out, b, "END", 1, &val);
	}
	// generate bm->a
	for (i = 0; i < bm->n_bgt; ++i) {
		bgt_rec_t *r = &bm->r[i];
		bgt_t *bgt = bm->bgt[i];
		if (bgt->n_out == 0) continue;
		if (r->b0 && bcfcmp(b, r->b0) == 0) { // copy
			r->b0 = 0;
			memcpy(bm->a[0] + off, r->a[0], bgt->n_out<<1);
			memcpy(bm->a[1] + off, r->a[1], bgt->n_out<<1);
		} else { // add missing values
			memset(bm->a[0] + off, 0, bgt->n_out<<1);
			memset(bm->a[1] + off, 1, bgt->n_out<<1);
		}
		off += bgt->n_out<<1;
	}
	// find samples having a set of alleles, or do haplotype counting
	if (bm->h_al) {
		// test if the current record matches an allele
		al_ret = al_present((khash_t(str)*)bm->h_al, bm->h_out, b);
		if (al_ret == 0) return 1;
	}
	// fill AC/AN/etc and test site_flt
	if ((bm->flag & BGT_F_SET_AC) || bm->site_flt || bm->n_fields > 0 || bm->n_groups > 1) {
		bgt_info_t ss;
		bgtm_cal_info(bm, &ss);
		bgtm_fill_info(bm->h_out, &ss, b);
		if (bm->n_fields > 0)
			bgtm_gen_tbl_line(bm, &ss, b);
		if (!bgtm_pass_site_flt(&ss, bm->site_flt))
			return 1;
	}
	if (bm->h_al) {
		// +1 to samples having the allele
		if ((bm->flag&BGT_F_CNT_AL) && bm->alcnt) {
			int is_ref = (al_ret == 2);
			for (i = 0; i < bm->n_out; ++i) {
				int g1 = bm->a[0][i<<1|0] | bm->a[1][i<<1|0]<<1;
				int g2 = bm->a[0][i<<1|1] | bm->a[1][i<<1|1]<<1;
				if (is_ref) bm->alcnt[i] += (g1 == 0 || g2 == 0);
				else bm->alcnt[i] += (g1 == 1 || g2 == 1);
			}
		}
		// generate haplotype
		if ((bm->flag&BGT_F_CNT_HAP) && bm->hap) {
			for (i = 0; i < bm->n_out<<1; ++i)
				if (bm->a[0][i] == 1 && bm->a[1][i] == 0) bm->hap[i] |= 1ULL<<bm->n_aal;
		}
		bgt_al_from_bcf(bm->h_out, b, &bm->aal[bm->n_aal++], 0);
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rsi, (%rsp)
movq %rdi, %r14
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %ebp, %ebp
movslq (%r14), %rax
cmpq %rax, %r15
jge 0x13e5c
movq 0x40(%r14), %rsi
cmpq $0x0, (%rsi,%rbx)
je 0x13e1c
incl %ebp
jmp 0x13e43
addq %rbx, %rsi
movq 0x38(%r14), %rax
movq (%rax,%r15,8), %rdi
callq 0x1204c
movq 0x40(%r14), %rax
movq (%rax,%rbx), %rax
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
addl %ecx, %ebp
testq %rax, %rax
je 0x13e53
movq 0x38(%r14), %rax
movq (%rax,%r15,8), %rax
movslq 0x34(%rax), %rax
addq %rax, 0x10(%r14)
incq %r15
addq $0x18, %rbx
jmp 0x13e05
testl %ebp, %ebp
je 0x14012
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebp, %ebp
cltq
cmpq %rax, %r12
jge 0x13ec5
movq 0x40(%r14), %r13
movq (%r13,%rbx), %rsi
testq %rsi, %rsi
je 0x13eb9
testq %r15, %r15
je 0x13ea0
movq %r15, %rdi
callq 0xd533
testl %eax, %eax
jle 0x13ea9
movq (%r13,%rbx), %r15
movzwl 0x12(%r15), %ebp
jmp 0x13eb9
movzwl 0x12(%rsi), %ebp
movq %rsi, %r15
jmp 0x13eb9
jne 0x13eb9
movq (%r13,%rbx), %rax
movzwl 0x12(%rax), %eax
cmpl %ebp, %eax
cmovgl %eax, %ebp
incq %r12
movl (%r14), %eax
addq $0x18, %rbx
jmp 0x13e6e
testq %r15, %r15
je 0x141e2
cmpl $0x1, %ebp
jle 0x141e2
xorl %r12d, %r12d
cmpl $0x3, %ebp
leaq 0x562a(%rip), %rdx      # 0x1950e
cmovbq %r12, %rdx
movq (%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xd59e
movl 0x8(%rbx), %ecx
cmpl %ecx, %eax
je 0x13f1f
addl 0x4(%rbx), %ecx
leaq 0x18(%rsp), %r8
movl %ecx, (%r8)
movq 0x50(%r14), %rdi
leaq 0x54cd(%rip), %rdx      # 0x193e1
pushq $0x1
popq %rcx
movq %rbx, %rsi
callq 0xd67c
pushq $0x1
popq %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
xorl %ebx, %ebx
movslq (%r14), %rax
cmpq %rax, %rbx
jge 0x13fee
movq 0x38(%r14), %rax
movq (%rax,%rbx,8), %r15
movl 0x34(%r15), %ecx
testl %ecx, %ecx
je 0x13fe2
movq 0x40(%r14), %r13
movq (%r13,%rbp), %rsi
movq %r12, 0x10(%rsp)
movslq %r12d, %r12
testq %rsi, %rsi
je 0x13f72
movq (%rsp), %rdi
callq 0xd533
movl 0x34(%r15), %ecx
testl %eax, %eax
je 0x13f9f
movq 0x58(%r14), %rdi
addq %r12, %rdi
addl %ecx, %ecx
movslq %ecx, %rdx
xorl %esi, %esi
callq 0x21e0
addq 0x60(%r14), %r12
movl 0x34(%r15), %eax
addl %eax, %eax
movslq %eax, %rdx
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x21e0
jmp 0x13fd5
andq $0x0, (%r13,%rbp)
movq 0x58(%r14), %rdi
addq %r12, %rdi
movq 0x8(%r13,%rbp), %rsi
addl %ecx, %ecx
movslq %ecx, %rdx
callq 0x2280
addq 0x60(%r14), %r12
movq 0x10(%r13,%rbp), %rsi
movl 0x34(%r15), %eax
addl %eax, %eax
movslq %eax, %rdx
movq %r12, %rdi
callq 0x2280
movl 0x34(%r15), %eax
movq 0x10(%rsp), %r12
leal (%r12,%rax,2), %r12d
incq %rbx
addq $0x18, %rbp
jmp 0x13f2b
movq 0xa0(%r14), %rdi
testq %rdi, %rdi
je 0x1401f
movq 0x50(%r14), %rsi
movq (%rsp), %rdx
callq 0x11f84
movl %eax, %ebp
testl %eax, %eax
jne 0x14021
jmp 0x141cb
pushq $-0x1
popq %rax
movq %rax, 0x8(%rsp)
jmp 0x141cb
xorl %ebp, %ebp
testb $0x1, 0xc(%r14)
jne 0x1403d
cmpq $0x0, 0x48(%r14)
jne 0x1403d
cmpl $0x0, 0x68(%r14)
jg 0x1403d
cmpl $0x2, 0x8(%r14)
jl 0x1408d
leaq 0x18(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x139cf
movq 0x50(%r14), %rdi
movq %r12, %rsi
movq (%rsp), %rbx
movq %rbx, %rdx
callq 0x13896
cmpl $0x0, 0x68(%r14)
jle 0x14077
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x13c0f
movq 0x48(%r14), %rsi
leaq 0x18(%rsp), %rdi
callq 0x13850
testl %eax, %eax
je 0x141cb
cmpq $0x0, 0xa0(%r14)
je 0x14131
movl 0xc(%r14), %ecx
testb $0x4, %cl
je 0x14143
movq 0xa8(%r14), %rax
testq %rax, %rax
je 0x14143
xorl %ecx, %ecx
cmpl $0x2, %ebp
setne %cl
pushq $0x1
popq %rdx
xorl %esi, %esi
movabsq $0x200000000, %rdi      # imm = 0x200000000
xorl %r8d, %r8d
movslq 0x4(%r14), %r9
cmpq %r9, %r8
jge 0x1413f
movq %rsi, %r9
sarq $0x20, %r9
movq 0x58(%r14), %r10
movq 0x60(%r14), %r11
movzbl (%r10,%r9), %ebx
movzbl (%r11,%r9), %r9d
addl %r9d, %r9d
orl %ebx, %r9d
movslq %edx, %rdx
movzbl (%r10,%rdx), %r10d
movzbl (%r11,%rdx), %r11d
addl %r11d, %r11d
orl %r10d, %r11d
cmpl %ecx, %r9d
sete %r9b
cmpl %ecx, %r11d
sete %r10b
orb %r9b, %r10b
movzbl %r10b, %r9d
addl %r9d, (%rax,%r8,4)
incq %r8
addl $0x2, %edx
addq %rdi, %rsi
jmp 0x140d2
movq $0x0, 0x8(%rsp)
jmp 0x141cb
movl 0xc(%r14), %ecx
testb $0x8, %cl
movq (%rsp), %rsi
je 0x1419b
movq 0xb0(%r14), %rax
testq %rax, %rax
je 0x1419b
movl 0x4(%r14), %edx
addl %edx, %edx
xorl %r9d, %r9d
testl %edx, %edx
cmovlel %r9d, %edx
pushq $0x1
popq %rdi
cmpq %r9, %rdx
je 0x1419b
movq 0x58(%r14), %rcx
cmpb $0x1, (%rcx,%r9)
jne 0x14196
movq 0x60(%r14), %rcx
cmpb $0x0, (%rcx,%r9)
jne 0x14196
movb 0x90(%r14), %cl
movq %rdi, %r8
shlq %cl, %r8
orq %r8, (%rax,%r9,8)
incq %r9
jmp 0x1416a
movq 0x50(%r14), %rdi
movslq 0x90(%r14), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x90(%r14)
imulq $0x30, %rax, %rdx
addq 0x98(%r14), %rdx
movq $0x0, 0x8(%rsp)
xorl %ecx, %ecx
callq 0x14201
movq 0x8(%rsp), %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5a61(%rip), %rdi      # 0x19c4a
leaq 0x55f4(%rip), %rsi      # 0x197e4
leaq 0x5a69(%rip), %rcx      # 0x19c60
movl $0x335, %edx            # imm = 0x335
callq 0x21b0