// File: /workspace/github_cpp_makefile_star2_O3/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 0x25d35
testb %al, %al
jne 0x1d1ff
leaq 0xd94e(%rip), %rsi      # 0x2aaa8
movq %r15, %rdi
callq 0x25d5a
movq %rax, 0x8(%rsp)
movsd 0x8(%r14), %xmm0
movslq %ebp, %r13
leal 0x1(%r13), %eax
movl %eax, 0x4(%rsp)
movl $0x1, %ecx
movl $0x2, %r12d
cmpl %ebp, %r12d
jg 0x1d1a9
movslq %r12d, %rax
decq %rax
ucomisd 0x8(%r14,%rax,8), %xmm0
jne 0x1d1ad
jp 0x1d1ad
incl %r12d
incq %rax
cmpq %r13, %rax
jl 0x1d18e
movl 0x4(%rsp), %r12d
movb $0x1, %bl
jmp 0x1d1af
xorl %ebx, %ebx
movslq %r12d, %r15
ucomisd (%r14,%r15,8), %xmm0
jne 0x1d1bc
jnp 0x1d1e3
decl %ecx
leal -0x1(%r15), %r8d
movq 0x8(%rsp), %rdi
leaq 0x10e06(%rip), %rsi     # 0x2dfd4
movq 0x10(%rsp), %rdx
movb $0x1, %al
callq 0x2250
movsd (%r14,%r15,8), %xmm0
movl %r12d, %ecx
testb %bl, %bl
je 0x1d183
movq 0x8(%rsp), %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2170
movq 0x1bdda(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x10dac(%rip), %rsi     # 0x2dfbc
movq %r15, %rdx
xorl %eax, %eax
callq 0x2250
leaq 0xe651(%rip), %rsi      # 0x2b872
jmp 0x1d15a