// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movsd %xmm2, 0x40(%rsp)
movsd %xmm1, 0x38(%rsp)
movapd %xmm0, 0x20(%rsp)
leaq 0xeed8(%rip), %rdi      # 0x2cfd5
leaq 0xfc04(%rip), %rsi      # 0x2dd08
callq 0x26387
movq %rax, %rbx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x60(%rsp), %r14
leaq 0x58(%rsp), %r13
leaq 0x50(%rsp), %rbp
leaq 0x18(%rsp), %r15
movq %r14, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdx
callq 0x22a0
testq %rax, %rax
je 0x1e1f0
movq %r14, %rdi
leaq 0x11010(%rip), %rsi     # 0x2f159
leaq 0xc(%rsp), %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %r15, %r9
xorl %eax, %eax
leaq 0x48(%rsp), %r10
pushq %r10
leaq 0x18(%rsp), %r10
pushq %r10
callq 0x2160
addq $0x10, %rsp
cmpl $0x6, %eax
jne 0x1e236
movl 0xc(%rsp), %r12d
movsd 0x48(%rsp), %xmm2
ucomisd 0x40(%rsp), %xmm2
jb 0x1e1b0
movsd 0x18(%rsp), %xmm0
movapd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1e1b0
movsd 0x10(%rsp), %xmm1
movapd 0x20(%rsp), %xmm3
ucomisd %xmm1, %xmm3
ja 0x1e1e7
ucomisd 0x38(%rsp), %xmm2
movb $0x1, %al
jbe 0x1e1f2
movsd 0x18(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movapd 0x20(%rsp), %xmm4
movapd %xmm4, %xmm3
cmpltpd %xmm0, %xmm3
cmpltpd %xmm1, %xmm4
orpd %xmm3, %xmm4
movd %xmm4, %ecx
testb $0x1, %cl
je 0x1e1f2
cmpb $0x1, 0x1bfe2(%rip)     # 0x3a1d0
je 0x1e1fc
xorl %eax, %eax
testb %al, %al
jne 0x1e126
jmp 0x1e219
movq 0x1bddd(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0x10f63(%rip), %rsi     # 0x2f170
movl %r12d, %edx
movb $0x3, %al
callq 0x2250
jmp 0x1e1f0
movq %rbx, %rdi
callq 0x2170
movl %r12d, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x60(%rsp), %rdi
callq 0x5200