// File: gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: select_l_p_per_stats_file(char const*, double, double, double)
int select_l_p_per_stats_file(const char* l_p_param_stats_fp, double target_max_p_val_fc_fpr, double target_min_sensitivity, 
	double min_sensitivity_per_satisfying_FPR)
{
	/******************************************************************
	TODO::Must make sure that this function returns a reasonable 
	window length, always. It is guaranteed to return a window length,
	but not a reasonable one.
	******************************************************************/

	FILE* f_l_p_param_stats = open_f("l_p_param_stats.txt", "r"); // This is the file to be read by MUSIC to select the l_p parameter.
	int satisfying_l_win = -1;
	while(1)
	{
/*
		double fc_FNR =			n_insig_pval_sig_FC_wins_per_l_win[l_win] / n_sig_FC_wins_per_l_win[l_win];
		double p_val_FNR =		n_insig_pval_sig_FC_wins_per_l_win[l_win] / n_insig_pval_wins_per_l_win[l_win];
		double fc_FPR =			n_sig_pval_insig_FC_wins_per_l_win[l_win] / n_insig_FC_wins_per_l_win[l_win];
		double p_val_FPR =		n_sig_pval_insig_FC_wins_per_l_win[l_win] / n_sig_pval_wins_per_l_win[l_win];
		double sensitivity =	n_sig_pval_sig_FC_wins_per_l_win[l_win] / n_sig_FC_wins_per_l_win[l_win];			

		fprintf(f_l_p_param_stats, "%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n", l_win,
			fc_FNR, p_val_FNR, 
			fc_FPR, p_val_FPR,
			sensitivity);
*/
		char cur_line[1000];
		if(fgets(cur_line, 1000, f_l_p_param_stats) == NULL)
		{
			break;
		}

		int l_win;
		double fc_FNR;
		double p_val_FNR;
		double fc_FPR;
		double p_val_FPR;
		double sensitivity;

		if(sscanf(cur_line, "%d %lf %lf %lf %lf %lf", &l_win, &fc_FNR, &p_val_FNR, &fc_FPR, &p_val_FPR, &sensitivity) != 6)
		{
			fprintf(stderr, "Could not parse: %s\n", cur_line);
			exit(0);
		}

		// Moving on increases sensitivity and also FPRs. Following checks whether we should continue to increase l_win at the 
		// expense of increasing FPR.
		satisfying_l_win = l_win;

		// If we are already at very high sensitivity with very low FPR's then return the window.
		if(sensitivity >= min_sensitivity_per_satisfying_FPR && 
			fc_FPR < target_max_p_val_fc_fpr && 
			p_val_FPR < target_max_p_val_fc_fpr)
		{
if(__DUMP_SIGNAL_ENRICHMENT_MSGS__)
{
			fprintf(stderr, "Selecting l_win = %d @ FPR: %.3f, %.3f; Sensitivity: %.3f\n", l_win, fc_FPR, p_val_FPR, sensitivity);
}

			break;
		}

		// If we are over the maximum FPR level and we have the minimum sensitivity, return. This may have high FPR since we are 
		// enforcing sensitivity requirement. 
		if((fc_FPR > target_max_p_val_fc_fpr || p_val_FPR > target_max_p_val_fc_fpr) && 
			(sensitivity > target_min_sensitivity))
		{
if(__DUMP_SIGNAL_ENRICHMENT_MSGS__)
{
			fprintf(stderr, "Selecting l_win = %d @ FPR: %.3f, %.3f; Sensitivity: %.3f\n", l_win, fc_FPR, p_val_FPR, sensitivity);
}

			break;
		}
	} // file reading loop.
	fclose(f_l_p_param_stats);	

	// Return the window length.
	return(satisfying_l_win);
}

// Assembly:
subq $0x458, %rsp            # imm = 0x458
movq %rdi, 0x450(%rsp)
movsd %xmm0, 0x448(%rsp)
movsd %xmm1, 0x440(%rsp)
movsd %xmm2, 0x438(%rsp)
leaq 0x1e37a(%rip), %rdi     # 0x5414b
leaq 0x1f2f6(%rip), %rsi     # 0x550ce
callq 0x455f0
movq %rax, 0x430(%rsp)
movl $0xffffffff, 0x42c(%rsp) # imm = 0xFFFFFFFF
leaq 0x40(%rsp), %rdi
movq 0x430(%rsp), %rdx
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x22a0
cmpq $0x0, %rax
jne 0x35e12
jmp 0x35f65
leaq 0x40(%rsp), %rdi
leaq 0x206e7(%rip), %rsi     # 0x56505
leaq 0x3c(%rsp), %rdx
leaq 0x30(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x20(%rsp), %r9
leaq 0x18(%rsp), %r10
leaq 0x10(%rsp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2170
cmpl $0x6, %eax
je 0x35e75
movq 0x2f188(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x1cf75(%rip), %rsi     # 0x52ddc
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl 0x3c(%rsp), %eax
movl %eax, 0x42c(%rsp)
movsd 0x10(%rsp), %xmm0
ucomisd 0x438(%rsp), %xmm0
jb 0x35ef4
movsd 0x20(%rsp), %xmm1
movsd 0x448(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x35ef4
movsd 0x18(%rsp), %xmm1
movsd 0x448(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x35ef4
testb $0x1, 0x2f31e(%rip)     # 0x651e0
je 0x35ef2
movq 0x2f115(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movl 0x3c(%rsp), %edx
movsd 0x20(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
leaq 0x20631(%rip), %rsi     # 0x5651c
movb $0x3, %al
callq 0x2250
jmp 0x35f65
movsd 0x20(%rsp), %xmm0
ucomisd 0x448(%rsp), %xmm0
ja 0x35f16
movsd 0x18(%rsp), %xmm0
ucomisd 0x448(%rsp), %xmm0
jbe 0x35f60
movsd 0x10(%rsp), %xmm0
ucomisd 0x440(%rsp), %xmm0
jbe 0x35f60
testb $0x1, 0x2f2b2(%rip)     # 0x651e0
je 0x35f5e
movq 0x2f0a9(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movl 0x3c(%rsp), %edx
movsd 0x20(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
leaq 0x205c5(%rip), %rsi     # 0x5651c
movb $0x3, %al
callq 0x2250
jmp 0x35f65
jmp 0x35df0
movq 0x430(%rsp), %rdi
callq 0x2180
movl 0x42c(%rsp), %eax
addq $0x458, %rsp            # imm = 0x458
retq
nopw %cs:(%rax,%rax)