// File: gersteinlab[P]MUSIC/src/ms_peak_calling_utils.cpp
// Function: load_normalized_multimappability_profile(char*, int&)
double* load_normalized_multimappability_profile(char* mapability_signal_profile_fp, int& l_mapability_profile)
{
	double* mapability_signal_profile = NULL;

	if(!check_file(mapability_signal_profile_fp))
	{
		l_mapability_profile = 0;
		return(NULL);
	}

#ifdef __DOUBLE_MAPPABILITY__
	// Load the mapability map signal profile, do filtering.
	mapability_signal_profile = load_per_nucleotide_binary_profile(mapability_signal_profile_fp, l_mapability_profile);

	// Do mapability aware median filtering on the current signal profile.
if(__DUMP_PEAK_CALLING_UTILS_MSGS__)
	fprintf(stderr, "Scaling the mapability map with %d.\n", l_read_mapability_signal * 2);

	int mapability_scaling = l_read * 2;
	for(int i = 1; i <= l_mapability_profile; i++)
	{
		mapability_signal_profile[i] /= mapability_scaling;
	} // i loop.
#elif defined(__UCHAR_MAPPABILITY__)
	// Following loads the mappability signal profile from the char version of the multi-mappability profile.
	// Load the mapability map signal profile, do filtering.
	unsigned char* mapability_signal_char_profile = load_per_nucleotide_binary_uchar_profile(mapability_signal_profile_fp, l_mapability_profile);
	mapability_signal_profile = new double[l_mapability_profile + 2];
	for(int i = 1; i <= l_mapability_profile; i++)
	{
		unsigned char unsigned_char_val = (unsigned char)(mapability_signal_char_profile[i]);
		mapability_signal_profile[i] = (double)(unsigned_char_val);
		mapability_signal_profile[i] /= 100;

		if(mapability_signal_profile[i] < 0)
		{
			fprintf(stderr, "Sanity check failed.\n");
			exit(0);
		}
	} // i loop.
	delete [] mapability_signal_char_profile;
#else
	#error "Must define the type of mappability."
#endif

	return(mapability_signal_profile);
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x28(%rsp), %rdi
callq 0x455a0
testb $0x1, %al
jne 0x138ee
movq 0x20(%rsp), %rax
movl $0x0, (%rax)
movq $0x0, 0x30(%rsp)
jmp 0x139fd
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x34400
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rax
movl (%rax), %eax
addl $0x2, %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, 0x18(%rsp)
movl $0x1, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl (%rcx), %eax
jg 0x139db
movq 0x10(%rsp), %rax
movslq 0xc(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, 0xb(%rsp)
movzbl 0xb(%rsp), %eax
cvtsi2sd %eax, %xmm0
movq 0x18(%rsp), %rax
movslq 0xc(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
movslq 0xc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x3e69b(%rip), %xmm1    # 0x52028
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
movslq 0xc(%rsp), %rcx
xorps %xmm0, %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x139c9
movq 0x5162f(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x41551(%rip), %rsi     # 0x54f0c
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
jmp 0x139cb
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x1393c
movq 0x10(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x139f3
movq (%rsp), %rdi
callq 0x2220
movq 0x18(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq $0x38, %rsp
retq
nopw (%rax,%rax)