// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/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
  );

  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_malloc(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(MAX_PATHNAME+2, 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));
    if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
      /* 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) ){
      osFchown(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;
#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 ){
    ((unixFile*)pFile)->lastErrno = 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;
  }
#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{
      if( statfs(zPath, &fsInfo) == -1 ){
        /* In theory, the close(fd) call is sub-optimal. If the file opened
        ** with fd is a database file, and there are other connections open
        ** on that file that are currently holding advisory locks on it,
        ** then the call to close() will cancel those locks. In practice,
        ** we're assuming that statfs() doesn't fail very often. At least
        ** not while other file descriptors opened by the same process on
        ** the same file are working.  */
        p->lastErrno = errno;
        robust_close(p, fd, __LINE__);
        rc = SQLITE_IOERR_ACCESS;
        goto open_finished;
      }
      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 $0x290, %rsp            # imm = 0x290
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 0xcb36
movb $0x1, %al
cmpl $0x4000, -0x44(%rbp)    # imm = 0x4000
movb %al, -0x28e(%rbp)
je 0xcb2a
movb $0x1, %al
cmpl $0x800, -0x44(%rbp)     # imm = 0x800
movb %al, -0x28e(%rbp)
je 0xcb2a
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)
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x70, %edx
callq 0x8200
cmpl $0x100, -0x44(%rbp)     # imm = 0x100
jne 0xcbd0
movq -0x278(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x1ed10
movq %rax, -0x280(%rbp)
cmpq $0x0, -0x280(%rbp)
je 0xcb96
movq -0x280(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
jmp 0xcbbf
movl $0x10, %edi
callq 0xa380
movq %rax, -0x280(%rbp)
cmpq $0x0, -0x280(%rbp)
jne 0xcbbd
movl $0x7, -0x4(%rbp)
jmp 0xce4c
jmp 0xcbbf
movq -0x280(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0xcc0f
cmpq $0x0, -0x278(%rbp)
jne 0xcc0d
leaq -0x270(%rbp), %rsi
movl $0x202, %edi            # imm = 0x202
callq 0x1e3d0
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0xcbff
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xce4c
leaq -0x270(%rbp), %rax
movq %rax, -0x278(%rbp)
jmp 0xcc0f
cmpl $0x0, -0x60(%rbp)
je 0xcc1e
movl -0x40(%rbp), %eax
orl $0x0, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0xcc2d
movl -0x40(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0xcc3c
movl -0x40(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0xcc4d
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 0xcd68
movq -0x278(%rbp), %rdi
movl -0x24(%rbp), %esi
leaq -0x284(%rbp), %rdx
leaq -0x288(%rbp), %rcx
leaq -0x28c(%rbp), %r8
callq 0x1ee80
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0xcc98
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xce4c
movq -0x278(%rbp), %rdi
movl -0x40(%rbp), %esi
movl -0x284(%rbp), %edx
callq 0x1d850
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0xcd0f
callq 0x8060
cmpl $0x15, (%rax)
je 0xcd0f
cmpl $0x0, -0x64(%rbp)
je 0xcd0f
cmpl $0x0, -0x54(%rbp)
jne 0xcd0f
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 0x1d850
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0xcd41
movl $0x7039, %edi           # imm = 0x7039
callq 0x1d9a0
movl %eax, %edi
movq -0x278(%rbp), %rdx
leaq 0xa13ec(%rip), %rsi     # 0xae11b
movl $0x7039, %ecx           # imm = 0x7039
callq 0x1d930
movl %eax, -0x4c(%rbp)
jmp 0xce33
movl -0x24(%rbp), %eax
andl $0x80800, %eax          # imm = 0x80800
cmpl $0x0, %eax
je 0xcd66
movq 0xc3d73(%rip), %rax     # 0xd0ac8
movl -0x3c(%rbp), %edi
movl -0x288(%rbp), %esi
movl -0x28c(%rbp), %edx
callq *%rax
jmp 0xcd68
cmpq $0x0, -0x30(%rbp)
je 0xcd78
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0xcd9e
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 0xcdb4
movq 0xc3cbd(%rip), %rax     # 0xd0a68
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 0xcdd5
movl -0x50(%rbp), %eax
orl $0x20, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x60(%rbp)
je 0xcde4
movl -0x50(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0xcdf5
movl -0x50(%rbp), %eax
orl $0x80, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0xce04
movl -0x50(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x50(%rbp)
movl -0x24(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0xce18
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 0x1efb0
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0xce46
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0xa420
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
nopl (%rax,%rax)