// File: /workspace/github_c_makefile_star2_O3/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: unixOpen
static int unixOpen(
  unqlite_vfs *pVfs,           /* The VFS for which this is the xOpen method */
  const char *zPath,           /* Pathname of file to be opened */
  unqlite_file *pFile,         /* The file descriptor to be filled in */
  unsigned int flags           /* Input flags to control the opening */
){
  unixFile *p = (unixFile *)pFile;
  int fd = -1;                   /* File descriptor returned by open() */
  int dirfd = -1;                /* Directory file descriptor */
  int openFlags = 0;             /* Flags to pass to open() */
  int noLock;                    /* True to omit locking primitives */
  int rc = UNQLITE_OK;            /* Function Return Code */
  UnixUnusedFd *pUnused;
  int isExclusive  = (flags & UNQLITE_OPEN_EXCLUSIVE);
  int isDelete     = (flags & UNQLITE_OPEN_TEMP_DB);
  int isCreate     = (flags & UNQLITE_OPEN_CREATE);
  int isReadonly   = (flags & UNQLITE_OPEN_READONLY);
  int isReadWrite  = (flags & UNQLITE_OPEN_READWRITE);
  /* 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 isOpenDirectory = isCreate ;
  const char *zName = zPath;

  SyZero(p,sizeof(unixFile));
  
  pUnused = findReusableFd(zName, flags);
  if( pUnused ){
	  fd = pUnused->fd;
  }else{
	  pUnused = (UnixUnusedFd *)unqlite_malloc(sizeof(*pUnused));
      if( !pUnused ){
        return UNQLITE_NOMEM;
      }
  }
  p->pUnused = pUnused;
  
  /* 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 */
    rc = findCreateFileMode(zName, flags, &openMode);
    if( rc!=UNQLITE_OK ){
      return rc;
    }
    fd = open(zName, openFlags, openMode);
    if( fd<0 ){
	  rc = UNQLITE_IOERR;
      goto open_finished;
    }
  }
  
  if( p->pUnused ){
    p->pUnused->fd = fd;
    p->pUnused->flags = flags;
  }

  if( isDelete ){
    unlink(zName);
  }

  if( isOpenDirectory ){
    rc = openDirectory(zPath, &dirfd);
    if( rc!=UNQLITE_OK ){
      /* It is safe to close fd at this point, because it is guaranteed not
      ** to be open on a database file. If it were open on a database file,
      ** it would not be safe to close as this would release any locks held
      ** on the file by this process.  */
      close(fd);             /* silently leak if fail, already in error */
      goto open_finished;
    }
  }

#ifdef FD_CLOEXEC
  fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
#endif

  noLock = 0;

#if defined(__APPLE__) 
  struct statfs fsInfo;
  if( fstatfs(fd, &fsInfo) == -1 ){
    ((unixFile*)pFile)->lastErrno = errno;
    if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */
    close(fd); /* silently leak if fail, in error */
    return UNQLITE_IOERR;
  }
  if (0 == SyStrncmp("msdos", fsInfo.f_fstypename, 5)) {
    ((unixFile*)pFile)->fsFlags |= UNQLITE_FSFLAGS_IS_MSDOS;
  }
#endif
  
  rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
