// File: 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:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movl %esi, 0x4c(%rsp)
movq %rdx, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x38(%rsp), %rdi
callq 0x455a0
testb $0x1, %al
jne 0x34c00
jmp 0x34c35
movq 0x303d9(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0x38(%rsp), %rdx
leaq 0x21776(%rip), %rsi     # 0x5638c
movb $0x0, %al
callq 0x2250
movq 0x38(%rsp), %rdi
leaq 0x1eca4(%rip), %rsi     # 0x538cd
callq 0x455f0
movq %rax, 0x30(%rsp)
jmp 0x34c4b
movq 0x38(%rsp), %rdi
leaq 0x1de63(%rip), %rsi     # 0x52aa4
callq 0x455f0
movq %rax, 0x30(%rsp)
movl $0x1, 0x2c(%rsp)
movq 0x50(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x34c7d
movl 0x2c(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jg 0x34cb1
movsd 0x20(%rsp), %xmm0
movq 0x50(%rsp), %rax
movslq 0x2c(%rsp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jne 0x34ca2
jp 0x34ca2
jmp 0x34ca4
jmp 0x34cb1
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x34c7d
movsd 0x20(%rsp), %xmm0
movq 0x50(%rsp), %rax
movslq 0x2c(%rsp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jne 0x34ccc
jp 0x34ccc
jmp 0x34d49
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x1c(%rsp), %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1fd20
movl %eax, 0x18(%rsp)
movl 0x2c(%rsp), %edi
subl $0x1, %edi
movl $0x1, %edx
movl %edx, %esi
callq 0x1fd20
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rdx
movl 0x18(%rsp), %ecx
movl %eax, %r8d
movsd 0x20(%rsp), %xmm0
leaq 0x2167f(%rip), %rsi     # 0x563a4
movb $0x1, %al
callq 0x2250
movq 0x50(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jle 0x34d55
jmp 0x34d5a
jmp 0x34c7b
movq 0x30(%rsp), %rdi
callq 0x2180
addq $0x58, %rsp
retq
nopl (%rax)