// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_signal_track_tools.cpp
// Function: dump_bedGraph_per_per_nucleotide_binary_profile(double*, int, char*, char*)
void dump_bedGraph_per_per_nucleotide_binary_profile(double* signal_profile_buffer, int l_profile, char* chrom, char* op_fp)
{
	FILE* f_op = NULL;
	if(check_file(op_fp))
	{
		fprintf(stderr, "%s exists, concatting.\n", op_fp);
		f_op = open_f(op_fp, "a");
	}
	else
	{
		f_op = open_f(op_fp, "w");
	}

	// Get the bedgraph for the current profile.
	int i_nuc = 1; 
	double cur_height = signal_profile_buffer[i_nuc];
	int cur_block_start_i = i_nuc;
	i_nuc++;
	while(1)
	{
		// Find the point where the height changes: The end goes till it's equal to the profile length since the profile is 1-based.
		while(i_nuc <= l_profile)
		{
			// Wait till there is a change in the height, which marks the start of a new block.
			if(cur_height != signal_profile_buffer[i_nuc])
			{
				break;
			}

			i_nuc++;
		} // i_nuc loop.

		// At this point, either this is the end of the profile, or there was a change in the height, either way, this was the end of the current block. Definitely dump it.
		if(cur_height != signal_profile_buffer[i_nuc])
		{
			// Dump the current block.
			fprintf(f_op, "%s\t%d\t%d\t%lf\n", chrom, 
				translate_coord(cur_block_start_i, CODEBASE_COORDS::start_base, BED_COORDS::start_base), 
				translate_coord(i_nuc-1, CODEBASE_COORDS::end_base, BED_COORDS::end_base), 
				cur_height);

			// Update the new height and new start.
			cur_height = signal_profile_buffer[i_nuc];
			
			// Current position starts the next block.
			cur_block_start_i = i_nuc; 
		}

		// If the above block end was the end of the whole profile, we are done, otherwise continue to the next block.
		if(i_nuc > l_profile)
		{
			break;
		}

		//i_nuc++;
	} // i_nuc loop.

	//fprintf(f_op, "%s\t%d\t%d\t%lf\n", chrom, 
	//			translate_coord(cur_block_start_i, CODEBASE_COORDS::start_base, BED_COORDS::start_base), 
	//			translate_coord(i_nuc-1, CODEBASE_COORDS::end_base, BED_COORDS::end_base), 
	//			cur_height);

	fclose(f_op);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r15
movq %rdx, 0x10(%rsp)
movl %esi, %ebp
movq %rdi, %r14
movq %rcx, %rdi
callq 0x20257
testb %al, %al
jne 0x18b78
leaq 0xbfc8(%rip), %rsi      # 0x24aa8
movq %r15, %rdi
callq 0x2027c
movq %rax, 0x8(%rsp)
movsd 0x8(%r14), %xmm0
movslq %ebp, %r13
leaq 0x1(%r13), %rbp
pushq $0x1
popq %rcx
pushq $0x2
popq %rbx
movslq %ebx, %r12
cmpq %r12, %rbp
movq %r12, %rbx
cmovgq %rbp, %rbx
cmpq %r12, %rbx
je 0x18b24
ucomisd (%r14,%r12,8), %xmm0
jne 0x18b21
jp 0x18b21
incq %r12
jmp 0x18b0d
movq %r12, %rbx
movslq %ebx, %r15
ucomisd (%r14,%r15,8), %xmm0
jne 0x18b31
jnp 0x18b5b
shlq $0x3, %r15
decl %ecx
leal -0x1(%rbx), %r8d
movq 0x8(%rsp), %rdi
leaq 0xf48d(%rip), %rsi      # 0x27fd4
movq 0x10(%rsp), %rdx
movb $0x1, %al
callq 0x2240
movsd (%r14,%r15), %xmm0
movl %ebx, %ecx
cmpq %r13, %r12
jle 0x18b00
movq 0x8(%rsp), %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2160
movq 0x1c461(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xf433(%rip), %rsi      # 0x27fbc
movq %r15, %rdx
xorl %eax, %eax
callq 0x2240
leaq 0xccd8(%rip), %rsi      # 0x25872
jmp 0x18ae0