// File: gersteinlab[P]MUSIC/src/ms_filter_utils.cpp
// Function: mapability_aware_median_filter(double*, int, double*, int, double, int)
double* mapability_aware_median_filter(double* signal_profile, int l_profile,
	double* scaled_mapability_profile, int l_mapability_profile,
	double max_mapable_signal_2_use_in_filter,
	int l_mapability_filtering_win)
{
	// Generate the signal profile to input to the filtering.
	double* input_signal_profile = new double[l_profile + 2];
	for(int i = 1; i <= l_profile; i++)
	{
		if(l_mapability_profile > i)
		{
			if(scaled_mapability_profile[i] > max_mapable_signal_2_use_in_filter)
			{
				input_signal_profile[i] = -1;
			}
			else
			{
				input_signal_profile[i] = signal_profile[i];
			}
		}
		else
		{
			input_signal_profile[i] = signal_profile[i];
		}
	} // i loop.

if(__DUMP_FILTER_MSGS__)
{
	fprintf(stderr, "Mapability aware filtering.\n");
}

	double* mapable_median_profile = median_filter_data(input_signal_profile,
													l_profile, 
													l_mapability_filtering_win,
													-1);

	double* filtered_signal = new double[l_profile + 5];

	// Compare the actual signal profile with the 
	for(int i = 1; i <= l_profile; i++)
	{
		filtered_signal[i] = MAX(mapable_median_profile[i], signal_profile[i]);
	} // i loop.

	delete [] mapable_median_profile;
	delete [] input_signal_profile;

	return(filtered_signal);
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movl %esi, 0x6c(%rsp)
movq %rdx, 0x60(%rsp)
movl %ecx, 0x5c(%rsp)
movsd %xmm0, 0x50(%rsp)
movl %r8d, 0x4c(%rsp)
movl 0x6c(%rsp), %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, 0x40(%rsp)
movl $0x1, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jg 0x25470
movl 0x5c(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jle 0x25440
movq 0x60(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x50(%rsp), %xmm0
jbe 0x25420
movq 0x40(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movsd 0x2cc3f(%rip), %xmm0    # 0x52058
movsd %xmm0, (%rax,%rcx,8)
jmp 0x2543e
movq 0x70(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x2545e
movq 0x70(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x25460
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x253d8
testb $0x1, 0x3fd66(%rip)     # 0x651dd
je 0x25491
movq 0x3fb60(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x30688(%rip), %rsi     # 0x55b12
movb $0x0, %al
callq 0x2250
movq 0x40(%rsp), %rdi
movl 0x6c(%rsp), %esi
movl 0x4c(%rsp), %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x22930
movq %rax, 0x30(%rsp)
movl 0x6c(%rsp), %eax
addl $0x5, %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, 0x28(%rsp)
movl $0x1, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jg 0x2555c
movq 0x30(%rsp), %rax
movslq 0x24(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x70(%rsp), %rax
movslq 0x24(%rsp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x25525
movq 0x30(%rsp), %rax
movslq 0x24(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x2553a
movq 0x70(%rsp), %rax
movslq 0x24(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
movq 0x28(%rsp), %rax
movslq 0x24(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x254e4
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x25576
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x25590
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x28(%rsp), %rax
addq $0x78, %rsp
retq
nopw (%rax,%rax)