// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r14
testq %r8, %r8
je 0x76331
movq %rbp, %rdi
callq 0x34fea
testb %al, %al
jne 0x764cd
testq %rbx, %rbx
je 0x764c3
testq %r12, %r12
je 0x764c8
movq %rbx, %rdi
callq 0x3b785
movq %rax, %rbx
movq %rax, %rdi
callq 0x3bb5d
movq %r14, %rdi
movq %rax, %rsi
callq 0x762a7
movq %r14, %rdi
callq 0x3bb86
xorl %r15d, %r15d
testq %rax, %rax
jne 0x764a9
movq %r13, %rdi
callq 0xb7c0
testq %rax, %rax
je 0x7645c
movq %rax, %rdi
callq 0x728c0
movq %rax, %r15
callq 0x39f34
movq %rax, %r13
movq %r15, %rdi
callq 0xb260
movq %r13, %rdi
movq %r15, (%rsp)
movq %r15, %rsi
movq %rax, %rdx
movl $0x3a, %ecx
callq 0x39f4f
movq %r13, %rdi
callq 0x3a16c
xorl %ebp, %ebp
testq %rax, %rax
je 0x76422
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3a130
movq %rax, %r15
movq %r14, %rdi
callq 0x3bbee
movq %r14, %rdi
movq %r15, %rsi
callq 0x3b7af
movq %r14, %rdi
movl $0x2f, %esi
callq 0x3b9df
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3b87d
movq %r14, %rdi
callq 0x3bb5d
movq %rax, %rdi
callq *%r12
testb %al, %al
jne 0x76422
incq %rbp
movq %r13, %rdi
callq 0x3a16c
cmpq %rax, %rbp
jb 0x763c7
movq %r13, %rdi
callq 0x3a16c
cmpq %rax, %rbp
jae 0x7647e
movq %r14, %rdi
callq 0x3bb5d
xorl %r15d, %r15d
movq %rax, %rdi
xorl %esi, %esi
callq 0xb1f0
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
callq 0x762a7
movq %r12, %rdi
callq 0xb790
jmp 0x76489
leaq 0x7ad66(%rip), %rsi     # 0xf11c9
xorl %r14d, %r14d
movq %rbp, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x34f1f
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r13d, %r13d
jmp 0x7648d
movq %r14, %rdi
callq 0x3bbee
xorl %r15d, %r15d
movq (%rsp), %r14
leaq 0x7ac2e(%rip), %rsi     # 0xf10c2
movq %r14, %rdi
movl $0xf2, %edx
callq 0x35c27
movq %r13, %rdi
callq 0x3a17c
movq %rbx, %rdi
callq 0x3c87b
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1c168
callq 0x1c12f
callq 0x1c0f6