// File: /workspace/github_c_makefile_star2_O1/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, %r12
movl %edi, %r15d
leaq 0x1b344(%rip), %rdx     # 0x1e16e
callq 0x23d0
testl %eax, %eax
js 0x2eb9
xorl %r13d, %r13d
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
leaq 0x1b325(%rip), %rbx     # 0x1e16e
movq 0x23160(%rip), %r14     # 0x25fb0
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
cmpl $0x52, %eax
jle 0x2e7d
cmpl $0x53, %eax
je 0x2e77
cmpl $0x6c, %eax
je 0x2e8d
cmpl $0x74, %eax
jne 0x2ea0
movq (%r14), %rax
movq %rax, 0x18(%rsp)
orl $0x1, %r13d
jmp 0x2ea0
cmpl $0x31, %eax
je 0x2e9b
cmpl $0x46, %eax
jne 0x2ea0
orl $0x4, %r13d
jmp 0x2ea0
movq (%r14), %rdi
callq 0x23e0
movl %eax, 0x8(%rsp)
jmp 0x2ea0
movl $0x1, %ebp
movl %r15d, %edi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x23d0
testl %eax, %eax
jns 0x2e5b
testl %ebp, %ebp
sete %cl
jmp 0x2ecf
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movq $0x0, 0x18(%rsp)
movb $0x1, %cl
xorl %r13d, %r13d
movq 0x230c2(%rip), %rax     # 0x25f98
movslq (%rax), %rbx
movl %r15d, %eax
subl %ebx, %eax
cmpl $0x1, %eax
jle 0x3374
movb %cl, 0x38(%rsp)
movq (%r12,%rbx,8), %rbp
movq %rbp, %rdi
callq 0x2170
leaq 0x9(%rax), %rdi
callq 0x22e0
movq %rax, %r14
movw $0x72, 0x20(%rsp)
testb $0x1, %r13b
jne 0x2f21
leaq 0x20(%rsp), %rdi
callq 0x2170
movw $0x62, 0x20(%rsp,%rax)
movq 0x8(%r12,%rbx,8), %rdi
leaq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x6414
testq %rax, %rax
je 0x3380
andl $0x4, %r13d
movq %rax, 0x40(%rsp)
movq %rax, %rdi
movq %r13, 0x80(%rsp)
movl %r13d, %esi
callq 0x12f62
movq %rax, 0x10(%rsp)
movq 0x28(%rax), %rdi
cmpl $0x0, 0x10(%rdi)
jle 0x339f
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1098f
movq %rax, %r13
leaq 0x1b33e(%rip), %rdx     # 0x1e2c0
movq %rax, %rdi
xorl %esi, %esi
callq 0x106de
testl %eax, %eax
jns 0x2fb0
leaq 0x1b32c(%rip), %rsi     # 0x1e2c3
movq %r13, %rdi
callq 0xb6db
leaq 0x1b31a(%rip), %rdx     # 0x1e2c0
movq %r13, %rdi
xorl %esi, %esi
callq 0x106de
movl %r15d, 0x4(%rsp)
leaq 0x1b344(%rip), %rsi     # 0x1e300
movq %r13, %rdi
callq 0xb6db
leaq 0x1b375(%rip), %rsi     # 0x1e340
movq %r14, %rdi
movq %rbp, 0x28(%rsp)
movq %rbp, %rdx
xorl %eax, %eax
callq 0x23f0
leaq 0x1b363(%rip), %rsi     # 0x1e347
movq %r14, 0x30(%rsp)
movq %r14, %rdi
callq 0x23b0
movq %rax, %rbx
movq 0x10(%rsp), %rbp
movq 0x28(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jle 0x303a
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 0x3009
movq %rbx, %rdi
callq 0x2160
leaq 0x1b301(%rip), %rsi     # 0x1e34a
xorl %r14d, %r14d
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq 0x28(%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 0x151d8
movq %rax, 0x78(%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, 0x38(%rsp)
jne 0x30ea
leaq 0x1b294(%rip), %rsi     # 0x1e351
movq %rbx, %rdi
movq 0x28(%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 0x151d8
movq %rax, %r14
movl 0x4(%rsp), %ebx
movq 0x28(%rbp), %rax
movslq 0x10(%rax), %rdi
addq %rdi, %rdi
movl $0x1, %esi
callq 0x2220
movq %rax, 0x48(%rsp)
movw $0x6277, 0x58(%rsp)     # imm = 0x6277
movb $0x0, 0x5a(%rsp)
movl 0x8(%rsp), %edx
cmpl $0x9, %edx
ja 0x3130
leaq 0x5a(%rsp), %rdi
leaq 0x1cbb4(%rip), %rsi     # 0x1fcdd
xorl %eax, %eax
callq 0x23f0
movq %r12, 0x38(%rsp)
leaq 0x1b21c(%rip), %rsi     # 0x1e358
movq 0x30(%rsp), %r15
movq %r15, %rdi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x23f0
leaq 0x58(%rsp), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x6414
movq %rax, %rbp
movq %rax, %rdi
movq %r13, %rsi
callq 0xc2f1
movq 0x22e24(%rip), %rax     # 0x25f98
movl (%rax), %eax
leal 0x1(%rax), %ecx
cmpl %ebx, %ecx
movq 0x40(%rsp), %r15
jge 0x3301
movslq %eax, %r12
incq %r12
xorl %ebx, %ebx
movq 0x22e03(%rip), %rcx     # 0x25f98
cmpl (%rcx), %eax
je 0x31c9
movq 0x38(%rsp), %rax
movq (%rax,%r12,8), %rdi
leaq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x6414
movq %rax, %r15
movq %rax, %rdi
movq 0x80(%rsp), %rsi
callq 0x12f62
movq %rax, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x40(%rsp)
xorl %eax, %eax
callq 0xb9db
movq %rax, %r12
movq 0x10(%rsp), %rdi
callq 0x130ba
testq %rax, %rax
je 0x32cb
movq %rax, %r15
movl %ebx, 0x54(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movl $0x1, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x13885
movq %r13, %rdi
movq %r12, %rsi
leaq 0x1b146(%rip), %rdx     # 0x1e35f
movl $0x1, %ecx
leaq 0x54(%rsp), %r8
callq 0x113c8
cmpl $0x0, 0x2c(%r15)
jle 0x3274
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 0x3231
movq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x15629
testq %r14, %r14
je 0x3295
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
callq 0x15629
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10c7a
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x1068d
incq %rbx
movq 0x10(%rsp), %rdi
callq 0x130ba
movq %rax, %r15
testq %rax, %rax
jne 0x31f3
movq %r12, %rdi
callq 0xb9ea
movq 0x10(%rsp), %rdi
callq 0x13052
movq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0x660f
movq 0x8(%rsp), %rcx
movl %ecx, %eax
incq %rcx
movq %rcx, %r12
cmpl 0x4(%rsp), %ecx
jne 0x318e
movq %rbp, %rdi
callq 0x660f
movq 0x48(%rsp), %rdi
callq 0x2050
movq 0x60(%rsp), %rdi
callq 0x2050
movq 0x68(%rsp), %rdi
callq 0x2050
testq %r14, %r14
je 0x3334
movq %r14, %rdi
callq 0x15515
movq 0x78(%rsp), %rdi
callq 0x15515
movq %r13, %rdi
callq 0xb84a
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movl $0xe, %esi
callq 0x1092f
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 0x3362
leaq 0x1acd8(%rip), %rdi     # 0x1e05f
leaq 0x1aeef(%rip), %rsi     # 0x1e27d
leaq 0x1aef1(%rip), %rcx     # 0x1e286
movl $0x2e, %edx
callq 0x21b0
leaq 0x1aefe(%rip), %rdi     # 0x1e2a4
leaq 0x1aed0(%rip), %rsi     # 0x1e27d
leaq 0x1aed2(%rip), %rcx     # 0x1e286
movl $0x30, %edx
callq 0x21b0