// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: unixOpen
static int unixOpen(
  sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
  const char *zPath,           /* Pathname of file to be opened */
  sqlite3_file *pFile,         /* The file descriptor to be filled in */
  int flags,                   /* Input flags to control the opening */
  int *pOutFlags               /* Output flags returned to SQLite core */
){
  unixFile *p = (unixFile *)pFile;
  int fd = -1;                   /* File descriptor returned by open() */
  int openFlags = 0;             /* Flags to pass to open() */
  int eType = flags&0x0FFF00;  /* Type of file to open */
  int noLock;                    /* True to omit locking primitives */
  int rc = SQLITE_OK;            /* Function Return Code */
  int ctrlFlags = 0;             /* UNIXFILE_* flags */

  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  int isCreate     = (flags & SQLITE_OPEN_CREATE);
  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
#if SQLITE_ENABLE_LOCKING_STYLE
  int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
#endif
#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
  struct statfs fsInfo;
#endif

  /* If creating a super- or main-file journal, this function will open
  ** a file-descriptor on the directory too. The first time unixSync()
  ** is called the directory file descriptor will be fsync()ed and close()d.
  */
  int isNewJrnl = (isCreate && (
        eType==SQLITE_OPEN_SUPER_JOURNAL
     || eType==SQLITE_OPEN_MAIN_JOURNAL
     || eType==SQLITE_OPEN_WAL
  ));

  /* If argument zPath is a NULL pointer, this function is required to open
  ** a temporary file. Use this buffer to store the file name in.
  */
  char zTmpname[MAX_PATHNAME+2];
  const char *zName = zPath;

  /* Check the following statements are true:
  **
  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
  **   (b) if CREATE is set, then READWRITE must also be set, and
  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
  */
  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
  assert(isCreate==0 || isReadWrite);
  assert(isExclusive==0 || isCreate);
  assert(isDelete==0 || isCreate);

  /* The main DB, main journal, WAL file and super-journal are never
  ** automatically deleted. Nor are they ever temporary files.  */
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL );
  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );

  /* Assert that the upper layer has set one of the "file-type" flags. */
  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_SUPER_JOURNAL
       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
  );

  /* Detect a pid change and reset the PRNG.  There is a race condition
  ** here such that two or more threads all trying to open databases at
  ** the same instant might all reset the PRNG.  But multiple resets
  ** are harmless.
  */
  if( randomnessPid!=osGetpid(0) ){
    randomnessPid = osGetpid(0);
    sqlite3_randomness(0,0);
  }
  memset(p, 0, sizeof(unixFile));

  if( eType==SQLITE_OPEN_MAIN_DB ){
    UnixUnusedFd *pUnused;
    pUnused = findReusableFd(zName, flags);
    if( pUnused ){
      fd = pUnused->fd;
    }else{
      pUnused = sqlite3_malloc64(sizeof(*pUnused));
      if( !pUnused ){
        return SQLITE_NOMEM_BKPT;
      }
    }
    p->pPreallocatedUnused = pUnused;

    /* Database filenames are double-zero terminated if they are not
    ** URIs with parameters.  Hence, they can always be passed into
    ** sqlite3_uri_parameter(). */
    assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );

  }else if( !zName ){
    /* If zName is NULL, the upper layer is requesting a temp file. */
    assert(isDelete && !isNewJrnl);
    rc = unixGetTempname(pVfs->mxPathname, zTmpname);
    if( rc!=SQLITE_OK ){
      return rc;
    }
    zName = zTmpname;

    /* Generated temporary filenames are always double-zero terminated
    ** for use by sqlite3_uri_parameter(). */
    assert( zName[strlen(zName)+1]==0 );
  }

  /* Determine the value of the flags parameter passed to POSIX function
  ** open(). These must be calculated even if open() is not called, as
  ** they may be stored as part of the file handle and used by the
  ** 'conch file' locking functions later on.  */
  if( isReadonly )  openFlags |= O_RDONLY;
  if( isReadWrite ) openFlags |= O_RDWR;
  if( isCreate )    openFlags |= O_CREAT;
  if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
  openFlags |= (O_LARGEFILE|O_BINARY|O_NOFOLLOW);

  if( fd<0 ){
    mode_t openMode;              /* Permissions to create file with */
    uid_t uid;                    /* Userid for the file */
    gid_t gid;                    /* Groupid for the file */
    rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
    if( rc!=SQLITE_OK ){
      assert( !p->pPreallocatedUnused );
      assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
      return rc;
    }
    fd = robust_open(zName, openFlags, openMode);
    OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
    assert( !isExclusive || (openFlags & O_CREAT)!=0 );
    if( fd<0 ){
      if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
        /* If unable to create a journal because the directory is not
        ** writable, change the error code to indicate that. */
        rc = SQLITE_READONLY_DIRECTORY;
      }else if( errno!=EISDIR && isReadWrite ){
        /* Failed to open the file for read/write access. Try read-only. */
        flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
        openFlags &= ~(O_RDWR|O_CREAT);
        flags |= SQLITE_OPEN_READONLY;
        openFlags |= O_RDONLY;
        isReadonly = 1;
        fd = robust_open(zName, openFlags, openMode);
      }
    }
    if( fd<0 ){
      int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
      if( rc==SQLITE_OK ) rc = rc2;
      goto open_finished;
    }

    /* The owner of the rollback journal or WAL file should always be the
    ** same as the owner of the database file.  Try to ensure that this is
    ** the case.  The chown() system call will be a no-op if the current
    ** process lacks root privileges, be we should at least try.  Without
    ** this step, if a root process opens a database file, it can leave
    ** behinds a journal/WAL that is owned by root and hence make the
    ** database inaccessible to unprivileged processes.
    **
    ** If openMode==0, then that means uid and gid are not set correctly
    ** (probably because SQLite is configured to use 8+3 filename mode) and
    ** in that case we do not want to attempt the chown().
    */
    if( openMode && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL))!=0 ){
      robustFchown(fd, uid, gid);
    }
  }
  assert( fd>=0 );
  if( pOutFlags ){
    *pOutFlags = flags;
  }

  if( p->pPreallocatedUnused ){
    p->pPreallocatedUnused->fd = fd;
    p->pPreallocatedUnused->flags =
                          flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
  }

  if( isDelete ){
#if OS_VXWORKS
    zPath = zName;
#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
    zPath = sqlite3_mprintf("%s", zName);
    if( zPath==0 ){
      robust_close(p, fd, __LINE__);
      return SQLITE_NOMEM_BKPT;
    }
#else
    osUnlink(zName);
#endif
  }
