// File: gersteinlab[P]MUSIC/src/ms_ansi_string.cpp
// Function: t_string::fast_search_string(char*, std::vector<char*, std::allocator<char*>>*, int, int)
int t_string::fast_search_string(char* query, vector<char*>* string_list, int i, int j)
{
	// Sort the string list.
	int i_mid = (i + j) / 2;

	if(i_mid == i ||
		i_mid == j)
	{
		return(i);
	}
	
	if(t_string::compare_strings(string_list->at(i_mid), query))
	{
		while(i_mid < (int)string_list->size() &&
			t_string::sort_strings(string_list->at(i_mid), query))
		{
			i_mid++;
		}

		// If the above loop hit the end, move back once.
		if(i_mid == (int)string_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 &&
			!sort_strings(string_list->at(i_mid), query))
		{
			i_mid--;
		}

		return(i_mid);
	}
	else if(sort_strings(query, string_list->at(i_mid)))
	{
		return(t_string::fast_search_string(query, string_list, i, i_mid));
	}
	else if(!sort_strings(query, string_list->at(i_mid)))
	{
		return(t_string::fast_search_string(query, string_list, i_mid, j));
	}
	else
	{
		fprintf(stderr, "Fast searching failed @ %s(%d)\n", __FILE__, __LINE__);
		exit(0);
	}
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movl %edx, 0x2c(%rsp)
movl %ecx, 0x28(%rsp)
movl 0x2c(%rsp), %eax
addl 0x28(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x2c(%rsp), %eax
je 0x478fe
movl 0x24(%rsp), %eax
cmpl 0x28(%rsp), %eax
jne 0x4790b
movl 0x2c(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x47aad
movq 0x30(%rsp), %rdi
movslq 0x24(%rsp), %rsi
callq 0xc240
movq (%rax), %rdi
movq 0x38(%rsp), %rsi
callq 0x47ac0
testb $0x1, %al
jne 0x47930
jmp 0x479f2
jmp 0x47932
movl 0x24(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x30(%rsp), %rdi
callq 0xc220
movl 0x1c(%rsp), %ecx
movl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x23(%rsp)
jge 0x47974
movq 0x30(%rsp), %rdi
movslq 0x24(%rsp), %rsi
callq 0xc240
movq (%rax), %rdi
movq 0x38(%rsp), %rsi
callq 0x47570
movb %al, 0x23(%rsp)
movb 0x23(%rsp), %al
testb $0x1, %al
jne 0x4797e
jmp 0x4798b
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x47932
movl 0x24(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x30(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jne 0x479b3
movl 0x24(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jle 0x479e5
movq 0x30(%rsp), %rdi
movslq 0x24(%rsp), %rsi
callq 0xc240
movq (%rax), %rdi
movq 0x38(%rsp), %rsi
callq 0x47570
testb $0x1, %al
jne 0x479e5
movl 0x24(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x47aad
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rdi
movslq 0x24(%rsp), %rsi
callq 0xc240
movq 0x10(%rsp), %rdi
movq (%rax), %rsi
callq 0x47570
testb $0x1, %al
jne 0x47a1e
jmp 0x47a3b
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x2c(%rsp), %edx
movl 0x24(%rsp), %ecx
callq 0x478c0
movl %eax, 0x44(%rsp)
jmp 0x47aad
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x30(%rsp), %rdi
movslq 0x24(%rsp), %rsi
callq 0xc240
movq 0x8(%rsp), %rdi
movq (%rax), %rsi
callq 0x47570
testb $0x1, %al
jne 0x47a82
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x24(%rsp), %edx
movl 0x28(%rsp), %ecx
callq 0x478c0
movl %eax, 0x44(%rsp)
jmp 0x47aad
movq 0x1d557(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0xf2e0(%rip), %rsi      # 0x56d73
leaq 0xf2f9(%rip), %rdx      # 0x56d93
movl $0xc6, %ecx
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)