// File: /workspace/github_c_makefile_star2_O3/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
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x0, (%rdi)
jle 0x1a669
movq %rsi, %r13
movq %rdi, %r14
movq 0x40(%rdi), %rsi
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq $0x0, (%rsi,%rbx)
je 0x1a2b6
incl %ebp
jmp 0x1a2dd
addq %rbx, %rsi
movq 0x38(%r14), %rax
movq (%rax,%r12,8), %rdi
callq 0x175cf
movq 0x40(%r14), %rsi
movq (%rsi,%rbx), %rax
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
addl %ecx, %ebp
testq %rax, %rax
je 0x1a2ed
movq 0x38(%r14), %rax
movq (%rax,%r12,8), %rax
movslq 0x34(%rax), %rax
addq %rax, 0x10(%r14)
incq %r12
movslq (%r14), %rax
addq $0x18, %rbx
cmpq %rax, %r12
jl 0x1a2ab
testl %ebp, %ebp
je 0x1a669
movq %r13, 0x8(%rsp)
testl %eax, %eax
jle 0x1a67e
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %r15d, %r15d
movq 0x40(%r14), %rbp
movq (%rbp,%r12), %rsi
testq %rsi, %rsi
je 0x1a360
testq %r15, %r15
je 0x1a347
movq %r15, %rdi
callq 0x113c4
testl %eax, %eax
jle 0x1a350
movq (%rbp,%r12), %r15
movzwl 0x12(%r15), %ebx
jmp 0x1a360
movzwl 0x12(%rsi), %ebx
movq %rsi, %r15
jmp 0x1a360
jne 0x1a360
movq (%rbp,%r12), %rax
movzwl 0x12(%rax), %eax
cmpl %ebx, %eax
cmovgl %eax, %ebx
incq %r13
movslq (%r14), %rax
addq $0x18, %r12
cmpq %rax, %r13
jl 0x1a31c
testq %r15, %r15
je 0x1a67e
cmpl $0x1, %ebx
jle 0x1a67e
xorl %edx, %edx
cmpl $0x3, %ebx
leaq 0x51c3(%rip), %rax      # 0x1f550
cmovaeq %rax, %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x11458
movl 0x8(%rbx), %ecx
cmpl %ecx, %eax
je 0x1a3cb
addl 0x4(%rbx), %ecx
leaq 0x18(%rsp), %r8
movl %ecx, (%r8)
movq 0x50(%r14), %rdi
leaq 0x5068(%rip), %rdx      # 0x1f426
movq %rbx, %rsi
movl $0x1, %ecx
callq 0x1156b
movl (%r14), %eax
testl %eax, %eax
jle 0x1a49f
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %edx, %edx
movq 0x38(%r14), %rcx
movq (%rcx,%r13,8), %rbx
movl 0x34(%rbx), %ecx
testl %ecx, %ecx
je 0x1a48c
movq 0x40(%r14), %rbp
movq (%rbp,%r12), %rsi
movq %rdx, 0x10(%rsp)
movslq %edx, %r15
testq %rsi, %rsi
je 0x1a418
movq 0x8(%rsp), %rdi
callq 0x113c4
movl 0x34(%rbx), %ecx
testl %eax, %eax
je 0x1a446
movq 0x58(%r14), %rdi
addq %r15, %rdi
addl %ecx, %ecx
movslq %ecx, %rdx
xorl %esi, %esi
callq 0x21e0
addq 0x60(%r14), %r15
movl 0x34(%rbx), %eax
addl %eax, %eax
movslq %eax, %rdx
movq %r15, %rdi
movl $0x1, %esi
callq 0x21e0
jmp 0x1a47e
movq $0x0, (%rbp,%r12)
movq 0x58(%r14), %rdi
addq %r15, %rdi
movq 0x8(%rbp,%r12), %rsi
addl %ecx, %ecx
movslq %ecx, %rdx
callq 0x2280
addq 0x60(%r14), %r15
movq 0x10(%rbp,%r12), %rsi
movl 0x34(%rbx), %eax
addl %eax, %eax
movslq %eax, %rdx
movq %r15, %rdi
callq 0x2280
movl 0x34(%rbx), %eax
movq 0x10(%rsp), %rdx
leal (%rdx,%rax,2), %edx
movl (%r14), %eax
incq %r13
movslq %eax, %rcx
addq $0x18, %r12
cmpq %rcx, %r13
jl 0x1a3de
movq 0xa0(%r14), %rdi
testq %rdi, %rdi
je 0x1a4c7
movq 0x50(%r14), %rsi
movq 0x8(%rsp), %r12
movq %r12, %rdx
callq 0x17341
movl %eax, %ebp
testl %eax, %eax
jne 0x1a4ce
jmp 0x1a65e
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
testb $0x1, 0xc(%r14)
jne 0x1a4ea
cmpq $0x0, 0x48(%r14)
jne 0x1a4ea
cmpl $0x0, 0x68(%r14)
jg 0x1a4ea
cmpl $0x2, 0x8(%r14)
jl 0x1a536
leaq 0x18(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x19c97
movq 0x50(%r14), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x19b43
cmpl $0x0, 0x68(%r14)
jle 0x1a520
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x19ed2
movq 0x48(%r14), %rsi
leaq 0x18(%rsp), %rdi
callq 0x19afb
testl %eax, %eax
je 0x1a65e
cmpq $0x0, 0xa0(%r14)
je 0x1a666
movl 0xc(%r14), %ecx
testb $0x4, %cl
je 0x1a5dc
movq 0xa8(%r14), %rax
testq %rax, %rax
je 0x1a5dc
cmpl $0x0, 0x4(%r14)
jle 0x1a5dc
xorl %ecx, %ecx
cmpl $0x2, %ebp
setne %cl
movq 0x58(%r14), %rdx
movq 0x60(%r14), %rsi
movl $0x1, %edi
xorl %r8d, %r8d
movabsq $0x200000000, %r9       # imm = 0x200000000
xorl %r10d, %r10d
movq %r8, %r11
sarq $0x20, %r11
movzbl (%rdx,%r11), %ebx
movzbl (%rsi,%r11), %r11d
addl %r11d, %r11d
orl %ebx, %r11d
movslq %edi, %rdi
movzbl (%rdx,%rdi), %ebx
movzbl (%rsi,%rdi), %ebp
addl %ebp, %ebp
orl %ebx, %ebp
cmpl %ecx, %r11d
sete %r11b
cmpl %ecx, %ebp
sete %bl
orb %r11b, %bl
movzbl %bl, %r11d
addl %r11d, (%rax,%r10,4)
incq %r10
movslq 0x4(%r14), %r11
addl $0x2, %edi
addq %r9, %r8
cmpq %r11, %r10
jl 0x1a589
movl 0xc(%r14), %ecx
testb $0x8, %cl
je 0x1a62b
movq 0xb0(%r14), %rax
testq %rax, %rax
je 0x1a62b
movl 0x4(%r14), %ecx
addl %ecx, %ecx
testl %ecx, %ecx
jle 0x1a62b
movq 0x58(%r14), %rdx
movl %ecx, %esi
xorl %edi, %edi
cmpb $0x1, (%rdx,%rdi)
jne 0x1a623
movq 0x60(%r14), %rcx
cmpb $0x0, (%rcx,%rdi)
jne 0x1a623
movb 0x90(%r14), %cl
movl $0x1, %r8d
shlq %cl, %r8
orq %r8, (%rax,%rdi,8)
incq %rdi
cmpq %rdi, %rsi
jne 0x1a5ff
movq 0x50(%r14), %rdi
movslq 0x90(%r14), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x90(%r14)
leaq (%rax,%rax,2), %rdx
shlq $0x4, %rdx
addq 0x98(%r14), %rdx
xorl %r15d, %r15d
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x1a69d
jmp 0x1a669
movl $0x1, %r15d
jmp 0x1a669
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5615(%rip), %rdi      # 0x1fc9a
leaq 0x51a8(%rip), %rsi      # 0x1f834
leaq 0x561d(%rip), %rcx      # 0x1fcb0
movl $0x335, %edx            # imm = 0x335
callq 0x21b0