#if SQLITE_ENABLE_LOCKING_STYLE
  else{
    p->openFlags = openFlags;
  }
#endif

#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
  if( fstatfs(fd, &fsInfo) == -1 ){
    storeLastErrno(p, errno);
    robust_close(p, fd, __LINE__);
    return SQLITE_IOERR_ACCESS;
  }
  if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
  }
  if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
  }
#endif

  /* Set up appropriate ctrlFlags */
  if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
  if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
  noLock = eType!=SQLITE_OPEN_MAIN_DB;
  if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
  if( isNewJrnl )               ctrlFlags |= UNIXFILE_DIRSYNC;
  if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;

#if SQLITE_ENABLE_LOCKING_STYLE
#if SQLITE_PREFER_PROXY_LOCKING
  isAutoProxy = 1;
#endif
  if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
    char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
    int useProxy = 0;

    /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
    ** never use proxy, NULL means use proxy for non-local files only.  */
    if( envforce!=NULL ){
      useProxy = atoi(envforce)>0;
    }else{
      useProxy = !(fsInfo.f_flags&MNT_LOCAL);
    }
    if( useProxy ){
      rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
      if( rc==SQLITE_OK ){
        rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
        if( rc!=SQLITE_OK ){
          /* Use unixClose to clean up the resources added in fillInUnixFile
          ** and clear all the structure's references.  Specifically,
          ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
          */
          unixClose(pFile);
          return rc;
        }
      }
      goto open_finished;
    }
  }
#endif

  assert( zPath==0 || zPath[0]=='/'
      || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL
  );
  rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);

