// File: 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:
subq $0x468, %rsp            # imm = 0x468
movb %sil, %al
movq %rdi, 0x460(%rsp)
andb $0x1, %al
movb %al, 0x45f(%rsp)
movq %rdx, 0x450(%rsp)
movq $0x0, 0x448(%rsp)
movq 0x460(%rsp), %rdi
leaq 0x2090f(%rip), %rsi     # 0x556bd
callq 0x47ac0
testb $0x1, %al
jne 0x34db9
jmp 0x34dcd
movq 0x30218(%rip), %rax     # 0x64fd8
movq (%rax), %rax
movq %rax, 0x448(%rsp)
jmp 0x34de9
movq 0x460(%rsp), %rdi
leaq 0x202f2(%rip), %rsi     # 0x550ce
callq 0x455f0
movq %rax, 0x448(%rsp)
movl $0x11e1a300, 0x444(%rsp) # imm = 0x11E1A300
movl 0x444(%rsp), %eax
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x438(%rsp)
movl $0x0, 0x434(%rsp)
movl 0x434(%rsp), %eax
cmpl 0x444(%rsp), %eax
jg 0x34e6f
movq 0x438(%rsp), %rax
movslq 0x434(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x434(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x434(%rsp)
jmp 0x34e34
movq 0x3016a(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0x450(%rsp), %rdx
leaq 0x2152a(%rip), %rsi     # 0x563b2
movb $0x0, %al
callq 0x2250
movq 0x448(%rsp), %rdi
callq 0x45400
movq %rax, 0x428(%rsp)
cmpq $0x0, 0x428(%rsp)
jne 0x34eb4
jmp 0x34fc3
movq 0x428(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x21506(%rip), %rsi     # 0x563ce
leaq 0x3c(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
movb $0x0, %al
callq 0x2170
cmpl $0x4, %eax
je 0x34f0a
movq 0x300f6(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0x428(%rsp), %rdx
leaq 0x214df(%rip), %rsi     # 0x563db
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl 0x3c(%rsp), %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1fd20
movl %eax, 0x2c(%rsp)
movl 0x38(%rsp), %edi
movl $0x1, %edx
movl %edx, %esi
callq 0x1fd20
movl %eax, 0x28(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x28(%rsp), %eax
jg 0x34fa1
movl 0x24(%rsp), %eax
cmpl 0x444(%rsp), %eax
jle 0x34f7c
movq 0x30088(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movl 0x24(%rsp), %edx
movq 0x428(%rsp), %rcx
leaq 0x21490(%rip), %rsi     # 0x563fe
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movsd 0x30(%rsp), %xmm0
movq 0x438(%rsp), %rax
movslq 0x24(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x34f3a
movq 0x428(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x34fbe
movq 0x8(%rsp), %rdi
callq 0x2220
jmp 0x34e8f
movq 0x460(%rsp), %rdi
leaq 0x206eb(%rip), %rsi     # 0x556bd
callq 0x47ac0
testb $0x1, %al
jne 0x34fe8
movq 0x448(%rsp), %rdi
callq 0x2180
movl 0x444(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x438(%rsp), %rax
movslq 0x20(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3501d
jp 0x3501d
movl 0x20(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x34ff3
movq 0x2ffbc(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movl 0x20(%rsp), %edx
leaq 0x21402(%rip), %rsi     # 0x56434
movb $0x0, %al
callq 0x2250
testb $0x1, 0x45f(%rsp)
je 0x35061
movq 0x438(%rsp), %rdi
movl 0x20(%rsp), %esi
movq 0x450(%rsp), %rdx
callq 0x35140
jmp 0x35136
movq $0x0, 0x18(%rsp)
movq 0x2ff6f(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x213f3(%rip), %rsi     # 0x5646e
movb $0x0, %al
callq 0x2250
movq 0x450(%rsp), %rdi
leaq 0x1da13(%rip), %rsi     # 0x52aa4
callq 0x455f0
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movl 0x20(%rsp), %edx
leaq 0x20a0b(%rip), %rsi     # 0x55ab6
movb $0x0, %al
callq 0x2250
movl $0x1, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x20(%rsp), %eax
jg 0x3512c
testb $0x1, 0x45f(%rsp)
je 0x350f8
movq 0x438(%rsp), %rdi
movslq 0x14(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq 0x18(%rsp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x2360
jmp 0x3511d
movq 0x18(%rsp), %rdi
movq 0x438(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x209f7(%rip), %rsi     # 0x55b0d
movb $0x1, %al
callq 0x2250
jmp 0x3511f
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x350ba
movq 0x18(%rsp), %rdi
callq 0x2180
addq $0x468, %rsp            # imm = 0x468
retq
nop