// File: 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:
subq $0x478, %rsp            # imm = 0x478
movq %rdi, 0x470(%rsp)
movsd %xmm0, 0x468(%rsp)
movsd %xmm1, 0x460(%rsp)
movsd %xmm2, 0x458(%rsp)
movsd %xmm3, 0x450(%rsp)
movsd %xmm4, 0x448(%rsp)
movsd 0x458(%rsp), %xmm1
movsd 0x460(%rsp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x35ba8
movsd 0x450(%rsp), %xmm0
ucomisd 0x468(%rsp), %xmm0
jbe 0x35bc7
movq 0x2f431(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x208f3(%rip), %rsi     # 0x564ac
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
leaq 0x1e57d(%rip), %rdi     # 0x5414b
leaq 0x1f4f9(%rip), %rsi     # 0x550ce
callq 0x455f0
movq %rax, 0x440(%rsp)
movl $0xffffffff, 0x43c(%rsp) # imm = 0xFFFFFFFF
leaq 0x50(%rsp), %rdi
movq 0x440(%rsp), %rdx
movl $0x3e8, %esi            # imm = 0x3E8
callq 0x22a0
cmpq $0x0, %rax
jne 0x35c0f
jmp 0x35d7d
leaq 0x50(%rsp), %rdi
leaq 0x208ea(%rip), %rsi     # 0x56505
leaq 0x4c(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
leaq 0x28(%rsp), %r10
leaq 0x20(%rsp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2170
cmpl $0x6, %eax
je 0x35c72
movq 0x2f38b(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x50(%rsp), %rdx
leaq 0x1d178(%rip), %rsi     # 0x52ddc
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl 0x4c(%rsp), %eax
movl %eax, 0x43c(%rsp)
movsd 0x30(%rsp), %xmm1
movsd 0x468(%rsp), %xmm0
xorl %eax, %eax
ucomisd %xmm1, %xmm0
movb %al, 0x1d(%rsp)
jb 0x35cb2
movsd 0x28(%rsp), %xmm1
movsd 0x468(%rsp), %xmm0
ucomisd %xmm1, %xmm0
setae %al
movb %al, 0x1d(%rsp)
movb 0x1d(%rsp), %al
andb $0x1, %al
movb %al, 0x1f(%rsp)
movsd 0x20(%rsp), %xmm0
ucomisd 0x460(%rsp), %xmm0
setae %al
andb $0x1, %al
movb %al, 0x1e(%rsp)
movsd 0x30(%rsp), %xmm0
ucomisd 0x448(%rsp), %xmm0
ja 0x35cf6
movsd 0x28(%rsp), %xmm0
ucomisd 0x448(%rsp), %xmm0
jbe 0x35cfb
jmp 0x35d7d
testb $0x1, 0x1f(%rsp)
jne 0x35d09
testb $0x1, 0x1e(%rsp)
je 0x35d76
testb $0x1, 0x1f(%rsp)
je 0x35d2a
testb $0x1, 0x1e(%rsp)
je 0x35d2a
movsd 0x20(%rsp), %xmm0
ucomisd 0x458(%rsp), %xmm0
jb 0x35d2a
jmp 0x35d7d
testb $0x1, 0x1e(%rsp)
je 0x35d64
testb $0x1, 0x1f(%rsp)
je 0x35d64
movsd 0x30(%rsp), %xmm1
movsd 0x450(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x35d64
movsd 0x28(%rsp), %xmm1
movsd 0x450(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x35d64
jmp 0x35d7d
testb $0x1, 0x1e(%rsp)
je 0x35d74
testb $0x1, 0x1f(%rsp)
jne 0x35d74
jmp 0x35d7d
jmp 0x35d78
jmp 0x35d78
jmp 0x35bed
movq 0x440(%rsp), %rdi
callq 0x2180
movl 0x43c(%rsp), %eax
addq $0x478, %rsp            # imm = 0x478
retq
nopl (%rax)