// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/core/fileutils.c
// Function: file_find_in_env_generic
static int file_find_in_env_generic(GtStr *path, const char *file_path,
                                    const char *env, FileExistsFunc file_exists,
                                    GtError *err)
{
  char *pathvariable, *pathcomponent = NULL;
  GtSplitter *splitter = NULL;
  bool found = false;
  GtStr *file;
  GtUword i;
  int had_err = 0;

  gt_error_check(err);
  gt_assert(file_path);
  gt_assert(file_exists);

  /* make writeable copy of 'file_path' */
  file = gt_str_new_cstr(file_path);

  /* check if 'file' has dirname */
  gt_file_dirname(path, gt_str_get(file));
  if (gt_str_length(path)) {
    gt_str_delete(file);
    return had_err;
  }
  /* 'file' has no dirname -> scan $env */
  pathvariable = getenv(env);
  if (pathvariable != NULL)
    pathvariable = gt_cstr_dup(pathvariable); /* make writeable copy */
  else {
    gt_error_set(err, "environment variable $%s is not defined", env);
    had_err = -1;
  }

#ifdef _WIN32
  if (!had_err && file_exists == file_exists_and_is_regular_executable) {
    append_dot_exe_if_necessary(file);
    /* in Windows '.' is implicitly included in $PATH, check first */
    gt_str_reset(path);
    gt_str_append_char(path, '.');
    gt_str_append_char(path, GT_PATH_SEPARATOR);
    gt_str_append_str(path, file);
    if (file_exists(gt_str_get(path))) {
      gt_str_reset(path);
      gt_str_append_char(path, '.');
      gt_str_append_char(path, GT_PATH_SEPARATOR);
      found = true;
    }
  }
#endif

  if (!had_err && !found) {
    splitter = gt_splitter_new();
    gt_splitter_split(splitter, pathvariable,
                      (GtUword) strlen(pathvariable), GT_PATH_VAR_SEPARATOR);
    for (i = 0; i < gt_splitter_size(splitter); i++) {
      pathcomponent = gt_splitter_get_token(splitter, i);
      gt_str_reset(path);
      gt_str_append_cstr(path, pathcomponent);
      gt_str_append_char(path, GT_PATH_SEPARATOR);
      gt_str_append_str(path, file);
      if (file_exists(gt_str_get(path)))
        break;
    }
    if (i < gt_splitter_size(splitter)) {
      /* file found in path */
#ifndef _WIN32
      char *abspath = realpath(gt_str_get(path), NULL);
      gt_file_dirname(path, abspath);
      free(abspath);
#else
      /* TODO: using old implementation for Windows correct? */
      gt_str_reset(path);
      gt_str_append_cstr(path, pathcomponent);
#endif
    }
    else {
      /* file not found in path */
      gt_str_reset(path);
    }
  }

  /* free */
  gt_free(pathvariable);
  gt_splitter_delete(splitter);
  gt_str_delete(file);

  return had_err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r8, %rbp
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r14
testq %r8, %r8
je 0x54c15
movq %rbp, %rdi
callq 0xc103
testb %al, %al
jne 0x54df2
testq %rbx, %rbx
je 0x54d96
testq %r15, %r15
je 0x54dc4
movq %rbx, %rdi
callq 0x13da9
movq %rax, %rbx
movq %rax, %rdi
callq 0x143d2
movq %r14, %rdi
movq %rax, %rsi
callq 0x54b8b
movq %r14, %rdi
callq 0x14461
xorl %r12d, %r12d
testq %rax, %rax
jne 0x54d7c
movq %r13, %rdi
callq 0xb7c0
testq %rax, %rax
je 0x54d31
movq %rax, %rdi
callq 0x4f8ae
movq %rax, %r12
callq 0x124cc
movq %rax, %r13
movq %r12, %rdi
callq 0xb260
pushq $0x3a
popq %rcx
movq %r13, %rdi
movq %r12, (%rsp)
movq %r12, %rsi
movq %rax, %rdx
callq 0x124e1
xorl %ebp, %ebp
movq %r13, %rdi
callq 0x127fd
cmpq %rax, %rbp
jae 0x54cf7
movq %r13, %rdi
movq %rbp, %rsi
callq 0x12765
movq %rax, %r12
movq %r14, %rdi
callq 0x1452f
movq %r14, %rdi
movq %r12, %rsi
callq 0x13dd3
movq %r14, %rdi
pushq $0x2f
popq %rsi
callq 0x140b1
movq %r14, %rdi
movq %rbx, %rsi
callq 0x13eeb
movq %r14, %rdi
callq 0x143d2
movq %rax, %rdi
callq *%r15
testb %al, %al
jne 0x54cf7
incq %rbp
jmp 0x54c9c
movq %r13, %rdi
callq 0x127fd
cmpq %rax, %rbp
jae 0x54d51
movq %r14, %rdi
callq 0x143d2
xorl %r12d, %r12d
movq %rax, %rdi
xorl %esi, %esi
callq 0xb1f0
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x54b8b
movq %r15, %rdi
callq 0xb790
jmp 0x54d5c
leaq 0x85441(%rip), %rsi     # 0xda179
xorl %r14d, %r14d
movq %rbp, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xc007
pushq $-0x1
popq %r12
xorl %r13d, %r13d
jmp 0x54d60
movq %r14, %rdi
callq 0x1452f
xorl %r12d, %r12d
movq (%rsp), %r14
leaq 0x8530b(%rip), %rsi     # 0xda072
movq %r14, %rdi
movl $0xf2, %edx
callq 0xd249
movq %r13, %rdi
callq 0x1283e
movq %rbx, %rdi
callq 0x152db
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xd921b(%rip), %rax     # 0x12dfb8
movq (%rax), %rdi
leaq 0x752bf(%rip), %rsi     # 0xca066
leaq 0x8953d(%rip), %rdx     # 0xde2eb
leaq 0x8539f(%rip), %rcx     # 0xda154
leaq 0x852b6(%rip), %r8      # 0xda072
movl $0xac, %r9d
jmp 0x54e1e
movq 0xd91ed(%rip), %rax     # 0x12dfb8
movq (%rax), %rdi
leaq 0x75291(%rip), %rsi     # 0xca066
leaq 0x85391(%rip), %rdx     # 0xda16d
leaq 0x85371(%rip), %rcx     # 0xda154
leaq 0x85288(%rip), %r8      # 0xda072
movl $0xad, %r9d
jmp 0x54e1e
movq 0xd91bf(%rip), %rax     # 0x12dfb8
movq (%rax), %rdi
leaq 0x75263(%rip), %rsi     # 0xca066
leaq 0x7584f(%rip), %rdx     # 0xca659
leaq 0x85343(%rip), %rcx     # 0xda154
leaq 0x8525a(%rip), %r8      # 0xda072
movl $0xab, %r9d
xorl %eax, %eax
callq 0xb8c0
callq 0xb300