// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_signal_track_tools.cpp
// Function: quantize_per_nucleotide_profiles(double*, int, std::vector<double, std::allocator<double>>*, std::vector<double, std::allocator<double>>*)
double* quantize_per_nucleotide_profiles(double* profile, int l_profile, vector<double>* thresholds, vector<double>* quantized_vals)
{
	//int l_profile = 0;
	//double* profile = load_per_nucleotide_binary_profile(prof_fp, l_profile);

	//vector<char*>* thresh_val_lines = buffer_file(thresh_val_fp);

	//vector<double>* quantized_vals = new vector<double>();
	//vector<double>* thresholds = new vector<double>();
	//for(int i_l = 0; i_l < thresh_val_lines->size(); i_l++)
	//{
	//	double cur_quantized_val;
	//	double cur_thresh;
	//	if(sscanf(thresh_val_lines->at(i_l), "%lf %lf", &cur_thresh, &cur_quantized_val) != 2)
	//	{
	//		fprintf(stderr, "Could not parse the line: %s\n", thresh_val_lines->at(i_l));
	//		exit(0);
	//	}

	//	quantized_vals->push_back(cur_quantized_val);
	//	thresholds->push_back(cur_thresh);
	//	fprintf(stderr, "%lf -> %lf\n", cur_thresh, cur_quantized_val);
	//} // i_l loop.

	if(thresholds->size() != quantized_vals->size())
	{
		fprintf(stderr, "The size of thresholds is not the same as size of quantized values.\n");
		exit(0);
	}

	double* quantized_profile = new double[l_profile+2];
	for(int i = 1; i <= l_profile; i++)
	{
		double cur_prof_val = profile[i];

		// Find the largest threshold that this value is greater than or equal to.
		bool quantized_current_val = false;
		for(int i_th = thresholds->size()-1;
			!quantized_current_val && i_th >= 0; 
			i_th--)
		{
			if(cur_prof_val >= thresholds->at(i_th))
			{
				quantized_current_val = true;
				quantized_profile[i] = quantized_vals->at(i_th);
			}
		} // i_th loop.

		if(!quantized_current_val)
		{
			fprintf(stderr, "Could not quantize the current value: %lf\n", profile[i]);
			exit(0);
		}
	} // i loop.

	return(quantized_profile);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r15
movq %rdi, 0x8(%rsp)
movq 0x8(%rdx), %rax
subq (%rdx), %rax
movq 0x8(%rcx), %rdx
movq %rcx, 0x18(%rsp)
subq (%rcx), %rdx
cmpq %rdx, %rax
jne 0x188b2
movl %esi, %r13d
leal 0x2(%r13), %eax
shlq $0x3, %rax
cmpl $-0x2, %esi
pushq $-0x1
popq %rdi
cmovgeq %rax, %rdi
callq 0x2040
movq %rax, %r12
xorl %eax, %eax
testl %r13d, %r13d
cmovgl %r13d, %eax
incl %eax
movq %rax, 0x10(%rsp)
pushq $0x1
popq %rax
cmpq 0x10(%rsp), %rax
je 0x188a0
movq %rax, %rbx
movq 0x8(%r15), %rbp
subq (%r15), %rbp
movq 0x8(%rsp), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
shrq $0x3, %rbp
decl %ebp
xorl %r14d, %r14d
testb $0x1, %r14b
jne 0x1886d
testl %ebp, %ebp
js 0x1886d
movl %ebp, %r13d
movq %r15, %rdi
movq %r13, %rsi
callq 0x7466
movsd 0x20(%rsp), %xmm0
ucomisd (%rax), %xmm0
setae %r14b
jb 0x18869
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x7466
movsd (%rax), %xmm0
movsd %xmm0, (%r12,%rbx,8)
decl %ebp
jmp 0x1882a
leaq 0x1(%rbx), %rax
testb $0x1, %r14b
jne 0x187fc
movq 0x1c762(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
movq 0x8(%rsp), %rax
movsd (%rax,%rbx,8), %xmm0
leaq 0xf6ff(%rip), %rsi      # 0x27f91
movb $0x1, %al
callq 0x2240
xorl %edi, %edi
callq 0x21d0
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1c727(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0xf689(%rip), %rdi      # 0x27f4c
pushq $0x44
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
jmp 0x18899