// File: /workspace/github_c_makefile_star2_O3/lh3[P]bgt/import.c
// Function: main_import
int main_import(int argc, char *argv[])
{
	int i, j, c, clevel = -1, flag = 0, id_GT = -1, gen_pb1 = 0;
	char *fn_ref = 0, moder[8], modew[8];
	char *prefix, *fn;
	uint8_t *bits[2], *bit1;
	int64_t n = 0;
	htsFile *in, *out;
	bcf_hdr_t *h0;
	FILE *fp;
	pbf_t *pb, *pb1 = 0;
	bcf_atombuf_t *ab;
	const bcf_atom_t *a;

	while ((c = getopt(argc, argv, "1l:SFt:")) >= 0) {
		switch (c) {
		case '1': gen_pb1 = 1; break;
		case 'l': clevel = atoi(optarg); flag |= 2; break;
		case 'S': flag |= 1; break;
		case 't': fn_ref = optarg; flag |= 1; break;
		case 'F': flag |= 4; break;
		}
	}
	if (argc - optind < 2) {
		fprintf(stderr, "Usage: bgt import [options] <out-prefix> <in.bcf>|<in.vcf>|<in.vcf.gz>\n");
		fprintf(stderr, "Options:\n");
		fprintf(stderr, "  -S           input is VCF\n");
		fprintf(stderr, "  -t FILE      list of reference names and lengths [null]\n");
		fprintf(stderr, "  -F           keep filtered variants\n");
		fprintf(stderr, "  -1           generate .pb1 file (not used for now)\n");
		return 1;
	}
	prefix = argv[optind];
	fn = (char*)malloc(strlen(prefix) + 9);
	strcpy(moder, "r");
	if ((flag&1) == 0) strcat(moder, "b");

	in = hts_open(argv[optind+1], moder, fn_ref);
	assert(in);
	ab = bcf_atombuf_init(in, flag&4);
	assert(ab->h->n[BCF_DT_SAMPLE] > 0);
	h0 = bcf_hdr_subset(ab->h, 0, 0, 0);
	id_GT = bcf_id2int(h0, BCF_DT_ID, "GT");
	if (id_GT < 0) {
		bcf_hdr_append(h0, "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">");
		id_GT = bcf_id2int(h0, BCF_DT_ID, "GT");
	}
	bcf_hdr_append(h0, "##INFO=<ID=_row,Number=1,Type=Integer,Description=\"row number\">");

	// write sample list
	sprintf(fn, "%s.spl", prefix);
	fp = fopen(fn, "wb");
	for (i = 0; i < ab->h->n[BCF_DT_SAMPLE]; ++i) {
		fputs(ab->h->id[BCF_DT_SAMPLE][i].key, fp);
		fputc('\n', fp);
	}
	fclose(fp);

	// prepare PBF to write
	sprintf(fn, "%s.pbf", prefix);
	pb = pbf_open_w(fn, ab->h->n[BCF_DT_SAMPLE]*2, 2, 13);
	bits[0] = (uint8_t*)calloc(ab->h->n[BCF_DT_SAMPLE]*2, 1);
	bits[1] = (uint8_t*)calloc(ab->h->n[BCF_DT_SAMPLE]*2, 1);

	if (gen_pb1) {
		sprintf(fn, "%s.pb1", prefix);
		pb1 = pbf_open_w(fn, ab->h->n[BCF_DT_SAMPLE]*2, 1, 13);
	}
	bit1 = (uint8_t*)calloc(ab->h->n[BCF_DT_SAMPLE]*2, 1);

	// write site-only BCF header
	strcpy(modew, "wb");
	if (clevel >= 0 && clevel <= 9) sprintf(modew + 2, "%d", clevel);
	sprintf(fn, "%s.bcf", prefix);
	out = hts_open(fn, modew, 0);
	vcf_hdr_write(out, h0);

	for (j = optind + 1; j < argc; ++j) {
		bcf1_t *b;
		if (j != optind + 1) { // the first file has already been opened
			in = hts_open(argv[j], moder, fn_ref);
			ab = bcf_atombuf_init(in, flag&4);
		}
		b = bcf_init1();
		while ((a = bcf_atom_read(ab)) != 0) {
			int32_t i, val = n;
			bcf_atom2bcf(a, b, 1, -1);
			bcf_append_info_ints(h0, b, "_row", 1, &val);
			for (i = 0; i < a->n_gt; ++i) {
				bits[0][i] = a->gt[i]&1, bits[1][i] = a->gt[i]>>1&1;
				bit1[i] = (a->gt[i] == 1);
			}
			pbf_write(pb, bits);
			if (pb1) pbf_write(pb1, &bit1);
			bcf_subset(h0, b, 0, 0);
			vcf_write1(out, h0, b);
			++n;
		}
		bcf_destroy1(b);
		bcf_atombuf_destroy(ab);
		hts_close(in);
	}

	hts_close(out);
	free(bit1); free(bits[0]); free(bits[1]);
	if (pb1) pbf_close(pb1);
	pbf_close(pb);
	bcf_hdr_destroy(h0);

	bcf_index_build(fn, 14);
	free(fn);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r15
movl %edi, %r12d
leaq 0x1b324(%rip), %rdx     # 0x1e16e
callq 0x23d0
testl %eax, %eax
js 0x2ed9
xorl %r13d, %r13d
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
leaq 0x1b305(%rip), %rbx     # 0x1e16e
movq 0x23140(%rip), %r14     # 0x25fb0
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
cmpl $0x52, %eax
jle 0x2e9d
cmpl $0x53, %eax
je 0x2e97
cmpl $0x6c, %eax
je 0x2ead
cmpl $0x74, %eax
jne 0x2ec0
movq (%r14), %rax
movq %rax, 0x18(%rsp)
orl $0x1, %r13d
jmp 0x2ec0
cmpl $0x31, %eax
je 0x2ebb
cmpl $0x46, %eax
jne 0x2ec0
orl $0x4, %r13d
jmp 0x2ec0
movq (%r14), %rdi
callq 0x23e0
movl %eax, 0x10(%rsp)
jmp 0x2ec0
movl $0x1, %ebp
movl %r12d, %edi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x23d0
testl %eax, %eax
jns 0x2e7b
testl %ebp, %ebp
sete %cl
jmp 0x2eef
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movq $0x0, 0x18(%rsp)
movb $0x1, %cl
xorl %r13d, %r13d
movq 0x230a2(%rip), %rax     # 0x25f98
movslq (%rax), %rbx
movl %r12d, %eax
subl %ebx, %eax
cmpl $0x1, %eax
jle 0x339c
movb %cl, 0xf(%rsp)
movq (%r15,%rbx,8), %rbp
movq %rbp, %rdi
callq 0x2170
leaq 0x9(%rax), %rdi
callq 0x22e0
movq %rax, 0x38(%rsp)
movw $0x72, 0x28(%rsp)
testb $0x1, %r13b
jne 0x2f43
leaq 0x28(%rsp), %rdi
callq 0x2170
movw $0x62, 0x28(%rsp,%rax)
movq 0x8(%r15,%rbx,8), %rdi
leaq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x6553
testq %rax, %rax
je 0x33a8
andl $0x4, %r13d
movq %rax, 0x40(%rsp)
movq %rax, %rdi
movq %r13, 0x80(%rsp)
movl %r13d, %esi
callq 0x13331
movq 0x28(%rax), %rdi
cmpl $0x0, 0x10(%rdi)
movq 0x38(%rsp), %rbx
jle 0x33c7
movq %rax, %r14
movq %r15, 0x78(%rsp)
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10b38
movq %rax, %r13
leaq 0x1b314(%rip), %rdx     # 0x1e2c0
movq %rax, %rdi
xorl %esi, %esi
callq 0x1088a
testl %eax, %eax
jns 0x2fda
leaq 0x1b302(%rip), %rsi     # 0x1e2c3
movq %r13, %rdi
callq 0xb817
leaq 0x1b2f0(%rip), %rdx     # 0x1e2c0
movq %r13, %rdi
xorl %esi, %esi
callq 0x1088a
leaq 0x1b31f(%rip), %rsi     # 0x1e300
movq %r13, %rdi
callq 0xb817
leaq 0x1b350(%rip), %rsi     # 0x1e340
movq %rbx, %rdi
movq %rbp, 0x30(%rsp)
movq %rbp, %rdx
xorl %eax, %eax
callq 0x23f0
leaq 0x1b33e(%rip), %rsi     # 0x1e347
movq %rbx, %rdi
callq 0x23b0
movq %rax, %rbx
movq 0x28(%r14), %rax
cmpl $0x0, 0x10(%rax)
movq %r14, %rbp
jle 0x3058
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x28(%rax), %rax
movq (%rax,%r14), %rdi
movq %rbx, %rsi
callq 0x21c0
movl $0xa, %edi
movq %rbx, %rsi
callq 0x21f0
incq %r15
movq 0x28(%rbp), %rax
movslq 0x10(%rax), %rcx
addq $0x10, %r14
cmpq %rcx, %r15
jl 0x3027
movq %rbx, %rdi
callq 0x2160
leaq 0x1b2e3(%rip), %rsi     # 0x1e34a
xorl %r14d, %r14d
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x23f0
movq 0x28(%rbp), %rax
movl 0x10(%rax), %esi
addl %esi, %esi
movq %rbx, %rdi
movl $0x2, %edx
movl $0xd, %ecx
callq 0x156a3
movq %rax, 0x70(%rsp)
movq 0x28(%rbp), %rax
movslq 0x10(%rax), %r15
addq %r15, %r15
movl $0x1, %esi
movq %r15, %rdi
callq 0x2220
movq %rax, 0x60(%rsp)
movl $0x1, %esi
movq %r15, %rdi
callq 0x2220
movq %rax, 0x68(%rsp)
cmpb $0x0, 0xf(%rsp)
movq %rbp, 0x20(%rsp)
jne 0x3118
leaq 0x1b271(%rip), %rsi     # 0x1e351
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x23f0
movq 0x28(%rbp), %rax
movl 0x10(%rax), %esi
addl %esi, %esi
movq %rbx, %rdi
movl $0x1, %edx
movl $0xd, %ecx
callq 0x156a3
movq %rax, %r14
movq 0x28(%rbp), %rax
movslq 0x10(%rax), %r15
addq %r15, %r15
movl $0x1, %esi
movq %r15, %rdi
callq 0x2220
movq %rax, 0x48(%rsp)
movw $0x6277, 0x58(%rsp)     # imm = 0x6277
movb $0x0, 0x5a(%rsp)
movl 0x10(%rsp), %edx
cmpl $0x9, %edx
ja 0x3152
leaq 0x5a(%rsp), %rdi
leaq 0x1cbb2(%rip), %rsi     # 0x1fcfd
xorl %eax, %eax
callq 0x23f0
leaq 0x1b1ff(%rip), %rsi     # 0x1e358
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x23f0
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x6553
movq %rax, %rbp
movq %rax, %rdi
movq %r13, %rsi
callq 0xc494
movq 0x22e0c(%rip), %rax     # 0x25f98
movl (%rax), %eax
leal 0x1(%rax), %ecx
cmpl %r12d, %ecx
movq 0x40(%rsp), %r15
jge 0x3329
movslq %eax, %rcx
incq %rcx
movq %rcx, 0x10(%rsp)
xorl %ebx, %ebx
movl %r12d, 0x50(%rsp)
movq 0x22de0(%rip), %rcx     # 0x25f98
cmpl (%rcx), %eax
je 0x31f1
movq 0x78(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x6553
movq %rax, %r15
movq %rax, %rdi
movq 0x80(%rsp), %rsi
callq 0x13331
movq %rax, 0x20(%rsp)
movq %r15, 0x40(%rsp)
xorl %eax, %eax
callq 0xbb1a
movq %rax, %r12
movq 0x20(%rsp), %rdi
callq 0x13483
testq %rax, %rax
je 0x32ed
movq %rax, %r15
movl %ebx, 0x54(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movl $0x1, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x13c7f
movq %r13, %rdi
movq %r12, %rsi
leaq 0x1b123(%rip), %rdx     # 0x1e35f
movl $0x1, %ecx
leaq 0x54(%rsp), %r8
callq 0x1156b
cmpl $0x0, 0x2c(%r15)
jle 0x3297
xorl %eax, %eax
movq 0x38(%r15), %rcx
movb (%rcx,%rax), %cl
andb $0x1, %cl
movq 0x60(%rsp), %rdx
movb %cl, (%rdx,%rax)
movq 0x38(%r15), %rcx
movb (%rcx,%rax), %cl
shrb %cl
andb $0x1, %cl
movq 0x68(%rsp), %rdx
movb %cl, (%rdx,%rax)
movq 0x38(%r15), %rcx
cmpb $0x1, (%rcx,%rax)
movq 0x48(%rsp), %rcx
sete (%rcx,%rax)
incq %rax
movslq 0x2c(%r15), %rcx
cmpq %rcx, %rax
jl 0x3254
movq 0x70(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x15ad6
testq %r14, %r14
je 0x32b8
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
callq 0x15ad6
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0e
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x10839
incl %ebx
movq 0x20(%rsp), %rdi
callq 0x13483
movq %rax, %r15
testq %rax, %rax
jne 0x3216
movq %r12, %rdi
callq 0xbb29
movq 0x20(%rsp), %rdi
callq 0x1341b
movq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0x677e
movq 0x10(%rsp), %rcx
movl %ecx, %eax
incq %rcx
movq %rcx, 0x10(%rsp)
movl 0x50(%rsp), %r12d
cmpl %r12d, %ecx
jne 0x31b1
movq %rbp, %rdi
callq 0x677e
movq 0x48(%rsp), %rdi
callq 0x2050
movq 0x60(%rsp), %rdi
callq 0x2050
movq 0x68(%rsp), %rdi
callq 0x2050
testq %r14, %r14
je 0x335c
movq %r14, %rdi
callq 0x159c2
movq 0x70(%rsp), %rdi
callq 0x159c2
movq %r13, %rdi
callq 0xb980
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movl $0xe, %esi
callq 0x10ad8
movq %rbx, %rdi
callq 0x2050
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2540
movl $0x1, %eax
jmp 0x338a
leaq 0x1acb0(%rip), %rdi     # 0x1e05f
leaq 0x1aec7(%rip), %rsi     # 0x1e27d
leaq 0x1aec9(%rip), %rcx     # 0x1e286
movl $0x2e, %edx
callq 0x21b0
leaq 0x1aed6(%rip), %rdi     # 0x1e2a4
leaq 0x1aea8(%rip), %rsi     # 0x1e27d
leaq 0x1aeaa(%rip), %rcx     # 0x1e286
movl $0x30, %edx
callq 0x21b0