// File: /workspace/github_c_makefile_star2_O3/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
subq $0x18, %rsp
testq %rdx, %rdx
je 0xa263
movq %rcx, %r13
movq %rdx, %r14
movl %esi, %r15d
movq %rdi, %rbx
movq %rdx, %rdi
callq 0x10b0
movq %rax, 0x8(%rsp)
movq 0x10235(%rip), %rax     # 0x1a270
testq %rax, %rax
je 0xa078
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
movq 0x8(%rsp), %rcx
callq *%rax
testl %eax, %eax
je 0xa078
movslq %r15d, %rax
movb $0x0, -0x1(%rbx,%rax)
leaq 0x9dab(%rip), %rsi      # 0x13e0e
movq %rbx, %rdi
callq 0x11c0
testq %rax, %rax
je 0xa078
movq %rax, %r12
jmp 0xa1ef
leaq 0x9d8f(%rip), %rsi      # 0x13e0e
movq %r14, %rdi
callq 0x11c0
testq %rax, %rax
je 0xa0bc
movq %rax, %r12
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 0xa1ef
testq %r13, %r13
je 0xa127
movq %r13, %rdi
callq 0x10b0
cmpl %r15d, %eax
jge 0xa127
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1080
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x10f0
testq %rax, %rax
je 0xa127
movslq 0x8(%rsp), %rcx
subq %rbx, %rcx
addq %rax, %rcx
incq %rcx
movslq %r15d, %rdx
cmpq %rdx, %rcx
jge 0xa127
incq %rax
movq %rax, %rdi
movq %r14, %rsi
callq 0x1080
leaq 0x9cf8(%rip), %rsi      # 0x13e0e
movq %rbx, %rdi
callq 0x11c0
testq %rax, %rax
jne 0xa070
cmpb $0x0, 0x1013e(%rip)     # 0x1a26c
jne 0xa169
movb $0x1, 0x10135(%rip)     # 0x1a26c
leaq 0x9a82(%rip), %rdi      # 0x13bc0
callq 0x1030
testq %rax, %rax
je 0xa169
cmpq $0x0, 0x10110(%rip)     # 0x1a260
je 0xa201
leaq 0x9a3c(%rip), %rsi      # 0x13b99
movl $0x7, %edi
xorl %eax, %eax
callq 0xa278
movq 0x100f0(%rip), %rbp     # 0x1a260
testq %rbp, %rbp
je 0xa1ec
movl 0x100ec(%rip), %r13d    # 0x1a268
decl %r15d
movq %rbx, %rdi
movq %r15, 0x10(%rsp)
movl %r15d, %esi
movq %rbp, %rdx
movl %r13d, %ecx
movq %r14, %r8
movq 0x8(%rsp), %r9
callq 0x9b34
testl %eax, %eax
je 0xa1ec
movl %eax, %r15d
movq %rbx, %rdi
leaq 0x9c60(%rip), %rsi      # 0x13e0e
callq 0x11c0
testq %rax, %rax
jne 0xa070
movslq %r15d, %rax
addq %rax, %rbp
incq %rbp
notl %eax
addl %eax, %r13d
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movl %r13d, %ecx
movq %r14, %r8
movq 0x8(%rsp), %r9
callq 0x9b34
movl %eax, %r15d
testl %eax, %eax
jne 0xa1a4
xorl %r12d, %r12d
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
movq %rax, %rdi
callq 0x10b0
movq %rax, %r13
movl %r13d, 0x10052(%rip)    # 0x1a268
incl %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, 0x10039(%rip)     # 0x1a260
testq %rax, %rax
je 0xa23f
movq %rax, %rbp
movq %rax, %rdi
movq %r12, %rsi
callq 0x1080
jmp 0xa17c
leaq 0x8ff6(%rip), %rsi      # 0x1323c
leaq 0x98f0(%rip), %rdx      # 0x13b3d
movl $0x7, %edi
movl $0x4a, %ecx
xorl %eax, %eax
callq 0xa278
jmp 0xa169
callq 0x1060