// File: 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:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movl %edx, 0x5c(%rsp)
movl %ecx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movl 0x5c(%rsp), %eax
addl 0x58(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x5c(%rsp), %eax
je 0x318f3
movl 0x4c(%rsp), %eax
cmpl 0x58(%rsp), %eax
jne 0x31900
movl 0x5c(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0x31ad6
movq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x60(%rsp), %rdi
movslq 0x4c(%rsp), %rsi
callq 0x31db0
movq %rax, %rcx
movq 0x40(%rsp), %rax
movq (%rcx), %rdi
movq 0x68(%rsp), %rsi
callq *%rax
cmpl $0x1, %eax
jne 0x31998
jmp 0x31932
xorl %eax, %eax
cmpl $0x0, 0x4c(%rsp)
movb %al, 0x3f(%rsp)
jle 0x31974
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x60(%rsp), %rdi
movslq 0x4c(%rsp), %rsi
callq 0x31db0
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq (%rcx), %rdi
movq 0x68(%rsp), %rsi
callq *%rax
cmpl $0x1, %eax
sete %al
movb %al, 0x3f(%rsp)
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x3197e
jmp 0x3198b
movl 0x4c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x31932
movl 0x4c(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0x31ad6
movq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x60(%rsp), %rdi
movslq 0x4c(%rsp), %rsi
callq 0x31db0
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq (%rcx), %rdi
movq 0x68(%rsp), %rsi
callq *%rax
cmpl $0x1, %eax
jle 0x319ed
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl 0x5c(%rsp), %edx
movl 0x4c(%rsp), %ecx
movq 0x50(%rsp), %r8
callq 0x318b0
movl %eax, 0x74(%rsp)
jmp 0x31ad6
movq 0x50(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rdi
movslq 0x4c(%rsp), %rsi
callq 0x31db0
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq (%rcx), %rdi
movq 0x68(%rsp), %rsi
callq *%rax
cmpl $0x1, %eax
jge 0x31a42
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl 0x4c(%rsp), %edx
movl 0x58(%rsp), %ecx
movq 0x50(%rsp), %r8
callq 0x318b0
movl %eax, 0x74(%rsp)
jmp 0x31ad6
movq 0x33597(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq 0x60(%rsp), %rdi
movslq 0x5c(%rsp), %rsi
callq 0x31db0
movq %rax, %rcx
movq (%rsp), %rax
movq (%rcx), %rdi
movq 0x68(%rsp), %rsi
callq *%rax
movl %eax, 0x1c(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rdi
movslq 0x58(%rsp), %rsi
callq 0x31db0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq (%rcx), %rdi
movq 0x68(%rsp), %rsi
callq *%rax
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %edx
movl %eax, %ecx
leaq 0x247ee(%rip), %rsi     # 0x562a9
leaq 0x242e1(%rip), %r8      # 0x55da3
movl $0xc26, %r9d            # imm = 0xC26
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nop