// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq %rsi, %rbx
movq $0x0, 0x1f876(%rip) # 0x24928
movw $0x0, 0x1f875(%rip)     # 0x24930
leaq 0x15f4e(%rip), %rax     # 0x1b010
movq %rax, 0x1f86f(%rip)     # 0x24938
movq %rax, 0x1f870(%rip)     # 0x24940
cmpl $0x2, %edi
jl 0x5444
movl %edi, %ebp
leaq 0x163b9(%rip), %r14     # 0x1b49b
movq 0x1eeef(%rip), %r15     # 0x23fd8
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3580
cmpl $0x6e, %eax
jle 0x5111
cmpl $0x72, %eax
je 0x512b
cmpl $0x6f, %eax
jne 0x5124
movq (%r15), %rax
movq %rax, 0x1f829(%rip)     # 0x24938
jmp 0x50e9
cmpl $0x3f, %eax
je 0x50e9
cmpl $-0x1, %eax
je 0x5137
cmpl $0x68, %eax
je 0x5444
callq 0x372d
jmp 0x50e9
movq (%r15), %rax
movq %rax, 0x1f80b(%rip)     # 0x24940
jmp 0x50e9
movq 0x1ee92(%rip), %rax     # 0x23fd0
movslq (%rax), %rax
leaq (%rbx,%rax,8), %rcx
movq %rcx, 0x1f7dc(%rip)     # 0x24928
subl %eax, %ebp
movw %bp, 0x1f7db(%rip)      # 0x24930
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3250
imulq $-0xf4240, (%rbx), %rbp # imm = 0xFFF0BDC0
subq 0x8(%rbx), %rbp
movswq 0x1f7b9(%rip), %rdi     # 0x24930
movl $0x28, %esi
callq 0x3330
movq %rax, %rbx
movq 0x1f7ad(%rip), %rdi     # 0x24938
leaq 0x16322(%rip), %rsi     # 0x1b4b4
callq 0x48ef
movq %rax, %r15
movl $0x30, %edi
callq 0x3440
movq %rax, %rdi
leaq 0x2b1(%rip), %rax       # 0x545f
movq %rax, (%rdi)
leaq 0x2af(%rip), %rax       # 0x5467
movq %rax, 0x8(%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x2a4(%rip), %rax       # 0x546f
movq %rax, 0x18(%rdi)
leaq 0x2a4(%rip), %rax       # 0x547a
movq %rax, 0x20(%rdi)
leaq 0x2a1(%rip), %rax       # 0x5482
movq %rax, 0x28(%rdi)
cmpw $0x0, 0x1f743(%rip)     # 0x24930
jle 0x541e
xorl %r13d, %r13d
leaq 0x28(%rsp), %r14
movq %rbx, 0x38(%rsp)
movq %rdi, 0x8(%rsp)
movq %r15, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq 0x1f712(%rip), %rax     # 0x24928
movq (%rax,%r13,8), %rdi
leaq 0x15e3c(%rip), %rsi     # 0x1b05d
xorl %edx, %edx
callq 0xfc8c
leaq (,%r13,4), %rcx
addq %r13, %rcx
movq %rax, (%rbx,%rcx,8)
movq 0x1f6ea(%rip), %rdx     # 0x24928
movq %r13, 0x40(%rsp)
movq (%rdx,%r13,8), %rdx
testq %rax, %rax
je 0x544c
leaq (%rbx,%rcx,8), %r12
movq %rdx, %rdi
callq 0x4a84
movq %rax, (%rsp)
movq (%r12), %rax
movq 0x10(%rax), %r13
movl (%r13), %eax
movl %eax, 0x14(%rsp)
movslq %eax, %rbx
movl $0x4, %esi
movq %rbx, %rdi
callq 0x3330
movq %rax, 0x18(%r12)
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3330
movq %rax, 0x20(%r12)
movq 0x1f6a2(%rip), %rdx     # 0x24940
cmpb $0x2d, (%rdx)
jne 0x535d
cmpb $0x0, 0x1(%rdx)
jne 0x535d
cmpl $0x0, 0x14(%rsp)
movq 0x1ed3b(%rip), %rbp     # 0x23ff8
movq (%rsp), %rbx
jle 0x537b
xorl %r15d, %r15d
movq 0x8(%r13), %rax
movq (%rax,%r15,8), %rdx
movq %r12, %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %r8
callq 0x502c
movq (%rbp), %rbx
movq (%r12), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax,%r15,8), %r13
movq %r14, %rdi
xorl %esi, %esi
callq 0x3250
imulq $0xf4240, 0x28(%rsp), %rax # imm = 0xF4240
movq 0x30(%rsp), %rcx
addq 0x18(%rsp), %rcx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
divsd 0x15cdd(%rip), %xmm0    # 0x1b008
movq %rbx, %rdi
movq (%rsp), %rbx
leaq 0x161a8(%rip), %rsi     # 0x1b4e1
movq %r13, %rdx
movb $0x1, %al
callq 0x3370
incq %r15
movq (%r12), %rax
movq 0x10(%rax), %r13
movslq (%r13), %rax
cmpq %rax, %r15
jl 0x52ca
jmp 0x537b
movq %r12, %rdi
movq (%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rcx
movq 0x8(%rsp), %r8
callq 0x502c
movq 0x1ec7d(%rip), %rbp     # 0x23ff8
movq 0x18(%r12), %rdi
callq 0x3060
movq 0x20(%r12), %rdi
callq 0x3060
movq %rbx, %rdi
callq 0x11645
movq (%r12), %rdi
callq 0x10107
movq (%rbp), %rbx
movq 0x1f57d(%rip), %rax     # 0x24928
movq 0x40(%rsp), %r13
movq (%rax,%r13,8), %r12
movq %r14, %rdi
xorl %esi, %esi
callq 0x3250
imulq $0xf4240, 0x28(%rsp), %rax # imm = 0xF4240
movq 0x30(%rsp), %rcx
movq 0x18(%rsp), %rbp
addq %rbp, %rcx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
divsd 0x15c21(%rip), %xmm0    # 0x1b008
movq %rbx, %rdi
leaq 0x160fe(%rip), %rsi     # 0x1b4ef
movq %r12, %rdx
movb $0x1, %al
callq 0x3370
incq %r13
movswq 0x1f52a(%rip), %rax     # 0x24930
cmpq %rax, %r13
movq 0x38(%rsp), %rbx
movq 0x20(%rsp), %r15
movq 0x8(%rsp), %rdi
jl 0x520f
callq 0x3060
movq %rbx, %rdi
callq 0x3060
movq %r15, %rdi
callq 0x31d0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x4a3a
leaq 0x1606b(%rip), %rsi     # 0x1b4be
movl $0x1, %edi
xorl %eax, %eax
callq 0x3430