// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: locate_posn_per_sorted_obj_list(void*, std::vector<void*, std::allocator<void*>>*, int, int, int (*)(void*, void*))
int locate_posn_per_sorted_obj_list(void* prop_ptr, vector<void*>* obj_list, int i, int j, int (*comparor)(void*, void*))
{
	int i_mid = (i + j) / 2;

	if(i_mid == i ||
		i_mid == j)
	{
		return(i);
	}

	// If the property of interest is matching the mid point's property, go to the left till posn point to a smaller property.
	if(comparor(obj_list->at(i_mid), prop_ptr) == 1)
	{
		while(i_mid > 0 &&
			comparor(obj_list->at(i_mid), prop_ptr) == 1)
		{
			i_mid--;
		}

		return(i_mid);
	}
	else if(comparor(obj_list->at(i_mid), prop_ptr) > 1)
	{
		// The mid region is higher than the property of interest. Move the mid point to left to get closer to the property of interest.
		return(locate_posn_per_sorted_obj_list(prop_ptr, obj_list, i, i_mid, comparor));
	}
	else if(comparor(obj_list->at(i_mid), prop_ptr) < 1)
	{
		// The mid region is lower than the property of interest. Move the mid point to right to get closer to the property of interest.
		return(locate_posn_per_sorted_obj_list(prop_ptr, obj_list, i_mid, j, comparor));
	}
	else
	{
		fprintf(stderr, "Current indices are (%d, %d) @ %s(%d)\n", comparor(obj_list->at(i), prop_ptr), comparor(obj_list->at(j), prop_ptr),
			__FILE__, __LINE__);
		exit(0);
	}	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %r14
movl %ecx, %r13d
movl %edx, %ebp
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r15
movq %rbp, (%rsp)
movl %r13d, %ebp
movl %ebp, %r13d
movq (%rsp), %rcx
leal (%rcx,%r13), %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
cmpl %ecx, %eax
je 0x17a07
movl %eax, %ebx
cmpl %r13d, %eax
je 0x17a07
movslq %ebx, %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x17bae
movq (%rax), %rdi
movq %r15, %rsi
callq *%r14
cmpl $0x1, %eax
je 0x17a0d
movq %r12, %rdi
movq %rbp, %rsi
callq 0x17bae
movq (%rax), %rdi
movq %r15, %rsi
callq *%r14
cmpl $0x1, %eax
jg 0x17926
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x17bae
movq (%rax), %rdi
movq 0x10(%rsp), %rsi
callq *%r14
testl %eax, %eax
jle 0x1791f
movq 0x1d63a(%rip), %rax     # 0x34fe0
movq (%rax), %rbx
movslq (%rsp), %rsi
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x17bae
movq (%rax), %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq *%r14
movl %eax, %ebp
movslq %r13d, %rsi
movq %r12, %rdi
callq 0x17bae
movq (%rax), %rdi
movq %r15, %rsi
callq *%r14
leaq 0x104bc(%rip), %rsi     # 0x27ea1
leaq 0xfff7(%rip), %r8       # 0x279e3
movq %rbx, %rdi
movl %ebp, %edx
movl %eax, %ecx
movl $0xc26, %r9d            # imm = 0xC26
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21d0
movq (%rsp), %rbx
jmp 0x17a40
movl %ebx, %r13d
movl %ebx, %ebp
sarl $0x1f, %ebp
andl %ebx, %ebp
incl %ebx
testl %r13d, %r13d
jle 0x17a3e
movq %r12, %rdi
movq %r13, %rsi
callq 0x17bae
movq (%rax), %rdi
movq %r15, %rsi
callq *%r14
decq %r13
decl %ebx
cmpl $0x1, %eax
je 0x17a19
jmp 0x17a40
movl %ebp, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq