// File: /workspace/github_cpp_makefile_star2_O1/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 0x293f1(%rip), %rsi     # 0x2e2fd
callq 0x2738a
testb %al, %al
movq %rbx, 0x18(%rsp)
je 0x4f26
movq 0x350b7(%rip), %rax     # 0x39fd8
movq (%rax), %r12
jmp 0x4f38
leaq 0x28ddb(%rip), %rsi     # 0x2dd08
movq %rbx, %rdi
callq 0x26387
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 0x35085(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x2a07d(%rip), %rsi     # 0x2efe2
movq %r14, 0x10(%rsp)
movq %r14, %rdx
xorl %eax, %eax
callq 0x2250
movq %rbx, %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
leaq 0xc(%rsp), %r15
leaq 0x8(%rsp), %rbp
leaq 0x28(%rsp), %r14
movq %r12, %rdi
callq 0x26258
testq %rax, %rax
je 0x5024
movq %rax, %r13
movq %rax, %rdi
leaq 0x2a051(%rip), %rsi     # 0x2effe
leaq 0x30(%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x2160
cmpl $0x4, %eax
jne 0x5147
movslq 0xc(%rsp), %rax
movslq 0x8(%rsp), %rcx
cmpl %ecx, %eax
jge 0x5013
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 0x5120
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
cmpq %rsi, %rcx
jne 0x4ffd
movq %r13, %rdi
callq 0x2220
testq %r13, %r13
jne 0x4f8f
leaq 0x292d2(%rip), %rsi     # 0x2e2fd
movq 0x18(%rsp), %rdi
callq 0x2738a
testb %al, %al
jne 0x5041
movq %r12, %rdi
callq 0x2170
movl $0x11e1a302, %ebp       # imm = 0x11E1A302
movl $0x11e1a301, %r14d      # imm = 0x11E1A301
xorpd %xmm0, %xmm0
movq 0x34f89(%rip), %r15     # 0x39fe0
movsd -0x8(%rbx,%r14,8), %xmm1
decl %ebp
decq %r14
ucomisd %xmm0, %xmm1
jne 0x506b
jnp 0x5057
movq (%r15), %rdi
leaq 0x29fef(%rip), %rsi     # 0x2f064
movl %r14d, %edx
xorl %eax, %eax
callq 0x2250
cmpb $0x0, 0x4(%rsp)
je 0x50a8
movq %rbx, %rdi
movl %r14d, %esi
movq 0x10(%rsp), %rdx
callq 0x1d6be
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rcx
leaq 0x29fec(%rip), %rdi     # 0x2f09e
pushq $0xf
popq %rsi
pushq $0x1
popq %r15
movq %r15, %rdx
callq 0x2340
leaq 0x269e6(%rip), %rsi     # 0x2baae
movq 0x10(%rsp), %rdi
callq 0x26387
movq %rax, %r12
leaq 0x2961a(%rip), %rsi     # 0x2e6f6
movq %rax, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2250
testq %r14, %r14
jle 0x5113
movl %ebp, %r13d
leaq 0x29655(%rip), %r14     # 0x2e74d
movsd (%rbx,%r15,8), %xmm0
movq %r12, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x2250
incq %r15
cmpq %r15, %r13
jne 0x50f8
movq %r12, %rdi
callq 0x2170
jmp 0x5096
leal (%rax,%rsi), %edx
incl %edx
movq 0x34eb4(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x29ef8(%rip), %rsi     # 0x2f02e
movq %r13, %rcx
xorl %eax, %eax
callq 0x2250
xorl %edi, %edi
callq 0x21e0
movq 0x34e92(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x29eb3(%rip), %rsi     # 0x2f00b
movq %r13, %rdx
xorl %eax, %eax
callq 0x2250
jmp 0x5140