// File: /workspace/github_c_makefile_star2_O3/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 0x25d97(%rip), %rdx     # 0x42e89
callq 0x3210
xorl %ebp, %ebp
testl %eax, %eax
js 0x1d12d
movq 0x32edc(%rip), %r12     # 0x4ffe0
leaq 0x25d7e(%rip), %r15     # 0x42e89
cmpl $0x51, %eax
jne 0x1d11b
movq (%r12), %rdi
callq 0x34c0
movl %eax, %ebp
movl %r14d, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3210
testl %eax, %eax
jns 0x1d10b
movq 0x32e84(%rip), %r12     # 0x4ffb8
movl (%r12), %ecx
leal 0x2(%rcx), %eax
cmpl %r14d, %eax
jg 0x1d8b3
notl %ecx
addl %r14d, %ecx
movq %rcx, 0x28(%rsp)
movslq %ecx, %r14
movl $0x8, %esi
movq %r14, %rdi
callq 0x3610
movq %rax, 0x10(%rsp)
movl $0x8, %esi
movq %r14, %rdi
callq 0x3610
movq %rax, 0x40(%rsp)
movq %r14, 0x48(%rsp)
testl %r14d, %r14d
jle 0x1d23a
xorl %r14d, %r14d
leaq 0x28bc2(%rip), %r15     # 0x45d4f
movq $0x0, 0x20(%rsp)
movq 0x10(%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 0x20308
movq (%r13,%r14,8), %rcx
movq %rax, (%rcx)
movl (%r12), %eax
addl %r14d, %eax
cltq
movq 0x8(%rbx,%rax,8), %rdi
callq 0x2a134
movq 0x40(%rsp), %rcx
movq %rax, (%rcx,%r14,8)
movq (%r13,%r14,8), %rcx
movq (%rcx), %rdi
testq %rdi, %rdi
je 0x1d8bf
testq %rax, %rax
je 0x1d8bf
movl $0x14, %esi
callq 0x2153b
testq %r14, %r14
jne 0x1d22a
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rdi
callq 0x235fb
movq %rax, 0x20(%rsp)
incq %r14
cmpq %r14, 0x48(%rsp)
jne 0x1d19b
jmp 0x1d243
movq $0x0, 0x20(%rsp)
movq 0x20(%rsp), %rdi
callq 0x22802
movl $0x8, %esi
movq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x3610
movq %rax, %rbp
movslq (%r12), %rax
movq (%rbx,%rax,8), %rdi
leaq 0x23e8b(%rip), %rsi     # 0x410fc
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, 0x38(%rsp)
movl $0x8, %esi
movq %r14, %rdi
callq 0x3610
movq %rax, 0x58(%rsp)
movq 0x28(%rsp), %rcx
leal (,%rcx,8), %eax
cltq
movq %rax, 0x78(%rsp)
movl %ecx, %eax
movq %rax, 0x18(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %r12, 0x60(%rsp)
movq %rbp, 0x50(%rsp)
cmpl %eax, %ecx
jl 0x1d304
cmpl $0x0, 0x10(%r12)
jne 0x1d7f2
xorl %ebx, %ebx
cmpl %eax, %ecx
jl 0x1d351
cmpl $0x0, 0x10(%r12)
jne 0x1d414
movl $0x0, 0x8(%r12)
movq (%r12), %rsi
movq 0x18(%r12), %rdi
movl $0x4000, %edx           # imm = 0x4000
callq 0x36d0
movl %eax, 0xc(%r12)
xorl %ecx, %ecx
cmpl $0x3fff, %eax           # imm = 0x3FFF
jg 0x1d351
movl $0x1, 0x10(%r12)
testl %eax, %eax
je 0x1d414
movl %ecx, %r14d
cmpl %eax, %ecx
jge 0x1d375
movslq %ecx, %rdx
addq (%r12), %rdx
movl %ecx, %r14d
cmpb $0xa, (%rdx)
je 0x1d375
incl %r14d
incq %rdx
cmpl %r14d, %eax
jne 0x1d362
movl %eax, %r14d
movq %r15, %rdx
subq %rbx, %rdx
movl %r14d, %eax
subl %ecx, %eax
leal 0x1(%rax), %esi
movslq %esi, %rsi
cmpq %rsi, %rdx
jae 0x1d3dc
cltq
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, %r15
shrq $0x10, %r15
orq %rax, %r15
incq %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x3720
movq %rax, %r13
movl 0x8(%r12), %ecx
movl %r14d, %eax
subl %ecx, %eax
leaq (%rbx,%r13), %rdi
movslq %ecx, %rsi
addq (%r12), %rsi
movslq %eax, %rdx
callq 0x37d0
movslq 0x8(%r12), %rax
movslq %r14d, %rcx
subq %rax, %rcx
addq %rcx, %rbx
leal 0x1(%r14), %ecx
movl %ecx, 0x8(%r12)
movl 0xc(%r12), %eax
cmpl %eax, %r14d
jge 0x1d306
testq %r13, %r13
je 0x1d434
cmpq $0x2, %rbx
movq 0x38(%rsp), %rdi
jb 0x1d451
xorl %eax, %eax
cmpb $0xd, -0x1(%r13,%rbx)
sete %al
subq %rax, %rbx
jmp 0x1d451
movl $0x1, %r15d
movl $0x1, %edi
movl $0x1, %esi
callq 0x3610
movq %rax, %r13
movq 0x38(%rsp), %rdi
movb $0x0, (%r13,%rbx)
testl %ebx, %ebx
js 0x1d7eb
leaq 0x1(%r13), %r14
movzbl -0x1(%r14), %eax
cmpl $0x9, %eax
je 0x1d47a
testl %eax, %eax
je 0x1d7c9
incq %r14
jmp 0x1d463
movb $0x0, -0x1(%r14)
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x22ce3
movl %eax, 0xc(%rsp)
movb $0x9, -0x1(%r14)
cmpl $0x0, 0xc(%rsp)
js 0x1d7c9
callq 0x33c0
movq (%rax), %rdx
movq %r14, %r12
movq %r12, %rbp
movsbq (%r12), %rcx
incq %r12
testb $0x8, 0x1(%rdx,%rcx,2)
jne 0x1d4ab
cmpb $0x9, %cl
jne 0x1d7c9
movb $0x0, -0x1(%r12)
movq %r14, %rdi
movq %rax, %r14
callq 0x34c0
movl %eax, 0x30(%rsp)
movb $0x9, -0x1(%r12)
movq (%r14), %rax
movsbq 0x1(%rbp), %r14
incq %rbp
testb $0x8, 0x1(%rax,%r14,2)
jne 0x1d4e4
movzbl %r14b, %eax
cmpl $0x9, %eax
je 0x1d505
testl %eax, %eax
jne 0x1d7c9
movb $0x0, (%rbp)
movq %r12, %rdi
callq 0x34c0
movl %eax, %r12d
movb %r14b, (%rbp)
cmpl $0x0, 0x28(%rsp)
movq 0x10(%rsp), %rdx
movq 0x40(%rsp), %r14
jle 0x1d568
xorl %ebp, %ebp
movq (%rdx,%rbp,8), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x1d53d
callq 0x2ac9a
movq (%r14,%rbp,8), %rdi
movl 0xc(%rsp), %esi
movl 0x30(%rsp), %edx
movl %r12d, %ecx
callq 0x2a484
movq 0x10(%rsp), %rdx
movq (%rdx,%rbp,8), %rcx
movq %rax, 0x8(%rcx)
incq %rbp
cmpq %rbp, 0x18(%rsp)
jne 0x1d52b
movq 0x28(%rsp), %rdi
leaq 0x35d(%rip), %rsi       # 0x1d8d1
callq 0x2c059
movq %rax, %r14
movq %rax, %rdi
movl $0xfa00, %esi           # imm = 0xFA00
callq 0x2c124
movq 0x50(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
movq 0x78(%rsp), %rdx
callq 0x3080
movq %r14, 0x68(%rsp)
movq %r14, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x34(%rsp), %rdx
movq 0x38(%rsp), %r14
movq %r14, %rcx
movq 0x58(%rsp), %r8
callq 0x2c19c
testl %eax, %eax
jle 0x1d604
movl 0x34(%rsp), %eax
cmpl 0x30(%rsp), %eax
movq 0x18(%rsp), %rdx
jl 0x1d5f3
cmpl %r12d, %eax
jge 0x1d5f3
cmpl $0x0, 0x48(%rsp)
jle 0x1d5f3
xorl %eax, %eax
movslq (%r14,%rax,4), %rcx
addq %rcx, (%rbp,%rax,8)
incq %rax
cmpq %rax, %rdx
jne 0x1d5e2
movq 0x68(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x34(%rsp), %rdx
jmp 0x1d5b4
cmpl $0x0, 0x28(%rsp)
jle 0x1d7a3
xorl %r14d, %r14d
leaq 0x1(%rbx), %r12
cmpq %r15, %r12
jb 0x1d65d
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, %r15
shrq $0x10, %r15
orq %rcx, %r15
incq %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x3720
movq %rax, %r13
movabsq $-0x3333333333333333, %r8 # imm = 0xCCCCCCCCCCCCCCCD
movw $0x9, (%r13,%rbx)
movq (%rbp,%r14,8), %rsi
testq %rsi, %rsi
je 0x1d73b
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 0x1d689
testq %rsi, %rsi
jns 0x1d6c5
movb $0x2d, 0x80(%rsp,%rbp)
incq %rbp
leaq (%rbx,%rbp), %rax
addq $0x2, %rax
cmpq %r15, %rax
jb 0x1d714
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, %r15
shrq $0x10, %r15
orq %rax, %r15
incq %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x3720
movq %rax, %r13
movq 0x18(%rsp), %rcx
incq %rbp
movb 0x7e(%rsp,%rbp), %al
movb %al, (%r13,%r12)
incq %r12
decq %rbp
cmpq $0x1, %rbp
jg 0x1d71c
movq %r12, %rbx
movq 0x50(%rsp), %rbp
jmp 0x1d791
addq $0x2, %rbx
cmpq %r15, %rbx
jb 0x1d786
movq %rbx, %rax
shrq %rax
orq %rbx, %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, %r15
shrq $0x10, %r15
orq %rcx, %r15
incq %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x3720
movq %rax, %r13
movq 0x18(%rsp), %rcx
movb $0x30, (%r13,%r12)
movb $0x0, (%r13,%rbx)
incq %r14
cmpq %rcx, %r14
jne 0x1d612
movq %r13, %rdi
callq 0x3150
movq 0x68(%rsp), %rdi
callq 0x2c142
movq 0x60(%rsp), %r12
movl 0x8(%r12), %ecx
movl 0xc(%r12), %eax
jmp 0x1d2f4
movq 0x32818(%rip), %rax     # 0x4ffe8
movq (%rax), %rdi
leaq 0x2570c(%rip), %rsi     # 0x42ee6
movq %r13, %rdx
xorl %eax, %eax
callq 0x3750
movq 0x50(%rsp), %rbp
jmp 0x1d7b5
movq 0x60(%rsp), %r12
jmp 0x1d7f7
movq 0x38(%rsp), %rdi
callq 0x3350
movq 0x58(%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, 0x28(%rsp)
movq 0x10(%rsp), %r14
jle 0x1d87b
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x1d849
callq 0x2ac9a
movq 0x40(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x298a9
movq 0x10(%rsp), %r14
movq (%r14,%rbx,8), %rax
movq (%rax), %rdi
callq 0x21311
movq (%r14,%rbx,8), %rdi
callq 0x3350
incq %rbx
cmpq %rbx, 0x48(%rsp)
jne 0x1d837
movq 0x20(%rsp), %rdi
callq 0x23577
movq %r14, %rdi
callq 0x3350
movq 0x40(%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 0x46ec
movl $0x1, %eax
jmp 0x1d8a1
movl %r14d, %edi
movq %rbx, %rsi
callq 0x46bc
movl $0x2, %eax
jmp 0x1d8a1