// File: 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:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movl %esi, 0x4c(%rsp)
movq %rdx, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq 0x40(%rsp), %rdi
callq 0x11c40
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rdi
callq 0x11c40
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
je 0x3471c
movq 0x308dc(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x21c0e(%rip), %rsi     # 0x5631c
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl 0x4c(%rsp), %eax
addl $0x2, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x30(%rsp)
movl $0x1, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jg 0x34852
movq 0x50(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movb $0x0, 0x1f(%rsp)
movq 0x40(%rsp), %rdi
callq 0x11c40
subq $0x1, %rax
movl %eax, 0x18(%rsp)
xorl %eax, %eax
testb $0x1, 0x1f(%rsp)
movb %al, 0xf(%rsp)
jne 0x347a6
cmpl $0x0, 0x18(%rsp)
setge %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x347b0
jmp 0x3480b
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x40(%rsp), %rdi
movslq 0x18(%rsp), %rsi
callq 0xc930
movsd (%rsp), %xmm0
ucomisd (%rax), %xmm0
jb 0x347fc
movb $0x1, 0x1f(%rsp)
movq 0x38(%rsp), %rdi
movslq 0x18(%rsp), %rsi
callq 0xc930
movsd (%rax), %xmm0
movq 0x30(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x347fe
movl 0x18(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x3478d
testb $0x1, 0x1f(%rsp)
jne 0x34840
movq 0x307c7(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0x50(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x21b2f(%rip), %rsi     # 0x56361
movb $0x1, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
jmp 0x34842
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x34753
movq 0x30(%rsp), %rax
addq $0x58, %rsp
retq
nopl (%rax)