// File: gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: locate_posn_per_sorted_posn_list(int, std::vector<int, std::allocator<int>>*, int, int)
int locate_posn_per_sorted_posn_list(int posn, vector<int>* sorted_posn_list, int i, int j)
{
	int i_mid = (i + j) / 2;

	if(i_mid == i ||
		i_mid == j)
	{
		return(i);
	}

	if(sorted_posn_list->at(i_mid) == posn)
	{
		// Move back while the entry is larger than or equal to the requestd position.
		while(i_mid > 0 &&
			sorted_posn_list->at(i_mid) >= posn)
		{
			i_mid--;
		}

		//// If the above loop hit the end, move back once.
		//if(i_mid == sorted_posn_list->size())
		//{
		//	i_mid--;
		//}

		//// If the above loop moved beyond the query move back once, which is the exact element just before query.
		//if(i_mid > 0 &&
		//	sorted_posn_list->at(i_mid) > posn)
		//{
		//	i_mid--;

		return(i_mid);
	}
	else if(sorted_posn_list->at(i_mid) > posn)
	{
		return(locate_posn_per_sorted_posn_list(posn, sorted_posn_list, i, i_mid));
	}
	else if(sorted_posn_list->at(i_mid) < posn)
	{
		return(locate_posn_per_sorted_posn_list(posn, sorted_posn_list, i_mid, j));
	}
	else
	{
		fprintf(stderr, "Current indices are (%d, %d), searching for %d @ %s(%d)\n", sorted_posn_list->at(i), sorted_posn_list->at(j),
			posn, __FILE__, __LINE__);
		exit(0);
	}
}

// Assembly:
subq $0x38, %rsp
movl %edi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movl %edx, 0x24(%rsp)
movl %ecx, 0x20(%rsp)
movl 0x24(%rsp), %eax
addl 0x20(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x24(%rsp), %eax
je 0x3175d
movl 0x1c(%rsp), %eax
cmpl 0x20(%rsp), %eax
jne 0x3176a
movl 0x24(%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x3189f
movq 0x28(%rsp), %rdi
movslq 0x1c(%rsp), %rsi
callq 0xc2c0
movl (%rax), %eax
cmpl 0x30(%rsp), %eax
jne 0x317d0
jmp 0x31783
xorl %eax, %eax
cmpl $0x0, 0x1c(%rsp)
movb %al, 0x1b(%rsp)
jle 0x317ac
movq 0x28(%rsp), %rdi
movslq 0x1c(%rsp), %rsi
callq 0xc2c0
movl (%rax), %eax
cmpl 0x30(%rsp), %eax
setge %al
movb %al, 0x1b(%rsp)
movb 0x1b(%rsp), %al
testb $0x1, %al
jne 0x317b6
jmp 0x317c3
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x31783
movl 0x1c(%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x3189f
movq 0x28(%rsp), %rdi
movslq 0x1c(%rsp), %rsi
callq 0xc2c0
movl (%rax), %eax
cmpl 0x30(%rsp), %eax
jle 0x31806
movl 0x30(%rsp), %edi
movq 0x28(%rsp), %rsi
movl 0x24(%rsp), %edx
movl 0x1c(%rsp), %ecx
callq 0x31720
movl %eax, 0x34(%rsp)
jmp 0x3189f
movq 0x28(%rsp), %rdi
movslq 0x1c(%rsp), %rsi
callq 0xc2c0
movl (%rax), %eax
cmpl 0x30(%rsp), %eax
jge 0x31839
movl 0x30(%rsp), %edi
movq 0x28(%rsp), %rsi
movl 0x1c(%rsp), %edx
movl 0x20(%rsp), %ecx
callq 0x31720
movl %eax, 0x34(%rsp)
jmp 0x3189f
movq 0x337a0(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rdi
movslq 0x24(%rsp), %rsi
callq 0xc2c0
movl (%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rdi
movslq 0x20(%rsp), %rsi
callq 0xc2c0
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %edx
movl (%rax), %ecx
movl 0x30(%rsp), %r8d
leaq 0x249ed(%rip), %rsi     # 0x56270
leaq 0x24519(%rip), %r9      # 0x55da3
movl $0xbfb, (%rsp)          # imm = 0xBFB
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopl (%rax,%rax)