// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bcftools/main.c
// Function: bcf_main_ldpair
int bcf_main_ldpair(int argc, char *argv[])
{
	bcf_t *fp;
	bcf_hdr_t *h;
	bcf1_t *b0, *b1;
	bcf_idx_t *idx;
	kstring_t str;
	void *str2id;
	gzFile fplist;
	kstream_t *ks;
	int dret, lineno = 0;
	if (argc < 3) {
		fprintf(stderr, "Usage: bcftools ldpair <in.bcf> <in.list>\n");
		return 1;
	}
	fplist = gzopen(argv[2], "rb");
	ks = ks_init(fplist);
	memset(&str, 0, sizeof(kstring_t));
	fp = bcf_open(argv[1], "rb");
	h = bcf_hdr_read(fp);
	str2id = bcf_build_refhash(h);
	idx = bcf_idx_load(argv[1]);
	if (idx == 0) {
		fprintf(stderr, "[%s] No bcf index is found. Abort!\n", __func__);
		return 1;
	}
	b0 = calloc(1, sizeof(bcf1_t));
	b1 = calloc(1, sizeof(bcf1_t));
	while (ks_getuntil(ks, '\n', &str, &dret) >= 0) {
		char *p, *q;
		int k;
		int tid0 = -1, tid1 = -1, pos0 = -1, pos1 = -1;
		++lineno;
		for (p = q = str.s, k = 0; *p; ++p) {
			if (*p == ' ' || *p == '\t') {
				*p = '\0';
				if (k == 0) tid0 = bcf_str2id(str2id, q);
				else if (k == 1) pos0 = atoi(q) - 1;
				else if (k == 2) tid1 = strcmp(q, "=")? bcf_str2id(str2id, q) : tid0;
				else if (k == 3) pos1 = atoi(q) - 1;
				q = p + 1;
				++k;
			}
		}
		if (k == 3) pos1 = atoi(q) - 1;
		if (tid0 >= 0 && tid1 >= 0 && pos0 >= 0 && pos1 >= 0) {
			uint64_t off;
			double r, f[4];
			off = bcf_idx_query(idx, tid0, pos0);
			bgzf_seek(fp->fp, off, SEEK_SET);
			while (bcf_read(fp, h, b0) >= 0 && b0->pos != pos0);
			off = bcf_idx_query(idx, tid1, pos1);
			bgzf_seek(fp->fp, off, SEEK_SET);
			while (bcf_read(fp, h, b1) >= 0 && b1->pos != pos1);
			r = bcf_pair_freq(b0, b1, f);
			r *= r;
			printf("%s\t%d\t%s\t%d\t%.4g\t%.4g\t%.4g\t%.4g\t%.4g\n", h->ns[tid0], pos0+1, h->ns[tid1], pos1+1,
				r, f[0], f[1], f[2], f[3]);
		} //else fprintf(stderr, "[%s] Parse error at line %d.\n", __func__, lineno);
	}
	bcf_destroy(b0); bcf_destroy(b1);
	bcf_idx_destroy(idx);
	bcf_str2id_destroy(str2id);
	bcf_hdr_destroy(h);
	bcf_close(fp);
	free(str.s);
	ks_destroy(ks);
	gzclose(fplist);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
cmpl $0x2, %edi
jle 0x703d
movq %rsi, %rbx
movq 0x10(%rsi), %rdi
leaq 0x13ea9(%rip), %r15     # 0x1aa83
movq %r15, %rsi
callq 0x22c0
movq %rax, %r12
movl $0x1, %edi
movl $0x20, %esi
callq 0x2260
movq %rax, %r14
movq %r12, 0x40(%rsp)
movq %r12, 0x18(%rax)
movl $0x10000, %edi          # imm = 0x10000
callq 0x2360
movq %rax, (%r14)
movq 0x8(%rbx), %rdi
movq %r15, %rsi
callq 0xb3a4
movq %rax, %r13
movq %rax, %rdi
callq 0xb51c
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0xe2ac
movq %rax, 0x38(%rsp)
movq 0x8(%rbx), %rdi
callq 0x17486
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x7044
movl $0x1, %edi
movl $0x68, %esi
callq 0x2260
movq %rax, 0x8(%rsp)
movl $0x1, %edi
movl $0x68, %esi
callq 0x2260
movq %rax, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x20(%rsp)
movq %r13, 0x48(%rsp)
movl 0x8(%r14), %eax
cmpl 0xc(%r14), %eax
jl 0x6ca9
cmpl $0x0, 0x10(%r14)
je 0x6ca9
xorl %eax, %eax
movq 0x20(%rsp), %rbp
jmp 0x6de8
xorl %ebx, %ebx
movq 0x20(%rsp), %rbp
movl 0x8(%r14), %eax
cmpl 0xc(%r14), %eax
jl 0x6cf9
cmpl $0x0, 0x10(%r14)
jne 0x6dbd
movl $0x0, 0x8(%r14)
movq (%r14), %rsi
movq 0x18(%r14), %rdi
movl $0x10000, %edx          # imm = 0x10000
callq 0x2280
movl %eax, 0xc(%r14)
cmpl $0xffff, %eax           # imm = 0xFFFF
jg 0x6cf1
movl $0x1, 0x10(%r14)
testl %eax, %eax
je 0x6dbd
movl 0x8(%r14), %eax
movl 0xc(%r14), %ecx
movl %eax, %r15d
cmpl %ecx, %eax
jge 0x6d24
movslq %eax, %rdx
addq (%r14), %rdx
movl %eax, %r15d
cmpb $0xa, (%rdx)
je 0x6d24
incl %r15d
incq %rdx
cmpl %r15d, %ecx
jne 0x6d11
movl %ecx, %r15d
movq 0x30(%rsp), %rdx
subq %rbx, %rdx
movl %r15d, %ecx
subl %eax, %ecx
leal 0x1(%rcx), %eax
cltq
cmpq %rax, %rdx
jae 0x6d85
movslq %ecx, %rax
addq %rbx, %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rbp, %rdi
movq %rsi, 0x30(%rsp)
callq 0x23d0
movq %rax, %rbp
leaq (%rbx,%rbp), %rdi
movslq 0x8(%r14), %rax
movq (%r14), %rsi
addq %rax, %rsi
movslq %r15d, %r12
movq %r12, %rdx
subq %rax, %rdx
callq 0x22e0
movslq 0x8(%r14), %rax
subq %rax, %r12
addq %r12, %rbx
leal 0x1(%r15), %eax
movl %eax, 0x8(%r14)
cmpl 0xc(%r14), %r15d
jge 0x6cb0
testq %rbp, %rbp
jne 0x6dde
movl $0x1, %eax
movq %rax, 0x30(%rsp)
movl $0x1, %edi
movl $0x1, %esi
callq 0x2260
movq %rax, %rbp
movb $0x0, (%rbp,%rbx)
testl %ebx, %ebx
setns %al
testb %al, %al
je 0x6fd0
leaq 0x1(%rbp), %r12
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movq %rbp, 0x20(%rsp)
movq %rbp, %r13
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movzbl -0x1(%r12), %eax
cmpl $0x9, %eax
je 0x6e35
cmpl $0x20, %eax
je 0x6e35
testl %eax, %eax
jne 0x6eb1
jmp 0x6eb9
movb $0x0, -0x1(%r12)
cmpl $0x3, %r15d
ja 0x6eab
movl %r15d, %eax
leaq 0x13bfd(%rip), %rcx     # 0x1aa48
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0xe599
movl %eax, (%rsp)
jmp 0x6eab
movq %r13, %rdi
leaq 0x12333(%rip), %rsi     # 0x191a3
callq 0x2270
testl %eax, %eax
je 0x6ea4
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0xe599
jmp 0x6ea7
movq %r13, %rdi
callq 0x2470
movl %eax, %ebp
decl %ebp
jmp 0x6eab
movq %r13, %rdi
callq 0x2470
movl %eax, %ebx
decl %ebx
jmp 0x6eab
movl (%rsp), %eax
movl %eax, 0x4(%rsp)
incl %r15d
movq %r12, %r13
incq %r12
jmp 0x6e18
cmpl $0x3, %r15d
jne 0x6ecb
movq %r13, %rdi
callq 0x2470
movl %eax, %ebp
decl %ebp
cmpl $0x0, (%rsp)
movq 0x48(%rsp), %r13
movl 0x4(%rsp), %r15d
js 0x6c8c
testl %r15d, %r15d
js 0x6c8c
testl %ebx, %ebx
js 0x6c8c
testl %ebp, %ebp
js 0x6c8c
movq 0x18(%rsp), %rdi
movl (%rsp), %esi
movl %ebx, %edx
callq 0x17835
movq 0x10(%r13), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x8945
movq 0x8(%rsp), %rdx
movq %r13, %rdi
movq 0x10(%rsp), %rsi
callq 0xbb50
testl %eax, %eax
movq 0x8(%rsp), %rdx
js 0x6f35
cmpl %ebx, 0x4(%rdx)
jne 0x6f1a
movq 0x18(%rsp), %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x17835
movq 0x10(%r13), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x8945
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0xbb50
movq 0x28(%rsp), %rsi
testl %eax, %eax
js 0x6f72
cmpl %ebp, 0x4(%rsi)
jne 0x6f52
movq 0x8(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x1586e
mulsd %xmm0, %xmm0
movq 0x10(%rsp), %rax
movq 0x30(%rax), %rax
movl (%rsp), %ecx
movq (%rax,%rcx,8), %rsi
incl %ebx
movl %r15d, %ecx
movq (%rax,%rcx,8), %rcx
incl %ebp
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0x60(%rsp), %xmm3
movsd 0x68(%rsp), %xmm4
leaq 0x13afb(%rip), %rdi     # 0x1aaba
movl %ebx, %edx
movl %ebp, %r8d
movb $0x5, %al
callq 0x21b0
jmp 0x6c8c
movq 0x8(%rsp), %rdi
callq 0xbc91
movq 0x28(%rsp), %rdi
callq 0xbc91
movq 0x18(%rsp), %rdi
callq 0x172b4
movq 0x38(%rsp), %rdi
callq 0xe4f6
movq 0x10(%rsp), %rdi
callq 0xb73e
movq %r13, %rdi
callq 0xb432
movq %rbp, %rdi
callq 0x2060
movq (%r14), %rdi
callq 0x2060
movq %r14, %rdi
callq 0x2060
movq 0x40(%rsp), %rdi
callq 0x2050
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2719
jmp 0x7049
callq 0x26fa
movl $0x1, %eax
jmp 0x702e