// File: /workspace/github_c_makefile_star2_O2/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, %r13d
pushq $-0x1
popq %rax
movl %eax, 0x18(%rsp)
movq $0x0, 0x38(%rsp)
leaq 0x15970(%rip), %rbx     # 0x1816e
movq 0x1e7ab(%rip), %r14     # 0x20fb0
pushq $0x1
popq %rbp
movl $0x0, (%rsp)
xorl %r12d, %r12d
movl %r13d, %edi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x23e0
testl %eax, %eax
js 0x2864
cmpl $0x74, %eax
je 0x2850
cmpl $0x46, %eax
je 0x285e
cmpl $0x53, %eax
je 0x2858
cmpl $0x6c, %eax
je 0x2842
cmpl $0x31, %eax
jne 0x2812
movl %ebp, (%rsp)
jmp 0x2812
movq (%r14), %rdi
callq 0x23f0
movl %eax, 0x18(%rsp)
jmp 0x2812
movq (%r14), %rax
movq %rax, 0x38(%rsp)
orl $0x1, %r12d
jmp 0x2812
orl $0x4, %r12d
jmp 0x2812
movq 0x1e72d(%rip), %rax     # 0x20f98
movslq (%rax), %rbx
movl %r13d, %eax
subl %ebx, %eax
cmpl $0x1, %eax
jle 0x2cf2
movq (%r15,%rbx,8), %rbp
movq %rbp, %rdi
callq 0x2170
leaq 0x9(%rax), %rdi
callq 0x22e0
movq %rax, 0x30(%rsp)
movw $0x72, 0x28(%rsp)
testb $0x1, %r12b
jne 0x28b4
leaq 0x28(%rsp), %rdi
callq 0x2170
movw $0x62, 0x28(%rsp,%rax)
movq 0x8(%r15,%rbx,8), %rdi
leaq 0x28(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x5e33
testq %rax, %rax
je 0x2d82
movq %rax, %rbx
movl %r13d, 0x4c(%rsp)
movl %r12d, %esi
andl $0x4, %esi
movq %rax, %rdi
movl %esi, 0x50(%rsp)
callq 0xebec
movq 0x28(%rax), %rdi
cmpl $0x0, 0x10(%rdi)
jle 0x2d9b
movq %rax, 0x8(%rsp)
movq %rbx, 0x20(%rsp)
movq %r15, 0x80(%rsp)
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xd078
movq %rax, %r12
leaq 0x159a0(%rip), %rdx     # 0x182c0
movq %rax, %rdi
xorl %esi, %esi
callq 0xcee5
testl %eax, %eax
jns 0x294e
leaq 0x1598e(%rip), %rsi     # 0x182c3
movq %r12, %rdi
callq 0xa387
leaq 0x1597c(%rip), %rdx     # 0x182c0
movq %r12, %rdi
xorl %esi, %esi
callq 0xcee5
leaq 0x159ab(%rip), %rsi     # 0x18300
movq %r12, %rdi
callq 0xa387
leaq 0x159dc(%rip), %rsi     # 0x18340
xorl %r14d, %r14d
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, 0x10(%rsp)
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2400
leaq 0x159c2(%rip), %rsi     # 0x18347
movq %rbx, %rdi
callq 0x23c0
movq %rax, %rbx
pushq $0xa
popq %rbp
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rax
movslq 0x10(%rax), %rcx
cmpq %rcx, %r15
jge 0x29cb
movq 0x28(%rax), %rax
movq (%rax,%r14), %rdi
movq %rbx, %rsi
callq 0x21c0
movl %ebp, %edi
movq %rbx, %rsi
callq 0x21f0
incq %r15
addq $0x10, %r14
jmp 0x2996
movq %rbx, %rdi
callq 0x2160
leaq 0x15970(%rip), %rsi     # 0x1834a
movq 0x30(%rsp), %r15
movq %r15, %rdi
movq 0x10(%rsp), %r13
movq %r13, %rdx
xorl %eax, %eax
callq 0x2400
movq 0x8(%rsp), %rbp
movq 0x28(%rbp), %rax
movl 0x10(%rax), %esi
addl %esi, %esi
pushq $0x2
popq %rdx
pushq $0xd
popq %rcx
movq %r15, %rdi
callq 0x10941
movq %rax, 0x78(%rsp)
movq 0x28(%rbp), %rax
movslq 0x10(%rax), %rbx
addq %rbx, %rbx
pushq $0x1
popq %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2220
movq %rax, 0x60(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2220
movq %rax, 0x68(%rsp)
cmpl $0x0, (%rsp)
je 0x2a83
leaq 0x15903(%rip), %rsi     # 0x18351
movq %r15, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2400
movq 0x28(%rbp), %rax
movl 0x10(%rax), %esi
addl %esi, %esi
pushq $0x1
popq %rdx
pushq $0xd
popq %rcx
movq %r15, %rdi
callq 0x10941
movq %rax, (%rsp)
movq 0x28(%rbp), %rax
movslq 0x10(%rax), %rbx
addq %rbx, %rbx
jmp 0x2a8b
movq $0x0, (%rsp)
movq 0x20(%rsp), %r14
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x2220
movq %rax, 0x40(%rsp)
movw $0x6277, 0x58(%rsp)     # imm = 0x6277
movb $0x0, 0x5a(%rsp)
movl 0x18(%rsp), %edx
cmpl $0x9, %edx
movl 0x4c(%rsp), %ebp
ja 0x2acc
leaq 0x5a(%rsp), %rdi
leaq 0x171e8(%rip), %rsi     # 0x19cad
xorl %eax, %eax
callq 0x2400
leaq 0x15885(%rip), %rsi     # 0x18358
movq %r15, %rdi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2400
leaq 0x58(%rsp), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x5e33
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0xad39
movq 0x1e490(%rip), %rax     # 0x20f98
movslq (%rax), %rax
movslq %ebp, %rcx
movq %rcx, 0x18(%rsp)
pushq $0x1
popq %r13
xorl %r15d, %r15d
movq 0x8(%rsp), %rbp
leaq 0x1(%rax), %rbx
cmpq 0x18(%rsp), %rbx
jge 0x2c7e
movq 0x1e463(%rip), %rcx     # 0x20f98
cmpl %eax, (%rcx)
je 0x2b66
movq 0x80(%rsp), %rax
movq (%rax,%rbx,8), %rdi
leaq 0x28(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x5e33
movq %rax, %r14
movq %rax, %rdi
movl 0x50(%rsp), %esi
callq 0xebec
movq %rax, %rbp
movq %rbx, 0x8(%rsp)
movq %r14, 0x20(%rsp)
xorl %eax, %eax
callq 0xa6a8
movq %rax, %r14
movq %rbp, %rbx
movq %rbp, %rdi
callq 0xed42
testq %rax, %rax
je 0x2c54
movq %rax, %rbp
movl %r15d, 0x54(%rsp)
movq %rax, %rdi
movq %r14, %rsi
movl %r13d, %edx
pushq $-0x1
popq %rcx
callq 0xf19c
movq %r12, %rdi
movq %r14, %rsi
leaq 0x157ab(%rip), %rdx     # 0x1835f
movl %r13d, %ecx
leaq 0x54(%rsp), %r8
callq 0xd67c
xorl %eax, %eax
movslq 0x2c(%rbp), %rcx
cmpq %rcx, %rax
jge 0x2c08
movq 0x38(%rbp), %rcx
movb (%rcx,%rax), %cl
andb $0x1, %cl
movq 0x60(%rsp), %rdx
movb %cl, (%rdx,%rax)
movq 0x38(%rbp), %rcx
movb (%rcx,%rax), %cl
shrb %cl
andb $0x1, %cl
movq 0x68(%rsp), %rdx
movb %cl, (%rdx,%rax)
movq 0x38(%rbp), %rcx
cmpb $0x1, (%rcx,%rax)
movq 0x40(%rsp), %rcx
sete (%rcx,%rax)
incq %rax
jmp 0x2bc3
movq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x10d5d
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2c2a
leaq 0x40(%rsp), %rsi
callq 0x10d5d
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xd1af
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xce98
incl %r15d
movq %rbx, %rbp
jmp 0x2b7a
movq %r14, %rdi
callq 0xa6b5
movq %rbx, %rbp
movq %rbx, %rdi
callq 0xece2
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x601f
movq 0x8(%rsp), %rax
jmp 0x2b1f
movq 0x10(%rsp), %rdi
callq 0x601f
movq 0x40(%rsp), %rdi
callq 0x2050
movq 0x60(%rsp), %rdi
callq 0x2050
movq 0x68(%rsp), %rdi
callq 0x2050
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2cb4
callq 0x10c4d
movq 0x78(%rsp), %rdi
callq 0x10c4d
movq %r12, %rdi
callq 0xa500
pushq $0xe
popq %rsi
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0xd01a
movq %rbx, %rdi
callq 0x2050
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1e2ff(%rip), %r14     # 0x20ff8
movq (%r14), %rcx
leaq 0x15473(%rip), %rdi     # 0x18176
pushq $0x47
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x2410
movq (%r14), %rcx
leaq 0x154a3(%rip), %rdi     # 0x181be
pushq $0x9
popq %rsi
movq %rbx, %rdx
callq 0x2410
movq (%r14), %rcx
leaq 0x15498(%rip), %rdi     # 0x181c8
pushq $0x1c
popq %rsi
movq %rbx, %rdx
callq 0x2410
movq (%r14), %rcx
leaq 0x154a0(%rip), %rdi     # 0x181e5
pushq $0x3a
popq %rsi
movq %rbx, %rdx
callq 0x2410
movq (%r14), %rcx
leaq 0x154c6(%rip), %rdi     # 0x18220
pushq $0x26
popq %rsi
movq %rbx, %rdx
callq 0x2410
movq (%r14), %rcx
leaq 0x154d8(%rip), %rdi     # 0x18247
pushq $0x35
popq %rsi
movq %rbx, %rdx
callq 0x2410
pushq $0x1
popq %rax
jmp 0x2ce0
leaq 0x152d6(%rip), %rdi     # 0x1805f
leaq 0x154ed(%rip), %rsi     # 0x1827d
leaq 0x154ef(%rip), %rcx     # 0x18286
pushq $0x2e
jmp 0x2db2
leaq 0x15502(%rip), %rdi     # 0x182a4
leaq 0x154d4(%rip), %rsi     # 0x1827d
leaq 0x154d6(%rip), %rcx     # 0x18286
pushq $0x30
popq %rdx
callq 0x21b0