// File: /workspace/github_c_makefile_star2_O2/xiongxu[P]bam2bedGraph2/bam2bedGraph.c
// Function: main
int main(int argc, char *argv[])
{
	int opt = 0;
	globalArgs.infiles=NULL;
	globalArgs.numInfiles=0;
	globalArgs.outfile="-";
	globalArgs.region="-";
	const char *optString = "o:r:h?";
	if (argc<2) display_usage(argv);
	opt = getopt( argc, argv, optString );
	while( opt != -1 ) {
		switch( opt ) {
			case 'o':
				globalArgs.outfile = optarg;
				break;
			case 'r':
				globalArgs.region = optarg;
				break;
			case '?':	/* fall-through is intentional */
				break;
			case 'h':
				display_usage(argv);
				break;
			default:
				fprintf(stderr,"error parameter!\n");
				break;
		}
		opt = getopt( argc, argv, optString );
	}
	globalArgs.infiles = argv + optind;
	globalArgs.numInfiles = argc - optind;
	long long begin;
	begin=usec();

	DataBuf *databuf=(DataBuf *)calloc(globalArgs.numInfiles,sizeof(DataBuf));
	FILE *bedGraph=fcreat_outfile(globalArgs.outfile,".bedGraph");
	dictType *mydictType=(dictType *)malloc(sizeof(dictType));
	initdictType(mydictType);
	int32_t i=0,j=0;
	for (i=0;i<globalArgs.numInfiles ;i++ ) {
		if (((databuf+i)->fp = samopen(*(globalArgs.infiles+i), "rb", 0)) == 0) {
			err(1, "bam2bed: Fail to open BAM file %s\n", *(globalArgs.infiles+i));
		}
		bam_index_t *idx=load_bam_index(*(globalArgs.infiles+i));
		(databuf+i)->chrRC = (uint32_t *)calloc((databuf+i)->fp->header->n_targets,sizeof(uint32_t));//databuf->fp->header->target_len
		(databuf+i)->chrReadLen =(uint64_t *)calloc((databuf+i)->fp->header->n_targets,sizeof(uint64_t));
		if (strncmp(globalArgs.region,"-",6)) { /* if a region is not specified */
			region_sam2wig(databuf+i ,idx,globalArgs.region,bedGraph,mydictType);
		}else{
			for (j=0;j<(databuf+i)->fp->header->n_targets ;j++ ) {
				region_sam2wig(databuf+i ,idx,(databuf+i)->fp->header->target_name[j],bedGraph,mydictType);
				fprintf(stderr,"%s at %.3f s\n",(databuf+i)->fp->header->target_name[j],(double)(usec()-begin)/CLOCKS_PER_SEC);
			}
		}
		free((databuf+i)->chrRC);
		free((databuf+i)->chrReadLen);
		bam_index_destroy(idx);
		samclose((databuf+i)->fp);
		fprintf(stderr,"Converted %s to wig format at %.3f s\n",*(globalArgs.infiles+i),(double)(usec()-begin)/CLOCKS_PER_SEC);
	}
	free(mydictType);
	free(databuf);
	fclose(bedGraph);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
andq $0x0, 0x1da9c(%rip)     # 0x21928
andw $0x0, 0x1da9c(%rip)     # 0x21930
leaq 0x13175(%rip), %rax     # 0x17010
movq %rax, 0x1da96(%rip)     # 0x21938
movq %rax, 0x1da97(%rip)     # 0x21940
cmpl $0x2, %edi
jl 0x41a0
movl %edi, %ebp
leaq 0x135e0(%rip), %r14     # 0x1749b
movq 0x1d116(%rip), %r15     # 0x20fd8
pushq $0x11
popq %r12
pushq $0x1
popq %r13
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3580
cmpl $0x3f, %eax
je 0x3eca
cmpl $0x6f, %eax
je 0x3ef2
cmpl $0x72, %eax
jne 0x3efe
movq (%r15), %rax
movq %rax, 0x1da50(%rip)     # 0x21940
jmp 0x3eca
movq (%r15), %rax
movq %rax, 0x1da3c(%rip)     # 0x21938
jmp 0x3eca
cmpl $-0x1, %eax
je 0x3f2a
cmpl $0x68, %eax
je 0x41a0
movq 0x1d0e5(%rip), %rax     # 0x20ff8
movq (%rax), %rcx
leaq 0x13585(%rip), %rdi     # 0x174a2
movq %r12, %rsi
movq %r13, %rdx
callq 0x35f0
jmp 0x3eca
movq 0x1d09f(%rip), %rax     # 0x20fd0
movslq (%rax), %rax
leaq (%rbx,%rax,8), %rcx
movq %rcx, 0x1d9e9(%rip)     # 0x21928
subl %eax, %ebp
movw %bp, 0x1d9e8(%rip)      # 0x21930
callq 0x41b9
movq %rax, 0x10(%rsp)
movswq 0x1d9d6(%rip), %rdi     # 0x21930
pushq $0x28
popq %rsi
callq 0x3330
movq %rax, %rbx
movq 0x1d9cc(%rip), %rdi     # 0x21938
leaq 0x13541(%rip), %rsi     # 0x174b4
callq 0x381b
movq %rax, 0x8(%rsp)
pushq $0x30
popq %rdi
callq 0x3440
leaq 0x250(%rip), %rcx       # 0x41dc
movq %rcx, (%rax)
leaq 0x24e(%rip), %rcx       # 0x41e4
movq %rcx, 0x8(%rax)
andq $0x0, 0x10(%rax)
leaq 0x246(%rip), %rcx       # 0x41ec
movq %rcx, 0x18(%rax)
leaq 0x246(%rip), %rcx       # 0x41f7
movq %rcx, 0x20(%rax)
leaq 0x243(%rip), %rcx       # 0x41ff
movq %rax, (%rsp)
movq %rcx, 0x28(%rax)
xorl %r15d, %r15d
movq %rbx, 0x18(%rsp)
movswq 0x1d95c(%rip), %rax     # 0x21930
cmpq %rax, %r15
jge 0x4174
movq 0x1d944(%rip), %rax     # 0x21928
movq (%rax,%r15,8), %rdi
leaq 0x1306e(%rip), %rsi     # 0x1705d
xorl %edx, %edx
callq 0xca54
imulq $0x28, %r15, %r13
movq %rax, (%rbx,%r13)
movq 0x1d923(%rip), %rcx     # 0x21928
movq (%rcx,%r15,8), %rdx
testq %rax, %rax
je 0x41a8
movq %r15, 0x20(%rsp)
addq %rbx, %r13
movq %rdx, %rdi
callq 0x39a2
movq %rax, %r14
movq (%r13), %rax
movq 0x10(%rax), %rbx
movl (%rbx), %r12d
movslq %r12d, %rbp
movq %rbp, %rdi
pushq $0x4
popq %rsi
callq 0x3330
movq %rax, 0x18(%r13)
movq %rbp, %rdi
pushq $0x8
popq %rsi
callq 0x3330
movq %rax, 0x20(%r13)
movq 0x1d8e8(%rip), %rdx     # 0x21940
cmpb $0x2d, (%rdx)
jne 0x40eb
cmpb $0x0, 0x1(%rdx)
jne 0x40eb
xorl %r15d, %r15d
movslq %r12d, %rax
cmpq %rax, %r15
jge 0x40ff
movq 0x8(%rbx), %rax
movq (%rax,%r15,8), %rdx
movq %r13, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
callq 0x3e09
movq 0x1cf5b(%rip), %rax     # 0x20ff8
movq (%rax), %rbp
movq (%r13), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax,%r15,8), %rbx
callq 0x41b9
subq 0x10(%rsp), %rax
cvtsi2sd %rax, %xmm0
divsd 0x12f41(%rip), %xmm0    # 0x17008
movq %rbp, %rdi
leaq 0x13410(%rip), %rsi     # 0x174e1
movq %rbx, %rdx
movb $0x1, %al
callq 0x3370
incq %r15
movq (%r13), %rax
movq 0x10(%rax), %rbx
movl (%rbx), %r12d
jmp 0x406e
movq %r13, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
callq 0x3e09
movq 0x18(%r13), %rdi
callq 0x3060
movq 0x20(%r13), %rdi
callq 0x3060
movq %r14, %rdi
callq 0xe0ae
movq (%r13), %rdi
callq 0xced2
movq 0x1cecf(%rip), %rax     # 0x20ff8
movq (%rax), %rbx
movq 0x1d7f5(%rip), %rax     # 0x21928
movq 0x20(%rsp), %r15
movq (%rax,%r15,8), %r14
callq 0x41b9
subq 0x10(%rsp), %rax
cvtsi2sd %rax, %xmm0
divsd 0x12eb5(%rip), %xmm0    # 0x17008
movq %rbx, %rdi
leaq 0x13392(%rip), %rsi     # 0x174ef
movq %r14, %rdx
movb $0x1, %al
callq 0x3370
incq %r15
movq 0x18(%rsp), %rbx
jmp 0x3fcc
movq (%rsp), %rdi
callq 0x3060
movq %rbx, %rdi
callq 0x3060
movq 0x8(%rsp), %rdi
callq 0x31d0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x395a
leaq 0x1330f(%rip), %rsi     # 0x174be
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x3430