// 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, double, double)
int select_l_p_per_stats_file(const char* l_p_param_stats_fp, 
	double max_target_fpr, double min_target_sensitivity,
	double min_sensitivity_per_satisfying_fpr, // When FPR is satisfied by sensitivity isnt. (Early stopping)
	double max_fpr_per_satisfying_sensitivity, // When sensitivity is satisfied by FPR isnt. (Early stopping)
	double absolute_max_fpr) // This value enables controlling the FPR if sensitivity could not be satisfied for target FPR. (Late stopping)
{
	// This is a sanity check on the early stop conditions: They must be more stringent.
	if(min_sensitivity_per_satisfying_fpr < min_target_sensitivity ||
		max_fpr_per_satisfying_sensitivity > max_target_fpr)
	{
		fprintf(stderr, "Early stop conditions on sensitivity and FPR must be more stringent than target values.\n");
		exit(0);
	}

	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);
		}

		// Update window length.
		satisfying_l_win = l_win;

		bool target_fpr_satisfied = (fc_FPR <= max_target_fpr && p_val_FPR <= max_target_fpr);
		bool sensitivity_satisfied = (sensitivity >= min_target_sensitivity);

		// Return if we already passed the max FPR that is allowed no matter what the sensitivity is.
		if(fc_FPR > absolute_max_fpr || 
			p_val_FPR > absolute_max_fpr)
		{
			break;
		}

		if(target_fpr_satisfied || sensitivity_satisfied)
		{
			// Early select 1: Sensitivity reached a very high value before we hit the FPR check.
			if(target_fpr_satisfied && sensitivity_satisfied &&
				sensitivity >= min_sensitivity_per_satisfying_fpr)
			{
				break;
			}

			// Early select 2: Sensitivity is satisfied and FPR is very low, stop increasing window.
			if(sensitivity_satisfied && target_fpr_satisfied && 
				fc_FPR <= max_fpr_per_satisfying_sensitivity && 
				p_val_FPR <= max_fpr_per_satisfying_sensitivity)
			{
				break;
			}

			// This is the normal exit scenario, FPR is not satisfied at this value and sensitivity is satisfied.
			if(sensitivity_satisfied && !target_fpr_satisfied)
			{
				break;
			}
		}
		else 
		{
			// None is satisfied; increase window length.
		}
	} // 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 $0x458, %rsp            # imm = 0x458
movsd %xmm4, 0x10(%rsp)
movapd %xmm2, 0x30(%rsp)
movapd %xmm1, 0x50(%rsp)
ucomisd %xmm2, %xmm1
ja 0x1e0ce
ucomisd %xmm0, %xmm3
ja 0x1e0ce
movapd %xmm0, 0x40(%rsp)
movsd %xmm3, 0x8(%rsp)
leaq 0xf07b(%rip), %rdi      # 0x2cfd5
leaq 0xfda7(%rip), %rsi      # 0x2dd08
callq 0x26387
movq %rax, %rbx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x70(%rsp), %r14
leaq 0x68(%rsp), %r13
leaq 0x60(%rsp), %rbp
leaq 0x28(%rsp), %r15
movq %r14, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdx
callq 0x22a0
testq %rax, %rax
je 0x1e094
movq %r14, %rdi
leaq 0x111b3(%rip), %rsi     # 0x2f159
leaq 0x4(%rsp), %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %r15, %r9
xorl %eax, %eax
leaq 0x18(%rsp), %r10
pushq %r10
leaq 0x28(%rsp), %r10
pushq %r10
callq 0x2160
addq $0x10, %rsp
cmpl $0x6, %eax
jne 0x1e0c4
movsd 0x28(%rsp), %xmm0
xorl %eax, %eax
movsd 0x10(%rsp), %xmm2
ucomisd %xmm2, %xmm0
movapd 0x50(%rsp), %xmm5
ja 0x1e09a
movsd 0x20(%rsp), %xmm1
ucomisd %xmm2, %xmm1
ja 0x1e09a
movapd %xmm0, %xmm2
movapd 0x40(%rsp), %xmm4
cmplepd %xmm4, %xmm2
movapd %xmm1, %xmm3
cmplepd %xmm4, %xmm3
andpd %xmm2, %xmm3
movsd 0x18(%rsp), %xmm2
ucomisd %xmm5, %xmm2
movd %xmm3, %eax
setae %cl
orb %al, %cl
testb $0x1, %cl
je 0x1e090
movapd 0x30(%rsp), %xmm3
cmplepd %xmm2, %xmm3
movapd %xmm5, %xmm4
cmplepd %xmm2, %xmm4
andpd %xmm3, %xmm4
movd %xmm4, %ecx
andb %al, %cl
testb $0x1, %cl
jne 0x1e098
ucomisd %xmm5, %xmm2
setb %cl
setae %dl
andb %al, %dl
xorb $0x1, %dl
movsd 0x8(%rsp), %xmm2
ucomisd %xmm0, %xmm2
setb %sil
ucomisd %xmm1, %xmm2
setb %dil
orb %sil, %dil
orb %dl, %dil
orb %al, %cl
andb %dil, %cl
cmpb $0x1, %cl
jne 0x1e098
movb $0x1, %al
jmp 0x1e09a
xorl %eax, %eax
jmp 0x1e09f
xorl %eax, %eax
movl 0x4(%rsp), %r12d
testb %al, %al
jne 0x1df83
movq %rbx, %rdi
callq 0x2170
movl %r12d, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x70(%rsp), %rdi
callq 0x51b9
callq 0x51dc