// File: /workspace/github_c_makefile_star2_O1/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 0x19f45
movq %rsi, %r13
movq %rdi, %r14
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x40(%r14), %rsi
cmpq $0x0, (%rsi,%rbx)
jne 0x19ba6
addq %rbx, %rsi
movq 0x38(%r14), %rax
movq (%rax,%r12,8), %rdi
callq 0x16f64
movq 0x40(%r14), %rax
movq (%rax,%rbx), %rcx
xorl %eax, %eax
testq %rcx, %rcx
setne %cl
je 0x19bc8
movq 0x38(%r14), %rdx
movq (%rdx,%r12,8), %rdx
movslq 0x34(%rdx), %rdx
addq %rdx, 0x10(%r14)
movb %cl, %al
addl %eax, %ebp
incq %r12
movslq (%r14), %rax
addq $0x18, %rbx
cmpq %rax, %r12
jl 0x19b8b
testl %ebp, %ebp
je 0x19f45
movq %r13, 0x8(%rsp)
cmpl $0x0, (%r14)
jle 0x19c4d
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x40(%r14), %r13
movq (%r13,%rbx), %rsi
testq %rsi, %rsi
je 0x19c3c
testq %r15, %r15
je 0x19c23
movq %r15, %rdi
callq 0x11224
testl %eax, %eax
jle 0x19c2c
movq (%r13,%rbx), %r15
movzwl 0x12(%r15), %ebp
jmp 0x19c3c
movzwl 0x12(%rsi), %ebp
movq %rsi, %r15
jmp 0x19c3c
jne 0x19c3c
movq (%r13,%rbx), %rax
movzwl 0x12(%rax), %eax
cmpl %ebp, %eax
cmovgl %eax, %ebp
incq %r12
movslq (%r14), %rax
addq $0x18, %rbx
cmpq %rax, %r12
jl 0x19bf8
jmp 0x19c52
xorl %r15d, %r15d
xorl %ebp, %ebp
testq %r15, %r15
je 0x19f5a
cmpl $0x1, %ebp
jle 0x19f5a
xorl %edx, %edx
cmpl $0x3, %ebp
leaq 0x58d5(%rip), %rax      # 0x1f545
cmovgeq %rax, %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x112b8
movl 0x8(%rbx), %ecx
cmpl %ecx, %eax
je 0x19cae
addl 0x4(%rbx), %ecx
leaq 0x18(%rsp), %r8
movl %ecx, (%r8)
movq 0x50(%r14), %rdi
leaq 0x577a(%rip), %rdx      # 0x1f41b
movq %rbx, %rsi
movl $0x1, %ecx
callq 0x113c8
cmpl $0x0, (%r14)
jle 0x19d7f
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x38(%r14), %rax
movq (%rax,%r13,8), %rbx
cmpl $0x0, 0x34(%rbx)
je 0x19d6c
movq 0x40(%r14), %r15
movq (%r15,%r12), %rsi
testq %rsi, %rsi
je 0x19ced
movq 0x8(%rsp), %rdi
callq 0x11224
testl %eax, %eax
je 0x19d21
movslq %ebp, %r15
movq 0x58(%r14), %rdi
addq %r15, %rdi
movl 0x34(%rbx), %eax
addl %eax, %eax
movslq %eax, %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 0x19d65
movq $0x0, (%r15,%r12)
movslq %ebp, %rax
movq %rax, 0x10(%rsp)
movq 0x58(%r14), %rdi
addq %rax, %rdi
movq 0x8(%r15,%r12), %rsi
movl 0x34(%rbx), %eax
addl %eax, %eax
movslq %eax, %rdx
callq 0x2280
movq 0x10(%rsp), %rdi
addq 0x60(%r14), %rdi
movq 0x10(%r15,%r12), %rsi
movl 0x34(%rbx), %eax
addl %eax, %eax
movslq %eax, %rdx
callq 0x2280
movl 0x34(%rbx), %eax
leal (%rbp,%rax,2), %ebp
incq %r13
movslq (%r14), %rax
addq $0x18, %r12
cmpq %rax, %r13
jl 0x19cc0
movq 0xa0(%r14), %rdi
testq %rdi, %rdi
je 0x19da7
movq 0x50(%r14), %rsi
movq 0x8(%rsp), %r12
movq %r12, %rdx
callq 0x16cc9
movl %eax, %ebp
testl %eax, %eax
jne 0x19dae
jmp 0x19f3a
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
testb $0x1, 0xc(%r14)
jne 0x19dca
cmpq $0x0, 0x48(%r14)
jne 0x19dca
cmpl $0x0, 0x68(%r14)
jg 0x19dca
cmpl $0x2, 0x8(%r14)
jl 0x19e16
leaq 0x18(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1957a
movq 0x50(%r14), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x193e7
cmpl $0x0, 0x68(%r14)
jle 0x19e00
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x197c4
movq 0x48(%r14), %rsi
leaq 0x18(%rsp), %rdi
callq 0x1939f
testl %eax, %eax
je 0x19f3a
cmpq $0x0, 0xa0(%r14)
je 0x19f42
testb $0x4, 0xc(%r14)
je 0x19eb6
movq 0xa8(%r14), %rax
testq %rax, %rax
je 0x19eb6
cmpl $0x0, 0x4(%r14)
jle 0x19eb6
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 0x19e67
testb $0x8, 0xc(%r14)
je 0x19f07
movq 0xb0(%r14), %rax
testq %rax, %rax
je 0x19f07
movl 0x4(%r14), %ecx
addl %ecx, %ecx
testl %ecx, %ecx
jle 0x19f07
movq 0x58(%r14), %rdx
movl %ecx, %esi
xorl %edi, %edi
cmpb $0x1, (%rdx,%rdi)
jne 0x19eff
movq 0x60(%r14), %rcx
cmpb $0x0, (%rcx,%rdi)
jne 0x19eff
movb 0x90(%r14), %cl
movl $0x1, %r8d
shlq %cl, %r8
orq %r8, (%rax,%rdi,8)
incq %rdi
cmpq %rdi, %rsi
jne 0x19edb
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 0x19f79
jmp 0x19f45
movl $0x1, %r15d
jmp 0x19f45
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 0x5d19(%rip), %rdi      # 0x1fc7a
leaq 0x58ac(%rip), %rsi      # 0x1f814
leaq 0x5d21(%rip), %rcx      # 0x1fc90
movl $0x335, %edx            # imm = 0x335
callq 0x21b0