// File: /workspace/github_c_makefile_star2_O2/MikeGray-APH[P]LibLouisAPH/source/lookup.c
// Function: lookup_open_file
FILE* lookup_open_file(char *path_name, const int path_name_max, const char *file_name, const char *adjacent_name)
{
	FILE *file;
	char *paths;
	int file_name_len, paths_len, len;

	ASSERT(file_name)

	file_name_len = strlen(file_name);

	/*   try lookup hook   */
	if(lookup_hook)
	if(lookup_hook(path_name, path_name_max, file_name, file_name_len))
	{
		/*   make sure path is NULL terminated   */
		path_name[path_name_max - 1] = 0;

		file = fopen(path_name, "r");
		if(file)
			return file;
	}

	/*   try file_name directly   */
	file = fopen(file_name, "r");
	if(file)
	{
		len = strlen(file_name);
		if(len >= path_name_max)
			len = path_name_max - 1;
		memcpy(path_name, file_name, len);
		path_name[len] = 0;
		return file;
	}

	/*   try directory of adjacent_name   */
	if(adjacent_name)
	{
		len = strlen(adjacent_name);
		if(len < path_name_max)
		{
			strcpy(path_name, adjacent_name);
			paths = strrchr(path_name, FILE_SEPARATOR_CHAR);
			if(paths)
			{
				paths++;
				if((paths - path_name) + file_name_len + 1 <= path_name_max)
				{
					strcpy(paths, file_name);
					file = fopen(path_name, "r");
					if(file)
						return file;
				}
			}
		}
	}

	/*   assume file_name is a base name   */
	check_env();
	if(table_paths)
	{
		paths = table_paths;
		paths_len = table_paths_len;
		while(len = create_path_file_name(path_name, path_name_max - 1, paths, paths_len, file_name, file_name_len))
		{
			file = fopen(path_name, "r");
			if(file)
				return file;

			len++;
			paths = &paths[len];
			paths_len -= len;
		}
	}

	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdx, %rdx
je 0x915d
movq %rcx, %rbp
movq %rdx, %r14
movl %esi, %r15d
movq %rdi, %rbx
movq %rdx, %rdi
callq 0x10b0
movq %rax, (%rsp)
movq 0x10281(%rip), %rax     # 0x19280
movslq %r15d, %r12
testq %rax, %rax
je 0x903c
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
movq (%rsp), %rcx
callq *%rax
testl %eax, %eax
je 0x903c
movb $0x0, -0x1(%rbx,%r12)
leaq 0x8d57(%rip), %rsi      # 0x11d7e
movq %rbx, %rdi
callq 0x11d0
testq %rax, %rax
je 0x903c
movq %rax, %r13
jmp 0x914b
leaq 0x8d3b(%rip), %rsi      # 0x11d7e
movq %r14, %rdi
callq 0x11d0
testq %rax, %rax
je 0x9080
movq %rax, %r13
movq %r14, %rdi
callq 0x10b0
leal -0x1(%r15), %ecx
cmpl %r15d, %eax
cmovll %eax, %ecx
movslq %ecx, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1180
movb $0x0, (%rbx,%r15)
jmp 0x914b
testq %rbp, %rbp
je 0x90e5
movq %rbp, %rdi
callq 0x10b0
cmpl %r15d, %eax
jge 0x90e5
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1080
pushq $0x2f
popq %rsi
movq %rbx, %rdi
callq 0x10f0
testq %rax, %rax
je 0x90e5
movslq (%rsp), %rcx
subq %rbx, %rcx
addq %rax, %rcx
incq %rcx
cmpq %r12, %rcx
jge 0x90e5
incq %rax
movq %rax, %rdi
movq %r14, %rsi
callq 0x1080
leaq 0x8caa(%rip), %rsi      # 0x11d7e
movq %rbx, %rdi
callq 0x11d0
testq %rax, %rax
jne 0x9034
callq 0x8808
movq 0x1017f(%rip), %rbp     # 0x19270
testq %rbp, %rbp
je 0x9148
movl 0x1017b(%rip), %r12d    # 0x19278
decl %r15d
movq %rbx, %rdi
movl %r15d, %esi
movq %rbp, %rdx
movl %r12d, %ecx
movq %r14, %r8
movq (%rsp), %r9
callq 0x8c0c
testl %eax, %eax
je 0x9148
movl %eax, %r13d
movq %rbx, %rdi
leaq 0x8c55(%rip), %rsi      # 0x11d7e
callq 0x11d0
testq %rax, %rax
jne 0x9034
movslq %r13d, %rax
addq %rax, %rbp
incq %rbp
notl %r13d
addl %r13d, %r12d
jmp 0x9100
xorl %r13d, %r13d
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1060
nop