// File: /workspace/github_cpp_makefile_star2_O1/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 0x26362
testb %al, %al
jne 0x1d697
leaq 0xe4c8(%rip), %rsi      # 0x2baae
movq %r15, %rdi
callq 0x26387
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
setg %bl
jle 0x1d651
movslq %r12d, %r15
ucomisd (%r14,%r15,8), %xmm0
jne 0x1d624
jnp 0x1d64b
decl %ecx
leal -0x1(%r15), %r8d
movq 0x8(%rsp), %rdi
leaq 0x1199e(%rip), %rsi     # 0x2efd4
movq 0x10(%rsp), %rdx
movb $0x1, %al
callq 0x2250
movsd (%r14,%r15,8), %xmm0
movl %r12d, %ecx
testb %bl, %bl
je 0x1d60f
jmp 0x1d67f
movslq %r12d, %rax
ucomisd (%r14,%rax,8), %xmm0
jne 0x1d617
jp 0x1d617
cmpq %r13, %rax
setge %bl
jge 0x1d676
leaq 0x1(%rax), %r12
ucomisd 0x8(%r14,%rax,8), %xmm0
movq %r12, %rax
jmp 0x1d65a
movl 0x4(%rsp), %eax
movl %eax, %r12d
jmp 0x1d617
movq 0x8(%rsp), %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2170
movq 0x1c942(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x11914(%rip), %rsi     # 0x2efbc
movq %r15, %rdx
xorl %eax, %eax
callq 0x2250
leaq 0xf1bf(%rip), %rsi      # 0x2c878
jmp 0x1d5e6