// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xf303
callq 0x1050
movq -0x20(%rbp), %rdi
callq 0x10a0
movl %eax, -0x3c(%rbp)
cmpq $0x0, 0x16f39(%rip)     # 0x26250
je 0xf372
movq 0x16f30(%rip), %rax     # 0x26250
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x3c(%rbp), %ecx
callq *%rax
cmpl $0x0, %eax
je 0xf370
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x10(%rbp), %rdi
leaq 0xfc7d(%rip), %rsi      # 0x1efce
callq 0x1190
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xf36e
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xf526
jmp 0xf370
jmp 0xf372
movq -0x20(%rbp), %rdi
leaq 0xfc51(%rip), %rsi      # 0x1efce
callq 0x1190
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xf3d4
movq -0x20(%rbp), %rdi
callq 0x10a0
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x14(%rbp), %eax
jl 0xf3aa
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movslq -0x44(%rbp), %rdx
callq 0x1150
movq -0x10(%rbp), %rax
movslq -0x44(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xf526
cmpq $0x0, -0x28(%rbp)
je 0xf485
movq -0x28(%rbp), %rdi
callq 0x10a0
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xf483
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x1070
movq -0x10(%rbp), %rdi
movl $0x2f, %esi
callq 0x10e0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0xf481
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movslq -0x3c(%rbp), %rcx
addq %rcx, %rax
addq $0x1, %rax
movslq -0x14(%rbp), %rcx
cmpq %rcx, %rax
jg 0xf47f
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x1070
movq -0x10(%rbp), %rdi
leaq 0xfb6e(%rip), %rsi      # 0x1efce
callq 0x1190
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xf47d
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xf526
jmp 0xf47f
jmp 0xf481
jmp 0xf483
jmp 0xf485
callq 0xe720
cmpq $0x0, 0x16dae(%rip)     # 0x26240
je 0xf51e
movq 0x16da1(%rip), %rax     # 0x26240
movq %rax, -0x38(%rbp)
movl 0x16d9f(%rip), %eax     # 0x26248
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
subl $0x1, %esi
movq -0x38(%rbp), %rdx
movl -0x40(%rbp), %ecx
movq -0x20(%rbp), %r8
movl -0x3c(%rbp), %r9d
callq 0xec80
movl %eax, -0x44(%rbp)
cmpl $0x0, %eax
je 0xf51c
movq -0x10(%rbp), %rdi
leaq 0xfaf1(%rip), %rsi      # 0x1efce
callq 0x1190
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xf4f7
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xf526
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rax
movslq -0x44(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl -0x44(%rbp), %ecx
movl -0x40(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x40(%rbp)
jmp 0xf4ac
jmp 0xf51e
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq