// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movq %rdx, 0x88(%rsp)
movq %rcx, 0x80(%rsp)
movq %r8, 0x78(%rsp)
movq $0x0, 0x68(%rsp)
movq $0x0, 0x60(%rsp)
movb $0x0, 0x5f(%rsp)
movl $0x0, 0x44(%rsp)
cmpq $0x0, 0x78(%rsp)
je 0x8488b
movq 0x78(%rsp), %rdi
callq 0xc270
testb $0x1, %al
jne 0x84853
jmp 0x8488b
movq 0x12f77e(%rip), %rax    # 0x1b3fd8
movq (%rax), %rdi
leaq 0xd9802(%rip), %rsi     # 0x15e066
leaq 0xd9ddd(%rip), %rdx     # 0x15e648
leaq 0xe9d13(%rip), %rcx     # 0x16e585
leaq 0xe9c2a(%rip), %r8      # 0x16e4a3
movl $0xab, %r9d
movb $0x0, %al
callq 0xb890
callq 0xb2e0
jmp 0x8488d
jmp 0x8488f
cmpq $0x0, 0x90(%rsp)
jne 0x848d2
movq 0x12f737(%rip), %rax    # 0x1b3fd8
movq (%rax), %rdi
leaq 0xd97bb(%rip), %rsi     # 0x15e066
leaq 0xee8e9(%rip), %rdx     # 0x17319b
leaq 0xe9ccc(%rip), %rcx     # 0x16e585
leaq 0xe9be3(%rip), %r8      # 0x16e4a3
movl $0xac, %r9d
movb $0x0, %al
callq 0xb890
callq 0xb2e0
jmp 0x848d4
jmp 0x848d6
cmpq $0x0, 0x80(%rsp)
jne 0x84919
movq 0x12f6f0(%rip), %rax    # 0x1b3fd8
movq (%rax), %rdi
leaq 0xd9774(%rip), %rsi     # 0x15e066
leaq 0xe9ca5(%rip), %rdx     # 0x16e59e
leaq 0xe9c85(%rip), %rcx     # 0x16e585
leaq 0xe9b9c(%rip), %r8      # 0x16e4a3
movl $0xad, %r9d
movb $0x0, %al
callq 0xb890
callq 0xb2e0
jmp 0x8491b
movq 0x90(%rsp), %rdi
callq 0x18f70
movq %rax, 0x50(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x50(%rsp), %rdi
callq 0x19940
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x84730
movq 0x98(%rsp), %rdi
callq 0x19a00
cmpq $0x0, %rax
je 0x8497e
movq 0x50(%rsp), %rdi
callq 0x1afc0
movl 0x44(%rsp), %eax
movl %eax, 0xa4(%rsp)
jmp 0x84b88
movq 0x88(%rsp), %rdi
callq 0xb790
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
je 0x849a9
movq 0x70(%rsp), %rdi
callq 0x7c400
movq %rax, 0x70(%rsp)
jmp 0x849cc
movq 0x78(%rsp), %rdi
movq 0x88(%rsp), %rdx
leaq 0xe9bed(%rip), %rsi     # 0x16e5aa
movb $0x0, %al
callq 0xc0d0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
cmpl $0x0, 0x44(%rsp)
jne 0x84b53
testb $0x1, 0x5f(%rsp)
jne 0x84b53
callq 0x16a10
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x70(%rsp), %rdi
callq 0xb240
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %rax, %rdx
movl $0x3a, %ecx
callq 0x16a30
movq $0x0, 0x48(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x60(%rsp), %rdi
callq 0x17010
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0x84ae7
movq 0x60(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x16f20
movq %rax, 0x68(%rsp)
movq 0x98(%rsp), %rdi
callq 0x19b70
movq 0x98(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x18fb0
movq 0x98(%rsp), %rdi
movl $0x2f, %esi
callq 0x194d0
movq 0x98(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x191b0
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x98(%rsp), %rdi
callq 0x19940
movq %rax, %rdi
movq 0x10(%rsp), %rax
callq *%rax
testb $0x1, %al
jne 0x84ad0
jmp 0x84ad2
jmp 0x84ae7
jmp 0x84ad4
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
jmp 0x84a2a
movq 0x48(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rdi
callq 0x17010
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x84b44
movq 0x98(%rsp), %rdi
callq 0x19940
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xb1d0
movq %rax, 0x38(%rsp)
movq 0x98(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x84730
movq 0x38(%rsp), %rdi
callq 0xb760
jmp 0x84b51
movq 0x98(%rsp), %rdi
callq 0x19b70
jmp 0x84b53
movq 0x70(%rsp), %rdi
leaq 0xe9944(%rip), %rsi     # 0x16e4a3
movl $0xf2, %edx
callq 0xdab0
movq 0x60(%rsp), %rdi
callq 0x17060
movq 0x50(%rsp), %rdi
callq 0x1afc0
movl 0x44(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nopw (%rax,%rax)