// File: /workspace/github_c_makefile_star2_O3/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 0x26e60(%rip), %rdx     # 0x423e0
callq 0x3210
testl %eax, %eax
js 0x1b645
xorl %r13d, %r13d
movq 0x34a49(%rip), %r14     # 0x4ffe0
leaq 0x26e42(%rip), %rbx     # 0x423e0
xorl %ebp, %ebp
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movl $0x0, 0x10(%rsp)
movq $0x0, 0x28(%rsp)
cmpl $0x6b, %eax
jg 0x1b5dc
cmpl $0x51, %eax
je 0x1b5fa
cmpl $0x62, %eax
je 0x1b617
cmpl $0x66, %eax
jne 0x1b631
movq (%r14), %rbp
jmp 0x1b631
cmpl $0x72, %eax
je 0x1b608
cmpl $0x71, %eax
je 0x1b626
cmpl $0x6c, %eax
jne 0x1b631
movq (%r14), %rdi
callq 0x34c0
movq %rax, 0x28(%rsp)
jmp 0x1b631
movq (%r14), %rdi
callq 0x34c0
movl %eax, 0x10(%rsp)
jmp 0x1b631
movq (%r14), %rdi
callq 0x3570
movq %rax, 0x38(%rsp)
jmp 0x1b631
movq (%r14), %rdi
callq 0x30fe5
movq %rax, 0x40(%rsp)
jmp 0x1b631
movq (%r14), %rdi
callq 0x34c0
movl %eax, %r13d
movl %r15d, %edi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3210
testl %eax, %eax
jns 0x1b5c3
jmp 0x1b66d
movq $0x0, 0x28(%rsp)
movl $0x0, 0x10(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x34944(%rip), %rax     # 0x4ffb8
subl (%rax), %r15d
setne %al
testq %rbp, %rbp
setne %cl
orb %al, %cl
je 0x1ba7b
testq %rbp, %rbp
je 0x1b6cb
leaq 0x5c(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq %rbp, %rdi
callq 0x75d4
movl $0x1, %r14d
testl %eax, %eax
jne 0x1ba69
movq %rbp, 0x60(%rsp)
movl 0x5c(%rsp), %r15d
movq 0x50(%rsp), %r12
movq 0x348f5(%rip), %rax     # 0x4ffb8
movl $0x0, (%rax)
jmp 0x1b6d0
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 0x1b800
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 0x3487f(%rip), %rax     # 0x4ffb8
movslq (%rax), %rax
addq %rbx, %rax
movq (%r12,%rax,8), %rdi
leaq 0x2a605(%rip), %rsi     # 0x45d4f
callq 0x20308
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 0x235fb
movq %rax, %r15
testq %rbx, %rbx
je 0x1b783
movq %r15, %rdi
callq 0x23577
jmp 0x1b7a9
movq 0x38(%rsp), %rsi
testq %rsi, %rsi
je 0x1b7a4
movq %r15, %rdi
leaq 0xc(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x18(%rsp), %r8
callq 0x22e08
movq %r15, 0x20(%rsp)
cmpl $0x0, 0xc(%rsp)
js 0x1b7f2
movq 0x34801(%rip), %rax     # 0x4ffb8
movslq (%rax), %rax
addq %rbx, %rax
movq (%r12,%rax,8), %rdi
callq 0x2a134
movq %rax, %r15
movl 0xc(%rsp), %esi
movl 0x1c(%rsp), %edx
movl 0x18(%rsp), %ecx
movq %rax, %rdi
callq 0x2a484
movq 0x48(%rsp), %rcx
movq (%rcx,%rbx,8), %rcx
movq %rax, 0x8(%rcx)
movq %r15, %rdi
callq 0x298a9
incq %rbx
cmpq %rbx, %rbp
jne 0x1b71a
jmp 0x1b809
movq $0x0, 0x20(%rsp)
leaq 0x278(%rip), %rsi       # 0x1ba88
movl %ebp, %edi
movq 0x48(%rsp), %rdx
callq 0x2c059
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 0x2c19c
testl %eax, %eax
jle 0x1b9b1
leaq 0x28b08(%rip), %r15     # 0x44373
movl 0x14(%rsp), %edx
cmpl 0x1c(%rsp), %edx
jl 0x1b98f
cmpl 0x18(%rsp), %edx
jge 0x1b98f
testq %r14, %r14
je 0x1b8ad
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 0x30e93
testl %eax, %eax
je 0x1b98f
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0xc(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x34732(%rip), %rax     # 0x4fff8
movq (%rax), %rsi
callq 0x34a0
movl 0x14(%rsp), %esi
incl %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x3060
cmpl $0x0, 0x34(%rsp)
jle 0x1b980
xorl %r14d, %r14d
movslq (%r12,%r14,4), %rsi
testq %rsi, %rsi
jle 0x1b966
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 0x1b958
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 0x1b909
jmp 0x1b968
xorl %eax, %eax
subl %eax, %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x3060
incq %r14
cmpq %rbp, %r14
jne 0x1b8ec
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 0x2c19c
testl %eax, %eax
jg 0x1b86b
movq %r12, %rdi
callq 0x3350
movq %rbx, %rdi
callq 0x3350
movq 0x28(%rsp), %rdi
callq 0x2c142
movq 0x20(%rsp), %rdi
callq 0x23577
movl 0x34(%rsp), %r15d
testl %r15d, %r15d
movq 0x48(%rsp), %r12
jle 0x1ba18
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rax
movq (%rax), %rdi
callq 0x21311
movq (%r12,%rbx,8), %rdi
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x1ba0b
movq %rax, %rdi
callq 0x2ac9a
movq (%r12,%rbx,8), %rdi
callq 0x3350
incq %rbx
cmpq %rbx, %rbp
jne 0x1b9e6
movq %r12, %rdi
callq 0x3350
movq 0x38(%rsp), %rdi
callq 0x3350
testq %r14, %r14
je 0x1ba37
movq %r14, %rdi
callq 0x31a85
xorl %r14d, %r14d
cmpq $0x0, 0x60(%rsp)
je 0x1ba69
testl %r15d, %r15d
jle 0x1ba5f
xorl %ebx, %ebx
movq 0x50(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x3350
incq %rbx
cmpq %rbx, %rbp
jne 0x1ba49
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 0x433f
movl $0x1, %r14d
jmp 0x1ba69