// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam2depth.c
// Function: main_depth
int main_depth(int argc, char *argv[])
#endif
{
	int i, n, tid, beg, end, pos, *n_plp, baseQ = 0, mapQ = 0, min_len = 0, nfiles;
	const bam_pileup1_t **plp;
	char *reg = 0; // specified region
	void *bed = 0; // BED data structure
    char *file_list = NULL, **fn = NULL;
	bam_header_t *h = 0; // BAM header of the 1st input
	aux_t **data;
	bam_mplp_t mplp;

	// parse the command line
	while ((n = getopt(argc, argv, "r:b:q:Q:l:f:")) >= 0) {
		switch (n) {
			case 'l': min_len = atoi(optarg); break; // minimum query length
			case 'r': reg = strdup(optarg); break;   // parsing a region requires a BAM header
			case 'b': bed = bed_read(optarg); break; // BED or position list file can be parsed now
			case 'q': baseQ = atoi(optarg); break;   // base quality threshold
			case 'Q': mapQ = atoi(optarg); break;    // mapping quality threshold
			case 'f': file_list = optarg; break;
		}
	}
	if (optind == argc && !file_list) {
        fprintf(stderr, "\n");
        fprintf(stderr, "Usage: samtools depth [options] in1.bam [in2.bam [...]]\n");
        fprintf(stderr, "Options:\n");
        fprintf(stderr, "   -b <bed>            list of positions or regions\n");
        fprintf(stderr, "   -f <list>           list of input BAM filenames, one per line [null]\n");
        fprintf(stderr, "   -l <int>            minQLen\n");
        fprintf(stderr, "   -q <int>            base quality threshold\n");
        fprintf(stderr, "   -Q <int>            mapping quality threshold\n");
        fprintf(stderr, "   -r <chr:from-to>    region\n");
        fprintf(stderr, "\n");
		return 1;
	}

	// initialize the auxiliary data structures
    if (file_list) 
    {
        if ( read_file_list(file_list,&nfiles,&fn) ) return 1;
        n = nfiles;
        argv = fn;
        optind = 0;
    }
    else
        n = argc - optind; // the number of BAMs on the command line
	data = calloc(n, sizeof(void*)); // data[i] for the i-th input
	beg = 0; end = 1<<30; tid = -1;  // set the default region
	for (i = 0; i < n; ++i) {
		bam_header_t *htmp;
		data[i] = calloc(1, sizeof(aux_t));
		data[i]->fp = bam_open(argv[optind+i], "r"); // open BAM
		data[i]->min_mapQ = mapQ;                    // set the mapQ filter
		data[i]->min_len  = min_len;                 // set the qlen filter
		htmp = bam_header_read(data[i]->fp);         // read the BAM header
		if (i == 0) {
			h = htmp; // keep the header of the 1st BAM
			if (reg) bam_parse_region(h, reg, &tid, &beg, &end); // also parse the region
		} else bam_header_destroy(htmp); // if not the 1st BAM, trash the header
		if (tid >= 0) { // if a region is specified and parsed successfully
			bam_index_t *idx = bam_index_load(argv[optind+i]);  // load the index
			data[i]->iter = bam_iter_query(idx, tid, beg, end); // set the iterator
			bam_index_destroy(idx); // the index is not needed any more; phase out of the memory
		}
	}

	// the core multi-pileup loop
	mplp = bam_mplp_init(n, read_bam, (void**)data); // initialization
	n_plp = calloc(n, sizeof(int)); // n_plp[i] is the number of covering reads from the i-th BAM
	plp = calloc(n, sizeof(void*)); // plp[i] points to the array of covering reads (internal in mplp)
	while (bam_mplp_auto(mplp, &tid, &pos, n_plp, plp) > 0) { // come to the next covered position
		if (pos < beg || pos >= end) continue; // out of range; skip
		if (bed && bed_overlap(bed, h->target_name[tid], pos, pos + 1) == 0) continue; // not in BED; skip
		fputs(h->target_name[tid], stdout); printf("\t%d", pos+1); // a customized printf() would be faster
		for (i = 0; i < n; ++i) { // base level filters have to go here
			int j, m = 0;
			for (j = 0; j < n_plp[i]; ++j) {
				const bam_pileup1_t *p = plp[i] + j; // DON'T modfity plp[][] unless you really know
				if (p->is_del || p->is_refskip) ++m; // having dels or refskips at tid:pos
				else if (bam1_qual(p->b)[p->qpos] < baseQ) ++m; // low base quality
			}
			printf("\t%d", n_plp[i] - m); // this the depth to output
		}
		putchar('\n');
	}
	free(n_plp); free(plp);
	bam_mplp_destroy(mplp);

	bam_header_destroy(h);
	for (i = 0; i < n; ++i) {
		bam_close(data[i]->fp);
		if (data[i]->iter) bam_iter_destroy(data[i]->iter);
		free(data[i]);
	}
	free(data); free(reg);
	if (bed) bed_destroy(bed);
    if ( file_list )
    {
        for (i=0; i<n; i++) free(fn[i]);
        free(fn);
    }
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r12
movl %edi, %r15d
movq $0x0, 0x50(%rsp)
leaq 0x26398(%rip), %rdx     # 0x41390
callq 0x3210
testl %eax, %eax
js 0x1b0bd
xorl %r13d, %r13d
movq 0x33fd1(%rip), %r14     # 0x4efe0
leaq 0x2637a(%rip), %rbx     # 0x41390
xorl %ebp, %ebp
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movl $0x0, 0x10(%rsp)
movq $0x0, 0x28(%rsp)
cmpl $0x6b, %eax
jg 0x1b054
cmpl $0x51, %eax
je 0x1b072
cmpl $0x62, %eax
je 0x1b08f
cmpl $0x66, %eax
jne 0x1b0a9
movq (%r14), %rbp
jmp 0x1b0a9
cmpl $0x72, %eax
je 0x1b080
cmpl $0x71, %eax
je 0x1b09e
cmpl $0x6c, %eax
jne 0x1b0a9
movq (%r14), %rdi
callq 0x34c0
movq %rax, 0x28(%rsp)
jmp 0x1b0a9
movq (%r14), %rdi
callq 0x34c0
movl %eax, 0x10(%rsp)
jmp 0x1b0a9
movq (%r14), %rdi
callq 0x3570
movq %rax, 0x38(%rsp)
jmp 0x1b0a9
movq (%r14), %rdi
callq 0x306fa
movq %rax, 0x40(%rsp)
jmp 0x1b0a9
movq (%r14), %rdi
callq 0x34c0
movl %eax, %r13d
movl %r15d, %edi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3210
testl %eax, %eax
jns 0x1b03b
jmp 0x1b0e5
movq $0x0, 0x28(%rsp)
movl $0x0, 0x10(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x33ecc(%rip), %rax     # 0x4efb8
subl (%rax), %r15d
setne %al
testq %rbp, %rbp
setne %cl
orb %al, %cl
je 0x1b4f0
testq %rbp, %rbp
je 0x1b143
leaq 0x5c(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq %rbp, %rdi
callq 0x7400
movl $0x1, %r14d
testl %eax, %eax
jne 0x1b4de
movq %rbp, 0x60(%rsp)
movl 0x5c(%rsp), %r15d
movq 0x50(%rsp), %r12
movq 0x33e7d(%rip), %rax     # 0x4efb8
movl $0x0, (%rax)
jmp 0x1b148
movq %rbp, 0x60(%rsp)
movslq %r15d, %rbp
movl $0x8, %esi
movq %rbp, %rdi
callq 0x3610
movq %rax, 0x48(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x40000000, 0x18(%rsp) # imm = 0x40000000
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
testl %ebp, %ebp
movq 0x40(%rsp), %r14
movl %r15d, 0x34(%rsp)
jle 0x1b278
xorl %ebx, %ebx
movq $0x0, 0x20(%rsp)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3610
movq 0x48(%rsp), %r15
movq %rax, (%r15,%rbx,8)
movq 0x33e07(%rip), %rax     # 0x4efb8
movslq (%rax), %rax
addq %rbx, %rax
movq (%r12,%rax,8), %rdi
leaq 0x29aad(%rip), %rsi     # 0x44c6f
callq 0x1fdb8
movq (%r15,%rbx,8), %rcx
movq %rax, (%rcx)
movq (%r15,%rbx,8), %rax
movl 0x10(%rsp), %ecx
movl %ecx, 0x10(%rax)
movq 0x28(%rsp), %rcx
movl %ecx, 0x14(%rax)
movq (%rax), %rdi
callq 0x22efa
movq %rax, %r15
testq %rbx, %rbx
je 0x1b1fb
movq %r15, %rdi
callq 0x22e78
jmp 0x1b221
movq 0x38(%rsp), %rsi
testq %rsi, %rsi
je 0x1b21c
movq %r15, %rdi
leaq 0xc(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x18(%rsp), %r8
callq 0x22482
movq %r15, 0x20(%rsp)
cmpl $0x0, 0xc(%rsp)
js 0x1b26a
movq 0x33d89(%rip), %rax     # 0x4efb8
movslq (%rax), %rax
addq %rbx, %rax
movq (%r12,%rax,8), %rdi
callq 0x29a74
movq %rax, %r15
movl 0xc(%rsp), %esi
movl 0x1c(%rsp), %edx
movl 0x18(%rsp), %ecx
movq %rax, %rdi
callq 0x29dc7
movq 0x48(%rsp), %rcx
movq (%rcx,%rbx,8), %rcx
movq %rax, 0x8(%rcx)
movq %r15, %rdi
callq 0x291f9
incq %rbx
cmpq %rbx, %rbp
jne 0x1b192
jmp 0x1b281
movq $0x0, 0x20(%rsp)
leaq 0x275(%rip), %rsi       # 0x1b4fd
movl %ebp, %edi
movq 0x48(%rsp), %rdx
callq 0x2b6af
movq %rax, %r15
movl $0x4, %esi
movq %rbp, %rdi
callq 0x3610
movq %rax, %r12
movl $0x8, %esi
movq %rbp, %rdi
callq 0x3610
movq %rax, %rbx
leaq 0xc(%rsp), %rsi
leaq 0x14(%rsp), %rdx
movq %r15, 0x28(%rsp)
movq %r15, %rdi
movq %r12, %rcx
movq %rax, %r8
callq 0x2b7f2
testl %eax, %eax
jle 0x1b429
leaq 0x27fae(%rip), %r15     # 0x43291
movl 0x14(%rsp), %edx
cmpl 0x1c(%rsp), %edx
jl 0x1b407
cmpl 0x18(%rsp), %edx
jge 0x1b407
testq %r14, %r14
je 0x1b325
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0xc(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
leal 0x1(%rdx), %ecx
movq %r14, %rdi
callq 0x305ac
testl %eax, %eax
je 0x1b407
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0xc(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x33cba(%rip), %rax     # 0x4eff8
movq (%rax), %rsi
callq 0x34a0
movl 0x14(%rsp), %esi
incl %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x3060
cmpl $0x0, 0x34(%rsp)
jle 0x1b3f8
xorl %r14d, %r14d
movslq (%r12,%r14,4), %rsi
testq %rsi, %rsi
jle 0x1b3de
movq (%rbx,%r14,8), %rcx
leaq (,%rsi,8), %rax
leaq (%rax,%rax,2), %rdx
xorl %edi, %edi
xorl %eax, %eax
movl $0x1, %r8d
testb $0x9, 0x14(%rcx,%rdi)
jne 0x1b3d0
movq (%rcx,%rdi), %r8
movq 0x8(%r8), %r9
movq %r9, %r10
shrq $0x30, %r10
shlq $0x2, %r10
addq 0x30(%r8), %r10
shrl $0x18, %r9d
movl 0x10(%r8), %r8d
incl %r8d
sarl %r8d
movslq %r8d, %r8
addq %r9, %r8
addq %r10, %r8
movslq 0x8(%rcx,%rdi), %r9
movzbl (%r9,%r8), %r9d
xorl %r8d, %r8d
cmpl %r9d, %r13d
setg %r8b
addl %r8d, %eax
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0x1b381
jmp 0x1b3e0
xorl %eax, %eax
subl %eax, %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x3060
incq %r14
cmpq %rbp, %r14
jne 0x1b364
movl $0xa, %edi
callq 0x31e0
movq 0x40(%rsp), %r14
movq 0x28(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x14(%rsp), %rdx
movq %r12, %rcx
movq %rbx, %r8
callq 0x2b7f2
testl %eax, %eax
jg 0x1b2e3
movq %r12, %rdi
callq 0x3350
movq %rbx, %rdi
callq 0x3350
movq 0x28(%rsp), %rdi
callq 0x2b798
movq 0x20(%rsp), %rdi
callq 0x22e78
movl 0x34(%rsp), %r15d
testl %r15d, %r15d
movq 0x48(%rsp), %r12
jle 0x1b48d
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rax
movq (%rax), %rdi
callq 0x20aae
movq (%r12,%rbx,8), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x1b47c
callq 0x2a2f1
movq (%r12,%rbx,8), %rdi
callq 0x3350
incq %rbx
cmpq %rbx, %rbp
jne 0x1b45e
movq %r12, %rdi
callq 0x3350
movq 0x38(%rsp), %rdi
callq 0x3350
testq %r14, %r14
je 0x1b4ac
movq %r14, %rdi
callq 0x30e77
xorl %r14d, %r14d
cmpq $0x0, 0x60(%rsp)
je 0x1b4de
testl %r15d, %r15d
jle 0x1b4d4
xorl %ebx, %ebx
movq 0x50(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x3350
incq %rbx
cmpq %rbx, %rbp
jne 0x1b4be
movq 0x50(%rsp), %rdi
callq 0x3350
movl %r14d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4344
movl $0x1, %r14d
jmp 0x1b4de