// File: /workspace/github_cpp_makefile_star2_O3/gersteinlab[P]MUSIC/src/ms_signal_track_tools.cpp
// Function: dump_per_nucleotide_binary_profile_per_bedgraph(char*, bool, char*)
void dump_per_nucleotide_binary_profile_per_bedgraph(char* bgr_fp, bool dump_binary, char* op_fp)
{
	FILE* f_bgr = NULL;
	if(t_string::compare_strings(bgr_fp, "stdin"))
	{
		f_bgr = stdin;
	}
	else
	{
		f_bgr = open_f(bgr_fp, "r");
	}

	// Initialize the signal profile.
	int l_max = 300*1000*1000;
	double* signal_profile = new double[l_max+1];
	for(int i_nuc = 0; i_nuc <= l_max; i_nuc++)
	{
		signal_profile[i_nuc] = 0.0;
	} // i_nuc loop.

	// Go over all the input file and process all the lines.
	fprintf(stderr, "Dumping the profile to %s.\n", op_fp);
	while(1)
	{
		char* cur_bgr_line = getline(f_bgr);
		if(cur_bgr_line == NULL)
		{
			break;
		}

		char cur_chrom[1000];
		int start;
		int end;
		double cur_sig;
		if(sscanf(cur_bgr_line, "%s %d %d %lf", cur_chrom, &start, &end, &cur_sig) != 4)
		{
			fprintf(stderr, "Could not parse bgr file line: %s\n", cur_bgr_line);
			exit(0);
		}

		int trans_start = translate_coord(start, BED_COORDS::start_base, CODEBASE_COORDS::start_base);
		int trans_end = translate_coord(end, BED_COORDS::end_base, CODEBASE_COORDS::end_base);
		for(int i_nuc = trans_start; i_nuc <= trans_end; i_nuc++)
		{
			if(i_nuc > l_max)
			{
				fprintf(stderr, "Cannot set the position, greater than l_max: %d (%s)\n", i_nuc, cur_bgr_line);
				exit(0);
			}
			signal_profile[i_nuc] = cur_sig;
		} // i_nuc loop.

		delete [] cur_bgr_line;
	} // file reading loop.	

	// Close the bedgraph file.
	if(!t_string::compare_strings(bgr_fp, "stdin"))
	{
		fclose(f_bgr);
	}

	// Get the end of the signal.
	int l_data = l_max;
	while(signal_profile[l_data] == 0.0)
	{
		l_data--;
	} // i_nuc loop.

	fprintf(stderr, "Signal length is %d, dumping the per nucleotide profile.\n", l_data);

	if(dump_binary)
	{
		dump_per_nucleotide_binary_profile(signal_profile, l_data, op_fp);
		return;
	}

	// Dump the per nucleotide signal profile.
	FILE* f_op = NULL;
	
	fprintf(stderr, "Dumping ASCII.\n");
	f_op = open_f(op_fp, "w");
	
	fprintf(f_op, "%d\n",  l_data);
	
	for(int i_nuc = 1; i_nuc <= l_data; i_nuc++)
	{
		if(dump_binary)
		{
			fwrite(&(signal_profile[i_nuc]), sizeof(double), 1, f_op);
		}
		else
		{
			fprintf(f_op, "%lf ",  signal_profile[i_nuc]);
		}
	} // i_nuc loop.

	fclose(f_op);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rdx, %r14
movl %esi, 0x4(%rsp)
movq %rdi, %rbx
leaq 0x2855c(%rip), %rsi     # 0x2d2fd
callq 0x26d70
testb %al, %al
movq %rbx, 0x18(%rsp)
je 0x4dbb
movq 0x34222(%rip), %rax     # 0x38fd8
movq (%rax), %r12
jmp 0x4dcd
leaq 0x27f46(%rip), %rsi     # 0x2cd08
movq %rbx, %rdi
callq 0x25d5a
movq %rax, %r12
movl $0x8f0d1808, %edi       # imm = 0x8F0D1808
callq 0x2040
movq %rax, %rbx
movl $0x8f0d1808, %edx       # imm = 0x8F0D1808
movq %rax, %rdi
xorl %esi, %esi
callq 0x20f0
movq 0x341f0(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x291e8(%rip), %rsi     # 0x2dfe2
movq %r14, 0x10(%rsp)
movq %r14, %rdx
xorl %eax, %eax
callq 0x2250
movq %r12, %rdi
callq 0x25c1e
testq %rax, %rax
je 0x4ec1
movq %rax, %r13
leaq 0x8(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0xc(%rsp), %r15
leaq 0x8(%rsp), %rbp
leaq 0x28(%rsp), %r14
movq %r13, %rdi
leaq 0x291bf(%rip), %rsi     # 0x2dffe
leaq 0x30(%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x2160
cmpl $0x4, %eax
jne 0x4fe4
movslq 0xc(%rsp), %rax
movslq 0x8(%rsp), %rcx
cmpl %ecx, %eax
jge 0x4ea5
movsd 0x28(%rsp), %xmm0
cmpl $0x11e1a301, %eax       # imm = 0x11E1A301
movl $0x11e1a300, %edx       # imm = 0x11E1A300
cmovgel %eax, %edx
subq %rax, %rdx
subq %rax, %rcx
movq 0x20(%rsp), %rsi
leaq (%rsi,%rax,8), %rdi
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x4fbd
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
cmpq %rsi, %rcx
jne 0x4e8f
movq %r13, %rdi
callq 0x2220
movq %r12, %rdi
callq 0x25c1e
movq %rax, %r13
testq %rax, %rax
jne 0x4e35
leaq 0x28435(%rip), %rsi     # 0x2d2fd
movq 0x18(%rsp), %rdi
callq 0x26d70
testb %al, %al
jne 0x4ede
movq %r12, %rdi
callq 0x2170
movl $0x11e1a302, %ebp       # imm = 0x11E1A302
movl $0x11e1a301, %r14d      # imm = 0x11E1A301
xorpd %xmm0, %xmm0
movq 0x340ec(%rip), %r15     # 0x38fe0
movsd -0x8(%rbx,%r14,8), %xmm1
decl %ebp
decq %r14
ucomisd %xmm0, %xmm1
jne 0x4f08
jnp 0x4ef4
movq (%r15), %rdi
leaq 0x29152(%rip), %rsi     # 0x2e064
movl %r14d, %edx
xorl %eax, %eax
callq 0x2250
cmpb $0x0, 0x4(%rsp)
je 0x4f45
movq %rbx, %rdi
movl %r14d, %esi
movq 0x10(%rsp), %rdx
callq 0x1d226
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rcx
leaq 0x2914f(%rip), %rdi     # 0x2e09e
pushq $0xf
popq %rsi
pushq $0x1
popq %r15
movq %r15, %rdx
callq 0x2340
leaq 0x25b43(%rip), %rsi     # 0x2aaa8
movq 0x10(%rsp), %rdi
callq 0x25d5a
movq %rax, %r12
leaq 0x2877d(%rip), %rsi     # 0x2d6f6
movq %rax, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2250
testq %r14, %r14
jle 0x4fb0
movl %ebp, %r13d
leaq 0x287b8(%rip), %r14     # 0x2d74d
movsd (%rbx,%r15,8), %xmm0
movq %r12, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x2250
incq %r15
cmpq %r15, %r13
jne 0x4f95
movq %r12, %rdi
callq 0x2170
jmp 0x4f33
leal (%rax,%rsi), %edx
incl %edx
movq 0x34017(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x2905b(%rip), %rsi     # 0x2e02e
movq %r13, %rcx
xorl %eax, %eax
callq 0x2250
xorl %edi, %edi
callq 0x21e0
movq 0x33ff5(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x29016(%rip), %rsi     # 0x2e00b
movq %r13, %rdx
xorl %eax, %eax
callq 0x2250
jmp 0x4fdd