// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
movq %r8, %r13
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r14
testq %r8, %r8
je 0x75e08
movq %r13, %rdi
callq 0x3566a
testb %al, %al
jne 0x75fbb
testq %rbx, %rbx
je 0x75fb1
testq %r15, %r15
je 0x75fb6
movq %rbx, %rdi
callq 0x3c03d
movq %rax, %rbx
movq %rax, %rdi
callq 0x3c40b
movq %r14, %rdi
movq %rax, %rsi
callq 0x75d63
movq %r14, %rdi
callq 0x3c434
xorl %ebp, %ebp
testq %rax, %rax
jne 0x75f98
movq %r12, %rdi
callq 0xb7c0
movq %rax, %rbp
testq %rax, %rax
je 0x75e70
movq %rbp, %rdi
callq 0x722dc
movq %rax, %r12
movl $0x0, 0xc(%rsp)
jmp 0x75e8f
leaq 0x7b3a2(%rip), %rsi     # 0xf1219
movq %r13, %rdi
movq %r12, %rdx
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x3559f
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
testq %rbp, %rbp
je 0x75f62
callq 0x3a794
movq %rax, %r13
movq %r12, %rdi
callq 0xb260
movq %r13, %rdi
movq %r12, 0x10(%rsp)
movq %r12, %rsi
movq %rax, %rdx
movl $0x3a, %ecx
callq 0x3a7af
movq %r13, %rdi
callq 0x3a9cc
xorl %r12d, %r12d
testq %rax, %rax
je 0x75f2b
movq %r13, %rdi
movq %r12, %rsi
callq 0x3a990
movq %rax, %rbp
movq %r14, %rdi
callq 0x3c49c
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3c067
movq %r14, %rdi
movl $0x2f, %esi
callq 0x3c28d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3c12b
movq %r14, %rdi
callq 0x3c40b
movq %rax, %rdi
callq *%r15
testb %al, %al
jne 0x75f2b
incq %r12
movq %r13, %rdi
callq 0x3a9cc
cmpq %rax, %r12
jb 0x75ed0
movq %r13, %rdi
callq 0x3a9cc
cmpq %rax, %r12
jae 0x75f6b
movq %r14, %rdi
callq 0x3c40b
movq %rax, %rdi
xorl %esi, %esi
callq 0xb1f0
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x75d63
movq %r15, %rdi
callq 0xb790
jmp 0x75f73
xorl %r13d, %r13d
movl 0xc(%rsp), %ebp
jmp 0x75f7c
movq %r14, %rdi
callq 0x3c49c
movl 0xc(%rsp), %ebp
movq 0x10(%rsp), %r12
leaq 0x7b18f(%rip), %rsi     # 0xf1112
movq %r12, %rdi
movl $0xf2, %edx
callq 0x362a0
movq %r13, %rdi
callq 0x3a9dc
movq %rbx, %rdi
callq 0x3d1eb
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1c1bd
callq 0x1c184
callq 0x1c14b