// 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, 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 0x195da
ucomisd %xmm0, %xmm3
ja 0x195da
movapd %xmm0, 0x60(%rsp)
movsd %xmm3, 0x28(%rsp)
leaq 0xcb79(%rip), %rdi      # 0x25fcf
leaq 0xd8ab(%rip), %rsi      # 0x26d08
callq 0x2027c
movq %rax, %rbx
pushq $-0x1
popq %rbp
leaq 0x80(%rsp), %r14
leaq 0x78(%rsp), %r13
leaq 0x70(%rsp), %r15
leaq 0x48(%rsp), %r12
movq %r14, %rdi
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdx
callq 0x2290
testq %rax, %rax
je 0x19597
movq %r14, %rdi
leaq 0xecb7(%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 0x195b3
movl 0xc(%rsp), %ebp
movsd 0x48(%rsp), %xmm0
movsd 0x30(%rsp), %xmm2
ucomisd %xmm2, %xmm0
ja 0x19597
movsd 0x40(%rsp), %xmm1
ucomisd %xmm2, %xmm1
ja 0x19597
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 0x1947f
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 0x19597
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 0x19597
orb %al, %cl
testb $0x1, %cl
jne 0x1947f
movq %rbx, %rdi
callq 0x2160
movl %ebp, %eax
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1ba26(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xb81c(%rip), %rsi      # 0x24de0
leaq 0x80(%rsp), %rdx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21d0
movq 0x1b9ff(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0xeb15(%rip), %rdi      # 0x28100
pushq $0x58
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
jmp 0x195d3