// File: /workspace/github_cpp_makefile_star2_O2/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 0xc9ad(%rip), %rdi      # 0x25fcf
leaq 0xd6df(%rip), %rsi      # 0x26d08
callq 0x2027c
movq %rax, %rbx
pushq $-0x1
popq %rbp
leaq 0x60(%rsp), %r14
leaq 0x58(%rsp), %r13
leaq 0x50(%rsp), %r15
leaq 0x48(%rsp), %r12
movq %r14, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdx
callq 0x2290
testq %rax, %rax
je 0x19708
movq %r14, %rdi
leaq 0xeaee(%rip), %rsi      # 0x28159
leaq 0xc(%rsp), %rdx
movq %r13, %rcx
movq %r15, %r8
movq %r12, %r9
xorl %eax, %eax
leaq 0x38(%rsp), %r10
pushq %r10
leaq 0x48(%rsp), %r10
pushq %r10
callq 0x2150
popq %rcx
popq %rdx
cmpl $0x6, %eax
jne 0x19749
movl 0xc(%rsp), %ebp
movsd 0x38(%rsp), %xmm2
ucomisd 0x30(%rsp), %xmm2
movsd 0x48(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
jb 0x196cf
movapd 0x10(%rsp), %xmm3
ucomisd %xmm0, %xmm3
jbe 0x196cf
movapd 0x10(%rsp), %xmm3
ucomisd %xmm1, %xmm3
ja 0x19724
ucomisd 0x28(%rsp), %xmm2
jbe 0x19648
movapd 0x10(%rsp), %xmm4
movapd %xmm4, %xmm3
cmpltpd %xmm1, %xmm3
cmpltpd %xmm0, %xmm4
orpd %xmm3, %xmm4
movd %xmm4, %eax
testb $0x1, %al
je 0x19648
cmpb $0x1, 0x1bac2(%rip)     # 0x351c8
je 0x1972d
movq %rbx, %rdi
callq 0x2160
movl %ebp, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x1ba9d(%rip)     # 0x351c8
je 0x19708
movq 0x1b8ac(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xea32(%rip), %rsi      # 0x28170
movl %ebp, %edx
movb $0x3, %al
callq 0x2240
jmp 0x19708
movq 0x1b890(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xb686(%rip), %rsi      # 0x24de0
leaq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21d0