// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq $0x0, 0x2c6cd(%rip) # 0x30928
movw $0x0, 0x2c6cc(%rip)     # 0x30930
leaq 0x22da5(%rip), %rax     # 0x27010
movq %rax, 0x2c6c6(%rip)     # 0x30938
leaq 0x22d97(%rip), %rax     # 0x27010
movq %rax, 0x2c6c0(%rip)     # 0x30940
leaq 0x23214(%rip), %rax     # 0x2749b
movq %rax, -0x20(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x429a
movq -0x10(%rbp), %rdi
callq 0x3ac0
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x3570
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
je 0x4345
movl -0x14(%rbp), %eax
movl %eax, -0x54(%rbp)
subl $0x3f, %eax
je 0x4308
jmp 0x42c4
movl -0x54(%rbp), %eax
subl $0x68, %eax
je 0x430a
jmp 0x42ce
movl -0x54(%rbp), %eax
subl $0x6f, %eax
je 0x42e2
jmp 0x42d8
movl -0x54(%rbp), %eax
subl $0x72, %eax
je 0x42f5
jmp 0x4315
movq 0x2bcef(%rip), %rax     # 0x2ffd8
movq (%rax), %rax
movq %rax, 0x2c645(%rip)     # 0x30938
jmp 0x432d
movq 0x2bcdc(%rip), %rax     # 0x2ffd8
movq (%rax), %rax
movq %rax, 0x2c63a(%rip)     # 0x30940
jmp 0x432d
jmp 0x432d
movq -0x10(%rbp), %rdi
callq 0x3ac0
jmp 0x432d
movq 0x2bcdc(%rip), %rax     # 0x2fff8
movq (%rax), %rdi
leaq 0x2317c(%rip), %rsi     # 0x274a2
movb $0x0, %al
callq 0x3360
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x3570
movl %eax, -0x14(%rbp)
jmp 0x42ad
movq -0x10(%rbp), %rax
movq 0x2bc80(%rip), %rcx     # 0x2ffd0
movslq (%rcx), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x2c5c7(%rip)     # 0x30928
movl -0x8(%rbp), %eax
movq 0x2bc65(%rip), %rcx     # 0x2ffd0
subl (%rcx), %eax
movw %ax, 0x2c5bc(%rip)      # 0x30930
callq 0x4720
movq %rax, -0x28(%rbp)
movswq 0x2c5ab(%rip), %rdi     # 0x30930
movl $0x28, %esi
callq 0x3320
movq %rax, -0x30(%rbp)
movq 0x2c59e(%rip), %rdi     # 0x30938
leaq 0x23113(%rip), %rsi     # 0x274b4
callq 0x3860
movq %rax, -0x38(%rbp)
movl $0x30, %edi
callq 0x3440
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
leaq 0x38d(%rip), %rcx       # 0x4750
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
leaq 0x3af(%rip), %rcx       # 0x4780
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x40(%rbp), %rax
leaq 0x3b4(%rip), %rcx       # 0x47a0
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rax
leaq 0x3d5(%rip), %rcx       # 0x47d0
movq %rcx, 0x20(%rax)
movq -0x40(%rbp), %rax
leaq 0x3e6(%rip), %rcx       # 0x47f0
movq %rcx, 0x28(%rax)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movswl 0x2c503(%rip), %ecx     # 0x30930
cmpl %ecx, %eax
jge 0x46ee
movq 0x2c4ec(%rip), %rax     # 0x30928
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x22c12(%rip), %rsi     # 0x2705d
xorl %eax, %eax
movl %eax, %edx
callq 0x14620
movq -0x30(%rbp), %rcx
movslq -0x44(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x448e
movq 0x2c4b5(%rip), %rax     # 0x30928
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movl $0x1, %edi
leaq 0x23037(%rip), %rsi     # 0x274be
movb $0x0, %al
callq 0x3430
movq 0x2c493(%rip), %rax     # 0x30928
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3b30
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movslq (%rax), %rdi
movl $0x4, %esi
callq 0x3320
movq %rax, %rcx
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movslq (%rax), %rdi
movl $0x8, %esi
callq 0x3320
movq %rax, %rcx
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x20(%rax)
movq 0x2c421(%rip), %rdi     # 0x30940
leaq 0x22aea(%rip), %rsi     # 0x27010
movl $0x6, %edx
callq 0x30b0
cmpl $0x0, %eax
je 0x4561
movq -0x30(%rbp), %rdi
movslq -0x44(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rdi
movq -0x50(%rbp), %rsi
movq 0x2c3f1(%rip), %rdx     # 0x30940
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x41a0
jmp 0x463f
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x30(%rbp), %rcx
movslq -0x44(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
cmpl (%rcx), %eax
jge 0x463d
movq -0x30(%rbp), %rdi
movslq -0x44(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rdi
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x41a0
movq 0x2ba26(%rip), %rax     # 0x2fff8
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
callq 0x4720
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rdx
subq -0x28(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x229eb(%rip), %xmm1    # 0x27008
divsd %xmm1, %xmm0
leaq 0x22eb9(%rip), %rsi     # 0x274e1
movb $0x1, %al
callq 0x3360
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x4568
jmp 0x463f
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
callq 0x3050
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rdi
callq 0x3050
movq -0x50(%rbp), %rdi
callq 0x16df0
movq -0x30(%rbp), %rax
movslq -0x44(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x14cf0
movq 0x2b962(%rip), %rax     # 0x2fff8
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq 0x2c284(%rip), %rax     # 0x30928
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x70(%rbp)
callq 0x4720
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rdx
subq -0x28(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x2293a(%rip), %xmm1    # 0x27008
divsd %xmm1, %xmm0
leaq 0x22e16(%rip), %rsi     # 0x274ef
movb $0x1, %al
callq 0x3360
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x4423
movq -0x40(%rbp), %rdi
callq 0x3050
movq -0x30(%rbp), %rdi
callq 0x3050
movq -0x38(%rbp), %rdi
callq 0x31d0
xorl %eax, %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)