// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bedcov.c
// Function: main_bedcov
int main_bedcov(int argc, char *argv[])
{
	extern void bam_init_header_hash(bam_header_t*);
	gzFile fp;
	kstring_t str;
	kstream_t *ks;
	bam_index_t **idx;
	bam_header_t *h = 0;
	aux_t **aux;
	int *n_plp, dret, i, n, c, min_mapQ = 0;
	int64_t *cnt;
	const bam_pileup1_t **plp;

	while ((c = getopt(argc, argv, "Q:")) >= 0) {
		switch (c) {
		case 'Q': min_mapQ = atoi(optarg); break;
		}
	}
	if (optind + 2 > argc) {
		fprintf(stderr, "Usage: samtools bedcov <in.bed> <in1.bam> [...]\n");
		return 1;
	}
	memset(&str, 0, sizeof(kstring_t));
	n = argc - optind - 1;
	aux = calloc(n, sizeof(void*));
	idx = calloc(n, sizeof(void*));
	for (i = 0; i < n; ++i) {
		aux[i] = calloc(1, sizeof(aux_t));
		aux[i]->min_mapQ = min_mapQ;
		aux[i]->fp = bam_open(argv[i+optind+1], "r");
		idx[i] = bam_index_load(argv[i+optind+1]);
		if (aux[i]->fp == 0 || idx[i] == 0) {
			fprintf(stderr, "ERROR: fail to open index BAM file '%s'\n", argv[i+optind+1]);
			return 2;
		}
		bgzf_set_cache_size(aux[i]->fp, 20);
		if (i == 0) h = bam_header_read(aux[0]->fp);
	}
	bam_init_header_hash(h);
	cnt = calloc(n, 8);

	fp = gzopen(argv[optind], "rb");
	ks = ks_init(fp);
	n_plp = calloc(n, sizeof(int));
	plp = calloc(n, sizeof(void*));
	while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0) {
		char *p, *q;
		int tid, beg, end, pos;
		bam_mplp_t mplp;

		for (p = q = str.s; *p && *p != '\t'; ++p);
		if (*p != '\t') goto bed_error;
		*p = 0; tid = bam_get_tid(h, q); *p = '\t';
		if (tid < 0) goto bed_error;
		for (q = p = p + 1; isdigit(*p); ++p);
		if (*p != '\t') goto bed_error;
		*p = 0; beg = atoi(q); *p = '\t';
		for (q = p = p + 1; isdigit(*p); ++p);
		if (*p == '\t' || *p == 0) {
			int c = *p;
			*p = 0; end = atoi(q); *p = c;
		} else goto bed_error;

		for (i = 0; i < n; ++i) {
			if (aux[i]->iter) bam_iter_destroy(aux[i]->iter);
			aux[i]->iter = bam_iter_query(idx[i], tid, beg, end);
		}
		mplp = bam_mplp_init(n, read_bam, (void**)aux);
		bam_mplp_set_maxcnt(mplp, 64000);
		memset(cnt, 0, 8 * n);
		while (bam_mplp_auto(mplp, &tid, &pos, n_plp, plp) > 0)
			if (pos >= beg && pos < end)
				for (i = 0; i < n; ++i) cnt[i] += n_plp[i];
		for (i = 0; i < n; ++i) {
			kputc('\t', &str);
			kputl(cnt[i], &str);
		}
		puts(str.s);
		bam_mplp_destroy(mplp);
		continue;

bed_error:
		fprintf(stderr, "Errors in BED line '%s'\n", str.s);
	}
	free(n_plp); free(plp);
	ks_destroy(ks);
	gzclose(fp);

	free(cnt);
	for (i = 0; i < n; ++i) {
		if (aux[i]->iter) bam_iter_destroy(aux[i]->iter);
		bam_index_destroy(idx[i]);
		bam_close(aux[i]->fp);
		free(aux[i]);
	}
	bam_header_destroy(h);
	free(aux); free(idx);
	free(str.s);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %rbx
