// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e8, %rsp            # imm = 0x4E8
movl %ecx, %r12d
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, -0x50(%rbp)
movl %ecx, %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
movl %ecx, %r15d
andl $0x4, %r15d
jne 0xb2d6
pxor %xmm0, %xmm0
movdqu %xmm0, 0x60(%rbx)
movdqu %xmm0, 0x50(%rbx)
movdqu %xmm0, 0x40(%rbx)
movdqu %xmm0, 0x30(%rbx)
movdqu %xmm0, 0x20(%rbx)
movdqu %xmm0, 0x10(%rbx)
movdqu %xmm0, (%rbx)
cmpl $0x100, %eax            # imm = 0x100
jne 0xb31a
movq %r8, -0x40(%rbp)
leaq -0x300(%rbp), %rsi
movq %r13, %rdi
callq *0x7c610(%rip)          # 0x878b8
testl %eax, %eax
je 0xb409
pushq $0x10
popq %rdi
callq 0x9c1c
testq %rax, %rax
je 0xb43f
pushq $-0x1
popq %rdx
movq %rax, 0x30(%rbx)
movb $0x1, %dil
xorl %esi, %esi
movq %r13, %r14
movq -0x40(%rbp), %r8
jmp 0xb32d
cmpl $0x800, %eax            # imm = 0x800
je 0xb2ef
cmpl $0x80000, %eax          # imm = 0x80000
je 0xb2ef
cmpl $0x4000, %eax           # imm = 0x4000
jne 0xb263
pxor %xmm0, %xmm0
movdqu %xmm0, 0x60(%rbx)
movdqu %xmm0, 0x50(%rbx)
movdqu %xmm0, 0x40(%rbx)
movdqu %xmm0, 0x30(%rbx)
movdqu %xmm0, 0x20(%rbx)
movdqu %xmm0, 0x10(%rbx)
movdqu %xmm0, (%rbx)
movb $0x1, %sil
jmp 0xb31c
xorl %esi, %esi
pushq $-0x1
popq %rdx
testq %r13, %r13
je 0xb3e6
xorl %edi, %edi
movq %r13, %r14
movl %r12d, %r9d
andl $0x8, %r9d
movl %r12d, %r10d
andl $0x1, %r10d
movl %r12d, %eax
andl $0x2, %eax
shll $0x4, %r15d
leal (%r15,%rax), %ecx
testb $0x10, %r12b
leal 0x20080(%r15,%rax), %r15d
cmovel %ecx, %r15d
testl %edx, %edx
js 0xb366
movq -0x50(%rbp), %r15
jmp 0xb4eb
movq %r13, -0x58(%rbp)
movl %r12d, %eax
andl $0x80800, %eax          # imm = 0x80800
movl %eax, -0x5c(%rbp)
movq %r8, -0x40(%rbp)
movl %esi, -0x30(%rbp)
movl %edi, -0x34(%rbp)
movq %r9, -0x68(%rbp)
movl %r10d, -0x2c(%rbp)
je 0xb448
movq %r14, %rdi
callq 0x8f8e
movl %eax, %r13d
cmpb $0x2d, -0x1(%r14,%r13)
leaq -0x1(%r13), %r13
jne 0xb398
leaq -0x300(%rbp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x82c0
movb $0x0, -0x300(%rbp,%r13)
leaq -0xf8(%rbp), %rsi
leaq -0x300(%rbp), %rdi
callq *0x7c4e5(%rip)          # 0x878b8
testl %eax, %eax
je 0xb476
movl $0x70a, %r15d           # imm = 0x70A
jmp 0xb7a4
movl %esi, -0x30(%rbp)
movq %r8, -0x40(%rbp)
leaq -0x510(%rbp), %r14
movl $0x202, %edi            # imm = 0x202
movq %r14, %rsi
callq 0x16f60
testl %eax, %eax
je 0xb465
pushq $0x1
jmp 0xb441
movq -0x300(%rbp), %rax
movq -0x2f8(%rbp), %rdx
leaq 0x7db32(%rip), %rcx     # 0x88f50
movq (%rcx), %rcx
testq %rcx, %rcx
je 0xb2b0
cmpq %rax, (%rcx)
jne 0xb439
cmpq %rdx, 0x8(%rcx)
je 0xb753
addq $0x38, %rcx
jmp 0xb41e
pushq $0x7
popq %r15
jmp 0xb7a4
testl %r9d, %r9d
movl $0x180, %r13d           # imm = 0x180
cmovel %r9d, %r13d
movl $0x0, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
jmp 0xb495
xorl %edi, %edi
movq -0x40(%rbp), %r8
pushq $-0x1
popq %rdx
movl -0x30(%rbp), %esi
jmp 0xb32d
movl $0x1ff, %r13d           # imm = 0x1FF
andl -0xe0(%rbp), %r13d
movl -0xdc(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0x48(%rbp)
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x16ba9
testl %eax, %eax
js 0xb6a0
movl %eax, %edx
movl -0x2c(%rbp), %r10d
cmpl $0x0, -0x5c(%rbp)
movq -0x58(%rbp), %r13
movq -0x50(%rbp), %r15
je 0xb4dd
movl %edx, %edi
movl %edx, -0x2c(%rbp)
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
movl %r10d, %r15d
callq *0x7c565(%rip)          # 0x87a38
movl %r15d, %r10d
movq -0x50(%rbp), %r15
movl -0x2c(%rbp), %edx
movq -0x40(%rbp), %r8
movl -0x30(%rbp), %esi
movl -0x34(%rbp), %edi
movq -0x68(%rbp), %r9
testq %r8, %r8
je 0xb4f3
movl %r12d, (%r8)
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0xb502
movl %edx, (%rax)
movl %r12d, 0x4(%rax)
testl %r9d, %r9d
je 0xb53c
movl %edi, -0x34(%rbp)
movq %r14, %rdi
movl %edx, %r14d
movq %r13, -0x58(%rbp)
movl %esi, %r13d
movq %r9, %r15
movl %r10d, -0x2c(%rbp)
callq *0x7c4b4(%rip)          # 0x879d8
movl -0x2c(%rbp), %r10d
movq %r15, %r9
movq -0x50(%rbp), %r15
movl -0x34(%rbp), %edi
movl %r13d, %esi
movq -0x58(%rbp), %r13
movl %r14d, %edx
leal (,%r9,4), %eax
testl %r10d, %r10d
leal 0x2(,%r9,4), %ecx
cmovel %eax, %ecx
leal 0x80(%rcx), %eax
testb %dil, %dil
cmovnel %ecx, %eax
movl %eax, %r14d
orl $0x8, %r14d
testb %sil, %sil
cmovel %eax, %r14d
andl $0x40, %r12d
orl %r14d, %r12d
movl %edx, -0x2c(%rbp)
movl %edx, 0x18(%rbx)
movq %r15, 0x8(%rbx)
movq %r13, 0x38(%rbx)
movw %r12w, 0x1e(%rbx)
movq 0x7bf9b(%rip), %rax     # 0x87528
movq %rax, 0x60(%rbx)
xorl %edi, %edi
testb $0x40, %r12b
cmovneq %r13, %rdi
leaq 0x601c4(%rip), %rsi     # 0x6b766
pushq $0x1
popq %rdx
callq 0x1520c
testl %eax, %eax
je 0xb5b2
orb $0x10, 0x1e(%rbx)
movq 0x18(%r15), %rdi
leaq 0x5fea7(%rip), %rsi     # 0x6b464
callq 0x8510
testl %eax, %eax
jne 0xb5ca
orb $0x1, 0x1e(%rbx)
testb %r14b, %r14b
js 0xb639
movq 0x20(%r15), %rax
movq %r13, %rdi
movq %rbx, %rsi
callq *(%rax)
leaq 0x7a6f6(%rip), %r14     # 0x85cd8
cmpq %r14, %rax
je 0xb657
leaq 0x7a81a(%rip), %r12     # 0x85e08
movq %rax, %r14
cmpq %r12, %rax
jne 0xb640
movq %r13, %rdi
callq 0x8170
movq %rax, %r14
addl $0x6, %r14d
movl %r14d, %edi
callq 0x9c1c
testq %rax, %rax
je 0xb77e
movq %rax, %r15
leaq 0x6014b(%rip), %rdx     # 0x6b76b
movl %r14d, %edi
movq %rax, %rsi
movq %r13, %rcx
xorl %eax, %eax
callq 0xaf26
movq %r15, 0x28(%rbx)
movq %r12, %r14
jmp 0xb640
leaq 0x7a730(%rip), %r14     # 0x85d70
andl $0x0, 0x20(%rbx)
movq %r14, (%rbx)
movq %rbx, %rdi
callq 0x169a9
xorl %r15d, %r15d
jmp 0xb7a4
movl 0x18(%rbx), %edi
leaq -0x300(%rbp), %rsi
callq *0x7c269(%rip)          # 0x878d0
testl %eax, %eax
je 0xb70a
callq 0x8050
movl (%rax), %eax
movl %eax, 0x20(%rbx)
cmpl $0x4b, %eax
pushq $0x16
popq %rax
pushq $0xa
popq %r15
cmovel %eax, %r15d
movq %rbx, %rdi
movl -0x2c(%rbp), %esi
movl $0x6e68, %edx           # imm = 0x6E68
callq 0x16d1b
andl $0x0, 0x20(%rbx)
jmp 0xb79b
callq 0x8050
movl %r12d, %ecx
andl $0x12, %ecx
cmpl $0x2, %ecx
jne 0xb6e0
cmpl $0x15, (%rax)
je 0xb6e0
andl $0x20080, %r15d         # imm = 0x20080
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x16ba9
testl %eax, %eax
js 0xb6e0
movl %eax, %edx
andl $-0x18, %r12d
incl %r12d
pushq $0x1
popq %r10
jmp 0xb4b1
movl $0x7039, %edi           # imm = 0x7039
callq 0x16cb0
leaq 0x5ff53(%rip), %rsi     # 0x6b644
pushq $0xe
popq %r15
movl %r15d, %edi
movq %r14, %rdx
movl $0x7039, %ecx           # imm = 0x7039
callq 0x16c4b
jmp 0xb79b
movdqu -0x300(%rbp), %xmm0
movdqu %xmm0, -0xf8(%rbp)
leaq 0x7d82f(%rip), %rax     # 0x88f50
movq (%rax), %rax
testq %rax, %rax
je 0xb7b9
movdqu (%rax), %xmm0
movdqu -0xf8(%rbp), %xmm1
pcmpeqb %xmm0, %xmm1
pmovmskb %xmm1, %ecx
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0xb80e
addq $0x38, %rax
jmp 0xb721
addq $0x30, %rcx
movq (%rcx), %rax
testq %rax, %rax
je 0xb2b0
movq %rcx, %rdx
leaq 0x8(%rax), %rcx
cmpl %r12d, 0x4(%rax)
jne 0xb757
movq 0x8(%rax), %rcx
movq %rcx, (%rdx)
movl (%rax), %edx
jmp 0xb2c4
andq $0x0, 0x28(%rbx)
pushq $0x7
popq %r15
andl $0x0, 0x20(%rbx)
movq %rbx, %rdi
movl -0x2c(%rbp), %esi
movl $0x6ebd, %edx           # imm = 0x6EBD
callq 0x16d1b
movq 0x30(%rbx), %rdi
callq 0x9c7b
movl %r15d, %eax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x48
popq %rdi
callq 0x9c1c
testq %rax, %rax
je 0xb81a
pxor %xmm0, %xmm0
movdqu %xmm0, 0x20(%rax)
movdqu %xmm0, 0x10(%rax)
andq $0x0, 0x30(%rax)
movdqu -0xf8(%rbp), %xmm0
movdqu %xmm0, (%rax)
movl $0x1, 0x18(%rax)
movq 0x7d75d(%rip), %rcx     # 0x88f50
movq %rcx, 0x38(%rax)
andq $0x0, 0x40(%rax)
testq %rcx, %rcx
je 0xb805
movq %rax, 0x40(%rcx)
movq %rax, 0x7d744(%rip)     # 0x88f50
jmp 0xb811
incl 0x18(%rax)
movq %rax, 0x10(%rbx)
jmp 0xb640
pushq $0x7
popq %r15
jmp 0xb687