// File: gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: locate_posn_region_per_region_starts(int, std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, int, int)
int locate_posn_region_per_region_starts(int start_posn, vector<t_annot_region*>* region_list, int i, int j)
{
	int i_mid = (i + j) / 2;

	if(i_mid == i ||
		i_mid == j)
	{
		//return(i);
		while(i_mid > 0 && region_list->at(i_mid)->start >= start_posn)
		{
			i_mid--;
		}

		return(i_mid);
	}

	if(region_list->at(i_mid)->start == start_posn)
	{
		while(i_mid > 0 && region_list->at(i_mid)->start >= start_posn)
		{
			i_mid--;
		}

		return(i_mid);
	}
	else if(region_list->at(i_mid)->start > start_posn)
	{
		return(locate_posn_region_per_region_starts(start_posn, region_list, i, i_mid));
	}
	else if(region_list->at(i_mid)->start < start_posn)
	{
		return(locate_posn_region_per_region_starts(start_posn, region_list, i_mid, j));
	}
	else
	{
		fprintf(stderr, "Current indices are (%d, %d), searching for %d @ %s(%d)\n", region_list->at(i)->start, region_list->at(j)->start,
			start_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 0x297dd
movl 0x1c(%rsp), %eax
cmpl 0x20(%rsp), %eax
jne 0x29830
jmp 0x297df
xorl %eax, %eax
cmpl $0x0, 0x1c(%rsp)
movb %al, 0x1b(%rsp)
jle 0x2980c
movq 0x28(%rsp), %rdi
movslq 0x1c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
cmpl 0x30(%rsp), %eax
setge %al
movb %al, 0x1b(%rsp)
movb 0x1b(%rsp), %al
testb $0x1, %al
jne 0x29816
jmp 0x29823
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x297df
movl 0x1c(%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x2997d
movq 0x28(%rsp), %rdi
movslq 0x1c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
cmpl 0x30(%rsp), %eax
jne 0x2989e
jmp 0x2984d
xorl %eax, %eax
cmpl $0x0, 0x1c(%rsp)
movb %al, 0x1a(%rsp)
jle 0x2987a
movq 0x28(%rsp), %rdi
movslq 0x1c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
cmpl 0x30(%rsp), %eax
setge %al
movb %al, 0x1a(%rsp)
movb 0x1a(%rsp), %al
testb $0x1, %al
jne 0x29884
jmp 0x29891
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x2984d
movl 0x1c(%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0x2997d
movq 0x28(%rsp), %rdi
movslq 0x1c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
cmpl 0x30(%rsp), %eax
jle 0x298d8
movl 0x30(%rsp), %edi
movq 0x28(%rsp), %rsi
movl 0x24(%rsp), %edx
movl 0x1c(%rsp), %ecx
callq 0x297a0
movl %eax, 0x34(%rsp)
jmp 0x2997d
movq 0x28(%rsp), %rdi
movslq 0x1c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
cmpl 0x30(%rsp), %eax
jge 0x2990f
movl 0x30(%rsp), %edi
movq 0x28(%rsp), %rsi
movl 0x1c(%rsp), %edx
movl 0x20(%rsp), %ecx
callq 0x297a0
movl %eax, 0x34(%rsp)
jmp 0x2997d
movq 0x3b6ca(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rdi
movslq 0x24(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rdi
movslq 0x20(%rsp), %rsi
callq 0xc800
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %edx
movq (%rax), %rax
movl 0xc(%rax), %ecx
movl 0x30(%rsp), %r8d
leaq 0x2c90f(%rip), %rsi     # 0x56270
leaq 0x2c43b(%rip), %r9      # 0x55da3
movl $0xbca, (%rsp)          # imm = 0xBCA
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)