movl %edi, %r14d
leaq 0x252e3(%rip), %rdx     # 0x41e39
callq 0x3210
xorl %ebp, %ebp
testl %eax, %eax
js 0x1cb91
movq 0x32478(%rip), %r12     # 0x4efe0
leaq 0x252ca(%rip), %r15     # 0x41e39
cmpl $0x51, %eax
jne 0x1cb7f
movq (%r12), %rdi
callq 0x34c0
movl %eax, %ebp
movl %r14d, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3210
testl %eax, %eax
jns 0x1cb6f
movq 0x32420(%rip), %r12     # 0x4efb8
movl (%r12), %ecx
leal 0x2(%rcx), %eax
cmpl %r14d, %eax
jg 0x1d35e
notl %ecx
addl %r14d, %ecx
movq %rcx, 0x20(%rsp)
movslq %ecx, %r14
movl $0x8, %esi
movq %r14, %rdi
callq 0x3610
movq %rax, 0x28(%rsp)
movl $0x8, %esi
movq %r14, %rdi
callq 0x3610
movq %rax, 0x48(%rsp)
movq %r14, 0x40(%rsp)
testl %r14d, %r14d
jle 0x1cc9e
xorl %r14d, %r14d
leaq 0x2807e(%rip), %r15     # 0x44c6f
movq $0x0, 0x18(%rsp)
movq 0x28(%rsp), %r13
movl $0x1, %edi
movl $0x18, %esi
callq 0x3610
movq %rax, (%r13,%r14,8)
movl %ebp, 0x10(%rax)
movl (%r12), %eax
addl %r14d, %eax
cltq
movq 0x8(%rbx,%rax,8), %rdi
movq %r15, %rsi
callq 0x1fdb8
movq (%r13,%r14,8), %rcx
movq %rax, (%rcx)
movl (%r12), %eax
addl %r14d, %eax
cltq
movq 0x8(%rbx,%rax,8), %rdi
callq 0x29a74
movq 0x48(%rsp), %rcx
movq %rax, (%rcx,%r14,8)
movq (%r13,%r14,8), %rcx
movq (%rcx), %rdi
testq %rdi, %rdi
je 0x1d36a
testq %rax, %rax
je 0x1d36a
movl $0x14, %esi
callq 0x20cd0
testq %r14, %r14
jne 0x1cc8e
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rdi
callq 0x22efa
movq %rax, 0x18(%rsp)
incq %r14
cmpq %r14, 0x40(%rsp)
jne 0x1cbff
jmp 0x1cca7
movq $0x0, 0x18(%rsp)
movq 0x18(%rsp), %rdi
callq 0x2210c
movl $0x8, %esi
movq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x3610
movq %rax, %rbp
movslq (%r12), %rax
movq (%rbx,%rax,8), %rdi
leaq 0x233e7(%rip), %rsi     # 0x400bc
callq 0x3420
movq %rax, %rbx
movl $0x1, %edi
movl $0x20, %esi
callq 0x3610
movq %rax, %r12
movq %rbx, 0x70(%rsp)
movq %rbx, 0x18(%rax)
movl $0x4000, %edi           # imm = 0x4000
callq 0x3240
movq %rax, (%r12)
movl $0x4, %esi
movq %r14, %rdi
callq 0x3610
movq %rax, %rbx
movl $0x8, %esi
movq %r14, %rdi
callq 0x3610
movq %rax, 0x60(%rsp)
movq 0x20(%rsp), %rcx
leal (,%rcx,8), %eax
cltq
movq %rax, 0x78(%rsp)
movl %ecx, %r15d
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
movq %r12, 0x50(%rsp)
movq %rbp, 0x58(%rsp)
movl 0x8(%r12), %eax
cmpl 0xc(%r12), %eax
movq 0x28(%rsp), %r14
jl 0x1cd77
cmpl $0x0, 0x10(%r12)
je 0x1cd77
xorl %r8d, %r8d
xorl %eax, %eax
jmp 0x1cf02
xorl %r8d, %r8d
movl 0x8(%r12), %eax
cmpl 0xc(%r12), %eax
movq %r8, 0x30(%rsp)
jl 0x1cdd5
cmpl $0x0, 0x10(%r12)
jne 0x1ceb9
movl $0x0, 0x8(%r12)
movq (%r12), %rsi
movq 0x18(%r12), %rdi
movl $0x4000, %edx           # imm = 0x4000
callq 0x36d0
movl %eax, 0xc(%r12)
cmpl $0x3fff, %eax           # imm = 0x3FFF
jg 0x1cdc8
movl $0x1, 0x10(%r12)
testl %eax, %eax
movq 0x30(%rsp), %r8
je 0x1ceb9
movl 0x8(%r12), %eax
movl 0xc(%r12), %ecx
movl %eax, %r14d
cmpl %ecx, %eax
jge 0x1ce03
movslq %eax, %rdx
addq (%r12), %rdx
movl %eax, %r14d
cmpb $0xa, (%rdx)
je 0x1ce03
incl %r14d
incq %rdx
cmpl %r14d, %ecx
jne 0x1cdf0
movl %ecx, %r14d
movq 0x8(%rsp), %rdx
subq %r8, %rdx
movl %r14d, %ecx
subl %eax, %ecx
leal 0x1(%rcx), %eax
cltq
cmpq %rax, %rdx
jae 0x1ce69
movslq %ecx, %rax
addq %r8, %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 %r13, %rdi
movq %rsi, 0x8(%rsp)
callq 0x3720
movq 0x30(%rsp), %r8
movq %rax, %r13
leaq (%r8,%r13), %rdi
movslq 0x8(%r12), %rax
movq (%r12), %rsi
addq %rax, %rsi
movslq %r14d, %r12
movq %r12, %rdx
subq %rax, %rdx
callq 0x37d0
movq 0x30(%rsp), %r8
movq 0x50(%rsp), %rax
movslq 0x8(%rax), %rax
subq %rax, %r12
addq %r12, %r8
movq 0x50(%rsp), %r12
leal 0x1(%r14), %eax
movl %eax, 0x8(%r12)
cmpl 0xc(%r12), %r14d
movq 0x28(%rsp), %r14
jge 0x1cd7a
testq %r13, %r13
je 0x1ced4
cmpq $0x2, %r8
jb 0x1cef6
xorl %eax, %eax
cmpb $0xd, -0x1(%r13,%r8)
sete %al
subq %rax, %r8
jmp 0x1cef6
movl $0x1, %eax
movq %rax, 0x8(%rsp)
movl $0x1, %edi
movl $0x1, %esi
movq %r8, %r13
callq 0x3610
movq %r13, %r8
movq %rax, %r13
movb $0x0, (%r13,%r8)
testl %r8d, %r8d
setns %al
testb %al, %al
je 0x1d2a9
leaq 0x1(%r13), %r14
movzbl -0x1(%r14), %eax
cmpl $0x9, %eax
je 0x1cf25
testl %eax, %eax
je 0x1d27f
incq %r14
jmp 0x1cf0e
movb $0x0, -0x1(%r14)
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq %r8, 0x30(%rsp)
callq 0x2235b
movl %eax, 0x14(%rsp)
movb $0x9, -0x1(%r14)
cmpl $0x0, 0x14(%rsp)
js 0x1d27f
callq 0x33c0
movq (%rax), %rdx
movq %r14, %r12
movq %r12, %rbp
movsbq (%r12), %rcx
incq %r12
testb $0x8, 0x1(%rdx,%rcx,2)
jne 0x1cf5b
cmpb $0x9, %cl
jne 0x1d27f
movb $0x0, -0x1(%r12)
movq %r14, %rdi
movq %rax, %r14
callq 0x34c0
movl %eax, 0x38(%rsp)
movb $0x9, -0x1(%r12)
movq (%r14), %rax
movsbq 0x1(%rbp), %r14
incq %rbp
testb $0x8, 0x1(%rax,%r14,2)
jne 0x1cf94
movzbl %r14b, %eax
cmpl $0x9, %eax
je 0x1cfb5
testl %eax, %eax
jne 0x1d27f
movb $0x0, (%rbp)
movq %r12, %rdi
callq 0x34c0
movl %eax, %r12d
movb %r14b, (%rbp)
cmpl $0x0, 0x20(%rsp)
movq 0x28(%rsp), %r14
jle 0x1d011
xorl %ebp, %ebp
movq (%r14,%rbp,8), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x1cfe8
callq 0x2a2f1
movq 0x48(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movl 0x14(%rsp), %esi
movl 0x38(%rsp), %edx
movl %r12d, %ecx
callq 0x29dc7
movq (%r14,%rbp,8), %rcx
movq %rax, 0x8(%rcx)
incq %rbp
cmpq %rbp, %r15
jne 0x1cfd6
movq 0x20(%rsp), %rdi
leaq 0x35f(%rip), %rsi       # 0x1d37c
movq %r14, %rdx
callq 0x2b6af
movq %rax, %r14
movq %rax, %rdi
movl $0xfa00, %esi           # imm = 0xFA00
callq 0x2b77a
movq 0x58(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
movq 0x78(%rsp), %rdx
callq 0x3080
movq %r14, 0x68(%rsp)
movq %r14, %rdi
leaq 0x14(%rsp), %rsi
leaq 0x3c(%rsp), %rdx
movq %rbx, %rcx
movq 0x60(%rsp), %r14
movq %r14, %r8
callq 0x2b7f2
testl %eax, %eax
jle 0x1d0ac
movl 0x3c(%rsp), %eax
cmpl 0x38(%rsp), %eax
jl 0x1d098
cmpl %r12d, %eax
jge 0x1d098
cmpl $0x0, 0x40(%rsp)
jle 0x1d098
xorl %eax, %eax
movslq (%rbx,%rax,4), %rcx
addq %rcx, (%rbp,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x1d087
movq 0x68(%rsp), %rdi
leaq 0x14(%rsp), %rsi
leaq 0x3c(%rsp), %rdx
movq %rbx, %rcx
jmp 0x1d063
cmpl $0x0, 0x20(%rsp)
jle 0x1d263
xorl %r14d, %r14d
movq 0x30(%rsp), %r9
leaq 0x1(%r9), %r12
cmpq 0x8(%rsp), %r12
jb 0x1d114
movq %r12, %rax
shrq %rax
orq %r12, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %r13, %rdi
movq %rsi, 0x8(%rsp)
movq %r9, %r13
callq 0x3720
movq %r13, %r9
movq %rax, %r13
movabsq $-0x3333333333333333, %r8 # imm = 0xCCCCCCCCCCCCCCCD
movw $0x9, (%r13,%r9)
movq (%rbp,%r14,8), %rsi
testq %rsi, %rsi
je 0x1d1f5
movq %rsi, %rcx
negq %rcx
cmovsq %rsi, %rcx
xorl %ebp, %ebp
movq %rcx, %rax
mulq %r8
shrq $0x3, %rdx
leal (%rdx,%rdx), %eax
leal (%rax,%rax,4), %eax
movl %ecx, %edi
subl %eax, %edi
orb $0x30, %dil
movb %dil, 0x80(%rsp,%rbp)
incq %rbp
cmpq $0x9, %rcx
movq %rdx, %rcx
ja 0x1d140
testq %rsi, %rsi
jns 0x1d17c
movb $0x2d, 0x80(%rsp,%rbp)
incq %rbp
leaq (%r9,%rbp), %rax
addq $0x2, %rax
cmpq 0x8(%rsp), %rax
jb 0x1d1cf
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 %r13, %rdi
movq %rsi, 0x8(%rsp)
callq 0x3720
movq %rax, %r13
incq %rbp
movb 0x7e(%rsp,%rbp), %al
movb %al, (%r13,%r12)
incq %r12
decq %rbp
cmpq $0x1, %rbp
jg 0x1d1d2
leaq (%r12,%r13), %rax
movq %r12, %r9
movq 0x58(%rsp), %rbp
jmp 0x1d254
addq $0x2, %r9
cmpq 0x8(%rsp), %r9
jb 0x1d24a
movq %r9, %rax
shrq %rax
orq %r9, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %r13, %rdi
movq %rsi, 0x8(%rsp)
movq %r9, %r13
callq 0x3720
movq %r13, %r9
movq %rax, %r13
movb $0x30, (%r13,%r12)
leaq (%r9,%r13), %rax
movb $0x0, (%rax)
incq %r14
cmpq %r15, %r14
jne 0x1d0bf
movq %r13, %rdi
callq 0x3150
movq 0x68(%rsp), %rdi
callq 0x2b798
movq 0x50(%rsp), %r12
jmp 0x1cd54
movq 0x31d62(%rip), %rax     # 0x4efe8
movq (%rax), %rdi
leaq 0x24c06(%rip), %rsi     # 0x41e96
movq %r13, %rdx
xorl %eax, %eax
callq 0x3750
movq 0x58(%rsp), %rbp
movq 0x50(%rsp), %r12
jmp 0x1cd54
movq %rbx, %rdi
callq 0x3350
movq 0x60(%rsp), %rdi
callq 0x3350
movq (%r12), %rdi
callq 0x3350
movq %r12, %rdi
callq 0x3350
movq 0x70(%rsp), %rdi
callq 0x31c0
movq %rbp, %rdi
callq 0x3350
cmpl $0x0, 0x20(%rsp)
jle 0x1d326
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x1d2f9
callq 0x2a2f1
movq 0x48(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x291f9
movq (%r14,%rbx,8), %rax
movq (%rax), %rdi
callq 0x20aae
movq (%r14,%rbx,8), %rdi
callq 0x3350
incq %rbx
cmpq %rbx, 0x40(%rsp)
jne 0x1d2e7
movq 0x18(%rsp), %rdi
callq 0x22e78
movq %r14, %rdi
callq 0x3350
movq 0x48(%rsp), %rdi
callq 0x3350
movq %r13, %rdi
callq 0x3350
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x46e2
movl $0x1, %eax
jmp 0x1d34c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x46b2
movl $0x2, %eax
jmp 0x1d34c