// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movsd %xmm1, 0x28(%rsp)
movapd %xmm0, 0x10(%rsp)
leaq 0xe369(%rip), %rdi      # 0x2bfcf
leaq 0xf09b(%rip), %rsi      # 0x2cd08
callq 0x25d5a
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rax, %rdx
callq 0x22a0
testq %rax, %rax
je 0x1dd57
leaq 0x60(%rsp), %r15
leaq 0x58(%rsp), %r13
leaq 0x50(%rsp), %r14
leaq 0x48(%rsp), %r12
movq %r15, %rdi
leaq 0x104ab(%rip), %rsi     # 0x2e159
leaq 0xc(%rsp), %rdx
movq %r13, %rcx
movq %r14, %r8
movq %r12, %r9
xorl %eax, %eax
leaq 0x38(%rsp), %r10
pushq %r10
leaq 0x48(%rsp), %r10
pushq %r10
callq 0x2160
addq $0x10, %rsp
cmpl $0x6, %eax
jne 0x1dda8
movl 0xc(%rsp), %ebp
movsd 0x38(%rsp), %xmm2
ucomisd 0x30(%rsp), %xmm2
movsd 0x48(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
jb 0x1dd14
movapd 0x10(%rsp), %xmm3
ucomisd %xmm0, %xmm3
jbe 0x1dd14
movapd 0x10(%rsp), %xmm3
ucomisd %xmm1, %xmm3
ja 0x1dd83
ucomisd 0x28(%rsp), %xmm2
jbe 0x1dd3c
movapd 0x10(%rsp), %xmm4
movapd %xmm4, %xmm3
cmpltpd %xmm1, %xmm3
cmpltpd %xmm0, %xmm4
orpd %xmm3, %xmm4
movd %xmm4, %eax
testb $0x1, %al
jne 0x1dd5e
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdx
callq 0x22a0
testq %rax, %rax
jne 0x1dca4
jmp 0x1dd67
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x1dd67
cmpb $0x1, 0x1b46b(%rip)     # 0x391d0
je 0x1dd8c
movq %rbx, %rdi
callq 0x2170
movl %ebp, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x1b446(%rip)     # 0x391d0
je 0x1dd67
movq 0x1b24d(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x103d3(%rip), %rsi     # 0x2e170
movl %ebp, %edx
movb $0x3, %al
callq 0x2250
jmp 0x1dd67
leaq 0x60(%rsp), %rdi
callq 0x509d