// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xffffffff, -0x20(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x24(%rbp)
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0xb0(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x248d7(%rip), %rdx     # 0x2716e
callq 0x23e0
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jl 0x2944
movl -0x1c(%rbp), %eax
movl %eax, -0xd4(%rbp)
subl $0x31, %eax
je 0x28ec
jmp 0x28b8
movl -0xd4(%rbp), %eax
subl $0x46, %eax
je 0x2936
jmp 0x28c5
movl -0xd4(%rbp), %eax
subl $0x53, %eax
je 0x2912
jmp 0x28d2
movl -0xd4(%rbp), %eax
subl $0x6c, %eax
je 0x28f5
jmp 0x28df
movl -0xd4(%rbp), %eax
subl $0x74, %eax
je 0x291d
jmp 0x293f
movl $0x1, -0x2c(%rbp)
jmp 0x293f
movq 0x2b6b4(%rip), %rax     # 0x2dfb0
movq (%rax), %rdi
callq 0x23f0
movl %eax, -0x20(%rbp)
movl -0x24(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x24(%rbp)
jmp 0x293f
movl -0x24(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x293f
movq 0x2b68c(%rip), %rax     # 0x2dfb0
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movl -0x24(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x293f
movl -0x24(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x24(%rbp)
jmp 0x2889
movl -0x8(%rbp), %eax
movq 0x2b64a(%rip), %rcx     # 0x2df98
subl (%rcx), %eax
cmpl $0x2, %eax
jge 0x29f5
movq 0x2b698(%rip), %rax     # 0x2dff8
movq (%rax), %rdi
leaq 0x2480c(%rip), %rsi     # 0x27176
movb $0x0, %al
callq 0x2260
movq 0x2b680(%rip), %rax     # 0x2dff8
movq (%rax), %rdi
leaq 0x2483c(%rip), %rsi     # 0x271be
movb $0x0, %al
callq 0x2260
movq 0x2b668(%rip), %rax     # 0x2dff8
movq (%rax), %rdi
leaq 0x2482e(%rip), %rsi     # 0x271c8
movb $0x0, %al
callq 0x2260
movq 0x2b650(%rip), %rax     # 0x2dff8
movq (%rax), %rdi
leaq 0x24833(%rip), %rsi     # 0x271e5
movb $0x0, %al
callq 0x2260
movq 0x2b638(%rip), %rax     # 0x2dff8
movq (%rax), %rdi
leaq 0x24856(%rip), %rsi     # 0x27220
movb $0x0, %al
callq 0x2260
movq 0x2b620(%rip), %rax     # 0x2dff8
movq (%rax), %rdi
leaq 0x24865(%rip), %rsi     # 0x27247
movb $0x0, %al
callq 0x2260
movl $0x1, -0x4(%rbp)
jmp 0x300e
movq -0x10(%rbp), %rax
movq 0x2b598(%rip), %rcx     # 0x2df98
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x2170
movq %rax, %rdi
addq $0x9, %rdi
callq 0x22f0
movq %rax, -0x58(%rbp)
leaq -0x40(%rbp), %rdi
leaq 0x256af(%rip), %rsi     # 0x280de
callq 0x2090
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x2a4f
leaq -0x40(%rbp), %rdi
leaq 0x25537(%rip), %rsi     # 0x27f81
callq 0x2400
movq -0x10(%rbp), %rax
movq 0x2b53e(%rip), %rcx     # 0x2df98
movl (%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x84f0
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x2a86
jmp 0x2aa5
leaq 0x245d2(%rip), %rdi     # 0x2705f
leaq 0x247e9(%rip), %rsi     # 0x2727d
movl $0x2e, %edx
leaq 0x247e6(%rip), %rcx     # 0x27286
callq 0x21b0
movq -0x88(%rbp), %rdi
movl -0x24(%rbp), %esi
andl $0x4, %esi
callq 0x17af0
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq 0x28(%rax), %rax
cmpl $0x0, 0x10(%rax)
jle 0x2ad1
jmp 0x2af0
leaq 0x247cc(%rip), %rdi     # 0x272a4
leaq 0x2479e(%rip), %rsi     # 0x2727d
movl $0x30, %edx
leaq 0x2479b(%rip), %rcx     # 0x27286
callq 0x21b0
movq -0xb8(%rbp), %rax
movq 0x28(%rax), %rdi
xorl %esi, %esi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x14e70
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
xorl %esi, %esi
leaq 0x247a0(%rip), %rdx     # 0x272c0
callq 0x14b90
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jge 0x2b59
movq -0x98(%rbp), %rdi
leaq 0x24787(%rip), %rsi     # 0x272c3
callq 0xfbd0
movq -0x98(%rbp), %rdi
xorl %esi, %esi
leaq 0x2476f(%rip), %rdx     # 0x272c0
callq 0x14b90
movl %eax, -0x28(%rbp)
movq -0x98(%rbp), %rdi
leaq 0x24799(%rip), %rsi     # 0x27300
callq 0xfbd0
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0x247c5(%rip), %rsi     # 0x27340
movb $0x0, %al
callq 0x2410
movq -0x58(%rbp), %rdi
leaq 0x247ba(%rip), %rsi     # 0x27347
callq 0x23c0
movq %rax, -0xa0(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0xb8(%rbp), %rcx
movq 0x28(%rcx), %rcx
cmpl 0x10(%rcx), %eax
jge 0x2bf8
movq -0xb8(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0xa0(%rbp), %rsi
callq 0x21c0
movq -0xa0(%rbp), %rsi
movl $0xa, %edi
callq 0x21f0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2ba0
movq -0xa0(%rbp), %rdi
callq 0x2160
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0x24737(%rip), %rsi     # 0x2734a
movb $0x0, %al
callq 0x2410
movq -0x58(%rbp), %rdi
movq -0xb8(%rbp), %rax
movq 0x28(%rax), %rax
movl 0x10(%rax), %esi
shll %esi
movl $0x2, %edx
movl $0xd, %ecx
callq 0x1b380
movq %rax, -0xa8(%rbp)
movq -0xb8(%rbp), %rax
movq 0x28(%rax), %rax
movl 0x10(%rax), %eax
shll %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x2230
movq %rax, -0x70(%rbp)
movq -0xb8(%rbp), %rax
movq 0x28(%rax), %rax
movl 0x10(%rax), %eax
shll %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x2230
movq %rax, -0x68(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x2ccc
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0x246b6(%rip), %rsi     # 0x27351
movb $0x0, %al
callq 0x2410
movq -0x58(%rbp), %rdi
movq -0xb8(%rbp), %rax
movq 0x28(%rax), %rax
movl 0x10(%rax), %esi
shll %esi
movl $0x1, %edx
movl $0xd, %ecx
callq 0x1b380
movq %rax, -0xb0(%rbp)
movq -0xb8(%rbp), %rax
movq 0x28(%rax), %rax
movl 0x10(%rax), %eax
shll %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x2230
movq %rax, -0x78(%rbp)
leaq -0x48(%rbp), %rdi
leaq 0x2464f(%rip), %rsi     # 0x27347
callq 0x2090
cmpl $0x0, -0x20(%rbp)
jl 0x2d22
cmpl $0x9, -0x20(%rbp)
jg 0x2d22
leaq -0x48(%rbp), %rdi
addq $0x2, %rdi
movl -0x20(%rbp), %edx
leaq 0x25c63(%rip), %rsi     # 0x2897e
movb $0x0, %al
callq 0x2410
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0x24627(%rip), %rsi     # 0x27358
movb $0x0, %al
callq 0x2410
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x84f0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x10e10
movq 0x2b22e(%rip), %rax     # 0x2df98
movl (%rax), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x2f9b
movl -0x18(%rbp), %eax
movq 0x2b210(%rip), %rcx     # 0x2df98
movl (%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
je 0x2dca
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x84f0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
movl -0x24(%rbp), %esi
andl $0x4, %esi
callq 0x17af0
movq %rax, -0xb8(%rbp)
movb $0x0, %al
callq 0x100d0
movq %rax, -0xc8(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x17d20
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
je 0x2f69
movq -0x80(%rbp), %rax
movl %eax, -0xd0(%rbp)
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movl $0x1, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x186a0
movq -0x98(%rbp), %rdi
movq -0xc8(%rbp), %rsi
leaq 0x2452e(%rip), %rdx     # 0x2735f
movl $0x1, %ecx
leaq -0xd0(%rbp), %r8
callq 0x158b0
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
movq -0xc0(%rbp), %rcx
cmpl 0x2c(%rcx), %eax
jge 0x2efb
movq -0xc0(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0xcc(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x1, %eax
movb %al, %dl
movq -0x70(%rbp), %rax
movslq -0xcc(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0xc0(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0xcc(%rbp), %rcx
movzbl (%rax,%rcx), %eax
sarl %eax
andl $0x1, %eax
movb %al, %dl
movq -0x68(%rbp), %rax
movslq -0xcc(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0xc0(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0xcc(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %dl
movq -0x78(%rbp), %rax
movslq -0xcc(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x2e4c
movq -0xa8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1b990
cmpq $0x0, -0xb0(%rbp)
je 0x2f25
movq -0xb0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x1b990
movq -0x98(%rbp), %rdi
movq -0xc8(%rbp), %rsi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x15070
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0xc8(%rbp), %rdx
callq 0x14af0
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x2dd8
movq -0xc8(%rbp), %rdi
callq 0x10100
movq -0xb8(%rbp), %rdi
callq 0x17c80
movq -0x88(%rbp), %rdi
callq 0x87b0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x2d72
movq -0x90(%rbp), %rdi
callq 0x87b0
movq -0x78(%rbp), %rdi
callq 0x2040
movq -0x70(%rbp), %rdi
callq 0x2040
movq -0x68(%rbp), %rdi
callq 0x2040
cmpq $0x0, -0xb0(%rbp)
je 0x2fd8
movq -0xb0(%rbp), %rdi
callq 0x1b7f0
movq -0xa8(%rbp), %rdi
callq 0x1b7f0
movq -0x98(%rbp), %rdi
callq 0xfe40
movq -0x58(%rbp), %rdi
movl $0xe, %esi
callq 0x14df0
movq -0x58(%rbp), %rdi
callq 0x2040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw (%rax,%rax)