// File: /workspace/github_c_makefile_star2_O0/shifengbin[P]bishe/Output/../Persistence/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&0xFFFFFF00;  /* 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 master 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 syncDir = (isCreate && (
        eType==SQLITE_OPEN_MASTER_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 master 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_MASTER_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_MASTER_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;
      }
    }
    p->pUnused = 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 && !syncDir);
    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);

  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->pUnused );
      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 && 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 ){
      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
      goto open_finished;
    }

    /* If this process is running as root and if creating a new rollback
    ** journal or WAL file, set the ownership of the journal or WAL to be
    ** the same as the original database.
    */
    if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
      robustFchown(fd, uid, gid);
    }
  }
  assert( fd>=0 );
  if( pOutFlags ){
    *pOutFlags = flags;
  }

  if( p->pUnused ){
    p->pUnused->fd = fd;
    p->pUnused->flags = flags;
  }

  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;
    }
#else
    osUnlink(zName);
#endif
  }
#if SQLITE_ENABLE_LOCKING_STYLE
  else{
    p->openFlags = openFlags;
  }
#endif

  noLock = eType!=SQLITE_OPEN_MAIN_DB;

  
#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;
  if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
  if( syncDir )                 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
  
  rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);

open_finished:
  if( rc!=SQLITE_OK ){
    sqlite3_free(p->pUnused);
  }
  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 $0xffffff00, %eax       # imm = 0xFFFFFF00
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, -0x28d(%rbp)
je 0x10646
movb $0x1, %al
cmpl $0x4000, -0x44(%rbp)    # imm = 0x4000
movb %al, -0x28e(%rbp)
je 0x1063a
movb $0x1, %al
cmpl $0x800, -0x44(%rbp)     # imm = 0x800
movb %al, -0x28e(%rbp)
je 0x1063a
cmpl $0x80000, -0x44(%rbp)   # imm = 0x80000
sete %al
movb %al, -0x28e(%rbp)
movb -0x28e(%rbp), %al
movb %al, -0x28d(%rbp)
movb -0x28d(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x278(%rbp)
movl 0xb1013(%rip), %eax     # 0xc1678
movl %eax, -0x294(%rbp)
callq 0xb1e0
movl %eax, %ecx
movl -0x294(%rbp), %eax
cmpl %ecx, %eax
je 0x10692
callq 0xb1e0
movl %eax, 0xb0ff1(%rip)     # 0xc1678
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0xb4b0
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x70, %edx
callq 0xb2b0
cmpl $0x100, -0x44(%rbp)     # imm = 0x100
jne 0x10713
movq -0x278(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x24ce0
movq %rax, -0x280(%rbp)
cmpq $0x0, -0x280(%rbp)
je 0x106d9
movq -0x280(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
jmp 0x10702
movl $0x10, %edi
callq 0xb270
movq %rax, -0x280(%rbp)
cmpq $0x0, -0x280(%rbp)
jne 0x10700
movl $0x7, -0x4(%rbp)
jmp 0x10987
jmp 0x10702
movq -0x280(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0x10754
cmpq $0x0, -0x278(%rbp)
jne 0x10752
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x270(%rbp), %rsi
callq 0x24370
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x10744
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x10987
leaq -0x270(%rbp), %rax
movq %rax, -0x278(%rbp)
jmp 0x10754
cmpl $0x0, -0x60(%rbp)
je 0x10763
movl -0x40(%rbp), %eax
orl $0x0, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0x10772
movl -0x40(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x10781
movl -0x40(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x10792
movl -0x40(%rbp), %eax
orl $0x20080, %eax          # imm = 0x20080
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
orl $0x0, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x108a3
movq -0x278(%rbp), %rdi
movl -0x24(%rbp), %esi
leaq -0x284(%rbp), %rdx
leaq -0x288(%rbp), %rcx
leaq -0x28c(%rbp), %r8
callq 0x24e50
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x107dd
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x10987
movq -0x278(%rbp), %rdi
movl -0x40(%rbp), %esi
movl -0x284(%rbp), %edx
callq 0x237e0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x1084e
callq 0xb0f0
cmpl $0x15, (%rax)
je 0x1084e
cmpl $0x0, -0x64(%rbp)
je 0x1084e
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 0x237e0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x10880
movl $0x800e, %edi           # imm = 0x800E
callq 0x23970
movl %eax, %edi
movq -0x278(%rbp), %rdx
leaq 0x95347(%rip), %rsi     # 0xa5bb5
movl $0x800e, %ecx           # imm = 0x800E
callq 0x23900
movl %eax, -0x4c(%rbp)
jmp 0x1096e
movl -0x24(%rbp), %eax
andl $0x80800, %eax          # imm = 0x80800
cmpl $0x0, %eax
je 0x108a1
movl -0x3c(%rbp), %edi
movl -0x288(%rbp), %esi
movl -0x28c(%rbp), %edx
callq 0x24ae0
jmp 0x108a3
cmpq $0x0, -0x30(%rbp)
je 0x108b3
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x108d9
movl -0x3c(%rbp), %ecx
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movl %ecx, (%rax)
movl -0x24(%rbp), %ecx
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movl %ecx, 0x4(%rax)
cmpl $0x0, -0x58(%rbp)
je 0x108ef
movq 0xaf1c2(%rip), %rax     # 0xbfaa8
movq -0x278(%rbp), %rdi
callq *%rax
cmpl $0x100, -0x44(%rbp)     # imm = 0x100
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x58(%rbp)
je 0x10910
movl -0x50(%rbp), %eax
orl $0x20, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x60(%rbp)
je 0x1091f
movl -0x50(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x10930
movl -0x50(%rbp), %eax
orl $0x80, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0x1093f
movl -0x50(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x50(%rbp)
movl -0x24(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x10953
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 0x24f80
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x10981
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0xb958
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
nopw %cs:(%rax,%rax)