// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rsi, %rbx
movq $0x0, 0x1fb5d(%rip) # 0x24928
movw $0x0, 0x1fb5c(%rip)     # 0x24930
leaq 0x16235(%rip), %rax     # 0x1b010
movq %rax, 0x1fb56(%rip)     # 0x24938
movq %rax, 0x1fb57(%rip)     # 0x24940
cmpl $0x2, %edi
jl 0x515a
movl %edi, %ebp
leaq 0x166a0(%rip), %r14     # 0x1b49b
movq 0x1f1d6(%rip), %r15     # 0x23fd8
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3580
cmpl $0x6e, %eax
jle 0x4e2a
cmpl $0x72, %eax
je 0x4e44
cmpl $0x6f, %eax
jne 0x4e3d
movq (%r15), %rax
movq %rax, 0x1fb10(%rip)     # 0x24938
jmp 0x4e02
cmpl $0x3f, %eax
je 0x4e02
cmpl $-0x1, %eax
je 0x4e50
cmpl $0x68, %eax
je 0x515a
callq 0x372d
jmp 0x4e02
movq (%r15), %rax
movq %rax, 0x1faf2(%rip)     # 0x24940
jmp 0x4e02
movq 0x1f179(%rip), %rax     # 0x23fd0
movslq (%rax), %rax
leaq (%rbx,%rax,8), %rcx
movq %rcx, 0x1fac3(%rip)     # 0x24928
subl %eax, %ebp
movw %bp, 0x1fac2(%rip)      # 0x24930
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3250
imulq $-0xf4240, (%rbx), %r15 # imm = 0xFFF0BDC0
subq 0x8(%rbx), %r15
movswq 0x1faa0(%rip), %rdi     # 0x24930
movl $0x28, %esi
callq 0x3330
movq %rax, %rbx
movq 0x1fa94(%rip), %rdi     # 0x24938
leaq 0x16609(%rip), %rsi     # 0x1b4b4
callq 0x460f
movq %rax, %rbp
movl $0x30, %edi
callq 0x3440
movq %rax, %r14
leaq 0x2ae(%rip), %rax       # 0x5175
movq %rax, (%r14)
leaq 0x2ac(%rip), %rax       # 0x517d
movq %rax, 0x8(%r14)
movq $0x0, 0x10(%r14)
leaq 0x2a1(%rip), %rax       # 0x5185
movq %rax, 0x18(%r14)
leaq 0x2a1(%rip), %rax       # 0x5190
movq %rax, 0x20(%r14)
leaq 0x29e(%rip), %rax       # 0x5198
movq %rax, 0x28(%r14)
cmpw $0x0, 0x1fa2a(%rip)     # 0x24930
jle 0x5131
xorl %r13d, %r13d
movq %rbx, 0x28(%rsp)
movq %r14, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movq 0x1fa03(%rip), %rax     # 0x24928
movq (%rax,%r13,8), %rdi
leaq 0x1612d(%rip), %rsi     # 0x1b05d
xorl %edx, %edx
callq 0xfbe1
leaq (,%r13,4), %rcx
addq %r13, %rcx
movq %rax, (%rbx,%rcx,8)
movq 0x1f9db(%rip), %rdx     # 0x24928
movq %r13, 0x30(%rsp)
movq (%rdx,%r13,8), %rdx
testq %rax, %rax
je 0x5162
leaq (%rbx,%rcx,8), %r13
movq %rdx, %rdi
callq 0x47a4
movq %rax, %rbx
movq (%r13), %rax
movq 0x10(%rax), %rax
movslq (%rax), %r12
movl $0x4, %esi
movq %r12, %rdi
callq 0x3330
movq %rax, 0x18(%r13)
movl $0x8, %esi
movq %r12, %rdi
callq 0x3330
movq %rax, 0x20(%r13)
movq 0x1f99e(%rip), %r12     # 0x24940
movl $0x6, %edx
movq %r12, %rdi
leaq 0x1605f(%rip), %rsi     # 0x1b010
callq 0x30c0
testl %eax, %eax
jne 0x5076
movq (%r13), %rax
movq 0x10(%rax), %rax
cmpl $0x0, (%rax)
movq %rbx, %r15
movq 0x1f025(%rip), %rbx     # 0x23ff8
jle 0x5094
xorl %r14d, %r14d
movq 0x8(%rax), %rax
movq (%rax,%r14,8), %rdx
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rcx
movq 0x20(%rsp), %r8
callq 0x4d45
movq (%rbx), %r12
movq (%r13), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq %rbp, %rbx
movq (%rax,%r14,8), %rbp
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x3250
imulq $0xf4240, 0x8(%rsp), %rax # imm = 0xF4240
movq 0x10(%rsp), %rcx
addq 0x18(%rsp), %rcx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
divsd 0x15fc9(%rip), %xmm0    # 0x1b008
movq %r12, %rdi
leaq 0x16498(%rip), %rsi     # 0x1b4e1
movq %rbp, %rdx
movq %rbx, %rbp
movq 0x1efa2(%rip), %rbx     # 0x23ff8
movb $0x1, %al
callq 0x3370
incq %r14
movq (%r13), %rax
movq 0x10(%rax), %rax
movslq (%rax), %rcx
cmpq %rcx, %r14
jl 0x4fdc
jmp 0x5094
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %rbp, %rcx
movq %r14, %r8
callq 0x4d45
movq %rbx, %r15
movq 0x1ef64(%rip), %rbx     # 0x23ff8
movq 0x18(%r13), %rdi
callq 0x3060
movq 0x20(%r13), %rdi
callq 0x3060
movq %r15, %rdi
callq 0x1150d
movq (%r13), %rdi
callq 0x10035
movq (%rbx), %rbx
movq 0x1f867(%rip), %rax     # 0x24928
movq 0x30(%rsp), %r13
movq (%rax,%r13,8), %r12
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x3250
imulq $0xf4240, 0x8(%rsp), %rax # imm = 0xF4240
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r15
addq %r15, %rcx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
divsd 0x15f09(%rip), %xmm0    # 0x1b008
movq %rbx, %rdi
leaq 0x163e6(%rip), %rsi     # 0x1b4ef
movq %r12, %rdx
movb $0x1, %al
callq 0x3370
incq %r13
movswq 0x1f812(%rip), %rax     # 0x24930
cmpq %rax, %r13
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r14
jl 0x4f1e
movq %r14, %rdi
callq 0x3060
movq %rbx, %rdi
callq 0x3060
movq %rbp, %rdi
callq 0x31d0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x475a
leaq 0x16355(%rip), %rsi     # 0x1b4be
movl $0x1, %edi
xorl %eax, %eax
callq 0x3430