open_finished:
  if( rc!=UNQLITE_OK ){
    unqlite_free(p->pUnused);
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %ecx, %r15d
movq %rdx, %rbx
movq %rsi, %r12
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
movl %ecx, %r13d
andl $0x4, %r13d
movl %ecx, %r14d
andl $0x2, %r14d
movq $-0x4, %rax
movl $0x0, 0x4(%rbx,%rax)
addq $0x4, %rax
cmpq $0x44, %rax
jb 0x336de
leaq -0xe8(%rbp), %rsi
movq %r12, %rdi
callq 0xc110
testl %eax, %eax
movq %r12, -0x48(%rbp)
jne 0x33765
movl $0x4, 0x61167(%rip)     # 0x94878
leaq 0x61138(%rip), %rdi     # 0x94850
callq 0xc330
movq 0x619b4(%rip), %rax     # 0x950d8
testq %rax, %rax
je 0x3374f
movq -0xe8(%rbp), %rcx
movq -0xe0(%rbp), %rdx
cmpq %rcx, (%rax)
jne 0x33746
cmpq %rdx, 0x8(%rax)
je 0x339b5
movq 0x28(%rax), %rax
testq %rax, %rax
jne 0x33737
movl $0x4, 0x6111f(%rip)     # 0x94878
leaq 0x610f0(%rip), %rdi     # 0x94850
callq 0xc2d0
leaq 0x60f54(%rip), %rdi     # 0x946c0
movl $0x10, %esi
callq 0x33d86
testq %rax, %rax
je 0x3381f
leaq 0x28(%rbx), %rcx
movq %rcx, -0x40(%rbp)
movq %rax, 0x28(%rbx)
testl %r13d, %r13d
sete -0x29(%rbp)
shll $0x4, %r13d
leal (%r14,%r13), %eax
testb $0x8, %r15b
leal 0x20080(%r13,%r14), %esi
cmovel %eax, %esi
testb $0x10, %r15b
movl $0x1a4, %eax            # imm = 0x1A4
movl $0x180, %edx            # imm = 0x180
cmovel %eax, %edx
movq -0x48(%rbp), %rdi
xorl %eax, %eax
callq 0xc510
testl %eax, %eax
js 0x338df
movl %eax, -0x30(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %r12
testq %r12, %r12
je 0x337e8
movl -0x30(%rbp), %eax
movl %eax, (%r12)
movl %r15d, 0x4(%r12)
testb $0x10, %r15b
movq -0x48(%rbp), %r14
je 0x337fa
movq %r14, %rdi
callq 0xc2c0
cmpb $0x0, -0x29(%rbp)
movl -0x30(%rbp), %r12d
jne 0x3382a
leaq -0x34(%rbp), %rsi
movq %r14, %rdi
callq 0x33cc0
testl %eax, %eax
je 0x3382a
movl %eax, %r13d
movl %r12d, %edi
jmp 0x338d8
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x338f8
movl %r12d, %edi
movl $0x1, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0xc1b0
orl $0x1, %eax
movl %r12d, %edi
movl $0x2, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0xc1b0
movl -0x34(%rbp), %r15d
movl %r12d, 0x10(%rbx)
movl %r15d, 0x14(%rbx)
movl $0x0, 0x30(%rbx)
movq %r14, 0x38(%rbx)
movl $0x4, 0x61008(%rip)     # 0x94878
leaq 0x60fd9(%rip), %rdi     # 0x94850
callq 0xc330
movl 0x10(%rbx), %edi
leaq -0xe8(%rbp), %rsi
callq 0xc9b0
testl %eax, %eax
je 0x3390d
callq 0xcb20
movl (%rax), %eax
movl %eax, 0x1c(%rbx)
cmpl $0x4b, %eax
movl $0xffffffef, %eax       # imm = 0xFFFFFFEF
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
cmovel %eax, %r13d
movl %r12d, %edi
callq 0xc2b0
movl $0x4, 0x60fbb(%rip)     # 0x94878
leaq 0x60f8c(%rip), %rdi     # 0x94850
callq 0xc2d0
movl $0x0, 0x1c(%rbx)
testl %r15d, %r15d
js 0x338e5
movl %r15d, %edi
callq 0xc2b0
jmp 0x338e5
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
movq -0x40(%rbp), %rax
movq (%rax), %rsi
leaq 0x60dcd(%rip), %rdi     # 0x946c0
callq 0x3490d
movl %r13d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movups -0xe8(%rbp), %xmm0
movups %xmm0, -0x58(%rbp)
movq 0x617b9(%rip), %r14     # 0x950d8
testq %r14, %r14
je 0x33949
leaq -0x58(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
movl $0x10, %edx
callq 0x347f1
testl %eax, %eax
je 0x33a30
movq 0x28(%r14), %r14
testq %r14, %r14
jne 0x33928
leaq 0x60d70(%rip), %rdi     # 0x946c0
movl $0x38, %esi
callq 0x33d86
testq %rax, %rax
je 0x33a67
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
xorl %r15d, %r15d
movq %r15, 0x30(%rax)
leaq -0x58(%rbp), %rdi
movq %rax, %rsi
movl $0x10, %edx
callq 0x34893
movl $0x1, 0x18(%r14)
movq 0x6173d(%rip), %rax     # 0x950d8
movq %rax, 0x28(%r14)
movq %r15, 0x30(%r14)
testq %rax, %rax
je 0x339ac
movq %r14, 0x30(%rax)
movq %r14, 0x61725(%rip)     # 0x950d8
jmp 0x33a34
movq 0x20(%rax), %r12
testq %r12, %r12
je 0x3374f
cmpl %r15d, 0x4(%r12)
jne 0x339cf
addq $0x20, %rax
jmp 0x339eb
movq %r12, %rax
movq 0x8(%r12), %r12
testq %r12, %r12
je 0x3374f
cmpl %r15d, 0x4(%r12)
jne 0x339cf
addq $0x8, %rax
movq 0x8(%r12), %rcx
movq %rcx, (%rax)
movl $0x4, 0x60e7b(%rip)     # 0x94878
leaq 0x60e4c(%rip), %rdi     # 0x94850
callq 0xc2d0
movl (%r12), %ecx
leaq 0x28(%rbx), %rax
movq %rax, -0x40(%rbp)
movq %r12, 0x28(%rbx)
testl %r13d, %r13d
sete -0x29(%rbp)
movl %ecx, -0x30(%rbp)
testl %ecx, %ecx
jns 0x337dc
jmp 0x33792
incl 0x18(%r14)
movq %r14, 0x8(%rbx)
movl $0x4, 0x60e36(%rip)     # 0x94878
leaq 0x60e07(%rip), %rdi     # 0x94850
callq 0xc2d0
movl $0x0, 0x1c(%rbx)
leaq 0x5d274(%rip), %rax     # 0x90cd0
movq %rax, (%rbx)
xorl %r13d, %r13d
jmp 0x338f8
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x338ab