// File: /workspace/github_cpp_makefile_star2_O2/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, %ebx
movq %rdi, %r15
leaq 0xe73e(%rip), %rsi      # 0x272fd
callq 0x2100e
testb %al, %al
movl %ebx, 0xc(%rsp)
movq %r15, 0x20(%rsp)
je 0x18bdd
movq 0x1c400(%rip), %rax     # 0x34fd8
movq (%rax), %r12
jmp 0x18bef
leaq 0xe124(%rip), %rsi      # 0x26d08
movq %r15, %rdi
callq 0x2027c
movq %rax, %r12
movl $0x8f0d1808, %edi       # imm = 0x8F0D1808
callq 0x2040
movq %rax, %rbx
xorl %eax, %eax
cmpq $0x11e1a301, %rax       # imm = 0x11E1A301
je 0x18c10
andq $0x0, (%rbx,%rax,8)
incq %rax
jmp 0x18bfe
movq 0x1c3c9(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xf3c1(%rip), %rsi      # 0x27fe2
movq %r14, 0x18(%rsp)
movq %r14, %rdx
xorl %eax, %eax
callq 0x2240
leaq 0x14(%rsp), %r15
leaq 0x10(%rsp), %rbp
leaq 0x28(%rsp), %r14
movq %r12, %rdi
callq 0x2017f
testq %rax, %rax
je 0x18cb2
movq %rax, %r13
movq %rax, %rdi
leaq 0xf3a5(%rip), %rsi      # 0x27ffe
leaq 0x30(%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x2150
cmpl $0x4, %eax
jne 0x18de0
movslq 0x14(%rsp), %rax
movslq 0x10(%rsp), %rcx
movsd 0x28(%rsp), %xmm0
leal 0x1(%rax), %edx
cmpq %rcx, %rax
jge 0x18ca8
cmpq $0x11e1a300, %rax       # imm = 0x11E1A300
jge 0x18dbe
movsd %xmm0, 0x8(%rbx,%rax,8)
incl %edx
incq %rax
jmp 0x18c8a
movq %r13, %rdi
callq 0x2210
jmp 0x18c3f
leaq 0xe644(%rip), %rsi      # 0x272fd
movq 0x20(%rsp), %rdi
callq 0x2100e
testb %al, %al
jne 0x18ccf
movq %r12, %rdi
callq 0x2160
movl $0x11e1a300, %r15d      # imm = 0x11E1A300
movabsq $0x11e1a30100000000, %r13 # imm = 0x11E1A30100000000
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
xorpd %xmm0, %xmm0
movq 0x1c2ec(%rip), %r14     # 0x34fe0
addq %rax, %r13
movsd (%rbx,%r15,8), %xmm1
decq %r15
ucomisd %xmm0, %xmm1
jne 0x18d08
jnp 0x18cf4
incq %r15
movq (%r14), %rdi
leaq 0xf34f(%rip), %rsi      # 0x28064
movl %r15d, %edx
xorl %eax, %eax
callq 0x2240
cmpb $0x0, 0xc(%rsp)
je 0x18d48
movq %rbx, %rdi
movl %r15d, %esi
movq 0x18(%rsp), %rdx
callq 0x18dfd
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rcx
leaq 0xf34c(%rip), %rdi      # 0x2809e
pushq $0xf
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x2330
leaq 0xbd40(%rip), %rsi      # 0x24aa8
movq 0x18(%rsp), %rdi
callq 0x2027c
movq %rax, %r12
leaq 0xe97a(%rip), %rsi      # 0x276f6
movq %rax, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x2240
sarq $0x20, %r13
leaq 0xe9b9(%rip), %r15      # 0x2774d
cmpq %r13, %r14
jg 0x18db1
movsd (%rbx,%r14,8), %xmm0
movq %r12, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x2240
incq %r14
jmp 0x18d94
movq %r12, %rdi
callq 0x2160
jmp 0x18d36
movq 0x1c21b(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xf25f(%rip), %rsi      # 0x2802e
movq %r13, %rcx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21d0
movq 0x1c1f9(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xf21a(%rip), %rsi      # 0x2800b
movq %r13, %rdx
xorl %eax, %eax
callq 0x2240
jmp 0x18dd9