// 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, 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 $0x468, %rsp            # imm = 0x468
movsd %xmm4, 0x30(%rsp)
movapd %xmm2, 0x50(%rsp)
movapd %xmm1, 0x10(%rsp)
ucomisd %xmm2, %xmm1
ja 0x1dc37
ucomisd %xmm0, %xmm3
ja 0x1dc37
movapd %xmm0, 0x60(%rsp)
movsd %xmm3, 0x28(%rsp)
leaq 0xe521(%rip), %rdi      # 0x2bfcf
leaq 0xf253(%rip), %rsi      # 0x2cd08
callq 0x25d5a
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rax, %rdx
callq 0x22a0
testq %rax, %rax
je 0x1dc07
leaq 0x80(%rsp), %r15
leaq 0x78(%rsp), %r13
leaq 0x70(%rsp), %rbp
leaq 0x48(%rsp), %r14
movq %r15, %rdi
leaq 0x1065d(%rip), %rsi     # 0x2e159
leaq 0xc(%rsp), %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %r14, %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 0x1dc2a
movl 0xc(%rsp), %r12d
movsd 0x48(%rsp), %xmm0
movsd 0x30(%rsp), %xmm2
ucomisd %xmm2, %xmm0
ja 0x1dc0d
movsd 0x40(%rsp), %xmm1
ucomisd %xmm2, %xmm1
ja 0x1dc0d
movapd %xmm0, %xmm2
movapd 0x60(%rsp), %xmm4
cmplepd %xmm4, %xmm2
movapd %xmm1, %xmm3
cmplepd %xmm4, %xmm3
andpd %xmm2, %xmm3
movsd 0x38(%rsp), %xmm2
ucomisd 0x10(%rsp), %xmm2
movd %xmm3, %eax
setae %cl
orb %al, %cl
testb $0x1, %cl
je 0x1dbec
movapd 0x50(%rsp), %xmm3
cmplepd %xmm2, %xmm3
movapd 0x10(%rsp), %xmm4
cmplepd %xmm2, %xmm4
andpd %xmm3, %xmm4
movd %xmm4, %ecx
andb %al, %cl
testb $0x1, %cl
jne 0x1dc0d
ucomisd 0x10(%rsp), %xmm2
setb %cl
setae %dl
andb %al, %dl
xorb $0x1, %dl
movsd 0x28(%rsp), %xmm2
ucomisd %xmm0, %xmm2
setb %sil
ucomisd %xmm1, %xmm2
setb %dil
orb %sil, %dil
orb %dl, %dil
cmpb $0x1, %dil
jne 0x1dc0d
orb %al, %cl
testb $0x1, %cl
je 0x1dc0d
movq %r15, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdx
callq 0x22a0
testq %rax, %rax
jne 0x1daf2
jmp 0x1dc0d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x2170
movl %r12d, %eax
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x80(%rsp), %rdi
callq 0x5056
callq 0x5079