open_finished:
  if( rc!=SQLITE_OK ){
    sqlite3_free(p->pPreallocatedUnused);
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x40(%rbp)
movl -0x24(%rbp), %eax
andl $0xfff00, %eax          # imm = 0xFFF00
movl %eax, -0x44(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x24(%rbp), %eax
andl $0x10, %eax
movl %eax, -0x54(%rbp)
movl -0x24(%rbp), %eax
andl $0x8, %eax
movl %eax, -0x58(%rbp)
movl -0x24(%rbp), %eax
andl $0x4, %eax
movl %eax, -0x5c(%rbp)
movl -0x24(%rbp), %eax
andl $0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x24(%rbp), %eax
andl $0x2, %eax
movl %eax, -0x64(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x5c(%rbp)
movb %al, -0x291(%rbp)
je 0x3c4136
movb $0x1, %al
cmpl $0x4000, -0x44(%rbp)    # imm = 0x4000
movb %al, -0x292(%rbp)
je 0x3c412a
movb $0x1, %al
cmpl $0x800, -0x44(%rbp)     # imm = 0x800
movb %al, -0x292(%rbp)
je 0x3c412a
cmpl $0x80000, -0x44(%rbp)   # imm = 0x80000
sete %al
movb %al, -0x292(%rbp)
movb -0x292(%rbp), %al
movb %al, -0x291(%rbp)
movb -0x291(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x278(%rbp)
movl 0x27f2bb(%rip), %eax    # 0x643410
movl %eax, -0x298(%rbp)
callq 0x19a30
movl %eax, %ecx
movl -0x298(%rbp), %eax
cmpl %ecx, %eax
je 0x3c4182
callq 0x19a30
movl %eax, 0x27f299(%rip)    # 0x643410
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x3c3b60
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x78, %edx
callq 0x19360
cmpl $0x100, -0x44(%rbp)     # imm = 0x100
jne 0x3c4203
movq -0x278(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x3d95b0
movq %rax, -0x280(%rbp)
cmpq $0x0, -0x280(%rbp)
je 0x3c41c9
movq -0x280(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
jmp 0x3c41f2
movl $0x10, %edi
callq 0x3c07c0
movq %rax, -0x280(%rbp)
cmpq $0x0, -0x280(%rbp)
jne 0x3c41f0
movl $0x7, -0x4(%rbp)
jmp 0x3c44cf
jmp 0x3c41f2
movq -0x280(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0x3c4244
cmpq $0x0, -0x278(%rbp)
jne 0x3c4242
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x270(%rbp), %rsi
callq 0x3d8b20
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x3c4234
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3c44cf
leaq -0x270(%rbp), %rax
movq %rax, -0x278(%rbp)
jmp 0x3c4244
cmpl $0x0, -0x60(%rbp)
je 0x3c4253
movl -0x40(%rbp), %eax
orl $0x0, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0x3c4262
movl -0x40(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x3c4271
movl -0x40(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x3c4282
movl -0x40(%rbp), %eax
orl $0x20080, %eax          # imm = 0x20080
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
orl $0x20000, %eax          # imm = 0x20000
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x3c43e8
movq -0x278(%rbp), %rdi
movl -0x24(%rbp), %esi
leaq -0x284(%rbp), %rdx
leaq -0x288(%rbp), %rcx
leaq -0x28c(%rbp), %r8
callq 0x3d9740
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x3c42cf
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3c44cf
movq -0x278(%rbp), %rdi
movl -0x40(%rbp), %esi
movl -0x284(%rbp), %edx
callq 0x3d7f50
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x3c4378
cmpl $0x0, -0x68(%rbp)
je 0x3c4321
callq 0x19060
cmpl $0xd, (%rax)
jne 0x3c4321
movq 0x27b9f0(%rip), %rax    # 0x63fcf8
movq -0x278(%rbp), %rdi
xorl %esi, %esi
callq *%rax
cmpl $0x0, %eax
je 0x3c4321
movl $0x608, -0x4c(%rbp)     # imm = 0x608
jmp 0x3c4376
callq 0x19060
cmpl $0x15, (%rax)
je 0x3c4374
cmpl $0x0, -0x64(%rbp)
je 0x3c4374
movl -0x24(%rbp), %eax
andl $-0x7, %eax
movl %eax, -0x24(%rbp)
movl -0x40(%rbp), %eax
andl $-0x43, %eax
movl %eax, -0x40(%rbp)
movl -0x24(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x40(%rbp), %eax
orl $0x0, %eax
movl %eax, -0x40(%rbp)
movl $0x1, -0x60(%rbp)
movq -0x278(%rbp), %rdi
movl -0x40(%rbp), %esi
movl -0x284(%rbp), %edx
callq 0x3d7f50
movl %eax, -0x3c(%rbp)
jmp 0x3c4376
jmp 0x3c4378
cmpl $0x0, -0x3c(%rbp)
jge 0x3c43bc
movl $0x9ae7, %edi           # imm = 0x9AE7
callq 0x3d80e0
movl %eax, %edi
movq -0x278(%rbp), %rdx
leaq 0x177931(%rip), %rsi    # 0x53bcc9
movl $0x9ae7, %ecx           # imm = 0x9AE7
callq 0x3d8070
movl %eax, -0x290(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x3c43b7
movl -0x290(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x3c44b6
cmpl $0x0, -0x284(%rbp)
je 0x3c43e6
movl -0x24(%rbp), %eax
andl $0x80800, %eax          # imm = 0x80800
cmpl $0x0, %eax
je 0x3c43e6
movl -0x3c(%rbp), %edi
movl -0x288(%rbp), %esi
movl -0x28c(%rbp), %edx
callq 0x3d93d0
jmp 0x3c43e8
cmpq $0x0, -0x30(%rbp)
je 0x3c43f8
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x3c4421
movl -0x3c(%rbp), %ecx
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movl %ecx, (%rax)
movl -0x24(%rbp), %ecx
andl $0x3, %ecx
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movl %ecx, 0x4(%rax)
cmpl $0x0, -0x58(%rbp)
je 0x3c4437
movq 0x27ba1a(%rip), %rax    # 0x63fe48
movq -0x278(%rbp), %rdi
callq *%rax
cmpl $0x0, -0x58(%rbp)
je 0x3c4446
movl -0x50(%rbp), %eax
orl $0x20, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x60(%rbp)
je 0x3c4455
movl -0x50(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x50(%rbp)
cmpl $0x100, -0x44(%rbp)     # imm = 0x100
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x3c4478
movl -0x50(%rbp), %eax
orl $0x80, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0x3c4487
movl -0x50(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x50(%rbp)
movl -0x24(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x3c449b
movl -0x50(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl -0x50(%rbp), %r8d
callq 0x3d98b0
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x3c44c9
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x3c0800
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
nopl (%rax,%rax)