// File: /workspace/github_c_makefile_star2_O1/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 0xa354
movq %rcx, %r13
movq %rdx, %r14
movl %esi, %r15d
movq %rdi, %rbx
movq %rdx, %rdi
callq 0x10b0
movq %rax, 0x8(%rsp)
movq 0x10136(%rip), %rax     # 0x1a270
testq %rax, %rax
je 0xa177
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
movq 0x8(%rsp), %rcx
callq *%rax
testl %eax, %eax
je 0xa177
movslq %r15d, %rax
movb $0x0, -0x1(%rbx,%rax)
leaq 0x9bdc(%rip), %rsi      # 0x13d3e
movq %rbx, %rdi
callq 0x11b0
testq %rax, %rax
je 0xa177
movq %rax, %rbp
jmp 0xa342
leaq 0x9bc0(%rip), %rsi      # 0x13d3e
movq %r14, %rdi
callq 0x11b0
testq %rax, %rax
je 0xa1bb
movq %rax, %rbp
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 0x1170
movb $0x0, (%rbx,%r15)
jmp 0xa342
testq %r13, %r13
je 0xa226
movq %r13, %rdi
callq 0x10b0
cmpl %r15d, %eax
jge 0xa226
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1080
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x10f0
testq %rax, %rax
je 0xa226
movslq 0x8(%rsp), %rcx
subq %rbx, %rcx
addq %rax, %rcx
incq %rcx
movslq %r15d, %rdx
cmpq %rdx, %rcx
jge 0xa226
incq %rax
movq %rax, %rdi
movq %r14, %rsi
callq 0x1080
leaq 0x9b29(%rip), %rsi      # 0x13d3e
movq %rbx, %rdi
callq 0x11b0
testq %rax, %rax
jne 0xa16f
cmpb $0x0, 0x1003f(%rip)     # 0x1a26c
jne 0xa2bd
movb $0x1, 0x10032(%rip)     # 0x1a26c
leaq 0x98af(%rip), %rdi      # 0x13af0
callq 0x1030
testq %rax, %rax
je 0xa2bd
cmpq $0x0, 0x1000d(%rip)     # 0x1a260
je 0xa26a
leaq 0x986d(%rip), %rsi      # 0x13ac9
movl $0x7, %edi
xorl %eax, %eax
callq 0xa36c
jmp 0xa2bd
movq %rax, %r12
movq %rax, %rdi
callq 0x10b0
movl %eax, 0xffed(%rip)      # 0x1a268
incl %eax
movslq %eax, %rdi
callq 0x1180
movq %rax, 0xffd4(%rip)      # 0x1a260
testq %rax, %rax
je 0xa29e
movq %rax, %rdi
movq %r12, %rsi
callq 0x1080
jmp 0xa2bd
leaq 0x8f4f(%rip), %rsi      # 0x131f4
leaq 0x97c1(%rip), %rdx      # 0x13a6d
movl $0x7, %edi
movl $0x4a, %ecx
xorl %eax, %eax
callq 0xa36c
movq 0xff9c(%rip), %r13      # 0x1a260
testq %r13, %r13
je 0xa340
movl 0xff98(%rip), %r12d     # 0x1a268
decl %r15d
movq %rbx, %rdi
movq %r15, 0x10(%rsp)
movl %r15d, %esi
movq %r13, %rdx
movl %r12d, %ecx
movq %r14, %r8
movq 0x8(%rsp), %r9
callq 0x9c32
testl %eax, %eax
je 0xa340
movl %eax, %r15d
movq %rbx, %rdi
leaq 0x9a3c(%rip), %rsi      # 0x13d3e
callq 0x11b0
testq %rax, %rax
jne 0xa16f
movslq %r15d, %rax
addq %rax, %r13
incq %r13
notl %eax
addl %eax, %r12d
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
movl %r12d, %ecx
movq %r14, %r8
movq 0x8(%rsp), %r9
callq 0x9c32
movl %eax, %r15d
testl %eax, %eax
jne 0xa2f8
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1060
nopl (%rax)